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 Multiple JSON Files #1240

Closed
schoen2016 opened this issue Sep 14, 2018 · 2 comments
Closed

Parsing Multiple JSON Files #1240

schoen2016 opened this issue Sep 14, 2018 · 2 comments

Comments

@schoen2016
Copy link

Hi,

Thank you for reading.

I am attempting to parse multiple json files and receiving an error :

[json.exception.parse_error.101] parse error at 15: syntax error - unexpected '}'; expected end of input.
Aborted

I use the following function to parse the json file
`

json JSON_Reader(std::string filename){
	char ch, json_ch[1000000];
	std::ifstream f(filename, fstream::in);
	int i = 0;
	while (f >> noskipws >> ch){
		json_ch[i] = ch;
		i++;
	}
	f.close();

	//Parse JSON file
	return json::parse(json_ch);
}

`

MAIN
`

using json = nlohmann::json;

json JSON_Reader(std::string);

int  main(int argc, char* argv[]) {

	if (argc < 3) {
		std::cerr << "Usage: " << argv[0] << " [JSON FILE]" << std::endl;
		return 1;
	}
	printf("arg %s\n", argv[1]);
	printf("arg %s\n", argv[2]);
	printf("arg %s\n", argv[3]);

	json setup;
	json setup2;
	json setup3;

	setup = JSON_Reader(argv[1]);
	cout << setup["num"] << "\n\n";
	setup2 = JSON_Reader(argv[2]);
	cout << setup2["num"] << "\n\n";
	setup3 = JSON_Reader(argv[3]);
	cout << setup3["num"] << "\n\n";
}

`

JSON file
{ "num": 2 }

All three arguments call a different json file with the same content shown above. The expected behavior is that '2' is printed in the terminal three times.

After some trial and error I found that the last one saved gives me the error. The three setup files are called setup1.json, setup2.json and setup3.json. If I save setup1.json or setup2.json last then I get the error mentioned above. If I save setup3.json last, there is no error.

However, lets say I do save setup3.json last :
When I use setup3.json for all three arguments I get no error.
When I exclude setup3.json, replacing it with one of the other json files, I get no error.

I am using gcc version 5.3.0 which I believe is said to be supported, please correct me if I am mistaken.
JSON for Modern C++ version 3.2.0.

@RokerHRO
Copy link

@schoen2016 : your problem seems to be completely unrelated to the json library, there are a lot of other problems in your code:

  1. fix file buffer of 1000000 bytes, without any check for overflows. Don't do that! Moreover, not all platforms support such big stack spaces (okay, the compiler should warn in these cases). Why don't you just call json::parse( std::istream_iterator<char>(f), std::istream_iterator<char>() ); ?
  2. instead of complicated descriptions just make a table which program parameters work and which won't work:
Example Result
program setup1.json ok
program setup1.json setup2.json ok
program setup1.json setup2.json setup3.json not ok
... ...

@schoen2016
Copy link
Author

@RokerHRO: Thank you for you help, your solution worked like a charm. I suspected my problem was with my implementation. I apologize if my post was trivial.

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