-
Notifications
You must be signed in to change notification settings - Fork 334
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
[FIX] JSON.dump for hash with duplicate key #564
[FIX] JSON.dump for hash with duplicate key #564
Conversation
I'm not sure why expected behavior is correct. Because result of
Please let me know if my understanding is wrong. |
@hsbt , if the colon (:) syntax is used to create symbols as keys, the output matches the expected result: ruby -e "gem 'json', '2.6.3'; require 'json'; puts JSON::VERSION, JSON.dump({ 'a': 1, a: 2 })"
-e:1: warning: key :a is duplicated and overwritten on line 1
2.6.3
{"a":2}
ruby -e "gem 'json', '2.7.1'; require 'json'; puts JSON::VERSION, JSON.dump({ 'a': 1, a: 2 })"
-e:1: warning: key :a is duplicated and overwritten on line 1
2.7.1
{"a":2} However, when the rocket (=>) syntax is used to associate keys with values in the hash, the JSON output contains duplicate values. Therefore, the output should be consistent regardless of the syntax used to create the hash. |
@maniSHarma7575 it's not a "syntax" difference. One is strings the other is symbols. However your PR is nowhere near enough, this repo contains 3 alternative implementations, one in Ruby, one in C, one in Java. All 3 would need to be changed. |
@hsbt he's not trying to fix a regression, but do prevent duplicated keys in the output. Arguably it's a minor bug, but it's true that it's not really expected for a JSON library to allow this. As you know JSON has multiple standards, most don't say anything about that, but RFC 8259 says keys SHOULD be unique. And more generally it's unclear how parsers will handle it, some may keep the first value, some the last, etc. |
@byroot , it's functioning well for all three alternative implementations, as I've implemented the fix in https://github.com/flori/json/blob/78643245d2b60f770946e2e252890655d61737fc/lib/json/common.rb#L614 For all the 3 implementations I have tested it:
Created a script
|
Hi @hsbt Could you please review this? |
I don't think This may cover simple cases but not, for example, differently encoded strings:
Covering that would have to be quite difficult. I think it's best we leave the responsibility to the user to provide reasonable input. (As I wrote in the Rails issue) I'd actually prefer |
Thanks for the PR but I agree with John here. This is too costly to protect against this. |
Motivation / Background
This Pull Request has been created because to fix #563
The output for the hash is inconsistent when using
JSON.dump
with the duplicate key. Specifically, if we examine the result below, the key 'a' appears twice in the output JSON for the same key 'a'.For example:
Actual Result:
"{\"a\":1,\"a\":2}"
Expected result:
"{\"a\":2}"
cc: @byroot @hsbt