Skip to content

Add Key name to Exception #932

Closed
@tcanabrava

Description

Bug Report

(I'm interested in developing this feature, so pointers are appreciated)

  • When working with an invalid json, the exception produced is not really helpfull.

I'v found out #160 - but that's a different issue than the one I'm having, and it will also be harder to fix because the values have no notion of key, the specific code I'm talking about on json.hpp follows this pattern:

template<typename BasicJsonType>
void from_json(const BasicJsonType& j, typename BasicJsonType::string_t& s)
{
    if (JSON_UNLIKELY(not j.is_string()))
    {
        JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name())));
    }
    s = *j.template get_ptr<const typename BasicJsonType::string_t*>();
}

but even if we have a small json to handle, the output is not really helpfull:

std::exception: [json.exception.type_error.302] type must be string, but is number

  • Small code example:
nlohmann::json j {{"firstElement", ""}};
j["firstElement"] = 0;
  • What is the expected behavior?
    std::exception: [json.exception.type_error.302] type for key firstElement must be string, but is number

  • Did you use a released version of the library or the version from the develop branch?

3.0.1

Metadata

Assignees

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions