StableJason
is a library for encoding Elixir values to a stable JSON with deterministic sorting for keys.
It works similar like OJSON but can output a pretty JSON string, using Jason as the underlying JSON encoder.
The initial use-case for this library is to display a diff between two JSON files. The problem with JSON is that the order of keys is not guaranteed, so a diff between two JSON files can be very noisy. By using StableJason
to encode the JSON files, the order of keys is deterministic, so the diff will be much more readable.
StableJason.encode(%{c: 3, b: 2, a: 1})
{:ok, ~S|{"a":1,"b":2,"c":3}|}
StableJason.encode(%{c: 3, b: 2, a: 1}, pretty: true)
{:ok, "{\n \"a\": 1,\n \"b\": 2,\n \"c\": 3\n}"}
StableJason.encode!(%{c: 3, b: 2, a: 1})
"{\"a\":1,\"b\":2,\"c\":3}"
StableJason.encode!(%{c: 3, b: 2, a: 1}, pretty: true)
"{\n \"a\": 1,\n \"b\": 2,\n \"c\": 3\n}"
Add stable_jason
to your list of dependencies in mix.exs
:
def deps do
[
{:stable_jason, "~> 1.0.0"}
]
end
StableJason is released under the Apache License 2.0 - see the LICENSE file.