-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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
istream >> json --- 1st character skipped in stream #976
Comments
I cannot reproduce this issue. We have a lot of test cases reading from files, and all these tests are executed on numerous compilers, including MSVC 2017 (see the end of section https://github.com/nlohmann/json#supported-compilers). I tried to add a character
(using Xcode Version 9.2 (9C40b)) I would need more information on this. |
Ok. Now I feel stupid. I kinda thought this "bug" could not have slipped thru. I went thru my original code and figured out what causes unget() to fail. unget() fails silently, even though I read that unget() should set failbit or badbit if unsuccessful (which it didn't). The simplified code (below) verifies the issue and was created as a Windows Console App:
It was only when I totally exhausted my list of runtime checks and did all my tracing that I hesitantly posted this in the first place - unget() claimed success even though it failed. Sorry about wasting your time. |
No worries! Thanks for reporting, and also for following up! |
Hi, IMHO this is actually a real bug. In the example above the input stream
This is... unfortunate. After the call The The library could provide a
The user would be responsible for testing/setting the stream flags and probably guarding the above code by first constructing an |
Just an idea, I haven't read through the source very carefully yet, but I think that stream parsing can be made more conforming by switching from I don't expect using
will continue to work (if the stream is valid) as before. (This is 100% of my use cases actually). But I really need to write some code before commenting further, to see if it works out ;-) |
|
Thanks @nlohmann. I have implemented |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
In #834 we discussed that the Maybe this can also simplify PR #984. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
I have a program that opens the same json file with this stream approach twice in two functions: std::ifstream i(jsonFIle);
json j_conf;
i >> j_conf; It would through the exception of parse error on the second function:
Is it related to this issue? Is there any workaround for now? |
Is the same file open twice at the same time, or does one function open, read, and close the function before the second one opens it? |
The first function should be closed/returned before the second one opens it. |
Oh. I found the reason, it' s my fault. 😅 |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
BUG: The first character in the stream file is skipped.
I took the single include source and used it in some test code.
Source code posted about Feb 13
https://github.com/nlohmann/json/blob/develop/single_include/nlohmann/json.hpp
What I did amounted to:
Tracing thru the source, I found this behaviour is the result of the code looking for a BOM at the start of the UTF-8 stream:
It fetches the first character. Since it is not part of the BOM, it performs:
Line 1669 is.unget(); // no byte order mark; process as usual
Which is what it should do BUT ...
when get() is called shortly after:
the variable 'current' contains the second char, as if unget() did not happen.
Not sure as to why. Repeated get() do get all subsequent chars one by one.
Since this was just a test, the workaround is to add in a return and start text on line two. Then it works fine. Actually, to verify the bug, placing ANY character in the first position shows that character is ignored.
General settings:
Using MSVC C++ V17 (v141)
MFC in shared DLL
EXE w/Unicode support. no CLR support.
Text file loaded, edited, saved in MSVC IDE
Tested on source uploaded 8 days ago and also reproduced in a version before that.
The text was updated successfully, but these errors were encountered: