Synctera.JSON (Synctera v1.0.0)

Copy Markdown View Source

A small, dependency-free JSON encoder/decoder.

This exists because synctera ships with zero runtime dependencies (see the "Design notes" section of the README) — it does not require callers to bring Jason or any other JSON library. It decodes objects into maps with string keys (never atoms, to avoid unbounded atom-table growth from server-controlled data), and encodes maps, lists, and the usual scalar types.

Not a general-purpose replacement for Jason — it covers exactly what the Synctera API's JSON needs (UTF-8 text, standard escapes, RFC 8259 numbers) and nothing more.

Summary

Types

Like json/0, but map keys may also be atoms (they get encoded as strings).

Functions

Decodes a JSON binary. Objects become maps with binary (string) keys.

Like decode/1 but raises on error.

Encodes an Elixir term to a JSON binary. Map keys may be atoms or binaries.

Like encode/1 but raises on error.

Types

encodable()

@type encodable() ::
  nil
  | boolean()
  | number()
  | binary()
  | [encodable()]
  | %{optional(atom() | binary()) => encodable()}

Like json/0, but map keys may also be atoms (they get encoded as strings).

json()

@type json() ::
  nil
  | boolean()
  | number()
  | binary()
  | [json()]
  | %{optional(binary()) => json()}

Functions

decode(bin)

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

Decodes a JSON binary. Objects become maps with binary (string) keys.

decode!(bin)

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

Like decode/1 but raises on error.

encode(term)

@spec encode(term()) :: {:ok, binary()} | {:error, Exception.t()}

Encodes an Elixir term to a JSON binary. Map keys may be atoms or binaries.

encode!(term)

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

Like encode/1 but raises on error.