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

Read Json from File #2139

Closed
llchris opened this issue May 26, 2020 · 30 comments
Closed

Read Json from File #2139

llchris opened this issue May 26, 2020 · 30 comments

Comments

@llchris
Copy link

llchris commented May 26, 2020

Hello, I am cynnad. I am loving so far your json library but i am encourring an issue with my code:

int read_json(const char* p_FilePath, json& p_Json) 
{
	std::ifstream stream;
	stream.open(p_FilePath);
	if(stream.is_open() && !stream.fail())
	{
		stream >> p_Json;
		return 0;
	}

	std::string errorMessage { "Failed to create an inputstream with the file in the path " };
	errorMessage.append(p_FilePath);
	__ERROR__(errorMessage);
	return 1;
}

Using the function:

          json myJsonConfig = {};
	  //Read from file and store into the json
	  if(!read_json("Test.json", myJsonConfig)) return -1;

This is what i came up after reading the wiki. This code seems not to work for me in fact i get a violation error :

Exception thrown at 0x00007FF797F6EF3B in Test.exe: 0xC0000005: access violation while reading the path 0x0000000000000050.

Did I read the json file correctly or did i misunderstand something? Thank you for reading.

@nlohmann
Copy link
Owner

Do you have a stacktrace?

Can you try this:

std::ifstream file("Test.json");
json j = json::parse(file);

@nlohmann nlohmann added the state: needs more info the author of the issue needs to provide more details label May 26, 2020
@llchris
Copy link
Author

llchris commented May 26, 2020

