JSONCodec

Compile-time generated codecs for JSON-shaped Elixir structs.

JSONCodec is not another JSON parser. It uses Jason for parsing and focuses on the annoying part that tends to be rewritten in every Elixir project: converting decoded string-keyed JSON maps into nested structs with aliases, defaults, computed fields, explicit atom policy, and schema export.

JSONCodec uses normal Elixir declarations as the source of truth:

defmodule FunctionID do
use JSONCodec
defstruct [:module, :function, :arity, :id]
@type t :: %__MODULE__{
module: String.t(),
function: String.t(),
arity: non_neg_integer(),
id: String.t() | nil
}
computed :id, fn function ->
"#{function.module}.#{function.function}/#{function.arity}"
end
end
defmodule DataRef do
use JSONCodec
defstruct [:type, :function, :name, :index]
@type t :: %__MODULE__{
type: :argument | :return | :variable,
function: FunctionID.t(),
name: atom() | nil,
index: non_neg_integer() | nil
}
codec :name, atom: :unsafe
end

Generated API:

FunctionID.decode!(json)
FunctionID.decode(json)
FunctionID.from_map!(map)
FunctionID.from_map(map)
FunctionID.to_map(struct)
FunctionID.schema()

Top-level helpers are also available:

JSONCodec.decode!(json, FunctionID)
JSONCodec.from_map!(map, FunctionID)
JSONCodec.schema(FunctionID)

Why another JSON library?

Because this is not trying to compete with JSON parsers. It sits after parsing.

Most Elixir JSON code starts with Jason.decode!/1, then hand-rolls from_map!/1 functions forever:

def from_map!(%{"from" => from, "to" => to} = map) do
%DataFlow{
from: DataRef.from_map!(from),
to: DataRef.from_map!(to),
through: Enum.map(Map.get(map, "through", []), &DataRef.from_map!/1),
variable_names: Enum.map(Map.get(map, "variable_names", []), &String.to_atom/1)
}
end

JSONCodec generates that boring code from normal struct/typespec declarations.

LibraryMain jobStruct decodeNested structsField aliasesComputed fieldsAtom policyHot-path goal
JasonJSON parser/encoderNoNoNoNokey option onlyparsing speed
Poisonas:parser + old struct decodeYesLimitedNoNokey optionlegacy parser path
Spectraltypespec-driven serialization/schemaYesYesYesvia codecssafe existing atomsvalidation/type coverage
Exdantic/Elixact/Zoi/Dropsvalidation frameworksSometimesYesSometimesYesframework-specificvalidation UX
TaramsPhoenix params castingMap outputNested mapsYestransformscasting-specificrequest params
SimpleSchemaJSON validation + structYesYesYescustom callbackslimitedvalidation pipeline
JSONCodecgenerated JSON-shaped struct codecsYesYesYesYesexplicit per fieldnear-handwritten decode

Use Jason for parsing. Use Tarams/Ecto for Phoenix params. Use a validation framework when rich validation is the main goal. Use JSONCodec when you own the struct shape and want fast, boring, explicit map-to-struct codecs.

Codec metadata

Most fields need no JSONCodec-specific declaration. Defaults come from defstruct; types come from @type t.

defmodule PackageManifest do
use JSONCodec, case: :camel, fast_path: :json
defstruct [:name, :version, dev_dependencies: %{}]
@type t :: %__MODULE__{
name: String.t(),
version: String.t() | nil,
dev_dependencies: %{String.t() => String.t()}
}
end

:camel maps :dev_dependencies to "devDependencies" automatically.

fast_path: :json generates an optimized first from_map!/1 clause for normal Jason-decoded JSON maps with string keys. If that fast string-key clause does not match, JSONCodec falls back to the full generic decoder, including atom-key lookup and detailed missing-field handling.

Use codec/2 for exceptions and special behavior:

codec :not_found, as: "not_found"
codec :variable_names, atom: :unsafe
codec :rotate, transform: :normalize_rotate

Local callback atoms are expanded to functions in the same module:

codec :rotate, transform: :normalize_rotate
# calls normalize_rotate(value)
codec :icons, values: :icon_value
# calls icon_value(key, value, source_map)

Remote captures are also supported:

codec :rotate, transform: &MyTransforms.normalize_rotate/1
codec :icons, values: &MyTransforms.icon_value/3

Advanced map value callbacks

For map fields, values: transforms each raw map value before JSONCodec decodes it as the declared value type:

codec :icons, values: :icon_value
# icon_value(key, raw_value, source_map) -> raw_value_for_normal_decode

If that callback needs shared context, use values_source: to compute the third argument once per map field:

codec :icons, values: :icon_value, values_source: :icon_defaults
# icon_defaults(source_map) -> defaults
# icon_value(key, raw_value, defaults) -> raw_value_for_normal_decode

For map-heavy data where a custom decoder is clearer or faster, decode_values: returns the final decoded map value directly:

codec :icons, decode_values: :decode_icon, values_source: :icon_defaults
# icon_defaults(source_map) -> defaults
# decode_icon(key, raw_value, defaults) -> final decoded value

Remote captures work for these callbacks too:

codec :icons, values: &MyTransforms.icon_value/3,
values_source: &MyTransforms.icon_defaults/1
codec :icons, decode_values: &MyTransforms.decode_icon/3,
values_source: &MyTransforms.icon_defaults/1

Atom policy is explicit:

codec :status, atom: :existing
codec :variable_name, atom: :unsafe

:unsafe uses String.to_atom/1; only use it for bounded/trusted internal data.

Supported type shapes

Read from @type t:

Schema export

Each codec module exports a JSON Schema-compatible map:

FunctionID.schema()
JSONCodec.schema(FunctionID)

json_schema/0 and JSONCodec.json_schema/1 are also available as explicit aliases.

This is intentionally compatible with the direction of JSONSpec: codecs are the fast construction layer; schema validation can remain a separate layer.

Benchmarks

Run:

MIX_ENV=dev mix run bench/program_facts_like.exs

Machine used for this snapshot: Apple M5, Elixir 1.20, Erlang/OTP 29. Payload: 142 KB, 250 nested data_flow records.

Caseipsavgmemory
JSONCodec map→struct4119.810.24 ms0.35 MB
handwritten map→struct4009.640.25 ms0.25 MB
Jason.decode only1378.280.73 ms1.10 MB
Spectral pre-decoded1252.960.80 ms3.23 MB
handwritten Jason+struct980.431.02 ms1.34 MB
JSONCodecJason+struct972.521.03 ms1.45 MB
Spectral native JSON654.311.53 ms4.06 MB

Interpretation:

Installation

{:json_codec, "~> 0.1.1"}

Development

See CHANGELOG.md for release notes.

This project was bootstrapped with VibeKit conventions.

mix deps.get
mix test
mix ci