quinnj / JSON3.jl

Other
214 stars 47 forks source link

Repeated key creates a problem when reading objects #216

Open nlw0 opened 2 years ago

nlw0 commented 2 years ago

Having a repeated key in an object definition causes the last value to be dropped. For example:

julia> using JSON3

julia> JSON3.read("""{"keyA": 1, "keyB": 2, "keyC": 3}""")
JSON3.Object{Base.CodeUnits{UInt8, String}, Vector{UInt64}} with 3 entries:
  :keyA => 1
  :keyB => 2
  :keyC => 3

julia> JSON3.read("""{"keyA": 1, "keyA": 2, "keyC": 3}""")
JSON3.Object{Base.CodeUnits{UInt8, String}, Vector{UInt64}} with 2 entries:
  :keyA => 1
  :keyA => 2
quinnj commented 2 years ago

Digging into this; a few thoughts up front though:

julia> x = JSON3.read("""{"keyA": 1, "keyA": 2, "keyC": 3}""")
JSON3.Object{Base.CodeUnits{UInt8, String}, Vector{UInt64}} with 2 entries:
  :keyA => 1
  :keyA => 2

julia> x.keyC
3

Anyway, I'll have to think about what to do here. Maybe we need to expose a way to index a property by ordinal number to allow getting either keyA value. It seems we also need to fix the length computation.

quinnj commented 2 years ago

Here's a PR that at least fixes the length computation: https://github.com/quinnj/JSON3.jl/pull/217

nlw0 commented 2 years ago

Great, thanks for looking into it. I must check in my actual application if the last key really is present, because I think that's how I found the problem, not just looking at the print.

I would expect a repeated key to just be discarded, with any one the possible values being chosen... Or maybe even an error. I had a repeated entry in my file by accident, but other JSON parsers just ignored it, so I never noticed.

nlw0 commented 2 years ago

The problem I had before was that converting the object to a Dict with copy drops the last value, but it's in the original object indeed. Hopefully the length fix already solves that as well.