Skip to content

avoid malformed json #433

Closed
Closed

Description

@whitecloud-sas

Hello!

if I deliver a malformed string like {"key1":"val1", "key.... it crashes at json j_complete = json::parse(v_string); line, exists something to check this before parse? thanks!

Activity

  1. nlohmann commented on Jan 21, 2017

    @nlohmann
    Owner

    No, the parser does this check. But you can catch the exception that is thrown in case of a parse error.

  2. nlohmann commented on Jan 24, 2017

    @nlohmann
    Owner

    @whitecloud-sas Does this help you?

  3. whitecloud-sas commented on Jan 24, 2017

    @whitecloud-sas
    Author

    Yes, this is what I did:

    try {
    json object;
    auto text = str;
    std::stringstream ss;
    ss << text;
    json j_complete = json::parse(ss);
    object = j_complete;
    auto key = object.find("key");
    if (*key== "value") {
    //do something
    }
    } catch (const std::invalid_argument&) {
    // parsing a JSON serialization must not fail
    printf("JSON malformed %s\n", str.c_str());
    }

    Thanks @nlohmann

  4. nlohmann commented on Jan 24, 2017

    @nlohmann
    Owner

    Note with #301 we shall add diagnosis information to the exceptions so you can provide the user the location in the JSON text that led to the error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions