JSON.Encoder protocol (json v1.4.1) View Source

Defines the protocol required for converting Elixir types into JSON and inferring their json types.

Link to this section Summary

Functions

Returns a JSON string representation of the Elixir term

Returns an atom that reprsents the JSON type for the term

Link to this section Types

Link to this section Functions

Specs

encode(
  tuple()
  | HashDict.t()
  | list()
  | integer()
  | float()
  | map()
  | list()
  | atom()
  | term()
) :: {atom(), bitstring()}

Returns a JSON string representation of the Elixir term

Examples

iex> JSON.Encoder.encode({1, :two, "three"})
{:ok, "[1,\"two\",\"three\"]"}

iex> JSON.Encoder.encode([result: "this will be a elixir result"])
{:ok, "{\"result\":\"this will be a elixir result\"}"}

iex> JSON.Encoder.encode(%{a: 1, b: 2})
{:ok, "{\"a\":1,\"b\":2}"}

Specs

typeof(term()) :: atom()

Returns an atom that reprsents the JSON type for the term

Examples

iex> JSON.Encoder.typeof(3)
:number

iex> JSON.Encoder.typeof({1, :two, "three"})
:array

iex> JSON.Encoder.typeof([foo: "this will be a elixir result"])
:object

iex> JSON.Encoder.typeof([result: "this will be a elixir result"])
:object

iex> JSON.Encoder.typeof(["this will be a elixir result"])
:array

iex> JSON.Encoder.typeof([foo: "bar"])
:object