Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decode null as undefined option #6

Open
sergseven opened this issue Sep 2, 2022 · 2 comments
Open

Decode null as undefined option #6

sergseven opened this issue Sep 2, 2022 · 2 comments

Comments

@sergseven
Copy link

sergseven commented Sep 2, 2022

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?

@crownedgrouse
Copy link
Owner

Hi,
thanks for your interest in jason.
In order to let people decide the value for 'null', I would prefere an option {null, ...} where right value is expected value.
In your case {null, undefined} .
But to avoid unexpected behavior, the default must stay like current behavior.

@crownedgrouse
Copy link
Owner

Are you able to do a PR with my requirements ? i.e allow to pass {null, something} ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants