Skip to main content

Module bson

Module bson 

Source
Expand description

BSON 跨 FFI 线格式契约

core 不持有驱动,Host 用原生驱动执行 Command 后把原始文档回喂 core 做后处理。 但 serde_json::Value 无法表达 ObjectId / Date / Decimal128 / Long, 因此双方约定一套 Extended JSON(canonical 子集) 线格式:

BSON 类型线格式
ObjectId{"$oid": "<24位小写hex>"}
Date{"$date": <毫秒整数>}
Decimal128{"$numberDecimal": "<字符串>"}
Long/BigInt{"$numberLong": "<字符串>"}
Binary{"$binary": {"base64": "..", "subType": ".."}}

约定:

  1. Host → core 方向必须调 canonicalize 归一化(ObjectId 小写、$date 统一为毫秒数值);
  2. core → Host 方向由本模块的构造函数产出,保证形状固定;
  3. 其余 JSON 类型原样透传,键序不作为契约(serde_json 默认按字典序)。

Constants§

KEY_BINARY
KEY_DATE
KEY_DECIMAL
KEY_LONG
KEY_OID

Functions§

as_date_ms
取日期的毫秒值
as_object_id
取 ObjectId 的 hex 字符串
canonicalize
递归归一化扩展类型(幂等),用于 Host 回喂文档前统一形状
date
日期:毫秒时间戳(与 JS Date.now() 同单位)
decimal128
Decimal128:字符串形式保留精度
id_key
文档主键的可比较字符串形式,对应 JS String(id)
is_extended
判断是否为扩展类型对象($oid / $date / … 之一)
number_long
64 位整数:字符串形式避免 JSON number 精度丢失
object_id
ObjectId:入参为 24 位 hex,统一转小写