msgspec icon indicating copy to clipboard operation
msgspec copied to clipboard

Duplicate key detection

Open DavidBuchanan314 opened this issue 1 year ago • 0 comments
trafficstars

Description

When decoding plain JSON with duplicate keys, all but the last are silently ignored, for example:

>>> msgspec.json.decode(b'{"abc":1,"abc":2}')
{'abc': 2}

This behaviour is "correct" (it's noted by rfc8259 as a possible behaviour), but I would nontheless like to detect this scenario and raise an exception - silently discarding information is undesirable for my use case.

Python's built-in JSON module can be extended to perform this checking like so: https://gist.github.com/DavidBuchanan314/bf57251b2519c5ea55042fc4af62e033, and ideally I'd like similar functionality from msgspec (I don't care about object_pairs_hook specifically, just the ability to check dupes).

p.s. this feature has also been requested (and closed as stale) in orjson ;) https://github.com/ijl/orjson/issues/456

DavidBuchanan314 avatar Feb 23 '24 09:02 DavidBuchanan314