jsonx-master

所属分类:其他
开发工具:Others
文件大小:30KB
下载次数:0
上传日期:2019-01-30 14:07:32
上 传 者JamesKan
说明:  JSONX is an Erlang library for efficient JSON decoding and encoding, implemented in Erlang NIFs. Works with binaries as strings, arrays as lists and only knows how to decode UTF-8 (and ASCII).

文件列表:
LICENSE (1103, 2015-03-10)
Makefile (284, 2015-03-10)
c_src (0, 2015-03-10)
c_src\decoder.c (13589, 2015-03-10)
c_src\encoder.c (11785, 2015-03-10)
c_src\jsonx.c (7867, 2015-03-10)
c_src\jsonx.h (5678, 2015-03-10)
c_src\jsonx_str.h (9833, 2015-03-10)
c_src\jstream.c (10903, 2015-03-10)
examples (0, 2015-03-10)
examples\example.json (602, 2015-03-10)
examples\records_examples.erl (741, 2015-03-10)
examples\stream_example.erl (1176, 2015-03-10)
rebar.config (934, 2015-03-10)
src (0, 2015-03-10)
src\jsonx.app.src (250, 2015-03-10)
src\jsonx.erl (8404, 2015-03-10)
src\jstream.erl (1486, 2015-03-10)
test (0, 2015-03-10)
test\array_tests.erl (451, 2015-03-10)
test\bench_encode_records.erl (1313, 2015-03-10)
test\decode_records_test.erl (1502, 2015-03-10)
test\encode_records_test.erl (1372, 2015-03-10)
test\include_json_test.erl (280, 2015-03-10)
test\intro_tests.erl (660, 2015-03-10)
test\jstream_test.erl (6168, 2015-03-10)
test\map_tests.erl (641, 2015-03-10)
test\num_tests.erl (895, 2015-03-10)
test\obj_tests.erl (1430, 2015-03-10)
test\str_tests.erl (3211, 2015-03-10)

