Skip to content

Expose the current JSON Pointer in Decoder callbacks - #260

Open
Aschenbath wants to merge 1 commit into
python-rapidjson:masterfrom
Aschenbath:feat/decoder-json-pointer-134
Open

Aschenbath wants to merge 1 commit into
python-rapidjson:masterfrom
Aschenbath:feat/decoder-json-pointer-134

Conversation

@Aschenbath

Copy link
Copy Markdown

Decoder callbacks currently cannot identify the object key or array position they are processing. Add a read-only Decoder.pointer property following the JSON Pointer approach suggested in #134. For example, string() sees /items/0/name while processing the first item's name; the document root is "", and the property is None outside callbacks.

Paths use RFC 6901 escaping and describe the input structure before callback replacements. They are constructed on demand from the existing parser context stack. Callback scopes are local to the calling thread and restore the enclosing scope after recursive calls, including failures. Tests cover all four callbacks, nested arrays/objects, duplicate and empty keys, Unicode and NUL keys, text/binary input and small stream chunks, recursive failures, concurrent use of one decoder, and the read-only contract.

The issue's proposed use of a discarding custom mapping also exposed an existing container lifetime bug: Handle() can release a newly created container before the parser takes its own reference. Retain the context reference before handing the value to its parent. A separate regression covers discarded nested objects/arrays and their eventual release; a minimal reproducer without the new property exits with SIGSEGV against a separately compiled baseline and succeeds with the patch.

Validation on Linux CPython 3.12.12 with GCC 15.2 and the native extension rebuilt:

  • All 29 pointer cases fail on the unchanged baseline; the final full suite passes 1,478 tests, with 31 skips and 2 expected failures.
  • All 276 Sphinx doctests pass. The repository's pinned Sphinx 9.2.0 was unavailable from the package index, so these ran with 9.1.1; the other test requirements use their specified versions. The existing missing _static warning remains.
  • A wheel installed outside the checkout passes mypy.stubtest and a Unicode streaming consumer that checks the loaded extension's location.
  • git diff --check passes.

A limited five-round alternating benchmark measured patched/baseline median ratios of 1.005 for nested loads, 1.010 for a plain Decoder, and 1.043 for a Decoder with string/end_object callbacks that do not read the new property. Flat numeric-array loads measured 0.927; this is not a speedup claim. Debug/free-threaded builds, sanitizers, other Python versions/platforms, and hosted CI have not been verified locally.

AI assistance was used for investigation, implementation, tests, and this description. A separate Codex agent reviewed all six changed files and independently checked 300 random documents against an RFC 6901 event oracle, every callback's recursive success/failure restoration, different-decoder nesting, shared-decoder threading, and discarded-container lifetime. No blocking issue was found. This is agent review; no human review is claimed.

Closes #134.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Any way to know the current object being parsed?

1 participant