BSON structure, json
BSON Structure
Flyfish
This section mainly describes two examples on the bsonspec.org website.
{"Hello": "world "}
Hello is the key, and world is the value.
\ X16 \ x00 \ x00 \ x00
\ X02
Hello \ x00
\ X06 \ x00 \ x00 \ x00world \ x00
\ X00
Explain the meaning of each line
\ X16 \ x00 \ x00 \ x00
\ X indicates the hexadecimal mode.
Four bytes indicate the document size, including '\ 0' at the end of the document, and' \ 0' is \ x00 0x16 in decimal format to 22. The document size is 22 bytes.
Use Little Endian: Each type must be serialized in little-endian format.
Each type must be serialized in the little-endian format.
\ X02
One byte represents the value type. You can view the bsonspec document
Original article: "\ x02" e_name string UTF-8
\ X02 indicates that the value type is string, and the string encoding uses UTF-8
Hello \ x00
String ending with '\ 0'
\ X06 \ x00 \ x00 \ x00world \ x00
The first four bytes of \ x06 \ x00 \ x00 indicate the length of the world string ending with '\ 0'
\ X00
Terminator
II
{& Quot; BSON & quot;: [& quot; awesome & quot;, 5.05, 1986]}
Original article: the array ['red', 'blue'] wocould be encoded as the document {'0': 'red', '1': 'blue '}. the keys must be in ascending numerical order.
Array ['red', 'blue'] will be encoded as {'0': 'red', '1': 'blue '}, the keys must be sorted in ascending order by numerical values ).
That is, ["awesome", 5.05, 1986] will be encoded as {"0": "awesome", "1": 5.05, "2": 1986}
{"BSON": ["0": "awesome", "1": 5.05, "2": 1986]}
Corresponding
\ X31 \ x00 \ x00 \ x00
\ X04BSON \ x00
\ X26 \ x00 \ x00 \ x00
\ X02 \ x30 \ x00 \ x08 \ x00 \ x00 \ x00awesome \ x00
\ X01 \ x31 \ x00 \ x33 \ x33 \ x33 \ x33 \ x33 \ x33 \ x33 \ x14 \ x40
\ X10 \ x32 \ x00 \ xc2 \ x07 \ x00 \ x00
\ X00
\ X00
Explain the meaning of each line
\ X31 \ x00 \ x00 \ x00
4 bytes indicates the document size. The 10th hexadecimal value of x31 is 49, and the document size is 49 bytes.
\ X04BSON \ x00
Original article: "\ x04" e_name document Array
\ X04 indicates that the value type is the document represented by an array.
BSON \ x00 indicates the string ending with '\ 0'
\ X26 \ x00 \ x00 \ x00
The four bytes indicate the size of the array, that is, the content of the brackets. The 10 hexadecimal value of x26 is 38.
\ X02 \ x30 \ x00 \ x08 \ x00 \ x00 \ x00awesome \ x00
\ X02 indicates that the value type is string.
X30 indicates the key. The ASCII code of the character 0 is 48, and the hexadecimal value is x30.
Which is exactly x30, x31, and x32.
\ X08 \ x00 \ x00 \ x00 four bytes indicate awesome \ x00 Length
\ X01 \ x31 \ x00 \ x33 \ x33 \ x33 \ x33 \ x33 \ x33 \ x33 \ x14 \ x40
\ X01
Original article \ x01 "e_name double 64-bit binary floating point
64-bit binary floating point number
X31 \ x00 indicates the string 1 ending with '\ 0', And the ASCII code of Character 1 is x31
X33 \ x33 \ x33 \ x33 \ x33 \ x33 \ x14 \ x40
Convert 5.5 of double to hexadecimal: 40 14 33 33 33 33 33 33
\ X10 \ x32 \ x00 \ xc2 \ x07 \ x00 \ x00
Original article: "\ x10" e_name int32 32-bit integer
\ X10 represents a 32-bit integer
\ X32 \ x00 indicates the string 2 ending with '\ 0', And the ASCII code of character 2 is x32
\ Xc2 \ x07 \ x00 \ x00
That is to say, the hexadecimal 7c2 is converted to a hexadecimal value of 1986.
\ X00
\ X00
Terminator
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.