View Source Json5 (json5 v0.4.0)

Convert Json5 to Elixir term and back

Summary

Functions

parse json5 input as elixir type.

Same as decode/2 but raises on error

Encode elixir input as json5. Contains some simple formatting options

Same as encode/2 but raises on error

Functions

Link to this function

decode(text, opts \\ [])

View Source

parse json5 input as elixir type.

To keep the precision of the given numbers the integers and floats are cast to Decimal

options:

  • object_key_function: (binary) -> any
    • use given function to format the object key
  • object_key_existing_atom: boolean
  • object_key_atom: boolean
    • format the object key with String.to_atom/1
    • if none of the above options are set return the key as a binary (String.t())
  • object_new_function: ({any, any}) -> any
    • function to create a map from the list of parsed tuples, by default uses Map.new/1
  • backend: [Json5.Decode.Backend.Combine, Json5.Decode.Backend.Yecc]
    • select the backend to be used (Defaults to Combine).
    • The Combine backend is coded with the json5 spec (with unicode) in mind, but a lot slower (about 2000x slower than Jason)
    • The Yecc backend is a lot faster (about 6x slower than Jason) but not that rigorous based on the json5 spec. It is just written to make the existing tests work.
iex> Json5.decode("{array: [1, 2, 3], map: {'null': null, test: 1, }, }")
{:ok, %{
  "map" => %{
    "test" => Decimal.new(1), 
    "null" => nil
  }, 
  "array" => [
    Decimal.new(1), 
    Decimal.new(2), 
    Decimal.new(3)
  ]
}}
Link to this function

decode!(text, opts \\ [])

View Source

Same as decode/2 but raises on error

Link to this function

encode(input, opts \\ [])

View Source

Encode elixir input as json5. Contains some simple formatting options

options:

  • pretty: boolean
  • compact: boolean
iex> Json5.encode(%{map: %{test: 1, null: nil}, array: [1,2,3]})
{:ok, "{array: [1, 2, 3], map: {'null': null, test: 1, }, }"}
iex> Json5.encode(%{map: %{test: 1, null: nil}, array: [1,2,3]}, pretty: true)
{:ok, """
  {
    array: [
      1,
      2,
      3,
    ],
    map: {
      'null': null,
      test: 1,
    },
  }
  """}
iex> Json5.encode(%{map: %{test: 1, null: nil}, array: [1,2,3]}, compact: true)
{:ok, "{array:[1,2,3],map:{'null':null,test:1}}"}
Link to this function

encode!(input, opts \\ [])

View Source

Same as encode/2 but raises on error