json icon indicating copy to clipboard operation
json copied to clipboard

Request for I-JSON (RFC 7493) Parsing Mode

Open jayeshbadwaik opened this issue 5 years ago • 12 comments

I was wondering if there is any interest in implementing the I-JSON parsing mode for boost.json. I-JSON is an interoperable version of JSON which aims to remove some ambiguities in the parsing. The changes required for such a compliance are listed in Section 2 of the document.

jayeshbadwaik avatar Jul 15 '20 03:07 jayeshbadwaik

It is very likely that we already follow these rules. If so, we should document it. If not, we should consider whether to follow it.

vinniefalco avatar Jul 16 '20 00:07 vinniefalco

The only normative requirement we don't comply with is checking for duplicate object key. There is some normative encouragements in there that we don't strictly follow, such as limiting precision.

sdkrystian avatar Aug 10 '20 19:08 sdkrystian

We never emit JSON with duplicates, as the DOM (via json::object) does not allow it. When we parse JSON, I believe we always take the first value if there are duplicate keys

vinniefalco avatar Aug 10 '20 20:08 vinniefalco

The restriction of allowing only a single object key is during the parsing, not emission.

jayeshbadwaik avatar Aug 11 '20 05:08 jayeshbadwaik

When an object is constructed during parsing, we check its elements for duplicates here: https://github.com/CPPAlliance/json/blob/ec29dfa768a16672534f758005dfe4841a8ea93d/include/boost/json/detail/impl/object_impl.ipp#L79 The duplicates are removed by replacing the duplicate with the last element in the object array, and then shrinking the array size by 1.

vinniefalco avatar Aug 11 '20 13:08 vinniefalco

@vinniefalco To conform to I-JSON we would have the check for duplicate keys within basic_parser, not the handler.

sdkrystian avatar Aug 11 '20 22:08 sdkrystian

Fuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuck

vinniefalco avatar Aug 11 '20 22:08 vinniefalco

basic_parser can never do such a thing, so I don't think we can claim I-JSON support for it. But that's okay, because we can claim I-JSON support in parser which is what 99% of users care about. For the other 1% of users who are implementing their own parser by using basic_parser directly, I think it is reasonable to expect that if they want I-JSON compliance, that can be responsible for it. How do you feel about that @jayeshbadwaik ?

vinniefalco avatar Aug 11 '20 22:08 vinniefalco

One way to implement that in your codebase I see is to throw in case of duplicate in here: https://github.com/CPPAlliance/json/blob/ec29dfa768a16672534f758005dfe4841a8ea93d/include/boost/json/detail/impl/object_impl.ipp#L79

And handle that exception upward to report that the provided I-JSON document is ill-formed. This will require adding an option to parse_options to allow the user to parse in "I-JSON" mode and require you to remove noexcept qualifer for object_impl.build(). Since I am not aware of how the rest of the code works, I wonder if that is palatable.

jayeshbadwaik avatar Aug 12 '20 05:08 jayeshbadwaik

Actually, when we detect a duplicate we can set a bit somewhere in the object and then in parser we can query new objects to see if there's a duplicate and throw as needed.

vinniefalco avatar Sep 15 '20 14:09 vinniefalco

That can work too, yeah.

jayeshbadwaik avatar Sep 16 '20 01:09 jayeshbadwaik

We should target this for the middle of year release?

vinniefalco avatar Mar 04 '21 05:03 vinniefalco