You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
the second "Width" in Thumbnail becomes "discarded" , and the "discarded" itself cannot be parsed if I feed the filtered json back to parser by : json parse_again=json::parse(j_filtered.dump());
it gives
terminate called after throwing an instance of 'nlohmann::detail::parse_error'
what(): [json.exception.parse_error.101] parse error at line 1, column 148: syntax error while parsing value - invalid literal; last read: '"Width":<'
When does the parser give the null object a "discarded" value or just remove the object if filtered by the parser callback? Could I chose which behavior it acts?
here's my original code:
#include <iostream>
#include <iomanip>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
int main()
{
// a JSON text
auto text = R"(
{
"Image": {
"Width": null,
"Height": 600,
"Title": "View from 15th Floor",
"Thumbnail": {
"Url": "http://www.example.com/image/481989943",
"Height": 125,
"Width": null
},
"Animated" : false,
"IDs": [116, 943, 234, 38793]
}
}
)";
// parse and serialize JSON
json j_complete = json::parse(text);
std::cout << std::setw(4) << j_complete << "\n\n";
// define parser callback
json::parser_callback_t cb = [](int depth, json::parse_event_t event, json & parsed)
{
// skip object elements with key "Thumbnail"
// if (event == json::parse_event_t::key and parsed == json("Thumbnail"))
if (event == nlohmann::json::parse_event_t::value and parsed.is_null())
{
return false;
}
else
{
return true;
}
};
// parse (with callback) and serialize JSON
json j_filtered = json::parse(text, cb);
std::cout << std::setw(4) << j_filtered << '\n';
json parse_again=json::parse(j_filtered.dump());
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
hi sir,
I'm studying the document of Parser Callbacks
I replace some of the values to null of the example , then expect the parser would remove the object if the value is null.
the input goes like :
Both "Width" under "Image" and "Thumbnail" are null.
in version 2.1.1 which directly installed from apt-get gives the result just as I expected, both the "Width" disappear due to the null value:
but in version 3.10.5 it gives the result like:
the second "Width" in Thumbnail becomes "discarded" , and the "discarded" itself cannot be parsed if I feed the filtered json back to parser by :
json parse_again=json::parse(j_filtered.dump());
it gives
When does the parser give the null object a "discarded" value or just remove the object if filtered by the parser callback? Could I chose which behavior it acts?
here's my original code:
Beta Was this translation helpful? Give feedback.
All reactions