sile / jsone

Erlang JSON library
MIT License
291 stars 72 forks source link

Encoding of undefined atom as null #21

Closed bkolodziej closed 7 years ago

bkolodziej commented 7 years ago

undefined is a default value for eg. not set fields in records. undefined is accepted standard for null in Erlang.

Encoding undefined as text "undefined" is problematic when converting mnesia records to maps or list of tuples. This behaviour requires additional steps (or redefining all records to set null as default value instead of undefined).

My proposition is to add following line to jsone_encode module:

value(undefined, Nexts, Buf, Opt) -> next(Nexts, <<Buf/binary, "null">>, Opt);

...or probably better option at this point:

value(undefined, Nexts, Buf, Opt = ?OPT{undefined_as_null = true}) -> next(Nexts, <<Buf/binary, "null">>, Opt);

and extend encode_opt_v2:

-record(encode_opt_v2, {
          native_utf8 = false :: boolean(),
          float_format = [{scientific, 20}] :: [jsone:float_format_option()],
          datetime_format = {iso8601, 0} :: {jsone:datetime_format(), jsone:utc_offset_seconds()},
          object_key_type = string :: string | scalar | value,
          space = 0 :: non_neg_integer(),
          indent = 0 :: non_neg_integer(),
          undefined_as_null = false :: bolean()
         }).
sile commented 7 years ago

Indeed, as a null value, undefined is the de facto standard in Erlang. But there is no enforceable to use undefined as the only nullable value. So, I prefer provides this functionality as an option like https://github.com/sile/jsone/pull/22.

sile commented 7 years ago

This issue was solved at https://github.com/sile/jsone/pull/22.