Which one is lighter, JSON or BSON?

前端 未结 2 1479
鱼传尺愫
鱼传尺愫 2021-02-01 14:54

I have written code to serialize objects to JSON and BSON. According to my output, the BSON produced is greater in size than the JSON. Is this expected?

From my

2条回答
  •  旧巷少年郎
    2021-02-01 15:32

    The property "foo":"bar" consumes 11 bytes in UTF-8 encoded JSON. In BSON it consumes 13:

    bytes       description
    ============================================
    1           entry type value \x02
    3           "foo"
    1           NUL \x00
    4           int32 string length (4 -- includes the NUL)
    3           "bar"
    1           NUL \x00
    

    There are many cases in which JSON will be more compact.

提交回复
热议问题