ForkPoison (ForkForkPoison v6.0.1)

Copy Markdown View Source

ForkPoison

Build Status Coverage Status) Hex.pm Version Hex.pm Download Total Hex.pm Dependents

Note: This is a forked version from Poison to update Decimal v3

ForkPoison is a new JSON library for Elixir focusing on wicked-fast speed without sacrificing simplicity, completeness, or correctness.

ForkPoison takes several approaches to be the fastest JSON library for Elixir.

ForkPoison uses extensive sub binary matching, a hand-rolled parser using several techniques that are known to benefit BeamAsm for JIT compilation, IO list encoding and single-pass decoding.

ForkPoison benchmarks sometimes puts ForkPoison's performance close to jiffy and usually faster than other Erlang/Elixir libraries.

ForkPoison fully conforms to RFC 8259, ECMA 404, and fully passes the JSONTestSuite.

Installation

First, add ForkPoison to your mix.exs dependencies:

def deps do
  [{:poison, "~> 6.0"}]
end

Then, update your dependencies:

mix deps.get

Usage

ForkPoison.encode!(%{"age" => 27, "name" => "Devin Torres"})
#=> "{\"name\":\"Devin Torres\",\"age\":27}"

ForkPoison.decode!(~s({"name": "Devin Torres", "age": 27}))
#=> %{"age" => 27, "name" => "Devin Torres"}

defmodule Person do
  @derive [ForkPoison.Encoder]
  defstruct [:name, :age]
end

ForkPoison.encode!(%Person{name: "Devin Torres", age: 27})
#=> "{\"name\":\"Devin Torres\",\"age\":27}"

ForkPoison.decode!(~s({"name": "Devin Torres", "age": 27}), as: %Person{})
#=> %Person{name: "Devin Torres", age: 27}

ForkPoison.decode!(~s({"people": [{"name": "Devin Torres", "age": 27}]}),
  as: %{"people" => [%Person{}]})
#=> %{"people" => [%Person{age: 27, name: "Devin Torres"}]}

Every component of ForkPoison (encoder, decoder, and parser) are all usable on their own without buying into other functionality. For example, if you were interested purely in the speed of parsing JSON without a decoding step, you could simply call ForkPoison.Parser.parse.

Parser

iex> ForkPoison.Parser.parse!(~s({"name": "Devin Torres", "age": 27}), %{})
%{"name" => "Devin Torres", "age" => 27}
iex> ForkPoison.Parser.parse!(~s({"name": "Devin Torres", "age": 27}), %{keys: :atoms!})
%{name: "Devin Torres", age: 27}

Note that keys: :atoms! reuses existing atoms, i.e. if :name was not allocated before the call, you will encounter an argument error message.

You can use the keys: :atoms variant to make sure all atoms are created as needed. However, unless you absolutely know what you're doing, do not do it. Atoms are not garbage-collected, see Erlang Efficiency Guide for more info:

Atoms are not garbage-collected. Once an atom is created, it will never be removed. The emulator will terminate if the limit for the number of atoms (1048576 by default) is reached.

Encoder

iex> ForkPoison.Encoder.encode([1, 2, 3], %{}) |> IO.iodata_to_binary
"[1,2,3]"

Anything implementing the Encoder protocol is expected to return an IO list to be embedded within any other Encoder's implementation and passable to any IO subsystem without conversion.

defimpl ForkPoison.Encoder, for: Person do
  def encode(%{name: name, age: age}, options) do
    ForkPoison.Encoder.BitString.encode("#{name} (#{age})", options)
  end
end

For maximum performance, make sure you @derive [ForkPoison.Encoder] for any struct you plan on encoding.

Encoding only some attributes

When deriving structs for encoding, it is possible to select or exclude specific attributes. This is achieved by deriving ForkPoison.Encoder with the :only or :except options set:

defmodule PersonOnlyName do
  @derive {ForkPoison.Encoder, only: [:name]}
  defstruct [:name, :age]
end

defmodule PersonWithoutName do
  @derive {ForkPoison.Encoder, except: [:name]}
  defstruct [:name, :age]
end

In case both :only and :except keys are defined, the :except option is ignored.

Key Validation

According to RFC 8259 keys in a JSON object should be unique. This is enforced and resolved in different ways in other libraries. In the Ruby JSON library for example, the output generated from encoding a hash with a duplicate key (say one is a string, the other an atom) will include both keys. When parsing JSON of this type, Chromium will override all previous values with the final one.

ForkPoison will generate JSON with duplicate keys if you attempt to encode a map with atom and string keys whose encoded names would clash. If you'd like to ensure that your generated JSON doesn't have this issue, you can pass the strict_keys: true option when encoding. This will force the encoding to fail.

Note: Validating keys can cause a small performance hit.

iex> ForkPoison.encode!(%{:foo => "foo1", "foo" => "foo2"}, strict_keys: true)
** (ForkPoison.EncodeError) duplicate key found: :foo

Benchmarking

MIX_ENV=bench mix run bench/run.exs

Current Benchmarks

As of 2024-06-06:

License

ForkPoison is released under the public-domain-equivalent 0BSD license.

Summary

Functions

Decode JSON to a value.

Decode JSON to a value. Raises an exception on error.

Encode a value to JSON.

Encode a value to JSON. Raises an exception on error.

Encode a value to JSON IO data.

Encode a value to JSON IO data. Raises an exception on error.

Functions

decode(iodata, options \\ %{})

Decode JSON to a value.

iex> ForkPoison.decode("[1,2,3]")
{:ok, [1, 2, 3]}

iex> ForkPoison.decode("[")
{:error, %ForkPoison.ParseError{data: "[", skip: 1, value: nil}}

decode!(value, options \\ %{})

Decode JSON to a value. Raises an exception on error.

iex> ForkPoison.decode!("[1,2,3]")
[1, 2, 3]

iex> ForkPoison.decode!("[")
** (ForkPoison.ParseError) unexpected end of input at position 1

encode(value, options \\ %{})

Encode a value to JSON.

iex> ForkPoison.encode([1, 2, 3])
{:ok, "[1,2,3]"}

iex> ForkPoison.encode({})
{:error, %ForkPoison.EncodeError{message: nil, value: {}}}

encode!(value, options \\ %{})

Encode a value to JSON. Raises an exception on error.

iex> ForkPoison.encode!([1, 2, 3])
"[1,2,3]"

iex> ForkPoison.encode!({})
** (ForkPoison.EncodeError) unable to encode value: {}

encode_to_iodata(value, options \\ %{})

@spec encode_to_iodata(
  ForkPoison.Encoder.t(),
  ForkPoison.Encoder.options() | [ForkPoison.Encoder.option()]
) :: {:ok, iodata()} | {:error, ForkPoison.EncodeError.t()}

Encode a value to JSON IO data.

iex> ForkPoison.encode_to_iodata([1, 2, 3])
{:ok, [91, ["1", 44, "2", 44, "3"], 93]}

iex> ForkPoison.encode_to_iodata({})
{:error, %ForkPoison.EncodeError{message: nil, value: {}}}

encode_to_iodata!(value, options \\ %{})

Encode a value to JSON IO data. Raises an exception on error.

iex> ForkPoison.encode_to_iodata!([1, 2, 3])
[91, ["1", 44, "2", 44, "3"], 93]

iex> ForkPoison.encode_to_iodata!({})
** (ForkPoison.EncodeError) unable to encode value: {}