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.
Do you have a stacktrace?
Can you try this:
std::ifstream file("Test.json");
json j = json::parse(file);
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);
}
This sounds broken. Do you have a stack trace? Can you make sure you can read the file without the library?
Unfortunately I dont have a stack trace but I can provide this info:
| this | 0x0000000000000050 {_Storage={_Value=??? } } | const std::_Atomic_storage
 | _As_bytes | -858993460 | long
 | _Order | memory_order_relaxed (0) | std::memory_order
How do you know this is an issue in the library?
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?
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?
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
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;
I get another violation access error of JSON_THROW(*static_cast<const detail::out_of_range*>(&ex)); using the file
With the stringstream code?
yea
That code does not involve the JSON library. Can you please execute it in isolation?
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;
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?
It correctly displays the file contents.
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?
If the file is not accessible i get a JSON_THROW(*static_cast<const detail::out_of_range*>(&ex));.
Can you provide the what() string from the exception?
`this | 0x00eff1c0 {root={m_type=null (0 '\0') m_value={object=0x00000000
nlohmann::detail::json_sax_dom_parser
 __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
Which compiler and library version are you using? Can you share your input file?
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...
Thanks for reporting back. Maybe you find some more insights with Valgrind or the sanitizers.
@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
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.
Unfortunately, the library does not have more information as that it can’t read a character. Any proposals?
Von meinem iPhone gesendet
Am 27.11.2020 um 23:00 schrieb Sergey Korol notifications@github.com:

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.—
You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub, or unsubscribe.
@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.
@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?
@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.
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.