Skip to content

istream >> json --- 1st character skipped in stream #976

@us-er-name

Description

@us-er-name

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:

// Extracted from my code
json j;
std::ifstream is("filename");
is >> j;

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:

Line 3024    token_type scan()
			{
				// read next character and ignore whitespace
				do
				{
					get();
			...

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.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions