View Source Tiny (Tiny v1.0.2)

This module contains the entirety of the Tiny project. There are interface functions for encoding and decoding JSON, and little else.

Both encoding and decoding are done per the JSON specification with direct mapping between JSON types and Elixir types:


JSON TypeElixir Type
ArrayList
Booleantrue or false
ObjectMap
NumberInteger or Float
StringBinary
nullnil

Numbers are parsed into Integer types when possible, falling back to Floats when required. This is due to functions which operate differently based on the numeric typing.

Summary

Functions

Safely decodes a JSON input binary to an Elixir term.

Decodes a JSON inout binary to an Elixir term.

Safely encodes a JSON compatible value to iodata or a binary.

Encodes a JSON compatible value to iodata or a binary.

Safely encodes a value to JSON as iodata.

Encodes a value to JSON as iodata.

Types

@type json() :: atom() | binary() | list() | map() | number()

Functions

@spec decode(binary(), Keyword.t()) :: {:ok, json()} | {:error, atom()}

Safely decodes a JSON input binary to an Elixir term.

Parsing errors are caught and an :error Tuple is returned. Any successful values are wrapped in an :ok Tuple.

Link to this function

decode!(bin, opts \\ [])

View Source
@spec decode!(binary(), Keyword.t()) :: json() | no_return()

Decodes a JSON inout binary to an Elixir term.

Rather than return Tuples, this function will raise ArgumentError on invalid inputs. No information is provided beyond the error, so log context as needed.

@spec encode(json(), Keyword.t()) :: {:ok, binary() | iodata()} | {:error, atom()}

Safely encodes a JSON compatible value to iodata or a binary.

Parsing errors are caught and an :error Tuple is returned. Any successful values are wrapped in an :ok Tuple.

Link to this function

encode!(val, opts \\ [])

View Source
@spec encode!(json(), Keyword.t()) :: binary() | iodata() | no_return()

Encodes a JSON compatible value to iodata or a binary.

Rather than return Tuples, this function will raise ArgumentError on invalid inputs. No information is provided beyond the error, so log context as needed.

Link to this function

encode_to_iodata(val, opts \\ [])

View Source
@spec encode_to_iodata(json(), Keyword.t()) :: {:ok, iodata()} | {:error, atom()}

Safely encodes a value to JSON as iodata.

Same as passing [ iodata: true ] to encode/2.

Link to this function

encode_to_iodata!(val, opts \\ [])

View Source
@spec encode_to_iodata!(json(), Keyword.t()) :: iodata() | no_return()

Encodes a value to JSON as iodata.

Same as passing [ iodata: true ] to encode!/2.