spectra_json (spectra v0.11.1)
View SourceJSON encode and decode traversal for sp_type() values.
to_json/4 walks an Erlang term against an sp_type() and produces a
json:encode_value() (a map/list/scalar tree accepted by json:encode/1).
from_json/4 does the inverse: it walks a decoded JSON value and produces
the corresponding Erlang term.
Neither function calls json:encode/1 or json:decode/1 — that is the
responsibility of the caller (typically spectra.erl).
Codec dispatch is handled mid-traversal at #sp_user_type_ref{},
#sp_remote_type{}, and #sp_rec_ref{} nodes via
spectra_codec:try_codec_encode/7 and try_codec_decode/7.
Summary
Functions
Decodes Json into an Erlang term according to Type.
Encodes Data to a JSON-compatible value according to Type.
Functions
-spec from_json(TypeInfo :: spectra:type_info(), Type :: spectra:sp_type(), Json :: json:decode_value(), Config :: spectra:sp_config()) -> {ok, dynamic()} | {error, [spectra:error()]}.
Decodes Json into an Erlang term according to Type.
The inverse of to_json/4. Binary map keys become atom keys, binary atom
values are converted via binary_to_existing_atom/2, JSON null maps to
nil or undefined where the type allows it, and JSON arrays become lists.
Returns {ok, Term} on success or {error, Errors} with structured errors.
-spec to_json(TypeInfo :: spectra:type_info(), Type :: spectra:sp_type(), Data :: dynamic(), Config :: spectra:sp_config()) -> {ok, json:encode_value()} | {error, [spectra:error()]}.
Encodes Data to a JSON-compatible value according to Type.
Walks the type tree recursively, converting each node. Records become maps
with binary keys, lists stay lists, atoms in unions become their binary
representations. Optional fields whose value equals the missing sentinel
(nil / undefined) are omitted from the output.
Returns {ok, Value} on success or {error, Errors} with a list of
structured #sp_error{} values describing every mismatch found.