atomvm_gleam/json
Types
Opaque continuation_state() from streaming decode.
pub type Continuation
Input for decode_continue.
pub type ContinueInput {
Bytes(BitArray)
EndOfInput
}
Constructors
-
Bytes(BitArray)Next JSON chunk (
binary()). -
EndOfInputFinalize a partial decode (
end_of_input).
Result of decode_start / decode_continue.
pub type DecodeProgress(result, acc) {
Complete(result, acc, BitArray)
NeedMore(Continuation)
}
Constructors
-
Complete(result, acc, BitArray)Finished value, updated accumulator, and unconsumed trailing bytes.
-
NeedMore(Continuation)Need another chunk (or
EndOfInput).
Opaque OTP decoders() map — use default_decoders
for the empty map (OTP built-in callbacks).
pub type Decoders
Wrappers for AtomVM’s OTP-compatible json module.
Source: json.erl.
Docs: Module json.
Decode returns Erlang terms (maps, lists, binaries, numbers, booleans,
null) — same as today; this module does not invent a parallel JSON AST.
Encode helpers that take an Encoder use the default OTP
encoder from default_encoder (fun json:encode_value/2).
Custom encoder/decoder callback funs are not expressible cleanly from Gleam
and are not wrapped; pass only the opaque defaults (or Erlang funs obtained
outside this API).
Opaque OTP encoder() — use default_encoder.
pub type Encoder
Values
pub fn decode(bytes: BitArray) -> Result(a, Error)
Decode a JSON binary to an Erlang term.
See json:decode/1.
pub fn decode_continue(
input: ContinueInput,
state: Continuation,
) -> Result(DecodeProgress(result, acc), Error)
Continue a streaming decode (json:decode_continue/2).
pub fn decode_start(
bytes: BitArray,
acc: acc,
decoders: Decoders,
) -> Result(DecodeProgress(result, acc), Error)
Begin a streaming decode (json:decode_start/3).
See json:decode_start/3.
pub fn decode_with(
bytes: BitArray,
acc: acc,
decoders: Decoders,
) -> Result(#(result, acc, BitArray), Error)
Decode with accumulator and decoder options (json:decode/3).
Returns #(value, acc, rest). Use default_decoders
for OTP defaults.
See json:decode/3.
pub fn default_decoders() -> Decoders
Empty decoder options map (#{}) — OTP default callbacks for arrays,
objects, floats, integers, strings, and null.
See json:decode/3.
pub fn default_encoder() -> Encoder
Default OTP encoder (fun json:encode_value/2).
See json:encode/2.
pub fn encode(term: a) -> Result(BitArray, Error)
Encode a term to a JSON binary (json:encode/1 → iolist flattened).
See json:encode/1.
pub fn encode_atom(
atom: a,
encoder: Encoder,
) -> Result(BitArray, Error)
Encode an atom (null / true / false, or via the encoder as a string).
See json:encode_atom/2.
pub fn encode_binary(bytes: BitArray) -> Result(BitArray, Error)
Encode a binary as a JSON string (escape control / quote / backslash).
See json:encode_binary/1.
pub fn encode_binary_escape_all(
bytes: BitArray,
) -> Result(BitArray, Error)
Encode a binary escaping all non-ASCII as \uXXXX.
pub fn encode_float(float: Float) -> Result(BitArray, Error)
Encode a float (float_to_binary short form).
See json:encode_float/1.
pub fn encode_integer(int: Int) -> Result(BitArray, Error)
Encode an integer.
pub fn encode_key_value_list(
pairs: List(#(k, v)),
encoder: Encoder,
) -> Result(BitArray, Error)
Encode a key/value list as a JSON object.
pub fn encode_key_value_list_checked(
pairs: List(#(k, v)),
encoder: Encoder,
) -> Result(BitArray, Error)
Encode a key/value list as a JSON object, rejecting duplicate keys.
pub fn encode_list(
list: List(a),
encoder: Encoder,
) -> Result(BitArray, Error)
Encode a list as a JSON array.
See json:encode_list/2.
pub fn encode_map(
map: a,
encoder: Encoder,
) -> Result(BitArray, Error)
Encode a map as a JSON object.
See json:encode_map/2.
pub fn encode_map_checked(
map: a,
encoder: Encoder,
) -> Result(BitArray, Error)
Encode a map as a JSON object, rejecting duplicate keys.
pub fn encode_value(
term: a,
encoder: Encoder,
) -> Result(BitArray, Error)
Encode a value by dispatching on its Erlang type (json:encode_value/2).
See json:encode_value/2.
pub fn encode_with(
term: a,
encoder: Encoder,
) -> Result(BitArray, Error)
Encode with an explicit encoder callback (json:encode/2).
Prefer default_encoder unless you have an Erlang
encoder() fun from elsewhere.
See json:encode/2.