Jexon (jexon v0.9.0)

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

Summary

Functions

Returns struct or map on given JSON and cast back elixir types

Returns JSON of any given map or struct.

Returns a string map of the struct or atom map

Functions

Link to this function

from_json(json, opts \\ [])

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

Returns struct or map on given JSON and cast back elixir types

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 :: struct() | map(), opts :: keyword()) ::
  {:ok, json :: String.t()} | {:error, Jason.EncodeError.t() | Exception.t()}

Returns JSON of any given map or struct.

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/{"bar":["__atom__","lol"],"baz":["__tuple__",2],"foo":1}/}
Link to this function

to_map(data, opts \\ [])

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

Returns a string map of the struct or atom map

Options

  • with_struct_info, boolean, default: true
    • includes __struct__ key

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
}