Closed sgshulman closed 1 year ago
That is not a bug, but documented behavior: https://json.nlohmann.me/api/basic_json/count
Thank you for your answer very much!
Now I see that json using both std::map
and nlohman::ordered_map
is not able to contain elements with the same name. std::multimap
may contain such elements, but do not provide operator[]
(operator[]
for multimap is a strange thing indeed). It also looks like json::emplace does not support same keys.
I'll have to find a better way to catch such errors.
You could use the parser callback mechanism (https://json.nlohmann.me/features/parsing/parser_callbacks/) to check for duplicate keys when you read the config file. I have not done it, but it should work with event parse_event_t::key
.
Thank you very much for this advice!!!
I made a brief experiment and it seems to work fine. It is a good question, how to implement it effective for a general use case, but in my case a simple class with std::vector<std::set<std::string>>
inside may be a reasonable choice.
Great thanks for your library!
Description
json may contain duplicated keys in the object but
json::count
returns only 0 or 1. We also havejson::contains
method, which returnstrue
offalse
. So, these two methods seems to do the same thing.I use json as a configuration file for my program. Once I had misprint/copy-paste error with json object having duplicated keys. I decided to check the configuration file for duplicated keys in object (to avoid such errors in the future) but discovered that
count
method does not provide such a possibility.Reproduction steps
Use
count
method on json with duplicated keys and check the return value (see code example below).Expected vs. actual results
count
method returns actual number of keys in object, which may exceed 1.Minimal code example
Error messages
No response
Compiler and operating system
clang++14
Library version
3.11.2
Validation
develop
branch is used.