View Source Jsonrs (jsonrs v0.3.3)

A JSON library powered by Rust's Serde through a NIF

Summary

Functions

Parses a JSON value from input string.

Parses a JSON value from input string.

Generates JSON corresponding to input.

Generates JSON corresponding to input.

Identical to encode/1. Exists to implement Phoenix interface and encodes to a single normal string.

Identical to encode!/1. Exists to implement Phoenix interface and encodes to a single normal string.

Types

Link to this type

compression_algorithm()

View Source
@type compression_algorithm() :: :gzip | :none
@type compression_level() :: non_neg_integer()
@type compression_options() :: {compression_algorithm(), compression_level()}

Functions

Link to this function

decode(input, opts \\ [])

View Source
@spec decode(iodata(), Keyword.t()) :: {:ok, term()} | {:error, String.t()}

Parses a JSON value from input string.

Examples

iex> Jsonrs.decode("{\"x\":[1,2]}")
{:ok, %{"x" => [1, 2]}}

iex> Jsonrs.decode("invalid")
{:error, "expected value at line 1 column 1"}
Link to this function

decode!(input, opts \\ [])

View Source
@spec decode!(iodata(), Keyword.t()) :: term()

Parses a JSON value from input string.

Similar to decode/2 except it will raise in case of errors.

Examples

iex> Jsonrs.decode!("{\"x\":[1,2]}")
%{"x" => [1, 2]}

iex> Jsonrs.decode!("invalid")
** (ErlangError) Erlang error: "expected value at line 1 column 1"
Link to this function

encode(input, opts \\ [])

View Source
@spec encode(
  term(),
  keyword()
) :: {:ok, String.t()} | {:error, String.t()}

Generates JSON corresponding to input.

Examples

iex> Jsonrs.encode(%{"x" => [1,2]})
{:ok, "{\"x\":[1,2]}"}

iex> Jsonrs.encode("\xFF")
{:error, "Expected to deserialize a UTF-8 stringable term"}
Link to this function

encode!(input, opts \\ [])

View Source
@spec encode!(
  term(),
  keyword()
) :: String.t()

Generates JSON corresponding to input.

Similar to encode/1 except it will raise in case of errors.

Examples

iex> Jsonrs.encode!(%{"x" => [1,2]})
"{\"x\":[1,2]}"

iex> Jsonrs.encode!("\xFF")
** (ErlangError) Erlang error: "Expected to deserialize a UTF-8 stringable term"
Link to this function

encode_to_iodata(input, opts \\ [])

View Source
@spec encode_to_iodata(
  term(),
  keyword()
) :: {:ok, String.t()} | {:error, :encode_error}

Identical to encode/1. Exists to implement Phoenix interface and encodes to a single normal string.

Link to this function

encode_to_iodata!(input, opts \\ [])

View Source
@spec encode_to_iodata!(
  term(),
  keyword()
) :: String.t()

Identical to encode!/1. Exists to implement Phoenix interface and encodes to a single normal string.