Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parsing error when there is a json string within a Json #3445

Closed
2 tasks
WorstCodeWay opened this issue Apr 18, 2022 · 9 comments
Closed
2 tasks

Parsing error when there is a json string within a Json #3445

WorstCodeWay opened this issue Apr 18, 2022 · 9 comments
Labels
state: needs more info the author of the issue needs to provide more details

Comments

@WorstCodeWay
Copy link

WorstCodeWay commented Apr 18, 2022

Description

In some situation, I want a special json object which has a json string as its value, take following as a example:

set std::string samplestring with following value:

{
  "some_key":"{\"in_key\":\"in_value\"}"
}

Reproduction steps

Just pass the string above for parsing

Expected vs. actual results

Successfully parse that string with a json object, and we can also get sub-string of inner-json object.

Minimal code example

auto json = nlohmann::json::parse(samplestring);

Error messages

error: std::exception

Compiler and operating system

ubuntu 18.04 g++ 7.5

Library version

Maybe 13? Don't get this project from GitHub, so don't know the version.

Validation

@WorstCodeWay WorstCodeWay changed the title Json string within a Json will parse error Parsing error when there is a json string within a Json Apr 18, 2022
@nlohmann
Copy link
Owner

What is the what() message of the exception?

@nlohmann nlohmann added state: needs more info the author of the issue needs to provide more details and removed kind: bug labels Apr 18, 2022
@falbrechtskirchinger
Copy link
Contributor

Please show a complete example. It works just fine here: https://godbolt.org/z/sjGq79Pnn
Output:

{
    "some_key": "{\"in_key\":\"in_value\"}"
}
{
    "in_key": "in_value"
}

@WorstCodeWay
Copy link
Author

@nlohmann

What is the what() message of the exception?

Just as the Error Messages says, std::exception, I capture this exception as std::exception:

  try{
    auto req = nlohmann::json::parse(data, data+size); // data is std::int8_t *
  }catch (std::exception e){
    std::cout << "error: " << e.what();
  }

@WorstCodeWay
Copy link
Author

WorstCodeWay commented Apr 19, 2022

@falbrechtskirchinger
I'm working on a minimal example

@WorstCodeWay
Copy link
Author

WorstCodeWay commented Apr 19, 2022

@falbrechtskirchinger @nlohmann
I think I get some useful information after many tries, the following are correct:

{"some_key":"{\"in_key\":\"in_value\" }"}
{"some_key":
    "{\"in_key\":\"in_value\" }"
}

Following are incorrect:

{"some_key":
    "{\
        \"in_key\":\"in_value\"\
    }"
}
{
   "some_key":"{"in_key":"in_value" }"
}
{"some_key":"{\"in_key\":\"in_value\"\
    }"
}
{"some_key":"{
        \"in_key\":\"in_value\"
    }"
}

Conclusion from my opinion, the sub-json string value shouldn't be operated into multi-lines. and the '' ESC expected。However, I don't know the root reason.

Besides, I'm introduce this project along with other third party project which is a tcp server, so I tested all this from a tcp client tool, pasting json strings above into client text widget as plain text, and send. So I guess the format reason is the most? Right?

@falbrechtskirchinger
Copy link
Contributor

That's simply not valid JSON. Putting a backslash before a line break to escape it is not a thing in JSON (as far as I'm aware of).

@WorstCodeWay
Copy link
Author

WorstCodeWay commented Apr 19, 2022

@falbrechtskirchinger

That's simply not valid JSON. Putting a backslash before a line break to escape it is not a thing in JSON (as far as I'm aware of).

{
    "some_key":"{
        \"in_key\":\"in_value\"
    }"
}

Sorry for incomplete example list, I have added fourth example of incorrect list. I notice your point: not invalid JSON for ESC. But the fourth bad example should be a valid json?

@falbrechtskirchinger
Copy link
Contributor

JSON doesn't support multi-line strings. Note that the Markdown renderer highlights your syntax errors in red. Below is the corrected version:

{
    "some_key":"{\n        \"in_key\":\"in_value\"\n    }"
}

@WorstCodeWay
Copy link
Author

JSON doesn't support multi-line strings. Note that the Markdown renderer highlights your syntax errors in red. Below is the corrected version:

{
    "some_key":"{\n        \"in_key\":\"in_value\"\n    }"
}

Thank you very much @falbrechtskirchinger . Now I understand what have happened. I will close this post, as it's not a bug.

Sorry for wasting your time.
Thanks, again. @nlohmann @falbrechtskirchinger

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
state: needs more info the author of the issue needs to provide more details
Projects
None yet
Development

No branches or pull requests

3 participants