Adapter that exposes the Erlang :json module under the bang-suffixed API
(encode!/1, decode!/1, encode_to_iodata!/1) expected by Ecto and Postgrex.
Provides Elixir-native semantics on top of :json:
nilencodes as JSONnull(not the string"nil").trueandfalseencode as JSON booleans.Structs encode as maps (the
:__struct__key is dropped).Other atoms encode as strings.
JSON
nulldecodes asnil(not the atom:null).
Configured by default in config/config.exs:
config :ecto, json_library: Localize.Translate.JSON
config :postgrex, json_library: Localize.Translate.JSON
Summary
Functions
Decodes a JSON binary into an Elixir term.
Encodes an Elixir term as a JSON binary.
Encodes an Elixir term as JSON iodata.
Functions
Decodes a JSON binary into an Elixir term.
Arguments
valueis JSON-encoded iodata: a binary or a list of binaries.
Returns
- The decoded Elixir term. Objects decode as maps with string keys, arrays as lists,
JSON
nullasnil, and JSON booleans astrue/false.
Examples
iex> Localize.Translate.JSON.decode!(~s({"name":"Ada"}))
%{"name" => "Ada"}
iex> Localize.Translate.JSON.decode!("[1,true,null]")
[1, true, nil]
Encodes an Elixir term as a JSON binary.
Arguments
valueis any term encodable as JSON: a map, list, struct, binary, number, boolean,nil, or atom.
Returns
- A binary containing the JSON encoding of
value.
Examples
iex> Localize.Translate.JSON.encode!(%{name: "Ada"})
~s({"name":"Ada"})
iex> Localize.Translate.JSON.encode!([1, true, nil])
"[1,true,null]"
iex> Localize.Translate.JSON.encode!(nil)
"null"
Encodes an Elixir term as JSON iodata.
Identical to encode!/1 but returns iodata for efficient writing to IO devices or
sockets without an intermediate binary.
Arguments
valueis any term encodable as JSON.
Returns
- An iodata value (a binary, a list of binaries, or a list of iodata) containing the JSON
encoding of
value.
Examples
iex> Localize.Translate.JSON.encode_to_iodata!(%{}) |> IO.iodata_to_binary()
"{}"