Closed schil227 closed 2 months ago
You forgot the _json
suffix for the string:
nlohmann::json fruit_json = R"(
{
"fruits": [
{
"key": "fruit.banana",
"value": "0"
},
{
"key": "fruit.apple",
"value": "4"
}
]
}
)"_json;
Without that string, you just cast the string to a json
, but do not parse it.
Ah... yes you're right. Thank you very much for the quick reply! Love the library btw.
Description
Depending on if json is loaded from a file, or if it is parsed from a string, it exhibits different behavior when iterating over lists of objects (see Reproduction steps for an example). When the json is read from a file, it iterates over the list of objects successfully. When the json is parsed from a string, an error is thrown.
The same behavior is observed when using the "json" syntax, e.g.
json j2 = { {"pi", 3.141}, ...
Reproduction steps
Consider the following json:
It contains an entry "fruits", which is a list of objects. I iterate over this list of object and make a change, like so:
(where
fruit_json
is a nlohmann::json object, containing the above json example)When
fruit_json
is created from loading a file, the loop is processed correctly:(fruits.json contains the json object above)
Output:
However, when the same object is parsed from a string, it does not successfully traverse the list:
Output:
Expected vs. actual results
I would expect the behavior to be the same, regardless of where the json is sourced from. I would also expect any json, regardless of the source, to iterate over lists of objects in the same manner as the code provided in the reproduction steps.
Minimal code example
Error messages
Compiler and operating system
gcc 10.2.1, Ubuntu 20.04
Library version
3.9.1
Validation
develop
branch is used.