BSON能走多远

代码:

 http://download.csdn.net/source/2139850

   BSON是在json基础上一哥们提出的新的数据形式,它就是直接把一个对象转化为二进制数字来表示。。目前百度上基本上没有资料,而google上它的资料也寥寥无几。。新的东西出现后是否大家能接受确实需要一段时间的考验。。小弟我不想去预测它的未来,但是我觉得提出这种形式本身就是一个进步。。。。相对大家现在都在使用的json格式post和get数据,如果你现在的web application或者是win app开始使用bson作为数据传输的方式,那么相信你的应用程序安全性就比较高。。顺便聊个题外话,我最近坐地铁发现的一个现象。。

大家只要是坐公共交通工具都会有过这样的经历:在车门口旁边站满了人,后面上来的人还是会继续挤在车门口,而不会费劲挤到车厢中间,所以车厢中间的人不会感觉到挤,甚至一个人有两个人的位置。。。就像下面的这个图:

在车厢中也形成了一个等压线。。图中绿色线。所以如果你以前坐车总是在红色区域,并不是因为你不想去蓝色区域而是你过不去,或者是因为你快要下车了,所以你在那里挡住了别人去蓝色区域的路线。实际上远不是这样。。。比如在公交车上我们有前门上车后门下车,但是如果很多人都是到终点站,你会发现大家还是都在前门那里挤着。。。。不去多分析原因了,这虽然只是坐车,但是你试着去想想在中国经商是不是也这样有多少人都是在从事红色区域的行业或者某个行业的某一系列。

言归正传,说技术吧,呵呵。其实我们做技术也一样,你要跳过那个等压线去绿色区域,这样你不仅过得舒服而且待遇还好。试着去多想想一定有好处的。

BSON的资料比较少。我给大家贴几个我查到的资料吧。。

BSON [bee · sahn], short for Bin­ary JSON, is a bin­ary-en­coded seri­al­iz­a­tion of JSON-like doc­u­ments. Like JSON, BSON sup­ports the em­bed­ding of doc­u­ments and ar­rays with­in oth­er doc­u­ments and ar­rays. BSON also con­tains ex­ten­sions that al­low rep­res­ent­a­tion of data types that are not part of the JSON spec. For ex­ample, BSON has a Date type and a BinData type.

BSON can be com­pared to bin­ary inter­change for­mats, like Proto­col Buf­fers. BSON is more "schema-less" than Proto­col Buf­fers, which can give it an ad­vant­age in flex­ib­il­ity but also a slight dis­ad­vant­age in space ef­fi­ciency (BSON has over­head for field names with­in the seri­al­ized data).

BSON was de­signed to have the fol­low­ing three char­ac­ter­ist­ics:

  1. Lightweight

    Keep­ing spa­tial over­head to a min­im­um is im­port­ant for any data rep­res­ent­a­tion format, es­pe­cially when used over the net­work.

  2. Traversable

    BSON is de­signed to be tra­versed eas­ily. This is a vi­tal prop­erty in its role as the primary data rep­res­ent­a­tion for Mon­goDB.

  3. Efficient

    En­cod­ing data to BSON and de­cod­ing from BSON can be per­formed very quickly in most lan­guages 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不支持二进制的传输。。它的优势说不定有哪位高人会让它显示出来。

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值