Bson (cbson v0.1.5) View Source
Bson
provides encoding and decoding function for Bson format
see http://bsonspec.org/
Usage:
iex> term = %{
...> a: -4.230845,
...> b: "hello",
...> c: %{x: -1, y: 2.2001},
...> d: [23, 45, 200],
...> eeeeeeeee: %Bson.Bin{ subtype: Bson.Bin.subtyx(:binary),
...> bin: <<200, 12, 240, 129, 100, 90, 56, 198, 34, 0, 0>>},
...> f: %Bson.Bin{ subtype: Bson.Bin.subtyx(:function),
...> bin: <<200, 12, 240, 129, 100, 90, 56, 198, 34, 0, 0>>},
...> g: %Bson.Bin{ subtype: Bson.Bin.subtyx(:uuid),
...> bin: <<49, 0, 0, 0, 4, 66, 83, 79, 78, 0, 38, 0, 0, 0,
...> 2, 48, 0, 8, 0, 0, 0, 97, 119, 101, 115, 111, 109,
...> 101, 0, 1, 49, 0, 51, 51, 51, 51, 51, 51, 20, 64,
...> 16, 50, 0, 194, 7, 0, 0, 0, 0>>},
...> h: %Bson.Bin{ subtype: Bson.Bin.subtyx(:md5),
...> bin: <<200, 12, 240, 129, 100, 90, 56, 198, 34, 0, 0>>},
...> i: %Bson.Bin{ subtype: Bson.Bin.subtyx(:user),
...> bin: <<49, 0, 0, 0, 4, 66, 83, 79, 78, 0, 38, 0, 0, 0, 2,
...> 48, 0, 8, 0, 0, 0, 97, 119, 101, 115, 111, 109, 101,
...> 0, 1, 49, 0, 51, 51, 51, 51, 51, 51, 20, 64, 16, 50,
...> 0, 194, 7, 0, 0, 0, 0>>},
...> j: %Bson.ObjectId{oid: <<82, 224, 229, 161, 0, 0, 2, 0, 3, 0, 0, 4>>},
...> k1: false,
...> k2: true,
...> l: Bson.UTC.from_now({1390, 470561, 277000}),
...> m: nil,
...> q1: -2000444000,
...> q2: -8000111000222001,
...> r: %Bson.Timestamp{ts: 2},
...> s1: :min_key,
...> s2: :max_key,
...> t: Bson.ObjectId.from_string("52e0e5a10000020003000004")
...> }
...> bson = Bson.encode(term)
<<100, 1, 0, 0, 1, 97, 0, 206, 199, 181, 161, 98, 236, 16, 192, 2, 98, 0, 6, 0, 0, 0, 104, 101, 108, 108, 111,
0, 3, 99, 0, 23, 0, 0, 0, 16, 120, 0, 255, 255, 255, 255, 1, 121, 0, 210, 111, 95, 7, 206, 153, 1, 64, 0, 4, 100,
0, 26, 0, 0, 0, 16, 48, 0, 23, 0, 0, 0, 16, 49, 0, 45, 0, 0, 0, 16, 50, 0, 200, 0, 0, 0, 0, 5, 101, 101, 101, 101,
101, 101, 101, 101, 101, 0, 11, 0, 0, 0, 0, 200, 12, 240, 129, 100, 90, 56, 198, 34, 0, 0, 5, 102, 0, 11, 0, 0, 0,
1, 200, 12, 240, 129, 100, 90, 56, 198, 34, 0, 0, 5, 103, 0, 49, 0, 0, 0, 4, 49, 0, 0, 0, 4, 66, 83, 79, 78, 0, 38,
0, 0, 0, 2, 48, 0, 8, 0, 0, 0, 97, 119, 101, 115, 111, 109, 101, 0, 1, 49, 0, 51, 51, 51, 51, 51, 51, 20, 64, 16, 50,
0, 194, 7, 0, 0, 0, 0, 5, 104, 0, 11, 0, 0, 0, 5, 200, 12, 240, 129, 100, 90, 56, 198, 34, 0, 0, 5, 105, 0, 49, 0, 0,
0, 128, 49, 0, 0, 0, 4, 66, 83, 79, 78, 0, 38, 0, 0, 0, 2, 48, 0, 8, 0, 0, 0, 97, 119, 101, 115, 111, 109, 101, 0, 1,
49, 0, 51, 51, 51, 51, 51, 51, 20, 64, 16, 50, 0, 194, 7, 0, 0, 0, 0, 7, 106, 0, 82, 224, 229, 161, 0, 0, 2, 0, 3, 0,
0, 4, 8, 107, 49, 0, 0, 8, 107, 50, 0, 1, 9, 108, 0, 253, 253, 128, 190, 67, 1, 0, 0, 10, 109, 0, 16, 113, 49, 0, 160,
165, 195, 136, 18, 113, 50, 0, 207, 6, 171, 1, 241, 147, 227, 255, 17, 114, 0, 2, 0, 0, 0, 0, 0, 0, 0, 255, 115, 49, 0,
127, 115, 50, 0, 7, 116, 0, 82, 224, 229, 161, 0, 0, 2, 0, 3, 0, 0, 4, 0>>
...> decodedTerm = Bson.decode(bson, [:return_atom])
...> # assert that one by one all decoded element are identical to the original
...> Enum.all? term, fn({k, v}) -> assert Map.get(decodedTerm, k) == v end
true
Link to this section Summary
Functions
Returns decoded terms from a Bson binary document into a map with keys in the form of atoms (for other options use Bson.Decoder.document/2
)
Returns a binary representing a Bson document.
Link to this section Functions
Returns decoded terms from a Bson binary document into a map with keys in the form of atoms (for other options use Bson.Decoder.document/2
)
iex> %{} |> Bson.encode |> Bson.decode
%{}
iex> %{a: "a"} |> Bson.encode |> Bson.decode([:return_atom])
%{a: "a"}
iex> %{a: 1, b: [2, "c"]} |> Bson.encode |> Bson.decode([:return_atom])
%{a: 1, b: [2, "c"]}
See CBson.decode/2
.
Returns a binary representing a Bson document.
It accepts a Map and returns a binary
iex> Bson.encode(%{})
<<5, 0, 0, 0, 0>>
iex> Bson.encode(%{a: 1})
<<12, 0, 0, 0, 16, 97, 0, 1, 0, 0, 0, 0>>
iex> Bson.encode(%{a: 1, b: 2})
<<19, 0, 0, 0, 16, 97, 0, 1, 0, 0, 0, 16, 98, 0, 2, 0, 0, 0, 0>>
See CBson.encode/2
.