Suggestion to handle duplication key properties in object-level #4649
Replies: 3 comments
-
Adding a function to the SAX interface would be a breaking change to existing implementations. I am further not convinced that adding this functionality is of great benefit, because, as you mentioned, JSON does not forbid duplicate keys. |
Beta Was this translation helpful? Give feedback.
-
Dear Mr. Lohmann, Thank you for your prompt reply and for maintaining such a valuable library. I understand the concerns regarding making modifications that could break existing implementations. Rest assured, I am not requesting any changes to the nLohmann library. I previously suggested an enhancement and it was rightly declined. I have accepted this decision. While the JSON standard allows for duplicate keys at the object level, there are instances where it is necessary to check for such duplications. The nlohmann::json::json_sax_t interface seems ideal for this purpose, as it allows for duplication checks before the actual parsing takes place, serving as a preliminary step. For example, your library currently takes the last found key. If a configuration JSON file is used in a security or safety context and contains duplicate keys, it is highly recommended to check for such issues. While the standard permits such cases, it falls upon developers to ensure these issues are addressed. The software should serve us, and in this case, my modification of your library is used privately within my own context. If you find my post unsuitable for your concept, please feel free to remove it, or I can remove it myself. Thank you once again for your time and for creating such an outstanding library. Kind regards, |
Beta Was this translation helpful? Give feedback.
-
All fine. |
Beta Was this translation helpful? Give feedback.
-
Dear nLohmann-Json Team,
Thank you once again for providing the amazing library.
I'm currently using the SAX interface to detect if any key-property within an object-level is duplicated.
To clarify, the term "object level" does not include any existing sub-object levels. Here are some examples to illustrate:
Example of duplication:
{"a":"s","a":"c"} // Key-property "a" is duplicated.
Example without duplication:
{"a":"s","z":{"a":"c"}} // Key-property "a" is not duplicated.
I have noticed that some JSON schema validator libraries do not report a failure when a key within an object level is duplicated,
which conforms to the standard. However, in my case, I want to catch such failures in JSON configurations.
See please below used code snip below.
In addtion. It was neccesary to modify (quick and dirty) the nlohmann library, so that the following mesages are provided, when a duplication has veen found.
The modification consist of extension of the nlohmann::json::json_sax_t with a new function.
void position(nlohmann::detail::position_t position).
this allows to get the currently position of the Json-file during the parsing.
patch for nLohmann/json.hpp:
Demo Class
How to use:
Your library has been incredibly helpful, and I appreciate the hard work you put into it.
Omar
Beta Was this translation helpful? Give feedback.
All reactions