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
Add drop_nulls plugin and bump version #35
Conversation
Sort of surprising the the 4> {ok, JSON} = euneus:encode_to_binary(#{a => 1, b => undefined, c => null}, #{plugins => [drop_nulls]}).
{ok,<<"{\"a\":1,\"c\":\"null\"}">>} edit: Should the same behavior apply to empty maps? IE, when the recursed processing of a map results in an empty map, should that be treated as a null/undefined? I'm really not sure, but it's not what I'd 'expect' 7> f(), {ok, JSON} = euneus:encode_to_binary(#{a => 1, b => undefined, c => null, e => #{f => undefined}}, #{plugins => [drop_nulls]}).
{ok,<<"{\"a\":1,\"c\":\"null\",\"e\":{}}">>} |
@leonardb Yes, that's intentional but sounds strange due to the plugin name. The default for the |
@leonardb I'm not aware of this. I think the behavior should be the same of the Javascript: 1> JSON.stringify({})
'{}'
2> JSON.stringify({f: undefined})
'{}' EditThe output via this PR is: 1> euneus:encode(#{f => undefined}, #{plugins => [drop_nulls]}).
{ok,<<"{}">>} |
The Principle of Least Surprise (based on the name of the plugin) would seem to dictate that the default be Since the initial request that launched the feature was around handling of |
The name is based on the The reason for this default is because I mostly see FWIW there is a 1> {ok, JSON} = euneus:encode_to_binary(#{a => undefined, b => null, c => nil}, #{nulls => [undefined, null, nil]}).
{ok,<<"{\"c\":null,\"a\":null,\"b\":null}">>}
2> euneus:decode(JSON, #{null_term => nil}).
{ok,#{<<"a">> => nil,<<"b">> => nil,<<"c">> => nil}} So we cannot have exactly the same behavior of the Javascript. EditThe |
It's a bit confusing thinking about null, undefined, nil, etc in Erlang vs JSON. I agree with you when you say about the Principle of Least Surprise, but I'm to implement the Feel free to open an issue about this. I will be happy to discuss it in a dedicated topic and with more people involved :) |
This PR implements the
drop_nulls
plugin.It ignores all keys of maps whose values are included in the 'nulls' option of the encoding.
Example
Maps
Proplists
Credits
Thanks both @asabil and @leonardb for the suggestions at the Erlang forums topic.