json - 我可以获得 BSON 的更多解释吗?

标签 json bson

我试图通过http://bsonspec.org/#/specification了解BSON ,但仍然存在一些问题。


让我们以上面网站中的示例为例:

{"hello": "world"} → "\x16\x00\x00\x00\x02hello\x00\x06\x00\x00\x00world\x00\x00"

问题1

在上面的例子中,对于编码的字节结果,双引号实际上不是结果的一部分,对吗?

问题2

据我了解,前 4 个字节 \x16\x00\x00\x00 是整个 BSON 文档的大小。

它是little endian格式。但为什么?为什么不采用大端

问题3

示例文档的大小怎么会是 \x16,即 22

问题4

通常情况下,如果我想自己对文档进行编码,如何计算文档的大小?我认为我的麻烦主要是如何确定 UTF-8 字符串的大小?


我们再举一个例子:

{"BSON": ["awesome", 5.05, 1986]}   

→   

"\x31\x00\x00\x00\x04BSON\x00\x26\x00\x00\x00\x020\x00\x08\x00\x00 
 \x00awesome\x00\x011\x00\x33\x33\x33\x33\x33\x33\x14\x40\x102\x00\xc2\x07\x00\x00 
 \x00\x00"

问题5

在这个例子中,有一个数组。根据规范,对于数组来说,它实际上是一个{key, value}对的列表,而key是0, 1,等等。我的问题是这里的 0, 1 也是字符串,对吗?

最佳答案

Question 1

in the above example, for the encoded bytes results, the double quotes actually are not part of the results, right?

引号不是字符串的一部分。它们用于标记 JSON 字符串

Question 2

And it is little endian format. But why? Why not take big endian?

字节序的选择很大程度上取决于偏好。 Little Endian 的优点之一是常用的平台都是 Little Endian,因此不需要反转字节。

Question 3

How comes the size of the example doc being \x16, i.e. 22?

共有22个字节(包括长度前缀)

Question 4

Normally, if I want to encode the doc by myself, how do I calculate the size of the doc? I think my trouble majorly is how to decide the size of UTF-8 string?

先把文档写出来,然后再回去填写长度。

Question 5

n this example, there is an array. according to the specification, for array, it is actually a list of {key, value} pairs, whereas the key is 0, 1, etc. My question is so the 0, 1 here are strings too, right?

是的。确切地说,没有长度前缀的零终止字符串。 (在列表中称为cstring)。就像嵌入文档一样。

关于json - 我可以获得 BSON 的更多解释吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16169879/

相关文章:

java - 如何在不使用循环的情况下在 java 的 json 中解析数组中的每个对象?

javascript - JSON 文件错误

java - 如何将 json 字符串解析为 ArrayList<NameValuePair>

json - 使用 MongoDB Driver Java API 将 BSON 转换为 JSON

json - 如何使用mongo-go-driver有效地将bson转换为json?

javascript - 将控制台输出的代码更改为 JSON 输出

javascript - js-xlsx 如何重新排列列?

Python bson : How to create list of ObjectIds in a New Column

c# - 在.net世界中将JSON转换为BSON的最简单方法是什么

c# - HttpClient 通过 BSON 到 RESTful API