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

Parse error on valid json file #2029

Closed
nittdeniz opened this issue Apr 6, 2020 · 2 comments
Closed

Parse error on valid json file #2029

nittdeniz opened this issue Apr 6, 2020 · 2 comments

Comments

@nittdeniz
Copy link

I validated my JSON file with various tools online and they confirm that my JSON file has valid syntax. Yet I get a parse error

[
  {
    "particles": [
      ["antilepton", "lepton"],
      ["antilepton", "lepton"],
      ["photon"]
    ],
    "indices": [
      [],
      [],
      ["mu"]
    ],
    "rule": "-i*e*GA[mu]"
  }
]

what(): [json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - unexpected end of input; expected '[', '{', or a literal

To read the file I copied the example code from the README

std::ifstream ifs("my_file.json");
/*
std::stringstream ss;
ss << ifs.rdbuf();
std::cout << ss.str() << "\n";
// prints the file, so path is correct
*/

JSON json;
ifs >> json;
std::cout << json << "\n";

I also tried adding zeros in the empty arrays, but that didn't help either.

@nlohmann
Copy link
Owner

nlohmann commented Apr 6, 2020

I wish I could say more than that the parse error indicates that it was not possible to retrieve any byte from the input. Can you try reading the input into a string and parse from there?

@nittdeniz
Copy link
Author

Thanks, the following seems to be a workaround:

    std::ifstream ifs("my_file.json");
    std::stringstream ss;
    ss << ifs.rdbuf();
    std::string json_string = ss.str();

    JSON json = JSON::parse(json_string);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants