JSV.Codec (jsv v0.7.2)

View Source

JSON encoder/decoder based on available implementation.

First looks for Jason, then Poision, then JSON (available since Elixir 1.18).

Summary

Functions

Returns the module used for JSON encoding and decoding.

Equivalent to JSON.decode/1.

Equivalent to JSON.decode!/1.

Equivalent to JSON.encode!/1.

Equivalent to JSON.encode!/1 with human readable indentation.

Equivalent to JSON.encode!/1 with map keys ordered according to the sorter function.

Like format_ordered!/2 but returns iodata instead of strings.

Equivalent to JSON.encode_to_iodata!/1 with human readable indentation.

Types

key()

@type key() :: binary() | atom()

key_sorter()

@type key_sorter() :: (key(), key() -> boolean())

Functions

codec()

@spec codec() :: module()

Returns the module used for JSON encoding and decoding.

decode(json)

@spec decode(binary()) :: {:ok, term()} | {:error, term()}

Equivalent to JSON.decode/1.

decode!(json)

@spec decode!(binary()) :: term()

Equivalent to JSON.decode!/1.

encode!(term)

@spec encode!(term()) :: binary()

Equivalent to JSON.encode!/1.

encode_to_iodata!(term)

@spec encode_to_iodata!(term()) :: iodata()

Equivalent to JSON.encode_to_iodata!/1.

format!(term)

@spec format!(term()) :: binary()

Equivalent to JSON.encode!/1 with human readable indentation.

Requires Jason or Poison for Elixir versions before 1.18 and OTP before 27.0.

format_ordered!(term, key_sorter)

@spec format_ordered!(term(), key_sorter()) :: binary()

Equivalent to JSON.encode!/1 with map keys ordered according to the sorter function.

The sorter function will be called with two keys from the same map and should return true if the first argument precedes or is in the same place as the second one.

Requires Jason for Elixir versions before 1.18 and OTP before 27.1.

Data must be normalized before ordered encoding. Passing structs will result in an error. Use JSV.Normalizer.normalize/1 to normalize data in a compatible form.

format_ordered_to_iodata!(term, key_sorter)

@spec format_ordered_to_iodata!(term(), key_sorter()) :: iodata()

Like format_ordered!/2 but returns iodata instead of strings.

format_to_iodata!(term)

@spec format_to_iodata!(term()) :: binary()

Equivalent to JSON.encode_to_iodata!/1 with human readable indentation.