I was trying to understand BSON Notations from the site BSON Site. However I was unable to understand the reason behind the correlations.
I also referred the following questions, but am not convinced because of the following reasons.
Question 1 : Not familiar with ruby implementations
Question 2 : I understood the byte allocation. But unsure about the notations.
I would like to know how the bson object is formed for the following examples in BSON Site
1.{"hello": "world"}
2.{"BSON": ["awesome", 5.05, 1986]}
{"hello": "world"}
\x16\x00\x00\x00
\x02 hello\x00 \x06\x00\x00\x00 world\x00
\x00
(overall: 22 bytes)
The first four bytes contain the overall length as a 32-bit little-endian integer.
\x16\x00\x00\x00
=> That's 22 in decimal.
Now comes the first element. The first byte gives the type of data.
\x02
=> That's a UTF-8 string.
Then comes the name of the first element, as a null-terminated string.
hello\x00
Next comes the data of the element in the previously given type, in this case a string. For scannability (so you can quickly skip when you don't need them), strings start with their length, and are null-terminated.
\x06\x00\x00\x00
=> That's length 6.
world\x00
Now would come subsequent elements, if there were any. The whole thing is terminated with a null byte.
\x00
{"BSON": ["awesome", 5.05, 1986]}
\x31\x00\x00\x00
\x04 BSON\x00 \x26\x00\x00\x00
\x02 0\x00 \x08\x00\x00\x00 awesome\x00
\x01 1\x00 \x33\x33\x33\x33\x33\x33\x14\x40
\x10 2\x00 \xc2\x07\x00\x00
\x00
\x00
(overall: 49 bytes, array: 38 bytes)
The first four bytes contain the overall length as a 32-bit little-endian integer.
\x31\x00\x00\x00
=> That's 49 in decimal.
Now comes the first element. The first byte gives the type of data.
\x04
=> That's an array.
Then comes the name of the first element, as a null-terminated string.
BSON\x00
Next comes the data of the element in the previously given type, in this case an array.
[Quote: "The document for an array is a normal BSON document with integers for the keys, starting with 0 (..)"]
For scannability and because they form document in their own right, arrays start with their length, and are null-terminated.
\x26\x00\x00\x00
=> That's 38 in decimal.
Now comes the first element of the array. The first byte gives the type of data.
\x02
=> That's a UTF-8 string.
Then comes the name of the first element of the array, null-terminated.
0\x00
=> That's key 0.
Next comes the data of the element in the previously given type, in this case a string. Strings start with their length, and are null-terminated.
\x08\x00\x00\x00
=> length 8
awesome\x00
Now comes the second element of the array. The first byte gives the type of data.
\x01
=> That's a double floating point number.
Then comes the name of the second element of the array, null-terminated.
1\x00
=> That's key 1.
Next comes the data of the element in the previously given type, in this case a double floating point number.
\x33\x33\x33\x33\x33\x33\x14\x40
=> That's 5.5.
Now comes the third element of the array. The first byte gives the type of data.
\x10
=> That's a 32-bit integer.
Then comes the name of the third element of the array, null-terminated.
2\x00
=> That's key 2.
Next comes the data of the element in the previously given type, in this case a 32-bit integer.
\xc2\x07\x00\x00
=> That's 1986.
The array is terminated with a null byte.
\x00
The whole thing is terminated with a null byte.
\x00
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With