This commit is contained in:
nlohmann committed 2026-11-12 11:46:38 +00:00
1 parent 8ddeea7128
commit 5097c20962
516 files changed
+7306 -5984

No files matched your search

+8 -6
View File
@@ -54,12 +54,14 @@ 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*` | 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 |
| 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; where the C library has unlocked stdio functions, 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 |
Defining [`JSON_DISABLE_STREAM_BULK_SCAN`](https://json.nlohmann.me/api/macros/json_disable_stream_bulk_scan/index.md) to `1` turns off the bulk scan of a stream buffer, so a `std::istream` is read one character at a time. Defining [`JSON_DISABLE_UNLOCKED_FILE_IO`](https://json.nlohmann.me/api/macros/json_disable_unlocked_file_io/index.md) to `1` makes a `FILE*` read every character with `std::fgetc`, without locking the file for the whole parse and without the bulk scan of its buffer (which `JSON_DISABLE_STREAM_BULK_SCAN` does not affect). Neither changes any result.
Input in one block of memory is read even faster with the default options (no [parser callback](https://json.nlohmann.me/api/basic_json/parser_callback_t/index.md), no comments, no trailing commas, and for `sax_parse()` `strict = true`): `parse()`, `accept()`, and `sax_parse()` then read the grammar and the tokens in one loop over the memory. Only after an error do `parse()` and `sax_parse()` read the input again, to report the error exactly as before. This faster path is not used when [`JSON_DIAGNOSTIC_POSITIONS`](https://json.nlohmann.me/api/macros/json_diagnostic_positions/index.md) is enabled, because the positions are recorded by the default parser: the input is then parsed as before, at the same speed as before.