Skip to content

docs: document how discarded values are removed by the parser callback - #5354

Open
nlohmann wants to merge 1 commit into
developfrom
claude/docs-callback-discard-5342
Open

docs: document how discarded values are removed by the parser callback#5354
nlohmann wants to merge 1 commit into
developfrom
claude/docs-callback-discard-5342

Conversation

@nlohmann

@nlohmann nlohmann commented Aug 3, 2026

Copy link
Copy Markdown
Owner

Follow-up to #5342, which left the documentation checkbox unchecked.

That PR fixed json::parse with a callback leaving a discarded member behind when an array — or a scalar — under an object key was rejected, so that dump() printed things like {"IDs":<discarded>,"Width":800}. The behavior is now correct, but parser_callback_t only said that "discarded values in structured types are skipped", which was equally compatible with the buggy behavior. Nothing documented that an object parent is covered too, that the key is removed along with the value, or that discarding an array at array_start and at array_end both work.

This PR:

  • spells out the discarding rules: a discarded element is removed from a surrounding array and a discarded member from a surrounding object together with its key; containers can be discarded at either their start or their end event, and discarding at the start event suppresses the callbacks for the content and for the matching end event; discarding a key event drops the whole member while the value callback still runs.
  • adds examples/parser_callback_t.cpp, which discards the same array at array_start and at array_end, a number with and without its key, and the top-level object. Compiled against the parent of remove discarded array from parent object in end_array #5342, three of its five output lines are invalid JSON containing <discarded>; on develop all five are correct — so the example is a regression demonstration, not just prose.
  • corrects the return value section, which claimed a discarded value is "replaced by an empty discarded object". A discarded top-level value is replaced by null (parser.hpp), and no discarded value survives anywhere else.
  • notes the fix in the version history for 3.13.0.

Every documented claim was verified against the current implementation by instrumenting the callback and printing the event sequence, rather than read off the source.

Breaking changes

No breaking changes. This PR touches only docs/: one Markdown page and two new example files. No header, no test, and no build file is modified, so there is no change to the public API, the ABI, or any behavior.

Checked locally: check_structure.py reports no warnings, and make mkdocs/docs/examples/parser_callback_t.test passes (generated output matches the committed .output).

This pull request was written by Claude Code.

Follow-up to #5342, which fixed the parser callback leaving a discarded
member behind when an array or a value under an object key was rejected.
The documentation of parser_callback_t only stated that discarded values
in structured types are skipped, without saying that this covers object
parents and that the key is removed along with the value, so there was no
way to tell the fixed behavior from the buggy one.

Spell out the discarding rules, add an example that exercises the cases
the fix repaired, and correct the return value description: a discarded
top-level value is replaced by null, not by "an empty discarded object".

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
@nlohmann nlohmann added this to the Release 3.13.0 milestone Aug 3, 2026
@nlohmann nlohmann added the review needed It would be great if someone could review the proposed changes. label Aug 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation M review needed It would be great if someone could review the proposed changes.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant