Jexon (jexon v0.9.2)

Library to parse JSON and convert structs into maps or json without deriving any encoder.

Summary

Functions

Returns (casted) value on given JSON

Returns JSON of any given datatype

Returns a map of the struct or atom map but keeps the key identity

Functions

Link to this function

from_json(json, opts \\ [])

@spec from_json(json :: String.t(), opts :: keyword()) ::
  {:ok, any()} | {:error, Jason.DecodeError.t()}

Returns (casted) value on given JSON

Options

  • raw, boolean, default: false
    • perform no type casting and keep keys as strings
Link to this function

to_json(data, opts \\ [])

@spec to_json(data :: any(), opts :: keyword()) ::
  {:ok, json :: String.t()} | {:error, Jason.EncodeError.t() | Exception.t()}

Returns JSON of any given datatype

Options

  • with_struct_info, boolean, default: true
    • includes __struct__ key
  • with_type_info, boolean, default: true
    • convert all non-existing types into lists starting with the elixir type as __<type>__
      • e.g. {1,2,3} = ["__tuple__", 1, 2, 3]

Example Usage

iex> data = %{foo: 1, baz: {2}, bar: :lol}
iex> Jexon.to_json(data, with_type_info: true)
{:ok, ~s/{"__atom__:bar":["__atom__","lol"],"__atom__:baz":["__tuple__",2],"__atom__:foo":1}/}
Link to this function

to_map(data, opts \\ [])

@spec to_map(data :: struct() | map(), opts :: keyword()) :: map()

Returns a map of the struct or atom map but keeps the key identity

Options

  • with_struct_info, boolean, default: true
    • includes __struct__ key
  • keep_key_identity, boolean, default: false
    • encodes atom type if key is an atom

Example Usage

iex> {:ok, datetime} = DateTime.new(~D[2018-07-28], ~T[12:30:00])
iex> Jexon.to_map(datetime)
%{
  "calendar" => Calendar.ISO,
  "day" => 28,
  "hour" => 12,
  "microsecond" => {0, 0},
  "minute" => 30,
  "month" => 7,
  "second" => 0,
  "std_offset" => 0,
  "time_zone" => "Etc/UTC",
  "utc_offset" => 0,
  "year" => 2018,
  "zone_abbr" => "UTC",
  "__struct__" => DateTime
}