This commit is contained in:
nlohmann committed 2026-10-27 12:14:00 +00:00
1 parent b36f8a9821
commit 11a863958e
1180 files changed
+14211 -6025

No files matched your search

+89 -2
View File
@@ -32,6 +32,8 @@ flowchart LR
oa --> output
```
<p class="nj-caption" markdown>Data flow through the library: input runs left to right through an input adapter and the lexer and parser (JSON text) or the `binary_reader` (binary formats) into SAX events, which build the value tree; the `serializer` or the `binary_writer` writes the tree to an output adapter.</p>
- **JSON text** is read by an [input adapter](#input-adapters), tokenized by the lexer, and turned into SAX events by
the parser.
- **Binary formats** (BJData, BON8, BSON, CBOR, MessagePack, UBJSON) are read by an input adapter and turned into the same SAX
@@ -192,6 +194,8 @@ packet-beta
96-127: "next"
```
<p class="nj-caption" markdown>Layout of one 16-byte node of a JSON view index. Each row is 32 bits wide, and the numbers are the bit offsets of the fields `kind`, `flags`, `extra`, `off`, `len`, and `next`.</p>
| Bytes | Field | Type | Contents |
|-------|---------|------------|-------------------------------------------------------------------------------------------------------------------------------|
| 0 | `kind` | `uint8_t` | the type, numbered as [`value_t`](../api/basic_json/value_t.md): 0 null, 1 object, 2 array, 3 string, 4 boolean, 5 signed integer, 6 unsigned integer, 7 float; 10 for a link (see below). There is no node for a discarded value: a [discarded](../api/basic_json_view/is_discarded.md) view has none |
@@ -201,6 +205,8 @@ packet-beta
| 8-11 | `len` | `uint32_t` | strings: the length after decoding; floats and literals: the length of the token; arrays and objects: the number of elements |
| 12-15 | `next` | `uint32_t` | arrays and objects: the number of nodes of the subtree, including the node itself |
<p class="nj-table-caption" markdown>Fields of a node, by their byte offset within the 16-byte node. Integers use bytes 8-15 differently, see below.</p>
- **Integers** keep their converted value in bytes 8-15 instead of `len` and `next`; the length of their token follows
from the number of digits in `extra` (and the sign). Non-negative integers are unsigned integers, as with
[`parse`](../api/basic_json/parse.md).
@@ -234,6 +240,8 @@ flowchart LR
n2 -. next .-> e
```
<p class="nj-caption" markdown>The five nodes of `#!json {"a": [1, 2.5]}` in document order. Solid arrows lead from each node to the one after it; dotted arrows show where `next` leads from the object and the array, past their subtrees.</p>
| Node | `kind` | `extra` | `off` | `len` | `next` |
|------|----------------------|---------|-------|-------|--------|
| 0 | 1 (object) | 0 | 0 | 1 | 5 |
@@ -242,6 +250,8 @@ flowchart LR
| 3 | 6 (unsigned integer) | 0x0001 | 7 | - | - |
| 4 | 7 (float) | 0x0101 | 10 | 3 | 0 |
<p class="nj-table-caption" markdown>Fields of the five nodes of `#!json {"a": [1, 2.5]}`. `off` is the byte offset in the text, and - marks the bytes 8-15 that hold the integer's value instead of `len` and `next`.</p>
All `flags` are 0. The integer's bytes 8-15 hold its value, 1; its `extra` says it has one digit. The float's `extra`
says it has one integer and one fraction digit, and its `len` is that of the token `2.5`.
@@ -287,8 +297,9 @@ template<class T>
std::size_t get_elements(T* dest, std::size_t count = 1);
```
The lexer detects two optional extensions at compile time. Only `iterator_input_adapter` provides them, and only for
random-access input of single-byte characters:
The lexer detects two optional extensions at compile time. `iterator_input_adapter` provides both, and only for
random-access input of single-byte characters. `input_stream_adapter` provides the second one (as a window onto the
streambuf's buffer) unless `JSON_DISABLE_STREAM_BULK_SCAN` is defined:
- `supports_seek`, `get_consumed_count()`, and `copy_consumed_range()` let the lexer reconstruct already consumed input
for error messages instead of copying every character it reads. With `get_read_count()` and
@@ -306,6 +317,40 @@ The function `input_adapter` picks the right adapter for the argument passed to
- `input_stream_adapter` reads from a `std::istream`.
- `file_input_adapter` reads from a `std::FILE*`.
The adapters have no common base class: the lexer and the parser are templates on the adapter type, and
`wide_string_input_adapter` wraps an `iterator_input_adapter`:
```mermaid
classDiagram
class input_adapter ["input adapter"] {
<<concept>>
+char_type
+get_character() int_type
+get_elements(T* dest, size_t count) size_t
}
class iterator_input_adapter {
-IteratorType current
-SentinelType end
}
class wide_string_input_adapter {
-BaseInputAdapter base_adapter
}
class input_stream_adapter {
-std::istream* is
-std::streambuf* sb
}
class file_input_adapter {
-std::FILE* m_file
}
input_adapter <|.. iterator_input_adapter
input_adapter <|.. wide_string_input_adapter
input_adapter <|.. input_stream_adapter
input_adapter <|.. file_input_adapter
wide_string_input_adapter o-- iterator_input_adapter : converts to UTF-8
```
<p class="nj-caption" markdown>The input adapters as a class diagram. The dotted arrows mean that each adapter implements the input adapter concept; the diamond shows that `wide_string_input_adapter` holds an `iterator_input_adapter`.</p>
## SAX interface
The parser does not build values itself. It reports what it reads as events to a [SAX](../features/parsing/sax_interface.md)
@@ -321,6 +366,7 @@ The library comes with two consumers in `detail/input/json_sax.hpp`:
The `binary_reader` emits the same events for binary formats, so [`sax_parse`](../api/basic_json/sax_parse.md) works
with a user-defined consumer for JSON and for all binary formats alike.
The [SAX event stepper](../features/parsing/sax_interface.md#sax-event-stepper) shows the events for a small input.
## Output adapters
@@ -339,6 +385,36 @@ The `serializer` (used by [`dump`](../api/basic_json/dump.md) and [`operator<<`]
- `output_stream_adapter` writes to a `std::ostream`.
- `output_string_adapter` appends to a string.
They implement the abstract class `output_adapter_protocol`. The class `output_adapter` creates the one that matches
its argument and holds it in a `std::shared_ptr` (`output_adapter_t`):
```mermaid
classDiagram
class output_adapter_protocol~CharType~ {
<<interface>>
+write_character(CharType c)*
+write_characters(const CharType* s, size_t length)*
}
class output_vector_adapter {
-std::vector& v
}
class output_stream_adapter {
-std::basic_ostream& stream
}
class output_string_adapter {
-StringType& str
}
class output_adapter {
-output_adapter_t oa
}
output_adapter_protocol <|-- output_vector_adapter
output_adapter_protocol <|-- output_stream_adapter
output_adapter_protocol <|-- output_string_adapter
output_adapter o-- output_adapter_protocol : shared_ptr
```
<p class="nj-caption" markdown>The output adapters as a class diagram. The three adapters derive from `output_adapter_protocol` (solid arrows), and `output_adapter` holds one of them through a `shared_ptr` (diamond).</p>
The `binary_writer` does not call an adapter for every byte. The `to_*` functions that return a `std::vector` write
into the vector through a sink (`output_vector_sink`) that is not virtual. The functions that take an output adapter
write through a sink (`output_adapter_sink`) that collects the output in a buffer of one kibibyte and passes it to the
@@ -377,3 +453,14 @@ release. Besides the components above, it contains:
- type traits to detect the capabilities of user-defined types (`detail/meta/type_traits.hpp`),
- backports of C++14/17 features to C++11 (`detail/meta/cpp_future.hpp`), and
- helpers such as `string_concat` and `string_escape`.
## See also
- [`basic_json`](../api/basic_json/index.md) - the main class and all its member functions
- [`adl_serializer`](../api/adl_serializer/index.md) - customization point for type conversions
- [`json_sax`](../api/json_sax/index.md) - interface of the SAX parser events
- [SAX Interface](../features/parsing/sax_interface.md) - how to consume parser events without building a DOM
- [Zero-copy JSON views](../features/json_view.md) - the flat node index that backs views and documents
- [Template Parameter Requirements](../features/types/template_parameters.md) - the template parameters of basic_json
- [Serialization](../features/serialization.md) - the output side of the library
- [`nlohmann` Namespace](../features/namespace.md) - how the library namespace is structured