-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
Comments
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 |
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<int,4> * |
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 |
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:
Can you do this with your file?
|
I get another violation access error of |
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: |
This cannot work as the stream is "empty" after it has been stream to the stringstream. can you remove the lines related to |
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 |
Can you provide the |
`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
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?
|
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 ***@***.***>:
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 - |
@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 Since it doesn't know anything about your files, the best the library could probably do is detect |
Hello, I am cynnad. I am loving so far your json library but i am encourring an issue with my code:
Using the function:
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.
The text was updated successfully, but these errors were encountered: