Giter Club home page Giter Club logo

jason's People

Contributors

crownedgrouse avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

jason's Issues

Add dict support

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.

badarg error while decoding json array with nested objects inside and option {binary, v}

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...

Empty array wrongly treat as string

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.

Decode null as undefined option

I would propose a new option to allow decoding null (or absent field in case of mode=record) as undefined, so:

  • it will be possible to have a round-trip encode/decode behaviour
  • some libraries or applications expect undefined as an empty value

undefined -> 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?

License?

Hi,
I'm just wondering under what license this code is released?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.