TypeDB.JSON behaviour (TypeDB v0.7.0)

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

In practice the list ends there. This driver requires Elixir 1.18, every version of which has the built-in JSON, so step 2 always matches and TypeDB.JSON.Jason is only ever reached by configuring it:

config :typedb, :json_codec, TypeDB.JSON.Jason

resolve!/0 still falls back to Jason and then to an error below step 2. Neither can execute while the Elixir floor is 1.18, and both are kept because they are unreachable owing to that floor rather than to being wrong — deleting them would turn a future change of floor into a silent change of behaviour.

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.