代码:
http://download.csdn.net/source/2139850
BSON是在json基础上一哥们提出的新的数据形式,它就是直接把一个对象转化为二进制数字来表示。。目前百度上基本上没有资料,而google上它的资料也寥寥无几。。新的东西出现后是否大家能接受确实需要一段时间的考验。。小弟我不想去预测它的未来,但是我觉得提出这种形式本身就是一个进步。。。。相对大家现在都在使用的json格式post和get数据,如果你现在的web application或者是win app开始使用bson作为数据传输的方式,那么相信你的应用程序安全性就比较高。。顺便聊个题外话,我最近坐地铁发现的一个现象。。
大家只要是坐公共交通工具都会有过这样的经历:在车门口旁边站满了人,后面上来的人还是会继续挤在车门口,而不会费劲挤到车厢中间,所以车厢中间的人不会感觉到挤,甚至一个人有两个人的位置。。。就像下面的这个图:
在车厢中也形成了一个等压线。。图中绿色线。所以如果你以前坐车总是在红色区域,并不是因为你不想去蓝色区域而是你过不去,或者是因为你快要下车了,所以你在那里挡住了别人去蓝色区域的路线。实际上远不是这样。。。比如在公交车上我们有前门上车后门下车,但是如果很多人都是到终点站,你会发现大家还是都在前门那里挤着。。。。不去多分析原因了,这虽然只是坐车,但是你试着去想想在中国经商是不是也这样有多少人都是在从事红色区域的行业或者某个行业的某一系列。
言归正传,说技术吧,呵呵。其实我们做技术也一样,你要跳过那个等压线去绿色区域,这样你不仅过得舒服而且待遇还好。试着去多想想一定有好处的。
BSON的资料比较少。我给大家贴几个我查到的资料吧。。
BSON [bee · sahn], short for Binary JSON, is a binary-encoded serialization of JSON-like documents. Like JSON, BSON supports the embedding of documents and arrays within other documents and arrays. BSON also contains extensions that allow representation of data types that are not part of the JSON spec. For example, BSON has a Date type and a BinData type.
BSON can be compared to binary interchange formats, like Protocol Buffers. BSON is more "schema-less" than Protocol Buffers, which can give it an advantage in flexibility but also a slight disadvantage in space efficiency (BSON has overhead for field names within the serialized data).
BSON was designed to have the following three characteristics:
-
Lightweight
Keeping spatial overhead to a minimum is important for any data representation format, especially when used over the network.
-
Traversable
BSON is designed to be traversed easily. This is a vital property in its role as the primary data representation for MongoDB.
-
Efficient
Encoding data to BSON and decoding from BSON can be performed very quickly in most languages due to the use of C data types.
http://en.wikipedia.org/wiki/BSON
http://bsonspec.org/#/specification
BSON is a binary-encoded serialization of JSON-like documents, which essentially means its an efficient way of transfering information. Part of my work on the MongoDB NoRM drivers, discussed in more details by Rob Conery, is to write an efficient and maintainable BSON serializer and deserializer. The goal of the serializer is that you give it a .NET object and you get a byte array out of it which represents valid BSON. The deserializer does the opposite - give it a byte array and out pops your object. Of course, there are limits to what they can do - they are mostly meant to be used against POCO/domain entities.
Grammar
The first thing to understand when building serializers is how to read grammar. In programming languages, grammar is a way to express the valid keywords and values a parser might run into. Both the JSON and BSON grammars are great to learn, given how simplistic yet powerful they are. The JSON grammar, available on the homepage of json.org gives a nice representation of what valid JSON should look like. The BSON grammar, available at bsonspec.org under the specification button, follows a more traditional dialect. Essentially, you have symbols on the left and expressions on the right. The expressions can, and often will be, made up of additional symbols and or actual values. Eventually though, you'll end up with a symbol which is only made up of values - which means you can stop going down the rabbit hole. Its also very common for a child symbol to reference a parent symbol - but eventually something breaks this cycle.
An Example
So, say we wanted to serialize the following json:
{"valid": true}
Everything in BSON starts with a document
. From the bson specification, we can see that a document
is made up of a 32bit integer (representing the total size of the document, including the integer itself), another symbol called an e_list
, and finally a termination character. As a start, we'd have something like:
Now, an e_list itself is made up of a symbol called an element
followed by another e_list
or an blank string. An element
is made up of a single byte type (with /x08
representing a boolean), a symbol called e_name
and a byte value for true or false. So now we have:
The only thing missing now is our e_name
(which represents the word "valid" in the original JSON). An e_name
is really just a cstring
which is our value UTF8 encoded into an array of bytes with a trailing byte of /x00
:
Our final byte array looks something like:
Serializing a single bool value might be the simplest of cases, but once you understand that, you're well on your way to being able to serialize anything. Sure, serializing an array might be a bit trickier, since each element within the array is its own document - but the challenge is mostly implementation versus conceptual.
Newtonjson现在已经有一个BSON的序列化和发序列化的库大家可以使用。.net开发的。
我一会会把代码传上去大家可以看看。
其实BSON与JSON相比,它对网速的要求是更低了,但是很多时候它的长度比json要更长。而且目前并没有太多的js库去序列化bson,它的未来还是个谜。。。
我自己琢磨着使用bson对于图片(通过二进制存储)的ajax获取会比较容易很多。目前我们都会在服务器端解析,json不支持二进制的传输。。它的优势说不定有哪位高人会让它显示出来。