* Review and extend the documentation, and check it in CI A review of all documentation pages found factual errors, dead links, missing cross-references, and gaps in examples. This fixes them and adds checks so the same problems are caught automatically. Fixes: - wrong signatures and version histories (operator!= C++20 member, binary() subtype type, get<PointerType>(), JSON_NO_THREAD_LOCAL, ...) - stale descriptions (number parsing since #5283, UBJSON table, SAX example that no longer compiled, tsl::ordered_map advice) - dead internal and external links; repology.org badges (the domain is suspended) replaced by badges that query the registries directly - deprecation notes link the migration guide; the guide itself fixed Additions: - "See also" sections, cross-references, 25 runnable examples, 12 Mermaid diagrams, new API pages for json_pointer::operator<=> and byte_container_with_subtype::operator==/!= - landing page, guides for untrusted input and performance - "unreleased" badge after versions newer than the latest release Checks: - strict documentation build (broken links/anchors fail it); CI and the publish workflow fetch the full history the build needs - weekly external link check, Mermaid syntax check in CI - check_structure.py: example titles, heading levels, alt texts, header links, docset index coverage; its unused-example check works again - all examples produce the same output on every platform Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Keep the customer links that could not be fixed A dead link on the customers page is still the evidence of where the use of the library was documented. Keep the original URLs of the entries without a working replacement (Marne, Cisco Webex Desk Camera, Philips Hue, CyberArk) and exclude exactly these URLs from the link check. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Correct the duplicate-key recipe's claim about SAX positions The SAX interface's key() receives no position either; only parse_error() does. Also note that the recipe does not report the path to the repeated key (see discussion #5085). Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Say the library is available as a single header and mention json_fwd.hpp Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Correct documentation errors found while hunting for bugs - patch/patch_inplace: list the JSON pointer errors parse_error.106-109 and out_of_range.402/404, and quote the actual parse_error.105 message. - unflatten: list parse_error.106/107/108 and out_of_range.404. - to_bson: list out_of_range.415 (binary subtype above 255) and note that 412 and 415 are new in 3.13.0. - to_string: state that string_t must be convertible to std::string, also in the StringType requirements table. - JSON Lines: a `while (input >> j)` loop also throws after the last value for concatenated JSON values; show a loop that works for both. - BON8: a string gets 0xFF only if nothing follows it in the message; a string at the end of an array or object is ended by 0xFE. - custom_string_type.hpp: add operator+=(char), which the "Always required" list asks for (json_pointer::to_string, flatten, unflatten, and diff did not compile), and an ADL int_to_string for diff and items. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Cache the release headers with functools.lru_cache Codacy (Pylint) flagged the mutable default argument that header() used as its cache. functools.lru_cache keeps the same memoization without it. The script's output is unchanged. Signed-off-by: Niels Lohmann <mail@nlohmann.me> --------- Signed-off-by: Niels Lohmann <mail@nlohmann.me>
5.4 KiB
nlohmann::basic_json::parser_callback_t
template<typename BasicJsonType>
using parser_callback_t =
std::function<bool(int depth, parse_event_t event, BasicJsonType& parsed)>;
With a parser callback function, the result of parsing a JSON text can be influenced. When passed to
parse, it is called on certain events (passed as parse_event_t via parameter
event) with a set recursion depth depth and context JSON value parsed. The return value of the callback function
is a boolean indicating whether the element that emitted the callback shall be kept or not.
We distinguish six scenarios (determined by the event type) in which the callback function can be called. The following
table describes the values of the parameters depth, event, and parsed.
parameter event |
description | parameter depth |
parameter parsed |
|---|---|---|---|
parse_event_t::object_start |
the parser read { and started to process a JSON object |
depth of the parent of the JSON object | a JSON value with type discarded |
parse_event_t::key |
the parser read a key of a value in an object | depth of the currently parsed JSON object | a JSON string containing the key |
parse_event_t::object_end |
the parser read } and finished processing a JSON object |
depth of the parent of the JSON object | the parsed JSON object |
parse_event_t::array_start |
the parser read [ and started to process a JSON array |
depth of the parent of the JSON array | a JSON value with type discarded |
parse_event_t::array_end |
the parser read ] and finished processing a JSON array |
depth of the parent of the JSON array | the parsed JSON array |
parse_event_t::value |
the parser finished reading a JSON value | depth of the value | the parsed JSON value |
Discarding a value (i.e., returning #!cpp false) has different effects depending on the context in which function was
called:
- Discarded values in structured types are skipped. That is, the parser will behave as if the discarded value was never read. This holds for every value type and for both kinds of parent: a discarded element is removed from the surrounding array, and a discarded member is removed from the surrounding object together with its key.
- Arrays and objects can be discarded either at their
parse_event_t::array_start/parse_event_t::object_startevent or at theirparse_event_t::array_end/parse_event_t::object_endevent, and both remove the whole value. Discarding it at the start event also means the callback is called neither for the content of the value nor for its matching end event. - Discarding a
parse_event_t::keyevent discards the whole object member. The callback is still called for the associated value, but its return value has no further effect. - In case a value outside a structured type is skipped, it is replaced with
null. This case happens if the top-level element is skipped.
Parameters
depth(in)- the depth of the recursion during parsing
event(in)- an event of type
parse_event_tindicating the context in the callback function has been called parsed(in, out)- the current intermediate parse result; note that
writing to this value has no effect for
parse_event_t::keyevents
Return value
Whether the JSON value which called the function during parsing should be kept (#!cpp true) or not (#!cpp false). In
the latter case, it is skipped completely, or replaced by null if it is the top-level value.
Examples
??? example "Example: skip an object key while parsing"
The example below demonstrates the `parse()` function with
and without callback function.
```cpp
--8<-- "examples/parse__string__parser_callback_t.cpp"
```
Output:
```json
--8<-- "examples/parse__string__parser_callback_t.output"
```
??? example "Example: how discarded values are removed"
The example below shows where discarded values are removed. The array and the number are discarded in different
ways, but in each case the parse result contains neither the value nor its key.
```cpp
--8<-- "examples/parser_callback_t.cpp"
```
Output:
```json
--8<-- "examples/parser_callback_t.output"
```
See also
- parse deserialize from a compatible input
- parse_event_t enumeration of parser events
Version history
- Added in version 1.0.0.
- Fixed in version 3.13.0 to also remove discarded values from a parent object; before, discarding an array or a value stored under an object key left a discarded member behind, which made the parse result serialize to invalid JSON.
- Fixed in version 3.13.0 so that discarding an array or object at its start event also hides its content from the callback, as documented above; before, the callback was still called for the content, and the key of every member of a discarded object was kept in memory until the parse ended.
