Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
503 lines (488 sloc) 26.2 KB
<!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 &middot; sahn]</span>, short for Bin&shy;ary <a href="http://json.org"><abbr title="JavaScript Object Notation">JSON</abbr></a>, is a bin&shy;ary-en&shy;coded seri&shy;al&shy;iz&shy;a&shy;tion of JSON-like doc&shy;u&shy;ments. Like JSON, BSON sup&shy;ports the em&shy;bed&shy;ding of doc&shy;u&shy;ments and ar&shy;rays with&shy;in oth&shy;er doc&shy;u&shy;ments and ar&shy;rays. BSON also con&shy;tains ex&shy;ten&shy;sions that al&shy;low rep&shy;res&shy;ent&shy;a&shy;tion of data types that are not part of the JSON spec. For ex&shy;ample, BSON has a Date type and a BinData type.</p>
<p>BSON can be com&shy;pared to bin&shy;ary inter&shy;change for&shy;mats, like <a href="http://code.google.com/p/protobuf/">Proto&shy;col Buf&shy;fers</a>. BSON is more "schema-less" than Proto&shy;col Buf&shy;fers, which can give it an ad&shy;vant&shy;age in flex&shy;ib&shy;il&shy;ity but also a slight dis&shy;ad&shy;vant&shy;age in space ef&shy;fi&shy;ciency (BSON has over&shy;head for field names with&shy;in the seri&shy;al&shy;ized data).</p>
</div>
<div class="yui-u">
<p>BSON was de&shy;signed to have the fol&shy;low&shy;ing three char&shy;ac&shy;ter&shy;ist&shy;ics:</p>
<ol>
<li>
<h3>Lightweight</h3>
<p>Keep&shy;ing spa&shy;tial over&shy;head to a min&shy;im&shy;um is im&shy;port&shy;ant for any data rep&shy;res&shy;ent&shy;a&shy;tion format, es&shy;pe&shy;cially when used over the net&shy;work.</p>
</li>
<li>
<h3>Traversable</h3>
<p>BSON is de&shy;signed to be tra&shy;versed eas&shy;ily. This is a vi&shy;tal prop&shy;erty in its role as the primary data rep&shy;res&shy;ent&shy;a&shy;tion for <a href="http://www.mongodb.org">Mon&shy;goDB</a>.</p>
</li>
<li>
<h3>Efficient</h3>
<p>En&shy;cod&shy;ing data to BSON and de&shy;cod&shy;ing from BSON can be per&shy;formed very quickly in most lan&shy;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 &mdash; <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 &mdash; <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>&rarr;</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>&nbsp;<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>&rarr;</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>&nbsp;\x00\x00</span><span class="hl g">\x02</span><span class="hl f">0\x00</span><span class="hl g">\x08\x00\x00
<br>&nbsp;\x00awesome\x00</span><span class="hl h">\x01</span><span class="hl f">1\x00</span><span class="hl h">333333
<br>&nbsp;\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>&nbsp;<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>
&mdash; 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>
&mdash; 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 &mdash; 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>