Repository navigation
v0.3.0
Retired on Hex:
fast_path:fails to compile. Use 0.3.1.
Breaking changes
cast:callbacks return{:ok, value},:error, or{:error, reason}, likeEcto.Type.cast/1. Rejections become aJSONCodec.Errorwithreason: :invalid_value, sodecode/1andfrom_map/1return errors instead of crashing. Wrap results in{:ok, value}; plain captures like&String.trim/1need a wrapper.- Invalid JSON is a
JSONCodec.Errorwithreason: :invalid_json, not aJason.DecodeError. use JSONCodecno longer replacesdefstruct. A field is required unless its type allowsnilor it has a non-nildefault, sodefstruct name: nilwith a non-nullable type is now required.- Removed
to_map/1(usedump/1, which respects JSON field names) and thejson_schema/0,1aliases (useschema/0,1). - The fast clause is always generated and the
fast_path:option has no effect.use JSONCodecrejects unknown options at compile time. - Modules outside JSONCodec provide schemas by implementing the
JSONCodec.Schemabehaviour instead of just exportingjson_schema/0; the callback isschema/0. dump/1returns structs that are not codecs, such asDateTime, unchanged for the JSON encoder instead of flattening their fields.
Changed
- Parse with Elixir's
JSONmodule on Elixir 1.18+, falling back to Jason, which becomes optional. - Generated decoders specialize more at compile time: nested codecs are called directly, atom policies are resolved when the codec compiles, defaults are inlined, and list and map value error paths are built only on failure. On the program facts benchmark this halves BEAM reductions, cuts memory by a third, and brings decoding from about 1.34× to about 1.1× the time of handwritten code.
- Unknown
atom:policies are compile errors instead of failing on the first decoded atom. - Defaults bypass
cast:, type decoding, andtransform:; a missing optional field takes its struct default as is.
Fixed
codec/2options can use module attributes, such asatom: {:enum, @states}; anonymous function callbacks now fail at compile time.- Missing required fields report
got: nilinstead of an internal marker.
Added
JSONCodec.Error.detailsholds the cast's rejection reason or the JSON parser message.