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

Failing to Parse Valid JSON #2209

Closed
1 of 5 tasks
jgibson2 opened this issue Jun 22, 2020 · 11 comments
Closed
1 of 5 tasks

Failing to Parse Valid JSON #2209

jgibson2 opened this issue Jun 22, 2020 · 11 comments
Labels
kind: bug solution: invalid the issue is not related to the library

Comments

@jgibson2
Copy link

The library fails to parse seemingly-valid JSON

What is the issue you have?

This code snippet:

std::string jsonStr = R"({ "key": [ { "id": "this_is_a_string", "color": { "r": 125, "g": 23, "b": 78 } } ] })";
auto json = nlohmann::json::parse(jsonStr);

Fails with:

libc++abi.dylib: terminating with uncaught exception of type nlohmann::detail::parse_error: [json.exception.parse_error.101] parse error at 62: syntax error - unexpected string literal; expected '}'

What is the expected behavior?

No error to be thrown; the JSON is apparently valid:
https://jsonlint.com/?json={%20%22key%22:%20[%20{%20%22id%22:%20%22this_is_a_string%22,%20%22color%22:%20{%20%22r%22:%20125,%20%22g%22:%2023,%20%22b%22:%2078%20}%20}%20]%20}

And what is the actual behavior instead?

A crash, seemingly when parsing the integers -- it works when the integers are quoted.

Which compiler and operating system are you using?

OSX, with

Apple clang version 11.0.3 (clang-1103.0.32.62)
Target: x86_64-apple-darwin19.5.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

Which version of the library did you use?

  • latest release version 3.7.3
  • other release - please state the version: ___
  • the develop branch

If you experience a compilation error: can you compile and run the unit tests?

  • yes
  • no - please copy/paste the error message below
@nlohmann
Copy link
Owner

I cannot reproduce the issue with the same compiler.

Apple clang version 11.0.3 (clang-1103.0.32.62)
Target: x86_64-apple-darwin19.5.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

Full code for reference (using develop branch 29ad217):

#include <iostream>
#include "json.hpp"

using json = nlohmann::json;

int main() {
    std::string jsonStr = R"({ "key": [ { "id": "this_is_a_string", "color": { "r": 125, "g": 23, "b": 78 } } ] })";
    auto j = json::parse(jsonStr);
    std::cout << j << std::endl;
}

Output:

{"key":[{"color":{"b":78,"g":23,"r":125},"id":"this_is_a_string"}]}

Can you please double check your code?

@jgibson2
Copy link
Author

jgibson2 commented Jun 22, 2020

I ran that exact code and got the same error... very strange! Let me try with that specific branch.

@nlohmann
Copy link
Owner

Can you try to execute the unit tests?

@jgibson2
Copy link
Author

My "develop" was old. Works as expected with latest version. Thank you :)

@nlohmann
Copy link
Owner

Thanks for checking back!

@nlohmann nlohmann added the solution: invalid the issue is not related to the library label Jun 22, 2020
@chloro-pn
Copy link

chloro-pn commented Jul 2, 2020

I ran that exact code and got the same error... very strange! Let me try with that specific branch.

I get the same error on version 3.7.3 ...
The error occurs occasionally, when I call a lot of json::parse func.

  • macOS 10.14.6

@nlohmann
Copy link
Owner

nlohmann commented Jul 2, 2020

The library will accept any valid JSON as long as it’s UTF-8 encoded. Can you copy the exact input and the code you use to parse it?

@chloro-pn
Copy link

Sorry, I can't provide the specific data, because I chunked a large file and called JSON:: parse to serialize it. I found this error in the log file, and I found that the error is not deterministic, even if the same input data. I will continue to test it with other third-party JSON libraries, and I will reply in time whether there is an error in my program itself or the cause of JSON:: parse.

@nlohmann
Copy link
Owner

nlohmann commented Jul 2, 2020

If it’s nondeterministic, maybe you pass an invalid input buffer. Try running it with ASAN.

@chloro-pn
Copy link

I will

@chloro-pn
Copy link

It's my mistake , I mistakenly used the network library, resulting in a message error.
sorry to bother you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: bug solution: invalid the issue is not related to the library
Projects
None yet
Development

No branches or pull requests

3 participants