PhoenixAssets.CanonicalJSON (PhoenixAssets v0.1.0)

View Source

Canonical, byte-stable JSON encoding for the artifacts the library writes.

Encodes with object keys sorted lexicographically at every depth and two-space pretty-printing, so identical data always serialises to identical bytes regardless of map size or insertion order. graph.json is written through this module; a plain map encoder would leak Erlang's map iteration order into the file (maps over 32 keys iterate in hash order), making the asset graph diff noisily between runs and OTP releases.

Scalars defer to the stdlib JSON encoder, so string escaping matches the rest of the library's generated output. Structs are rejected: graph payloads are plain data, and silently encoding a struct would hide a plugin bug.

Summary

Functions

Encodes data as canonical pretty-printed JSON with a trailing newline.

Functions

encode!(data)

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

Encodes data as canonical pretty-printed JSON with a trailing newline.

iex> PhoenixAssets.CanonicalJSON.encode!(%{"b" => [1, true], "a" => nil})
~s({\n  "a": null,\n  "b": [\n    1,\n    true\n  ]\n}\n)