talentdeficit/jsx

strict option: no_duplicate_keys

andreineculau opened this issue · 5 comments

as per https://tools.ietf.org/html/rfc7159#section-4

The names within an object SHOULD be unique.

yet JSX works like this:

1> jsx:decode(<<"{\"a\": \"b\", \"a\": \"c\"}">>).
[{<<"a">>,<<"b">>},{<<"a">>,<<"c">>}]

Consider adding no_duplicate_keys as as flag part of the strict option, or similar.

i'll look at adding this to the in progress 3.0

probably won't backport it to 2.x tho, because there's no low impact way to track multiple keys when decoding to a map

how is "there's no low impact way..." relevant if we're talking about adding a flag? current performance can stay the same, but those who want no_duplicate_keys can deal with whatever "performance" loss?

a friendly bump. what's the status on this? thanks!

@andreineculau: is this still an issue (it's been more than 2 years)? How would you go about detecting the duplicates? I imagine a map, since a list would be even more performance-degrading. Is this what you mean by your question? You'd like a flag and then a runtime validation if that flag was active. Is that it?

@paulo-ferraz-oliveira I missed your comment, sorry. But yes, I think it's still an issue.

jsx@3.1.0 behaviour is unchanged

1> jsx:decode(<<"{\"a\": \"b\", \"a\": \"c\"}">>).
#{<<"a">> => <<"c">>}
2> jsx:decode(<<"{\"a\": \"b\", \"a\": \"c\"}">>, [{return_maps, false}]).
[{<<"a">>,<<"b">>},{<<"a">>,<<"c">>}]

from jsx@2.8.0

1> jsx:decode(<<"{\"a\": \"b\", \"a\": \"c\"}">>).
[{<<"a">>,<<"b">>},{<<"a">>,<<"c">>}]
2> jsx:decode(<<"{\"a\": \"b\", \"a\": \"c\"}">>, [{return_maps, true}]).
#{<<"a">> => <<"c">>}

For starters, it would be good if there's no behaviour difference dictated by return_maps.
At this point, it is "better" to return_maps: false and do the validation outside of JSX, because when returning maps, there's simply no way to detect duplicate keys.