This commit is contained in:
nlohmann committed 2026-11-07 09:32:17 +00:00
1 parent 5531ec9380
commit cf8df97ba7
12 files changed
+440 -428

No files matched your search

+6 -6
View File
@@ -54,12 +54,12 @@ When using `operator>>` to read several concatenated values this way, a value th
All inputs produce the same values and the same exceptions, but they are read in different ways, which makes some of them faster than others:
| input | how it is read |
| ---------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------- |
| strings, string views, string literals, contiguous containers, pointer or iterator ranges over contiguous memory | the whole input is scanned in bulk: runs of string characters, numbers, and indentation are recognized several bytes at a time |
| `std::istream` (`std::ifstream`, `std::istringstream`, ...) | the characters the stream buffer holds are scanned in bulk like a string; the stream buffer is asked for more when they are used up |
| `FILE*` | one character at a time, with the file locked once for the whole parse |
| other iterator ranges (e.g., `std::list<char>`, `std::istreambuf_iterator<char>`), wide strings | one character at a time |
| input | how it is read |
| ---------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| strings, string views, string literals, contiguous containers, pointer or iterator ranges over contiguous memory | the whole input is scanned in bulk: runs of string characters, numbers, and indentation are recognized several bytes at a time |
| `std::istream` (`std::ifstream`, `std::istringstream`, ...) | the characters the stream buffer holds are scanned in bulk like a string; the stream buffer is asked for more when they are used up |
| `FILE*` | with glibc and the Apple C library, the characters the `FILE` holds in its buffer are scanned in bulk like a stream's; elsewhere one character at a time; the file is locked once for the whole parse |
| other iterator ranges (e.g., `std::list<char>`, `std::istreambuf_iterator<char>`), wide strings | one character at a time |
Parsing a file from a `std::ifstream` is therefore only about 10 to 20 % slower than reading the whole file into a `std::string` first and parsing that, and it needs no memory for a copy of the file. The stream is never read ahead: after parsing, it is positioned exactly as if every character had been read one at a time, so [`operator>>`](https://json.nlohmann.me/api/operator_gtgt/index.md) can still read several values from one stream (see also [JSON Lines](https://json.nlohmann.me/features/parsing/json_lines/index.md)).