Thank you for answering, I get the same error even using the parse method. I fugured out it is something related to atomic in _NODISCARD _Ty load(const memory_order _Order) const noexcept { // load with given memory order auto _As_bytes = _ISO_VOLATILE_LOAD32(_Storage); _Load_barrier(_Order); return reinterpret_cast<_Ty&>(_As_bytes); }

@nlohmann
Copy link
Owner

This sounds broken. Do you have a stack trace? Can you make sure you can read the file without the library?

@llchris
Copy link
Author

llchris commented May 26, 2020

Unfortunately I dont have a stack trace but I can provide this info:

| this | 0x0000000000000050 {_Storage={_Value=??? } } | const std::_Atomic_storage<int,4> *
  | _As_bytes | -858993460 | long
  | _Order | memory_order_relaxed (0) | std::memory_order

@nlohmann
Copy link
Owner

How do you know this is an issue in the library?

@llchris
Copy link
Author

llchris commented May 26, 2020

Honestly I don't know, I came here to understand if this was my problem or a library problem. Sometimes running this code even made my windows to reload with errors. If the file is inaccessible shouldnt the stream.is_open() return false and then print the error message?

@nlohmann
Copy link
Owner

Maybe. But code like

std::ifstream file("Test.json");
json j = json::parse(file);

has been executed millions of times without crashing a machine. As there is no exception, it seems that there is anything else very broken on the system. Can you try to run in debugger or with santizers?

@llchris
Copy link
Author

llchris commented May 26, 2020

Ok so now I tried to read from a different json and it worked but if i take it off the directory i still get the problem. So that error occures when the file is not reachable. My windows system is fine, that crash happened just on that code. I don't want to other users to possibly "crash windows" if the file is not in the directory so is there a way to prevent this? Thanks

@nlohmann
Copy link
Owner

If you try to read from a file that could not be opened, you should get an exception:

[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

Can you do this with your file?

std::ifstream i("your_file");
std::stringstream ss;
ss << i.rdbuf();
std::cout << ss.str() << std::endl;

@llchris
Copy link
Author

llchris commented May 26, 2020

I get another violation access error of JSON_THROW(*static_cast<const detail::out_of_range*>(&ex)); using the file

@nlohmann
Copy link
Owner

With the stringstream code?

@llchris
Copy link
Author

llchris commented May 26, 2020

yea

@nlohmann
Copy link
Owner

That code does not involve the JSON library. Can you please execute it in isolation?

@llchris
Copy link
Author

llchris commented May 26, 2020

I get this new error using this code:
std::ifstream stream("Test.json"); std::stringstream ss; ss << stream.rdbuf(); std::cout << ss.str() << std::endl; json myJson; stream >> myJson;

@nlohmann
Copy link
Owner

This cannot work as the stream is "empty" after it has been stream to the stringstream. can you remove the lines related to json can verify that the content of the file is displayed on std::cout?

@llchris
Copy link
Author

llchris commented May 26, 2020

It correctly displays the file contents.

@nlohmann
Copy link
Owner

Can you try a minimal example?

#include <fstream>
#include <nlohmann/json.hpp>

int main()
{
  std::ifstream file("Test.json");
  json j = json::parse(file);
}

without any further libraries?

@llchris
Copy link
Author

llchris commented May 27, 2020

If the file is not accessible i get a JSON_THROW(*static_cast<const detail::out_of_range*>(&ex));.

@nlohmann
Copy link
Owner

Can you provide the what() string from the exception?

@llchris
Copy link
Author

llchris commented May 27, 2020

`this | 0x00eff1c0 {root={m_type=null (0 '\0') m_value={object=0x00000000 array=0x00000000 string=...} } ...}

nlohmann::detail::json_sax_dom_parser<nlohmann::basic_json<std::map,std::vector,std::string,bool,__int64,unsigned __int64,double,std::allocator,nlohmann::adl_serializer,std::vector<unsigned char,std::allocator>>> *

 __formal | 1 | unsigned int
__formal | "" | const std::string &
ex | {byte=1 } | const nlohmann::detail::exception & {nlohmann::detail::parse_error} `

Unhandled exception at 0x74BA4402 in test333.exe: Microsoft C ++ exception: nlohmann :: detail :: parse_error at memory location 0x004FE1E0.

This is all I get

@nlohmann
Copy link
Owner

Which compiler and library version are you using? Can you share your input file?

@llchris
Copy link
Author

llchris commented May 27, 2020

I figured out what the problem was. The problem was the stream, for some reason it can't be made in static libraries ( I am compiling the project as a static lib and testing it in a exe project)? If I move the stream creation in the executable everything is magically fixed. I don't get why i can't create streams in libraries but that seems the solution...

@nlohmann
Copy link
Owner

Thanks for reporting back. Maybe you find some more insights with Valgrind or the sanitizers.

@nlohmann nlohmann removed the state: needs more info the author of the issue needs to provide more details label May 28, 2020
@sskorol
Copy link

sskorol commented Nov 27, 2020

@llchris hi, I'm getting the same error and building my code as an executable. Could you please explain how exactly did you resolve it?

[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

@sskorol
Copy link

sskorol commented Nov 27, 2020

Seems like that was my bad. I've tried to read a file that was missing in a build folder. However, this error is a bit confusing.

@nlohmann
Copy link
Owner

nlohmann commented Nov 27, 2020 via email

@sskorol
Copy link

sskorol commented Nov 29, 2020

@nlohmann can we check if the file is empty? Or if an open stream returns false? Throwing an exception seems fine to me. But would be really great if it'd be self-explanatory. As the wrong file path and syntax error are 2 different root causes. My first thought when I saw this message was a missing double-quote or coma, or just a wrong bracket in a json.

@gregmarr
Copy link
Contributor

gregmarr commented Dec 1, 2020

@sskorol Did you see this section in the error - unexpected end of input? Is that not enough of an indicator? How could this be made clearer?

@sskorol
Copy link

sskorol commented Dec 1, 2020

@gregmarr maybe we live in different worlds, but unexpected end of input with a hint about brackets doesn't seem like a synonym of a file doesn't exist to me. It might seem so for a person who learned this pattern once and now thinks it's obvious. But again, this particular error would make more sense in the case of a real syntax error. I'm talking about UX now. Moreover, TS mentioned the same concern in one of his comments above. So it's not a coincidence.

@gregmarr
Copy link
Contributor

gregmarr commented Dec 1, 2020

I don't see it as a synonym for file doesn't exist as this library doesn't deal with files. It deals with strings and streams. It's the code that's opening the file that would tell you file doesn't exist. If you're using ifstream you should be checking that the stream successfully opened the file.

Since it doesn't know anything about your files, the best the library could probably do is detect unexpected end of input combined with line 1, column 1 and say zero-length input. Would that be more helpful? It still doesn't indicate file doesn't exist.

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

No branches or pull requests

4 participants