Understanding BSON Notation

I tried to understand the BSON notes from the BSON website . However, I could not understand the reason for the correlations.

I also referred to the following questions, but I am not convinced for the following reasons.

Question 1 : not familiar with ruby implementation

Question 2 : I understood the distribution of bytes. But not sure about the notation.

I would like to know how the bson object is formed for the following examples in the BSON site p>

1. {"hello": "world"}
2. {"BSON": ["awesome", 5.05, 1986]}

+4
source share
1 answer

{"hello": "world"}

\x16\x00\x00\x00 \x02 hello\x00 \x06\x00\x00\x00 world\x00 \x00 

(total: 22 bytes)

The first four bytes contain the total length as a 32-bit small-term integer.

\x16\x00\x00\x00 => That is 22 in decimal form.

Now comes the first element. The first byte gives the data type.

\x02 => This is a UTF-8 string.

Then the name of the first element appears as a string with a terminating zero.

hello\x00

Next comes the element data in the previously specified type, in this case a string. For scanning (so that you can skip quickly when you don't need them), the lines start at their length and end at zero.

\x06\x00\x00\x00 => This length is 6.

world\x00

Now come the next elements, if any. All this ends with a zero 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 

(total: 49 bytes, array: 38 bytes)

The first four bytes contain the total length as a 32-bit small-term integer.

\x31\x00\x00\x00 => This is 49 in decimal form.

Now comes the first element. The first byte gives the data type.

\x04 => This is an array.

Then the name of the first element appears as a string with a terminating zero.

BSON\x00

This is followed by element data in a previously defined type, in this case an array.

[Quote: β€œA document for an array is a normal BSON document with integers for keys starting at 0 (..)”]

For scanning, and because they themselves form a document, arrays begin with their length and end with zeros.

\x26\x00\x00\x00 => This is 38 in decimal form.

Now comes the first element of the array. The first byte gives the data type.

\x02 => This is a UTF-8 string.

Then the name of the first element of the array appears with the completion of a null value.

0\x00 => This key is 0.

Next comes the element data in the previously specified type, in this case a string. Lines begin with their length and end with zero.

\x08\x00\x00\x00 => length 8

awesome\x00

Now the second element of the array appears. The first byte gives the data type.

\x01 => This is a double floating point number.

Then the name of the second element of the array with zero completion appears.

1\x00 => This key is 1.

Next comes the item data in the previously specified type, in this case, a double floating point number.

\x33\x33\x33\x33\x33\x33\x14\x40 => This is 5.5.

Now comes the third element of the array. The first byte gives the data type.

\x10 => This is a 32-bit integer.

Then the name of the third element of the array with zero completion appears.

2\x00 => This key is 2.

Next comes the element data in the previously specified type, in this case a 32-bit integer.

\xc2\x07\x00\x00 => In 1986.

The array ends with a null byte.

\x00

All this ends with a zero byte.

\x00

+16
source

All Articles