I have the following function:
// get-function that can handle nullptr.
template <typename T>
static const T get(const nlohmann::json& json, const T& retOnNull = T())
{
return json.is_null() ? retOnNull : json.get<T>();
}
Then I call it like this:
nlohmann::json j;
// ...
// Load json from file into j
auto ret = get<std::string>(j["SomeKey"], "");
Now I would expect one of three things to happen:
- If "SomeKey" exists and is a string it should return that value
- If "SomeKey" doesn't exist it should first be created with null as default value and then sent into the function which should return an empty string as it received a null value
- If "SomeKey" existed but wasn't a string it should throw a
json.exception.type_error.302
Instead I sometimes (randomly) get the json.exception.type_error.302
exception even though, in my case, "SomeKey" doesn't exist. I checked what happens at that point and the data sent into the get
function seems to be not null and of type 105 (which is an indication the data is undefined). So how can this call into this function yield undefined data? Is it something about the new json entry going out of scope due to the reference pointer? Why does it happen randomly and not consistently? Is this an invalid way of using a new key?