Shim module that adapts Erlang's built-in :json (OTP 27+) to
Postgrex's expected JSON-library interface.
Postgrex's jsonb extension calls
library.encode_to_iodata!/1 and library.decode!/1 (the
shapes Jason exposes). Erlang's :json exposes encode/2 and
decode/3, so this module bridges the two. It also configures
nil ↔ JSON null translation in both directions, sparing
application code from the :null atom the raw :json module
returns.
Use from Postgrex
Postgrex.Types.define(MyApp.PostgresTypes, [], json: Tempo.SQL.JSON)
Summary
Functions
Decodes a JSON binary, in the shape Postgrex expects of a JSON library.
Encodes a term as JSON iodata, in the shape Postgrex expects of a JSON library.
Functions
Decodes a JSON binary, in the shape Postgrex expects of a JSON library.
Arguments
binaryis the JSON document to decode.
Returns
- The decoded term, with JSON
nullbecomingnilrather than the:nullatom.
Examples
iex> Tempo.SQL.JSON.decode!(~s({"hours":8}))
%{"hours" => 8}
iex> Tempo.SQL.JSON.decode!(~s({"ends":null}))
%{"ends" => nil}
Encodes a term as JSON iodata, in the shape Postgrex expects of a JSON library.
Arguments
termis any JSON-serialisable term.
Returns
- The encoded JSON as iodata.
nilencodes as JSONnullrather than as the:nullatom the raw:jsonmodule uses.
Examples
iex> Tempo.SQL.JSON.encode_to_iodata!(%{"hours" => 8})
...> |> IO.iodata_to_binary()
~s({"hours":8})
iex> Tempo.SQL.JSON.encode_to_iodata!(%{"ends" => nil})
...> |> IO.iodata_to_binary()
~s({"ends":null})