Weavr.JSON (Weavr v1.0.0)

Copy Markdown View Source

Minimal, dependency-free JSON encoder/decoder.

The weavr package intentionally ships with zero runtime dependencies (see the package README for rationale). This module implements just enough of RFC 8259 to encode request bodies and decode response bodies for the Weavr API: objects, arrays, strings, numbers, booleans, and null, with UTF-8 string handling.

If your application already depends on Jason, Poison, or OTP 27's built-in :json, you can override JSON handling by configuring:

config :weavr, json_library: Jason

Any module implementing encode!/1 and decode!/1 with the same contract as Jason works as a drop-in replacement.

Known limitation

\uXXXX escapes for codepoints above U+FFFF (i.e. those requiring a UTF-16 surrogate pair, like many emoji) are not supported when written as an explicit escape sequence in the JSON source — only literal UTF-8 bytes for those characters decode correctly. This is vanishingly rare for the JSON Weavr's API actually returns (it controls its own encoder), but if you need full RFC 8259 fidelity, configure Jason as above.

Summary

Functions

Decodes a JSON binary into Elixir terms (maps, lists, numbers, binaries, booleans, nil). Object keys are always returned as strings.

Encodes an Elixir term into a JSON binary.

Types

json()

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

Functions

decode!(binary)

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

Decodes a JSON binary into Elixir terms (maps, lists, numbers, binaries, booleans, nil). Object keys are always returned as strings.

Raises Weavr.JSON.DecodeError on malformed input.

encode!(value)

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

Encodes an Elixir term into a JSON binary.

Maps with atom keys are supported; atom keys are converted to strings. Raises Weavr.JSON.EncodeError on values that cannot be represented.