eduardbme / e-sort-json

Sort json object in Erlang

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

e-sort-json

Build Status Coverage Status

Sort JSON object in Erlang

Motivation

Sometimes you want to be sure two JSON objects are equal.

Perhaps you store them in a database under unique constraint. You cannot just assume that {a: 1, b: 2} will never became {b: 2, a: 1} during processing/communication with other service. That's why you want to sort them before inserting to database or something like that.

This is not what you commonly do with a JSON object, but sometimes such necessity has place.

How to use

%% key is a tuple
1> e_sort_json:sort([{a, 1}, {b, 1}]).    
[{a,1},{b,1}]
2> e_sort_json:sort([{b, 1}, {a, 1}]).
[{a,1},{b,1}]

%% key is a binary value
3> e_sort_json:sort([{<<"b">>, 1}, {<<"a">>, 1}]).
[{<<"a">>,1},{<<"b">>,1}]
4> e_sort_json:sort([{<<"a">>, 1}, {<<"b">>, 1}]).
[{<<"a">>,1},{<<"b">>,1}] 

Performance

The work on performance improvements is still in progress.

About

Sort json object in Erlang


Languages

Language:Erlang 79.5%Language:Makefile 20.5%