JSONX is an Erlang library for efficient JSON decoding and encoding, implemented in Erlang NIFs. Works with binaries as strings, arrays as lists and only knows how to decode UTF-8 (and ASCII). Map encoding/decoding supported in Erlang/OTP 17+ JSONX IS VERY FAST! ------------------ Check out a benchmark [si14/erl_json_test](https://github.com/si14/erl_json_test) or [davisp/erljson_bench](https://github.com/davisp/erljson_bench) and record encoding tests in `/test/bench_encode_records.erl` INSTALLATION and DOCUMENTATION ------------------------------ * cd jsonx * make * make doc * firefox doc/index.html& JSONX can encode and decode Erlang records! ------------------------------------------- ```erlang -module(record_example). -compile(export_all). -record(person, {name, age, friends}). -record(person2, {name, age, phone}). encoder1() -> jsonx:encoder([{person, record_info(fields, person)}, {person2, record_info(fields, person2)} ]). decoder1() -> jsonx:decoder([{person, record_info(fields, person)}, {person2, record_info(fields, person2)}]). nonstrict_decoder1() -> jsonx:decoder([{person, record_info(fields, person)}, {person2, record_info(fields, person2)}], [{format, proplist}]). ``` ```erlang 1> c(records_examples). {ok,records_examples} 2> rr(record_examples). [person,person2] 3> BabaYaga = #person2{name = <<"BabaYaga">>, age = 118, phone = <<"666-66-66">>}. #person2{name = <<"BabaYaga">>,age = 118, phone = <<"666-66-66">>} 4> Vasya = #person{name = <<"Vasya">>, age = 18, friends = [BabaYaga]}. #person{name = <<"Vasya">>,age = 18, friends = [#person2{name = <<"BabaYaga">>,age = 118, phone = <<"666-66-66">>}]} 5> Encoder = record_examples:encoder1(). #Fun 6> Decoder = record_examples:decoder1(). #Fun 7> Json = Encoder(BabaYaga). <<"{\"name\": \"BabaYaga\",\"age\": 118,\"phone\": \"666-66-66\"}">> 8> Decoder(Json). #person2{name = <<"BabaYaga">>,age = 118, phone = <<"666-66-66">>} 9> Json2 = Encoder(Vasya). <<"{\"name\": \"Vasya\",\"age\": 18,\"friends\": [{\"name\": \"BabaYaga\",\"age\": 118,\"phone\": \"666-66-66\"}]}">> 10> Decoder(Json2). #person{name = <<"Vasya">>,age = 18, friends = [#person2{name = <<"BabaYaga">>,age = 118, phone = <<"666-66-66">>}]} 11> Json3 = <<"[{\"name\": \"BabaYaga\",\"age\": 118,\"phone\": \"666-66-66\"}, {\"record\": \"undefined\", \"strict\": false}]">>. <<"[{\"name\": \"BabaYaga\",\"age\": 118,\"phone\": \"666-66-66\"}, {\"record\": \"undefined\", \"strict\": false}]">> 12> Decoder(Json3). {error,undefined_record,***} 13> NonStrictDecoder = record_examples:nonstrict_decoder1(). #Fun 14> JTerm = NonStrictDecoder(Json3). [#person2{name = <<"BabaYaga">>,age = 118, phone = <<"666-66-66">>}, [{<<"record">>,<<"undefined">>},{<<"strict">>,false}]] 15> Encoder(JTerm). <<"[{\"name\": \"BabaYaga\",\"age\": 118,\"phone\": \"666-66-66\"},{\"record\":\"undefined\",\"strict\":false}]">> ``` Examples encoding JSON ---------------------- ```erlang 1> jsonx:encode([1, 2.3, true, false, null, atom, <<"string">>, []]). <<"[1,2.3,true,false,null,\"atom\",\"string\",[]]">> %% Object as proplist 2> jsonx:encode( [{name, <<"Ivan">>}, {age, 33}, {phones, [3332211, 4443322]}] ). <<"{\"name\":\"Ivan\",\"age\":33,\"phones\":[3332211,4443322]}">> %% Object as struct 3> jsonx:encode( {struct, [{name, <<"Ivan">>}, {age, 33}, {phones, [3332211, 4443322]}]} ). <<"{\"name\":\"Ivan\",\"age\":33,\"phones\":[3332211,4443322]}">> %% Object as eep18 propsal 4> jsonx:encode( {[{name, <<"Ivan">>}, {age, 33}, {phones, [3332211, 4443322]}]} ). <<"{\"name\":\"Ivan\",\"age\":33,\"phones\":[3332211,4443322]}">> %% Object as Map 5> jsonx:encode( #{name => <<"Ivan">>, age => 33, phones => [3332211, 4443322]} ). <<"{\"age\":33,\"name\":\"Ivan\",\"phones\":[3332211,4443322]}">> ``` Examples decoding JSON ---------------------- ```erlang 1> jsonx:decode(<<"{\"name\":\"Ivan\",\"age\":33,\"phones\":[3332211,4443322]}">>). {[{<<"name">>,<<"Ivan">>}, {<<"age">>,33}, {<<"phones">>,[3332211,4443322]}]} 2> jsonx:decode(<<"{\"name\":\"Ivan\",\"age\":33,\"phones\":[3332211,4443322]}">>, [{format, eep18}]). {[{<<"name">>,<<"Ivan">>}, {<<"age">>,33}, {<<"phones">>,[3332211,4443322]}]} 3> jsonx:decode(<<"{\"name\":\"Ivan\",\"age\":33,\"phones\":[3332211,4443322]}">>, [{format, proplist}]). [{<<"name">>,<<"Ivan">>}, {<<"age">>,33}, {<<"phones">>,[3332211,4443322]}] 4> jsonx:decode(<<"{\"name\":\"Ivan\",\"age\":33,\"phones\":[3332211,4443322]}">>, [{format, struct}]). {struct,[{<<"name">>,<<"Ivan">>}, {<<"age">>,33}, {<<"phones">>,[3332211,4443322]}]} 5> jsonx:decode(<<"{\"name\":\"Ivan\",\"age\":33,\"phones\":[3332211,4443322]}">>, [{format, map}]). {map,#{<<"age">> => 33, <<"name">> => <<"Ivan">>, <<"phones">> => [3332211,4443322]}} ``` Example streaming parse ----------------------- More example see `examples/stream_example.erl` . ```erlang 1> D = jstream:new_decoder(<<"{\"key1\": \"val1\",\n">>). <<>> 2> jstream:get_event(D). start_map 3> jstream:get_event(D). {map_key,<<"key1">>} 4> jstream:get_event(D). <<"val1">> 5> jstream:get_event(D). parse_buf 6> ok = jstream:update_decoder(D, <<"\"key2\": \"val2\"}\n">>). ok 7> jstream:get_event(D). {map_key,<<"key2">>} 8> jstream:get_event(D). <<"val2">> 9> jstream:get_event(D). end_map 10> jstream:get_event(D). {parse_end,<<>>} ``` Mapping (JSON -> Erlang) ---------------------- null :-> null true :-> true false :-> false "string" :-> <<"binary">> [1, 2.3, []] :-> [1, 2.3, []] {"this": "json"} :-> {[{<<"this">>: <<"json">>}]} %% default eep18 {"this": "json"} :-> [{<<"this">>: <<"json">>}] %% optional proplist {"this": "json"} :-> {struct, [{<<"this">>: <<"json">>}]} %% optional struct {"this": "json"} :-> {map, #{<<"this">> => <<"json">>}} %% optional map JSONObject :-> #rec{...} %% decoder must be predefined Mapping (Erlang -> JSON) ----------------------- null :-> null true :-> true false :-> false atom :-> "atom" <<"str">> :-> "str" [1, 2.99] :-> [1, 2.99] {struct, [{<<"this">>: <<"json">>}]} :-> {"this": "json"} {map, #{this => <<"json">>}} :-> {"this": "json"} [{<<"this">>: <<"json">>}] :-> {"this": "json"} {[{<<"this">>: <<"json">>}]} :-> {"this": "json"} {json, IOList} :-> `iolist_to_binary(IOList)` %% include with no validation #rec{...} :-> JSONObject %% encoder must be predefined

近期下载者

相关文件


收藏者