json: stream error

I’ve been trying to implement this solution and I have been consistently getting the same stream error, regardless of the json file or example I use/follow. I know I’m probably doing something silly but I’m a lowly junior in my CS degree. Here is my code:

#include "json.hpp"

#include <fstream>

using json = nlohmann::json;

int main() {
    std::ifstream ifs{"file.json"};
    json parsed_json = json::parse(ifs);
    return 0;
}

and here is the json I’ve been using:

[
  {
    "stringKey": "Nathen",
    "intKey": 236
  },
  {
    "stringKey": "Emerald",
    "intKey": 739
  },
  {
    "stringKey": "Franco",
    "intKey": 544
  }
]

and here is the error: /home/bry/CLionProjects/jsonexample/cmake-build-debug/jsonexample terminate called after throwing an instance of 'std::invalid_argument' what(): stream error Process finished with exit code 134 (interrupted by signal 6: SIGABRT)

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 29 (11 by maintainers)

Most upvoted comments

@nlohmann You are right the string which I was receiving was incomplete. Your library works perfect! Thank you.

So apparently my issue has something to do with initializer lists, dsteeam and command line args. When I pass a command line argument [: jsonFileIn(argv[2])] through an fstream initializer list, it didn’t read anything in the file. But when I explicitly write[ : jsonFileIn(“./result.json”)] it works. I still don’t get why this works, but I guess it does?

Long story short, it had nothing to do with your library.