TypeDB.JSON behaviour (TypeDB v0.2.2)

Copy Markdown View Source

JSON codec indirection.

JSON needs no dependency of its own. The codec is resolved, once, in this order:

  1. the module configured as config :typedb, :json_codec, MyCodec
  2. TypeDB.JSON.Native, backed by the built-in JSON module — this is what every supported Elixir version (1.18+) resolves to
  3. TypeDB.JSON.Jason, when the host application depends on :jason
  4. otherwise an error is raised, asking you to pick one

A codec is any module implementing this behaviour.

Summary

Callbacks

Decodes a JSON binary. Returns {:error, reason} on malformed input.

Encodes a term to JSON iodata. Raises on unencodable input.

Functions

Returns the resolved codec module.

Decodes a JSON binary.

Encodes term to a JSON binary.

Encodes term to JSON iodata.

Forgets the memoised codec. Intended for tests.

Callbacks

decode(binary)

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

Decodes a JSON binary. Returns {:error, reason} on malformed input.

encode_to_iodata!(term)

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

Encodes a term to JSON iodata. Raises on unencodable input.

Functions

codec()

@spec codec() :: module()

Returns the resolved codec module.

The result is memoised in :persistent_term. Change :json_codec before the first call, or call reset/0 afterwards.

decode(binary)

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

Decodes a JSON binary.

encode!(term)

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

Encodes term to a JSON binary.

encode_to_iodata!(term)

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

Encodes term to JSON iodata.

reset()

@spec reset() :: :ok

Forgets the memoised codec. Intended for tests.