forked from mongodb/bsonspec.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
502 lines (488 loc) · 26.2 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<title>BSON - Binary JSON</title>
<link rel="stylesheet" href="css/reset-fonts-grids.css" type="text/css">
<link rel="stylesheet" href="css/style.css" type="text/css">
<script type="text/javascript" src="js/jquery-1.4.1.min.js"></script>
<script type="text/javascript" src="js/jquery.address-1.1.min.js"></script>
<script type="text/javascript" src="js/jquery.tooltip.min.js"></script>
<script type="text/javascript" src="js/bsonspec.js"></script>
</head>
<body>
<div id="doc" class="yui-t7">
<div id="hd">
<h1><a href="#/">BSON</a></h1>
<div id="graphic">
<div id="right-brace" class="brace">}</div>
<div id="bits">
01010100<br>
11101011<br>
10101110<br>
01010101
</div>
<div id="left-brace" class="brace">{</div>
</div>
</div>
<div id="bd">
<a class="nojs" name="/"></a>
<div class="yui-g intro">
<div class="yui-u first">
<p><abbr title="Binary JSON">BSON</abbr> <span class="pronounce">[bee · sahn]</span>, short for Bin­ary <a href="http://json.org"><abbr title="JavaScript Object Notation">JSON</abbr></a>, 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.</p>
<p>BSON can be com­pared to bin­ary inter­change for­mats, like <a href="http://code.google.com/p/protobuf/">Proto­col Buf­fers</a>. 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).</p>
</div>
<div class="yui-u">
<p>BSON was de­signed to have the fol­low­ing three char­ac­ter­ist­ics:</p>
<ol>
<li>
<h3>Lightweight</h3>
<p>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.</p>
</li>
<li>
<h3>Traversable</h3>
<p>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 <a href="http://www.mongodb.org">Mon­goDB</a>.</p>
</li>
<li>
<h3>Efficient</h3>
<p>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.</p>
</li>
</ol>
</div>
</div>
<div id="buttons">
<span class="button" id="first-button">
<a class="nav spec" href="#/specification" rel="address:specification">specification</a>
</span>
<span class="button">
<a class="nav lib" href="#/implementation" rel="address:implementation">implementations</a>
</span>
<span class="button">
<a class="nav faq" href="#/faq" rel="address:faq">FAQ</a>
</span>
<span class="button">
<a class="nav" href="http://groups.google.com/group/bson">discussion</a>
</span>
</div>
<a class="nojs" name="/specification"></a>
<h2 class="nojs">Specification</h2>
<div class="yui-g">
<div class="info" id="spec">
<h3>Version 1.0</h3>
<p>BSON is a binary format in which zero or more key/value
pairs are stored as a single entity. We call this entity
a <em>document</em>.</p>
<p>The following grammar specifies version 1.0 of the
BSON standard. We've written the grammar using a
pseudo-<a href="http://en.wikipedia.org/wiki/Backus–Naur_Form">BNF</a>
syntax. Valid BSON data is represented by
the <code>document</code> non-terminal.</p>
<div id="grammar">
<h3>Basic Types</h3>
<p>The following basic types are used as terminals in
the rest of the grammar. Each type must be serialized in
little-endian format.</p>
<div class="tb">
<table>
<tr>
<td class="t">byte</td>
<td>1 byte (8-bits)</td>
</tr>
<tr>
<td class="t">int32</td>
<td>4 bytes (32-bit signed integer)</td>
</tr>
<tr>
<td class="t">int64</td>
<td>8 bytes (64-bit signed integer)</td>
</tr>
<tr>
<td class="t">double</td>
<td>8 bytes (64-bit IEEE 754 floating point)</td>
</tr>
</table>
</div>
<h3>Non-terminals</h3>
<p>The following specifies the rest of the BSON
grammar. Note that quoted strings represent terminals,
and should be interpreted with C semantics
(e.g. <code>"\x01"</code> represents the byte <code>0000
0001</code>). Also note that we use the <code>*</code>
operator as shorthand for repetition
(e.g. <code>("\x01"*2)</code>
is <code>"\x01\x01"</code>). When used as a unary
operator, <code>*</code> means that the repetition can
occur 0 or more times.</p>
<div class="tb">
<table>
<tr>
<td class="fix nt">document</td>
<td class="fix op">::=</td>
<td class="fix ex">int32 e_list "\x00"</td>
<td class="tt" title="BSON Document - The int32 is the total number of bytes comprising the document.">BSON Document</td>
</tr>
<tr class="section">
<td class="fix nt">e_list</td>
<td class="fix op">::=</td>
<td class="fix ex">element e_list</td>
<td>Sequence of elements</td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">""</td>
<td></td>
</tr>
<tr class="section">
<td class="fix nt">element</td>
<td class="fix op">::=</td>
<td class="fix ex">"\x01" e_name double</td>
<td>Floating point</td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">"\x02" e_name string</td>
<td>UTF-8 string</td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">"\x03" e_name document</td>
<td>Embedded document</td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">"\x04" e_name document</td>
<td class="tt" title="Array - The document for an array is a normal BSON document with integer values for the keys, starting with 0 and continuing sequentially. For example, the array ['red', 'blue'] would be encoded as the document {'0': 'red', '1': 'blue'}. The keys must be in ascending numerical order.">Array</td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">"\x05" e_name binary</td>
<td>Binary data</td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">"\x06" e_name</td>
<td>Undefined — <em>Deprecated</em></td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">"\x07" e_name (byte*12)</td>
<td><a href="http://dochub.mongodb.org/core/objectids">ObjectId</a></td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">"\x08" e_name "\x00"</td>
<td>Boolean "false"</td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">"\x08" e_name "\x01"</td>
<td>Boolean "true"</td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">"\x09" e_name int64</td>
<td class="tt" title="UTC datetime - The int64 is UTC milliseconds since the Unix epoch.">UTC datetime</td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">"\x0A" e_name</td>
<td>Null value</td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">"\x0B" e_name cstring cstring</td>
<td class="tt" title="Regular expression - The first cstring is the regex pattern, the second is the regex options string. Options are identified by characters, which must be stored in alphabetical order. Valid options are 'i' for case insensitive matching, 'm' for multiline matching, 'x' for verbose mode, 'l' to make \w, \W, etc. locale dependent, 's' for dotall mode ('.' matches everything), and 'u' to make \w, \W, etc. match unicode.">Regular expression</td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">"\x0C" e_name string (byte*12)</td>
<td>DBPointer — <em>Deprecated</em></td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">"\x0D" e_name string</td>
<td>JavaScript code</td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">"\x0E" e_name string</td>
<td class="tt" title="Symbol - Similar to a string but for languages with a distinct symbol type.">Symbol</td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">"\x0F" e_name code_w_s</td>
<td>JavaScript code w/ scope</td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">"\x10" e_name int32</td>
<td>32-bit Integer</td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">"\x11" e_name int64</td>
<td class="tt" title="Timestamp - Special internal type used by MongoDB replication and sharding. First 4 bytes are an increment, second 4 are a timestamp. Setting the timestamp to 0 has special semantics.">Timestamp</td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">"\x12" e_name int64</td>
<td>64-bit integer</td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">"\xFF" e_name</td>
<td class="tt" title="Min key - Special type which compares lower than all other possible BSON element values.">Min key</td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">"\x7F" e_name</td>
<td class="tt" title="Max key - Special type which compares higher than all other possible BSON element values.">Max key</td>
</tr>
<tr class="section">
<td class="fix nt">e_name</td>
<td class="fix op">::=</td>
<td class="fix ex">cstring</td>
<td>Key name</td>
</tr>
<tr class="section">
<td class="fix nt">string</td>
<td class="fix op">::=</td>
<td class="fix ex">int32 (byte*) "\x00"</td>
<td class="tt" title="String - The int32 is the number bytes in the (byte*) + 1 (for the trailing '\x00'). The (byte*) is zero or more UTF-8 encoded characters.">String</td>
</tr>
<tr class="section">
<td class="fix nt">cstring</td>
<td class="fix op">::=</td>
<td class="fix ex">(byte*) "\x00"</td>
<td class="tt" title="CString - Zero or more modified UTF-8 encoded characters followed by '\x00'. The (byte*) MUST NOT contain '\x00', hence it is not full UTF-8.">CString</td>
</tr>
<tr class="section">
<td class="fix nt">binary</td>
<td class="fix op">::=</td>
<td class="fix ex">int32 subtype (byte*)</td>
<td class="tt" title="Binary - The int32 is the number of bytes in the (byte*).">Binary</td>
</tr>
<tr class="section">
<td class="fix nt">subtype</td>
<td class="fix op">::=</td>
<td class="fix ex">"\x00"</td>
<td class="tt" title="Generic binary subtype - This is the most commonly used binary subtype and should be the 'default' for drivers and tools.">Binary / Generic</td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">"\x01"</td>
<td>Function</td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">"\x02"</td>
<td class="tt" title="Old generic subtype - This used to be the default subtype, but was deprecated in favor of \x00. Drivers and tools should be sure to handle \x02 appropriately. The structure of the binary data (the byte* array in the binary non-terminal) must be an int32 followed by a (byte*). The int32 is the number of bytes in the repetition.">Binary (Old)</td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">"\x03"</td>
<td>UUID</td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">"\x05"</td>
<td>MD5</td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">"\x80"</td>
<td class="tt" title="User defined - The structure of the binary data can be anything.">User defined</td>
</tr>
<tr class="section">
<td class="fix nt">code_w_s</td>
<td class="fix op">::=</td>
<td class="fix ex">int32 string document</td>
<td class="tt" title="Code w/ scope - The int32 is the length in bytes of the entire code_w_s value. The string is JavaScript code. The document is a mapping from identifiers to values, representing the scope in which the string should be evaluated.">Code w/ scope</td>
</tr>
</table>
</div>
</div>
<h3>Examples</h3>
<p>The following are some example documents (in JavaScript
/ Python style syntax) and their corresponding BSON
representations. Try mousing over them for some useful
correlation.</p>
<table id="examples">
<tr><td><code><span class="hl a">{</span><span class="hl b">"hello"</span>: <span class="hl c">"world"</span><span class="hl a">}</span></code></td><td>→</td><td>
<code>"<span class="hl a">\x16\x00\x00\x00</span><span class="hl c">\x02</span><span class="hl b">hello\x00</span>
<br> <span class="hl c">\x06\x00\x00\x00world\x00</span><span class="hl a">\x00</span>"</code></td></tr>
<tr><td><code><span class="hl d">{</span><span class="hl e">"BSON"</span>: <span class="hl f">[</span><span class="hl g">"awesome"</span>, <span class="hl h">5.05</span>, <span class="hl i">1986</span><span class="hl f">]</span><span class="hl d">}</span></code></td><td>→</td><td>
<code>"<span class="hl d">1\x00\x00\x00</span><span class="hl f">\x04</span><span class="hl e">BSON\x00</span><span class="hl f">&\x00
<br> \x00\x00</span><span class="hl g">\x02</span><span class="hl f">0\x00</span><span class="hl g">\x08\x00\x00
<br> \x00awesome\x00</span><span class="hl h">\x01</span><span class="hl f">1\x00</span><span class="hl h">333333
<br> \x14@</span><span class="hl i">\x10</span><span class="hl f">2\x00</span><span class="hl i">\xc2\x07\x00\x00</span>
<br> <span class="hl f">\x00</span><span class="hl d">\x00"</span></code></td></tr>
</table>
</div>
<a class="nojs" name="/implementation"></a>
<h2 class="nojs">Implementations</h2>
<div class="info" id="lib">
<p>
Implementations of the BSON specification exist for many
different languages / environments. Some implementations
are currently embedded
within <a href="http://www.mongodb.org">MongoDB</a>
drivers, since MongoDB was the first large project to
make use of BSON. Over time those libraries will be made
more stand-alone, but they should be usable
independently of MongoDB in their current state.
</p>
<div class="col" id="col1">
<h3>BSON Libraries</h3>
<ul>
<li>
<p><a href="http://github.com/argoncloud/ActionBSON/">ActionScript</a></p>
</li>
<li>
<p><a href="http://github.com/mongodb/mongo-c-driver/tree/master/src/">C</a></p>
</li>
<li>
<p>C# / .Net: <a href="https://github.com/mongodb/mongo-csharp-driver/tree/master/Bson/">Official MongoDB driver</a> or <a href="http://github.com/samus/mongodb-csharp/tree/master/MongoDBDriver/Bson/">mongodb-csharp</a> or <a href="http://james.newtonking.com/archive/2009/12/26/json-net-3-5-release-6-binary-json-bson-support.aspx">Json.NET</a> or <a href="http://github.com/karlseguin/Metsys.Bson">Metsys.Bson</a></p>
</li>
<li>
<p><a href="http://dochub.mongodb.org/core/bsoncplusplus">C++</a></p>
</li>
<li>
<p><a href="http://code.google.com/p/pebongo/source/browse/trunk/_bson.pas">Delphi</a></p>
</li>
<li>
<p><a href="http://github.com/JacobVorreuter/emongo/blob/master/src/emongo_bson.erl">Erlang</a> or <a href="http://github.com/wpntv/erlmongo/blob/master/mongodb.erl">Erlang</a></p>
</li>
<li>
<p><a href="http://github.com/slavapestov/factor/tree/master/extra/bson/">Factor</a></p>
</li>
<li>
<p><a href="http://bitbucket.org/liamstask/fantomongo/src/tip/fan/bson/">Fantom</a></p>
</li>
<li>
<p><a href="http://github.com/mikejs/gomongo">Go</a></p>
</li>
<li>
<p><a href="http://hackage.haskell.org/package/bson">Haskell</a></p>
</li>
<li>
<p>Java: <a href="http://github.com/mongodb/mongo-java-driver/blob/master/src/main/com/mongodb/RawDBObject.java">Official MongoDB driver</a> or <a href="https://github.com/michel-kraemer/bson4jackson">bson4jackson</a></p>
</li>
<li>
<p><a href="http://github.com/paracycle/BsonME">J2ME</a> (work in progress)</p>
</li>
<li>
<p><a href="http://github.com/fons/cl-mongo/blob/master/src/bson.lisp">Lisp</a> </p>
</li>
<li>
<p><a href="https://github.com/christkv/node-mongodb-native/tree/master/lib/mongodb/bson">Node.js</a> (JavaScript) </p>
</li>
<li>
<p><a href="http://github.com/mongodb/mongo-perl-driver/blob/master/lib/MongoDB/BSON.pm">Perl</a></p>
</li>
<li>
<p><a href="http://github.com/mongodb/mongo-php-driver">PHP</a></p>
</li>
<li>
<p><a href="http://github.com/mongodb/mongo-python-driver">Python</a>
— with optional C extension</p>
</li>
<li>
<p><a href="http://www.pythomnic3k.org/files/bson.py">Python 3</a></p>
</li>
<li>
<p><a href="http://rubygems.org/gems/bson">Ruby</a>
— with <a href="http://rubygems.org/gems/bson_ext">optional C extension</a></p>
</li>
<li>
<p><a href="http://github.com/mdirolf/mlmongo/blob/master/bson.sml">Standard ML (SML)</a></p>
</li>
</ul>
</div>
<div class="col" id="col2">
<h3>Projects Using BSON</h3>
<ul>
<li>
<p><a href="http://www.mongodb.org">MongoDB</a>, the
document-oriented database, uses BSON as both the
network and on-disk representation of
documents.</p>
</li>
</ul>
</div>
<p class="clear">
If you know of other BSON implementations or projects
using BSON, please <a href="http://github.com/mongodb/bsonspec.org">add them</a>.
</p>
</div>
<a class="nojs" name="/faq"></a>
<h2 class="nojs">FAQ</h2>
<div class="info" id="faq">
<h3>What is the point of BSON when it is no smaller than
JSON in many cases?</h3>
<p>BSON is designed to be efficient in space, but in many
cases is not much more efficient than JSON. In some cases
BSON uses even more space than JSON. The reason for this
is another of the BSON design goals: traversability. BSON
adds some "extra" information to documents, like length
prefixes, that make it easy and fast to traverse.</p>
<p>BSON is also designed to be fast to encode and
decode. For example, integers are stored as 32 (or 64) bit
integers, so they don't need to be parsed to and from
text. This uses more space than JSON for small integers,
but is much faster to parse.</p>
<h3>Where can I get more help/infomation?</h3>
<p>The best place to ask questions about BSON is on
the <a href="http://groups.google.com/group/bson">BSON
mailing list</a>.</p>
<h3>How can I contribute or make fixes to this site?</h3>
<p>The best way to contribute to this site is to
fork <a href="http://github.com/mongodb/bsonspec.org">the
project</a> and send us a pull request.</p>
</div>
</div>
</div>
<div id="ft">
<div>
<a href="http://creativecommons.org/publicdomain/zero/1.0/" id="cc" class="ro"><span>Creative Commons — CC0</span></a>
</div>
</div>
</div>
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-7301842-4");
pageTracker._trackPageview();
} catch(err) {}</script>
</body>
</html>