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:

https://github.com/egze/stable_jason

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 Announcing Top

tmbb
I’ve published the first version of my Makeup library. It’s a syntax highlighter for Elixir in the spirit of Pygments, Currently it highl...
New
mathieuprog
Hello :waving_hand: Allow me to introduce you to Tz, an alternative time zone database support to Tzdata. Why another library? First a...
New
mspanc
I am pleased to announce an initial release of the Membrane Framework - an Elixir-based framework with special focus on processing multim...
New
zorbash
I created Kitto a framework for dashboards inspired by Dashing. The distributed characteristics of Elixir and the low memory footprint...
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 52341 488
New
aditya7iyengar
Rummage.Ecto and Rummage.Phoenix provide ways to perform Searching, Sorting and Pagination over Ecto queries and Phoenix collections. Fo...
New
achempion
Hi, I would like to tell about my initiative to further maintain and develop Waffle project which is the fork of Arc library. The progre...
New
tmbb
I’ve decided to create this topic to discuss optimization possibilities for something like Phoenix LiveView. I’ve created this topic unde...
144 10187 141
New
woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
New
mattludwigs
Grizzly is a library for working with Z-Wave devices. Z-Wave is a low-frequency radio protocol for controlling smart home devices on a me...
New

Other popular topics Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36128 110
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
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 records...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement