elixir-bson

Build StatusHex VersionHex Downloads

BSON implementation for Elixir Language

elixir-bson on GitHub source repo - documentation

BSON is a binary format in which zero or more key/value pairs are stored as a single entity, called a document. It is a data type with a standard binary representation defined at http://www.bsonspec.org.

This implements version 1.0 of that spec.

This project is used by elixir-mongo, a MongoDB driver in Elixir.

This implementation maps the Bson grammar with Elixir terms in the following way:

This is how to encode a sample Elixir Map into a Bson Document:

bson = Bson.encode %{a: 1, b: "2", c: [1,2,3], d: %{d1: 10, d2: 11} }

In this case, bson would be a document with 4 elements (an Integer, a String, an Array and an embeded document). This document would correspond in Javascript to:

{a: 1, b: "2", c: [1,2,3], d: {d1: 10, d2: 11} }

Conversly, to decode a bson document:

%{a: 1} == Bson.decode <<12, 0, 0, 0, 16, 97, 0, 1, 0, 0, 0, 0>>

Special Bson element that do not have obvious corresponding type in Elixir are represented with Record, for example:

jsbson = Bson.encode js: %Bson.JS{code:"function(a) return a+b;", scope: [b: 2]}
rebson = Bson.encode re: %Bson.Regex{pattern: "\d*", opts: "g"}

Some configuration can be done using fun or protocol implementation, ie, it is possible to redefine encoder end decoder of Bson.Bin to implement specific encoding