Monzo.JSON (monzo_client v1.0.0)

Copy Markdown View Source

A small, dependency-free JSON encoder/decoder.

monzo deliberately vendors its own minimal JSON codec rather than depending on Jason (or any other library) so that it never forces a version choice on a host application. It supports every JSON construct needed to talk to the Monzo API: objects, arrays, strings (with unicode escapes), numbers, booleans, and null.

This is not meant to be a general-purpose, maximally-optimised JSON library - if your application already depends on Jason, feel free to swap it in via a custom Monzo.HTTP.Adapter if you need different performance characteristics. For typical API payload sizes, this codec is more than fast enough.

Summary

Functions

Decodes a JSON string into Elixir terms. Objects become maps with string keys, arrays become lists, and numbers become integers or floats as appropriate.

Encodes an Elixir term to a JSON string.

Types

json()

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

Functions

decode(binary)

@spec decode(String.t()) :: {:ok, json()} | {:error, term()}

Decodes a JSON string into Elixir terms. Objects become maps with string keys, arrays become lists, and numbers become integers or floats as appropriate.

decode!(binary)

@spec decode!(String.t()) :: json()

encode(term)

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

Encodes an Elixir term to a JSON string.

Maps, keyword lists (of primitives), lists, strings, numbers, atoms (true/false/nil), and any struct implementing Monzo.JSON.Encoder (or a plain map after Map.from_struct/1) are supported.

encode!(value)

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