crownedgrouse / jason Goto Github PK
View Code? Open in Web Editor NEWJSON encode/decode library written in Erlang
License: ISC License
JSON encode/decode library written in Erlang
License: ISC License
Expected tests to pass:
jason_encode_dict_test() ->
?assertEqual("{}", jason:encode(dict:from_list([]))),
?assertEqual("{\"abc\": \"def\"}", jason:encode(dict:from_list([{<<"abc">>, <<"def">>}]))),
?assertEqual("{\"abc\": \"def\",\"abc2\": \"def2\"}", jason:encode(dict:from_list([{<<"abc">>, <<"def">>}, {<<"abc2">>, <<"def2">>}]))),
?assertEqual(
"{\"abc\": {\"k1\": 1,\"k2\": \"ab\"},\"abc2\": {\"k1\": 2,\"k2\": \"cd\"}}",
jason:encode(
dict:from_list([
{<<"abc">>, {r, 1, <<"ab">>}},
{<<"abc2">>, {r, 2, <<"cd">>}}]),
[{records, [{r, [k1, k2]}]}])),
ok.
Test to reproduce:
Input182 = <<"{\"k1\":\"aaa2"\,\"k2\":[{\"n1\": 123},{\"n1\": 456}]}">>,
?assertMatch( {ok,{r,"aaa2",[{n,123},{n,456}]}} , jason:decode(
Input182,
[
{return, tuple},
{binary, v},
{mode, record},
[{records, [{r, [k1,k2]}, {n, [n1]}]}]
])
),
error:
jason_tests: jason_decode_objects_test...*failed*
in function jason_tests:'-jason_decode_objects_test/0-fun-17-'/0 (src/jason_tests.erl, line 275)
in call from jason_tests:jason_decode_objects_test/0 (src/jason_tests.erl, line 275)
in call from eunit_test:'-mf_wrapper/2-fun-0-'/2 (eunit_test.erl, line 273)
in call from eunit_test:run_testfun/1 (eunit_test.erl, line 71)
in call from eunit_proc:run_test/1 (eunit_proc.erl, line 510)
in call from eunit_proc:with_timeout/3 (eunit_proc.erl, line 335)
in call from eunit_proc:handle_test/2 (eunit_proc.erl, line 493)
in call from eunit_proc:tests_inorder/3 (eunit_proc.erl, line 435)
**error:{assertMatch,[{module,jason_tests},
{line,275},
{expression,"jason : decode ( Input182 , [ { return , tuple } , { binary , v } , { mode , record } , [ { records , [ { r , [ k1 , k2 ] } , { n , [ n1 ] } ] } ] ] )"},
{pattern,"{ ok , { r , \"aaa1\" , [ { n , 123 } , { n , 456 } ] } }"},
{value,{error,badarg}}]}
To fix this issue, I would propose to avoid casting each record field to list when decoding json, but leave it as binary.
This will be a breaking change, though...
I had try to run my example and find some mistake:
Json = #{allowed_updates => [],limit => 1,offset => 1,timeout => 30}}.
jason:encode(JSON,[{mode, map}, {binary, kv}]). "{\"allowed_updates\": \"\",\"limit\": 1,\"offset\": 1,\"timeout\": 30}"
allowed_updates transform [] to ""
Please help me to understand - can I strict this behavior and treat all lists as arrays. Thank You.
binary output
Expected the test(from examples) to pass:
?assertEqual("123.456789", jason:encode(123.456789)).
but the actual value is
"123.456789000000001"
I would propose a new option to allow decoding null
(or absent field in case of mode=record) as undefined
, so:
undefined
as an empty valueundefined -> null -> undefined
with option null_as_undefined=true
Example 1:
?assertEqual({ok, undefined}, jason:decode(<<"null">>, [{return, tuple}, {null_as_undefined, true}])).
Example 2:
% a null in array decoded as undefined when null_as_undefined
Input11 = <<"[1,2,\"abc def\",null]">>,
Expected11 = {ok, [1, 2, <<"abc def">>, undefined]},
?assertEqual(Expected11, jason:decode(Input11, [{return, tuple}, {null_as_undefined, true}])),
Example 3:
% object: record / null_as_undefined
Input19 = <<"{\"1\":2,\"key\":null}">>,
% null_as_undefined false
?assertMatch({ok,{'49797875',2,null}} , jason:decode(Input19, [{return, tuple}, {mode, record}, {null_as_undefined, false}])),
% null_as_undefined true
?assertMatch({ok,{'49797875',2,undefined}} , jason:decode(Input19, [{return, tuple}, {mode, record}, {null_as_undefined, true}])),
Is a PR acceptable?
Expected:
?assertEqual("{}", jason:encode(#{})).
Actual:
?assertEqual("[]", jason:encode(#{})).
Hi,
I'm just wondering under what license this code is released?
Add option for binary key instead atom
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.