Packages
mongodb_driver
1.6.4
1.6.4
1.6.3
1.6.2
1.6.1
1.6.0
1.5.6
1.5.5
1.5.4
1.5.2
1.5.1
1.5.0
1.4.1
1.4.0
1.2.1
1.2.0
1.1.0
1.0.3
1.0.2
1.0.1
1.0.0
0.9.2
0.9.1
0.9.0
0.9.0-rc.1
0.9.0-rc.0
0.8.4
0.8.3
0.8.2
0.8.1
0.8.0
0.7.4
0.7.3
0.7.2
0.7.1
0.7.0
0.6.5
0.6.4
0.6.3
0.6.2
0.6.1
0.6.0
0.5.7
0.5.6
0.5.5
0.5.4
0.5.3
0.5.2
0.5.1
0.5.0
The MongoDB driver for Elixir
Current section
Files
Jump to
Current section
Files
lib/bson.ex
defmodule BSON do
@moduledoc """
Functions for encoding and decoding BSON documents.
"""
@type t :: document | String.t() | atom | number | boolean | BSON.Binary.t() | BSON.ObjectId.t() | BSON.Regex.t() | BSON.JavaScript.t() | BSON.Timestamp.t() | BSON.LongNumber.t() | [t]
@type document :: %{atom => BSON.t()} | %{String.t() => BSON.t()} | [{atom, BSON.t()}] | [{String.t(), BSON.t()}]
@doc """
Encode a BSON document to `iodata`.
"""
@spec encode(document) :: iodata
def encode(map) when is_map(map) do
case Map.has_key?(map, :__struct__) do
true ->
BSON.Encoder.encode(Map.to_list(map))
false ->
BSON.Encoder.encode(map)
end
end
def encode([{_, _} | _] = keyword) do
BSON.Encoder.encode(keyword)
end
@doc """
Decode `iodata` to a BSON document.
"""
@spec decode(iodata) :: document
def decode(iodata) do
iodata
|> IO.iodata_to_binary()
|> BSON.Decoder.decode()
end
end