View Source Euneus
An incredibly flexible and performant JSON parser, generator and formatter in pure Erlang.
Euneus is a rewrite of Thoas.
Like Thoas, both the parser and generator fully conform to RFC 8259 and ECMA 404.
Table of Contents
- Installation
- Basic Usage
- Data Mapping
- Plugins
- Differences to Thoas
- Benchmarks
- Tests
- Smart modules
- Credits
- Why the name Euneus?
- Sponsors
- Contributing
- License
Installation
Erlang
% rebar.config
{deps, [{euneus, "1.2.0"}]}
Elixir
# mix.exs
def deps do
[{:euneus, "~> 1.2"}]
end
Basic Usage
1> {ok, JSON} = euneus:encode_to_binary(#{name => #{english => <<"Charmander">>, japanese => <<"ヒトカゲ"/utf8>>}, type => [fire], profile => #{height => 0.6, weight => 8}, ability => #{0 => <<"Blaze">>, 1 => undefined}}).
{ok, <<"{\"name\":{\"english\":\"Charmander\",\"japanese\":\"ヒトカゲ\"},\"profile\":{\"height\":0.6,\"weight\":8},\"type\":[\"fire\"],\"ability\":{\"0\":\"Blaze\",\"1\":null}}">>}
2> euneus:decode(JSON).
{ok,#{<<"ability">> =>
#{<<"0">> => <<"Blaze">>,<<"1">> => undefined},
<<"name">> =>
#{<<"english">> => <<"Charmander">>,
<<"japanese">> =>
<<227,131,146,227,131,136,227,130,171,227,130,178>>},
<<"profile">> => #{<<"height">> => 0.6,<<"weight">> => 8},
<<"type">> => [<<"fire">>]}}
3> euneus:decode(JSON, #{
keys => fun
(<<Char>> = Key, _Opts) when Char >= $0, Char =< $9 ->
binary_to_integer(Key);
(Key, _Opts) ->
binary_to_existing_atom(Key)
end
}).
{ok,#{name =>
#{english => <<"Charmander">>,
japanese =>
<<227,131,146,227,131,136,227,130,171,227,130,178>>},
profile => #{height => 0.6,weight => 8},
type => [<<"fire">>],
ability => #{0 => <<"Blaze">>,1 => undefined}}}
Data Mapping
[!TIP]
More types can be handled by using custom plugins. Please see the Plugins section for more info.
Erlang -> | Encode Options -> | JSON -> | Decode Options -> | Erlang |
---|---|---|---|---|
undefined | #{} | null | #{} | undefined |
undefined | #{} | null | #{null_term => nil} | nil |
true | #{} | true | #{} | true |
false | #{} | false | #{} | false |
abc | #{} | "abc" | #{} | <<"abc">> |
"abc" | #{} | [97,98,99] | #{} | "abc" |
<<"abc">> | #{} | "abc" | #{} | <<"abc">> |
123 | #{} | 123 | #{} | 123 |
123.45600 | #{} | 123.456 | #{} | 123.456 |
[<<"foo">>,true,0,undefined] | #{} | ["foo",true,0,null] | #{} | [<<"foo">>,true,0,undefined] |
#{foo => bar} | #{} | {"foo":"bar"} | #{} | #{<<"foo">> => <<"bar">>} |
#{foo => bar} | #{} | {"foo":"bar"} | #{keys => to_existing_atom} | #{foo => <<"bar">>} |
#{0 => 0} | #{} | {"0":0} | #{keys => to_integer} | #{0 => 0} |
{{1970,1,1},{0,0,0}} | #{plugins => [datetime]} | "1970-01-01T00:00:00Z" | #{plugins => [datetime]} | {{1970,1,1},{0,0,0}} |
{127,0,0,1} | #{plugins => [inet]} | "127.0.0.1" | #{plugins => [inet]} | {127,0,0,1} |
{16#3ffe,16#b80,16#1f8d,16#2,16#204,16#acff,16#fe17,16#bf38} | #{plugins => [inet]} | "3ffe:b80:1f8d:2:204:acff:fe17:bf38" | #{plugins => [inet]} | {16#3ffe,16#b80,16#1f8d,16#2,16#204,16#acff,16#fe17,16#bf38} |
<0.92.0> | #{plugins => [pid]} | "<0.92.0>" | #{plugins => [pid]} | <0.92.0> |
#Port<0.1> | #{plugins => [port]} | "#Port<0.1>" | #{plugins => [port]} | #Port<0.1> |
[{foo, bar}] | #{plugins => [proplist]} | {"foo":"bar"} | #{plugins => [proplist]} | #{<<"foo">> => <<"bar">>} |
#Ref<0.957048870.857473026.108035> | #{plugins => [reference]} | "#Ref<0.957048870.857473026.108035>" | #{plugins => [reference]} | #Ref<0.957048870.857473026.108035> |
{0,0,0} | #{plugins => [timestamp]} | "1970-01-01T00:00:00.000Z" | #{plugins => [timestamp]} | {0,0,0} |
#{foo => bar, baz => undefined} | #{plugins => [drop_nulls]} | {"foo":"bar"} | #{} | #{<<"foo">> => <<"bar">>} |
[{foo, bar}, {baz, undefined}] | #{plugins => [drop_nulls, proplist]} | {"foo":"bar"} | #{} | #{<<"foo">> => <<"bar">>} |
#{foo => bar, baz => undefined, fizz => nil} | #{nulls => [undefined, nil], plugins => [drop_nulls]} | {"foo":"bar"} | #{} | #{<<"foo">> => <<"bar">>} |
{myrecord, val} | #{unhandled_encoder => fun({myrecord, Val}, Opts) -> <br> euneus_encoder:encode_list([myrecord, #{key => Val}], Opts)<br><br>end}) | ["myrecord", {"key":"val"}] | #{arrays => fun([<<"myrecord">>, #{<<"key">> := Val}], _Opts) -><br> {myrecord, binary_to_atom(Val)}<br>end} | {myrecord, val} |
Why not more built-in types?
The goal of Euneus
is to have built-in types that can be commonly encoded and decoded, but the range of types can be easily extended by using plugins. Please see the Plugins section for more info.
Note about proplists
Proplists are not handled by Euneus by default.
There are three options:
- Use the built-in, or create your own, proplist plugin;
- Convert proplists to maps before the encoding;
- Override the
list_encoder
option in the encoder to handle them, for example:
1> Options = #{
list_encoder => fun
([{K, _} | _] = Proplist, Opts)
when is_binary(K); is_atom(K); is_integer(K) ->
Map = proplists:to_map(Proplist),
euneus_encoder:encode_map(Map, Opts);
(List, Opts) ->
euneus_encoder:encode_list(List, Opts)
end
}.
2> Proplist = [{foo, bar}, {bar, [{0, ok}]}].
3> euneus:encode_to_binary(Proplist, Options).
{ok,<<"{\"foo\":\"bar\",\"bar\":{\"0\":\"ok\"}}">>}
The reason for that is because it's impossible to know when a list is a proplist and also because a proplist cannot be decoded. Please see the Why not more built-in types? section for more info about this decision.
Plugins
Euneus has a mechanism to easily plug in encoders and decoders. You can use the built-in plugins to handle common types or create your own in a module by implementing the euneus_plugin behavior.
If you have a built-in plugin suggestion, feel free to open a new issue to discuss it.
[!IMPORTANT] The plugins mechanism deprecated the
datetime_encoder
and thetimestamp_encoder
option in favor of thedatetime
andtimestamp
plugins.
Usage
Encode
euneus:encode(Term, #{plugins => [
% list of built-in or custom plugins
]})
Decode
euneus:decode(Term, #{plugins => [
% list of built-in or custom plugins
]})
Built-in Plugins
datetime
Encodes calendar:datetime()
to ISO8601 as JSON string and decodes it back, for example:
1> {ok, JSON} = euneus:encode_to_binary({{1970,1,1},{0,0,0}}, #{plugins => [datetime]}).
{ok,<<"\"1970-01-01T00:00:00Z\"">>}
2> euneus:decode(JSON, #{plugins => [datetime]}).
{ok,{{1970,1,1},{0,0,0}}}
inet
Encodes inet:ip_address()
to IPV4 or IPV6 as JSON string and decodes it back, for example:
1> {ok, JSON} = euneus:encode_to_binary({127,0,0,1}, #{plugins => [inet]}).
{ok,<<"\"127.0.0.1\"">>}
2> euneus:decode(JSON, #{plugins => [inet]}).
{ok,{127,0,0,1}}
pid
Encodes erlang:pid()
to JSON string and decodes it back, for example:
1> {ok, JSON} = euneus:encode_to_binary(list_to_pid("<0.92.0>"), #{plugins => [pid]}).
{ok,<<"\"<0.92.0>\"">>}
2> euneus:decode(JSON, #{plugins => [pid]}).
{ok,<0.92.0>}
port
Encodes erlang:port()
to JSON string and decodes it back, for example:
1> {ok, JSON} = euneus:encode_to_binary(list_to_port("#Port<0.1>"), #{plugins => [port]}).
{ok,<<"\"#Port<0.1>\"">>}
2> euneus:decode(JSON, #{plugins => [port]}).
{ok,#Port<0.1>}
proplist
Encodes [{binary() | atom() | integer(), term()}]
to JSON object, for example:
1> {ok, JSON} = euneus:encode_to_binary([{foo, bar}], #{plugins => [proplist]}).
{ok,<<"{\"foo\":\"bar\"}">>}
reference
Encodes erlang:reference()
to JSON string and decodes it back, for example:
1> {ok, JSON} = euneus:encode_to_binary(make_ref(), #{plugins => [reference]}).
{ok,<<"\"#Ref<0.957048870.857473026.108035>\"">>}
2> euneus:decode(JSON, #{plugins => [reference]}).
{ok,#Ref<0.957048870.857473026.108035>}
timestamp
Encodes erlang:timestamp/0
to ISO8601 as JSON string and decodes it back, for example:
1> {ok, JSON} = euneus:encode_to_binary({0,0,0}, #{plugins => [timestamp]}).
{ok,<<"\"1970-01-01T00:00:00.000Z\"">>}
2> euneus:decode(JSON, #{plugins => [timestamp]}).
{ok,{0,0,0}}
drop_nulls
Remove keys from maps whose terms are members of the encode 'nulls' option, for example:
1> {ok, JSON} = euneus:encode_to_binary(#{a => 1, b => undefined}, #{plugins => [drop_nulls]}).
{ok,<<"{\"a\":1}">>}
1> {ok, JSON} = euneus:encode_to_binary(#{a => 1, b => undefined, c => foo}, #{nulls => [undefined, foo], plugins => [drop_nulls]}).
{ok,<<"{\"a\":1}">>}
[!IMPORTANT]
The
drop_nulls
plugin also works forproplists
.
Differences to Thoas
The main difference between Euneus
to Thoas
is that Euneus gives more control to encoding or decoding data. All encode functions can be overridden and extended and all decoded data can be overridden and transformed. Also, there is no plugin mechanism in Thoas.
Encode
Available encode options:
#{
%% nulls defines what terms will be replaced with the null literal (default: ['undefined']).
nulls => nonempty_list(),
%% binary_encoder allow override the binary() encoding.
binary_encoder => function((binary(), euneus_encoder:options()) -> iolist()),
%% atom_encoder allow override the atom() encoding.
atom_encoder => function((atom(), euneus_encoder:options()) -> iolist()),
%% integer_encoder allow override the integer() encoding.
integer_encoder => function((integer(), euneus_encoder:options()) -> iolist()),
%% float_encoder allow override the float() encoding.
float_encoder => function((float(), euneus_encoder:options()) -> iolist()),
%% list_encoder allow override the list() encoding.
list_encoder => function((list(), euneus_encoder:options()) -> iolist()),
%% map_encoder allow override the map() encoding.
map_encoder => function((map(), euneus_encoder:options()) -> iolist()),
%% unhandled_encoder allow encode any custom term (default: raise unsupported_type error).
unhandled_encoder => function((term(), euneus_encoder:options()) -> iolist()),
%% escaper allow override the binary escaping (default: json).
escaper => json
| html
| javascript
| unicode
| function((binary(), euneus_encoder:options()) -> iolist()),
error_handler => function(( error | exit | throw
, term()
, erlang:stacktrace() ) -> euneus_encoder:result()),
%% plugins extends the encode types.
plugins => datetime
| inet
| pid
| port
| proplist
| reference
| timestamp
| module() % that implements the `euneus_plugin` behavior.
}
For example:
1> EncodeOpts = #{
binary_encoder => fun
(<<"foo">>, Opts) ->
euneus_encoder:encode_binary(<<"bar">>, Opts);
(Bin, Opts) ->
euneus_encoder:encode_binary(Bin, Opts)
end,
unhandled_encoder => fun
({_, _, _, _} = Ip, Opts) ->
case inet:ntoa(Ip) of
{error, einval} ->
throw(invalid_ip);
IpStr ->
IpBin = list_to_binary(IpStr),
euneus_encoder:encode_binary(IpBin, Opts)
end;
(Term, Opts) ->
euneus_encoder:throw_unsupported_type_error(Term, Opts)
end,
error_handler => fun
(throw, invalid_ip, _Stacktrace) ->
{error, invalid_ip};
(Class, Reason, Stacktrace) ->
euneus_encoder:handle_error(Class, Reason, Stacktrace)
end
}.
2> Data = #{<<"foo">> => bar, ipv4 => {127,0,0,1}, none => undefined}.
3> euneus:encode_to_binary(Data, EncodeOpts).
{ok, <<"{\"bar\":\"bar\",\"ipv4\":\"127.0.0.1\",\"none\":null}">>}
4> euneus:encode_to_binary({1270,0,0,1}, EncodeOpts).
{error, invalid_ip}
Decode
Available decode options:
#{
%% null_term is the null literal override (default: 'undefined').
null_term => term(),
%% arrays allow override any array/list().
arrays => function((list(), euneus_decoder:options()) -> term()),
%% objects allow override any object/map().
objects => function((map(), euneus_decoder:options()) -> term()),
%% keys allow override the keys from JSON objects.
keys => copy
| to_atom
| to_existing_atom
| to_integer
| function((binary(), euneus_decoder:options()) -> term()),
%% values allow override any other term, like array item or object value.
values => copy
| to_atom
| to_existing_atom
| to_integer
| function((binary(), euneus_decoder:options()) -> term()),
%% plugins extends the decode types.
plugins => datetime
| inet
| pid
| port
| reference
| timestamp
| module() % that implements the `euneus_plugin` behavior.
}
For example:
1> DecodeOpts = #{
null_term => nil,
keys => fun
(<<"bar">>, _Opts) ->
foo;
(Key, _Opts) ->
binary_to_atom(Key)
end,
values => fun
(<<"127.0.0.1">>, _Opts) ->
{127, 0, 0, 1};
(Value, _Opts) ->
Value
end
}.
2> JSON = <<"{\"bar\":\"bar\",\"ipv4\":\"127.0.0.1\",\"none\":null}">>.
3> euneus:decode(JSON, DecodeOpts).
{ok,#{foo => <<"bar">>,
ipv4 => {127,0,0,1},
none => nil}}
Resuming
Euneus permits resuming the decoding when an invalid token is found. Any value can replace the invalid token by overriding the error_handler
option, e.g.:
1> ErrorHandler = fun
(throw, {{token, Token}, Rest, Opts, Input, Pos, Buffer}, _Stacktrace) ->
Replacement = foo,
euneus_decoder:resume(Token, Replacement, Rest, Opts, Input, Pos, Buffer);
(Class, Reason, Stacktrace) ->
euneus_decoder:handle_error(Class, Reason, Stacktrace)
end.
2> Opts = #{error_handler => ErrorHandler}.
3> euneus:decode(<<"[1e999,1e999,{\"foo\": 1e999}]">>, Opts).
{ok,[foo,foo,#{<<"foo">> => foo}]}
[!NOTE]
By using
euneus_decoder:resume/6
the replacement will be thenull_term
option.
Format
Minify
Remove extra spaces and line feeds from JSON, e.g.:
1> euneus:minify_to_binary(<<"{\n \"foo\": \"bar\",\n \"baz\": {\n \"foo\": \"bar\",\n \"0\": [\n \"foo\",\n 0\n ]\n }\n}">>).
<<"{\"foo\":\"bar\",\"baz\":{\"foo\":\"bar\",\"0\":[\"foo\",0]}}">>
Prettify
Format JSON for printing, e.g.:
1> io:format("~s~n", [euneus:prettify(<<"{\"foo\":\"bar\",\"baz\":{\"foo\":\"bar\",\"0\":[\"foo\",0]}}">>)]).
{
"foo": "bar",
"baz": {
"foo": "bar",
"0": [
"foo",
0
]
}
}
Custom
Use euneus:format/2
or euneus:format_parsed/2
for custom formatting, e.g.:
1> Opts = #{spaces => <<$\t>>, indent => <<$\t, $\t>>, crlf => <<$\n>>}.
2> io:format("~s~n", [euneus:format(<<"{\"foo\":\"bar\",\"baz\":{\"foo\":\"bar\",\"0\":[\"foo\",0]}}">>, Opts)]).
{
"foo": "bar",
"baz": {
"foo": "bar",
"0": [
"foo",
0
]
}
}
Why Euneus over Thoas?
Thoas
is incredible, works performant and perfectly fine, but Euneus
is more flexible, permitting more customizations, and is more performant than Thoas. See the benchmarks.
The motivation for Euneus is this PR.
Benchmarks
All the latest runs details can be found under the runs directory in the benchmark project.
Encode
Smart encoding
This benchmark uses the JSON smart module via the euneus:encode/1
function. Smart modules only receive the input as the argument, so no option is available to set. Smart modules are the fastest euneus modules.
All benchmark details are available here.
Encoding with empty map as option
This benchmark passes the input and options parsed as the euneus:encode_parsed/2
function arguments. There is no option set, just an empty map, so all the default options are used. This function it's a bit slower than the smart one, but all options are analyzed in the run.
All benchmark details are available here.
Encoding with all built-in plugins
This benchmark passes all the encode built-in plugins to the plugins option:
euneus:parse_encode_opts(#{
plugins => [
datetime,
inet,
pid,
port,
proplist,
reference,
timestamp
]
}).
[!IMPORTANT]
The
drop_nulls
plugin was introduced in v1.2.0 and was not included yet in the built-in plugins benchmarks.
It's the slowest euneus encode run, but at the same time it is very efficient.
All benchmark details are available here.
Decode
Smart decoding
This benchmark uses the decode smart module via the euneus:decode/1
function. Smart modules only receive the input as the argument, so no option is available to set. Smart modules are the fastest euneus modules.
All benchmark details are available here.
Decoding with empty map as option
This benchmark passes the input and options parsed as the euneus:decode_parsed/2
function arguments. There is no option set, just an empty map, so all the default options are used. This function it's a bit slower than the smart one, but all options are analyzed in the run.
All benchmark details are available here.
Decoding with all built-in plugins
This benchmark passes all the decode built-in plugins to the plugins option:
euneus:parse_decode_opts(#{
plugins => [
datetime,
timestamp,
pid,
port,
reference,
inet
]
}).
[!NOTE]
The
proplist
plugin is only available for encoding.
It's the slowest euneus decode run, but at the same time it is very efficient.
All benchmark details are available here.
Tests
There are Eunit tests in euneus_encoder and euneus_decoder and tests suites in a specific project under the euneus_test directory. Euneus has more than 330 tests.
Also, the parser is tested using JSONTestSuite and all tests passes:
See the Euneus parser in JSONTestSuite.
[!NOTE]
All of the JSONTestSuite tests are embedded in Euneus tests.
Smart modules
Euneus has modules that permit customizations and others that use the default options. The modules without customizations are called smart. The smart versions are faster because they do not do any option checks.
If you are good to go with the default options, please use the smart versions:
- Encode:
- Decode:
Credits
Euneus is a rewrite of Thoas, so all credits go to Michał Muskała, Louis Pilfold, also both Jason and Thoas contributors. Thanks for the hard work!
Why the name Euneus?
Euneus is the twin brother of Thoas.
Sponsors
If you like this tool, please consider sponsoring me. I'm thankful for your never-ending support :heart:
I also accept coffees :coffee:
Contributing
Issues
Feel free to submit an issue on Github.
Installation
# Clone this repo
git clone git@github.com:williamthome/euneus.git
# Navigate to the project root
cd euneus
# Compile (ensure you have rebar3 installed)
rebar3 compile
Commands
# Benchmark euneus:encode/1
$ make bench.encode
# Benchmark euneus:decode/1
$ make bench.decode
# Run all tests
$ make test
# Run all tests and dialyzer
$ make check
[!NOTE]
Open the Makefile to see all commands.
License
Euneus is released under the Apache License 2.0.
Euneus is based on Thoas, which is also Apache 2.0 licensed.
Some elements have their origins in the Poison library and were initially licensed under CC0-1.0.