| commit | f71616198f008c13a1f8571bb2f7d726ba84764d | [log] [tgz] |
|---|---|---|
| author | Paul J. Davis <paul.joseph.davis@gmail.com> | Thu Jun 07 13:15:38 2012 -0500 |
| committer | Paul J. Davis <paul.joseph.davis@gmail.com> | Thu Jun 07 13:15:38 2012 -0500 |
| tree | caa593ba9748ec571b3975eb2d63ea0636ada9b1 | |
| parent | 1772539a42ab91ee3d03a6b86df8df959853d1dc [diff] |
Fix code reloading for Jiffy As it turns out I did not understand the documenation for load/upgrade/unload correctly. load/upgrade are called conditionally if there's code in the VM for the NIF. Ie, no code means load is called, where as if code exists, upgrade is called. unload is called regardless once per load/unload. This means that load/upgrade in Jiffy's case will each create a state object and unload will free it each time. I was missing the fact that unload is called every time and hence I don't need to clear the state in upgrade.
A JSON parser as a NIF. This is a complete rewrite of the work I did in EEP0018 that was based on Yajl. This new version is a hand crafted state machine that does its best to be as quick and efficient as possible while not placing any constraints on the parsed JSON.
Jiffy is a simple API. The only thing that might catch you off guard is that the return type of jiffy:encode/1 is an iolist even though it returns a binary most of the time.
A quick note on unicode. Jiffy only understands utf-8 in binaries. End of story. Also, there is a jiffy:encode/2 that takes a list of options for encoding. Currently the only supported option is uescape.
Errors are raised as exceptions.
Eshell V5.8.2 (abort with ^G)
1> jiffy:decode(<<"{\"foo\": \"bar\"}">>).
{[{<<"foo">>,<<"bar">>}]}
2> Doc = {[{foo, [<<"bing">>, 2.3, true]}]}.
{[{foo,[<<"bing">>,2.3,true]}]}
3> jiffy:encode(Doc).
<<"{\"foo\":[\"bing\",2.2999999999999998224,true]}">>
Erlang JSON Erlang
==========================================================================
null -> null -> null
true -> true -> true
false -> false -> false
"hi" -> [104, 105] -> [104, 105]
<<"hi">> -> "hi" -> <<"hi">>
hi -> "hi" -> <<"hi">>
1 -> 1 -> 1
1.25 -> 1.25 -> 1.25
[] -> [] -> []
[true, 1.0] -> [true, 1.0] -> [true, 1.0]
{[]} -> {} -> {[]}
{[{foo, bar}]} -> {"foo": "bar"} -> {[{<<"foo">>, <<"bar">>}]}
{[{<<"foo">>, <<"bar">>}]} -> {"foo": "bar"} -> {[{<<"foo">>, <<"bar">>}]}
Jiffy should be in all ways an improvemnt over EEP0018. It no longer imposes limits on the nesting depth. It is capable of encoding and decoding large numbers and it does quite a bit more checking for validity of valid UTF-8 in strings.