Replies: 2 comments
-
|
I think the answer is: yes, it would be reasonable as an opt-in parse mode, but today it probably needs a custom layer because the current DOM result cannot represent duplicate object names. Once parsing has produced a A parser callback can catch the common case by keeping a stack of key sets, one set per object depth, for example: #include <nlohmann/json.hpp>
#include <stdexcept>
#include <string>
#include <unordered_set>
#include <vector>
std::vector<std::unordered_set<std::string>> keys;
auto reject_duplicate_keys = [&](int depth,
nlohmann::json::parse_event_t event,
nlohmann::json& parsed) {
using event_t = nlohmann::json::parse_event_t;
if (event == event_t::object_start) {
if (keys.size() <= static_cast<std::size_t>(depth)) {
keys.resize(static_cast<std::size_t>(depth) + 1);
}
keys[static_cast<std::size_t>(depth)].clear();
return true;
}
if (event == event_t::key) {
auto& seen = keys[static_cast<std::size_t>(depth)];
const auto key = parsed.get_ref<const std::string&>();
if (!seen.insert(key).second) {
throw std::runtime_error("duplicate JSON object key: " + key);
}
return true;
}
if (event == event_t::object_end && keys.size() > static_cast<std::size_t>(depth)) {
keys[static_cast<std::size_t>(depth)].clear();
}
return true;
};
const auto j = nlohmann::json::parse(input, reject_duplicate_keys);That said, your two objections are valid:
So if this were added to the library, I would expect it to live below the DOM-building layer, probably as a parser option or SAX-level behavior, not as a post-parse validation step. That would also allow the library to report the duplicate at the point where the key token is read. For now, if you need strict validation for user-edited config files, I would wrap parsing in one small helper like the callback above and treat the thrown exception as a validation error. For high-quality diagnostics with positions, a SAX handler or a dedicated pre-validation parser would be a better fit than checking the completed |
Beta Was this translation helpful? Give feedback.
-
|
Thanks @dingminghbut for the detailed workaround — it matches the direction we want to go: no library-code change here, since the parser-callback approach already covers this today. We have documented it as a recipe instead: PR #5259. While writing it up as a compiled, tested example, we found and fixed an off-by-one bug in the depth-indexed bookkeeping from the snippet above — This comment was written by Claude Code on behalf of @nlohmann. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
I would like to re-open the discussion on how objects with non-unique should be handled at load. This topic has been discussed previously there:
As stated by @nlohmann, RFC 7159 states that:
... and currently, the nlohmann::json library implements the 1st behavior only (i.e. only the last key/value pair is loaded in case of duplicated keys).
@nlohmann suggests that the 2nd behavior (i.e. reporting an error) could be implemented using parser callbacks. However:
Then, would it be possible to implement the "raise error in case of duplicate keys" behavior as a builtin optional feature in the nlohmann::json library? (typically a behavior for which it could be possible to opt-in when invoking
nlohmann::json::parse(..))Beta Was this translation helpful? Give feedback.
All reactions