egze

egze

StableJason - a library for encoding Elixir values to a stable JSON with deterministic sorting for keys

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.

It also supports a :sorter option (:asc, :desc, or a function), if you need to override it even further.

Examples

StableJason.encode(%{c: 3, b: 2, a: 1})
{:ok, ~S|{"a":1,"b":2,"c":3}|}

StableJason.encode(%{c: 3, b: 2, a: 1}, sorter: :asc, 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}, sorter: :asc, pretty: true)
"{\n  \"a\": 1,\n  \"b\": 2,\n  \"c\": 3\n}"

Links

Hex.pm: https://hex.pm/packages/stable_jason
Hexdocs: https://hexdocs.pm/stable_jason/readme.html
GitHub:

Most Liked

LostKobrakai

LostKobrakai

Jason can already decode to Jason.OrderedObject and encode this again preserving ordering.

egze

egze

Yes, that’s what the library is using, but also sorts (which Jason doesn’t do).

mudasobwa

mudasobwa

Creator of Cure

FWIW, if the shape is known in advance, one might reach out to Estructura.diff/3 to get a difference between two JSON objects.

Where Next?

Popular in Libraries Top

Qqwy
TypeCheck: Fast and flexible runtime type-checking for your Elixir projects. Core ideas Type- and function specifications are const...
336 13801 100
New
hpopp
After just over two years in development, this latest version of Pigeon is what I finally consider done in regards to my original vision ...
New
kevinlang
Hey all, We have made an Ecto3 Adapter for SQLite3, ecto_sqlite3! We have successfully on-boarded the full suite of integration tests (...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
treble37
Just looking for a little feedback on a tiny helper library I built - Sometimes I find the need to convert maps with atom keys to maps...
New
arkgil
Hi all! I’m happy to announce that Telemetry v0.3.0 is out! This release marks the conversion from Elixir to Erlang so that all the libr...
New
ericlathrop
I built a silly site for Halloween that uses Phoenix Channels on the backend, and React on the frontend. I had many problems integrating ...
New
Crowdhailer
Experimenting with this code. OK.try do user <- fetch_user(1) cart <- fetch_cart(1) order = checkout(cart, user) save_or...
New
OvermindDL1
I created a new library (rather I pulled out a couple files from my big project), it manages an operating system PID file for the BEAM. ...
New
tmbb
I’ve decided to create this topic to discuss optimization possibilities for something like Phoenix LiveView. I’ve created this topic unde...
New

Other popular topics Top

gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New

Sub Categories:

We're in Beta

About us Mission Statement