Skip to content

Treat JSON views as dicts (0370) - #752

Draft
rmorozov wants to merge 4 commits into
masterfrom
claude/project-thread-88a3h9
Draft

rmorozov wants to merge 4 commits into
masterfrom
claude/project-thread-88a3h9

Conversation

@rmorozov

@rmorozov rmorozov commented Nov 3, 2026

Copy link
Copy Markdown
Member

Requested by Ruslan · project thread

Parity batch 3, group E. Closes 0370.

Problem

A JSON object passed with jinja2::ToValue is a dict to the template. Passed with jinja2::Reflect, it became a GenericMapAdapter with the struct policy (KeysFirst, HasAttributes() == true), so the same document rendered differently depending on how the host passed it. Examples: d.items with a key items, attr('name'), map('attr', ...), is sequence. The ParityReflect pass (0322) listed six such lines.

Change

  • include/jinja2cpp/json_adapter.h adds an exported marker, jinja2::IJsonNode. It is empty for now. json::ObjectAccessor and json::ArrayAccessor derive from it. This is the marker from the approved 0049 plan (D7 option c). Public header change for G1: it is additive and changes no existing vtable.
  • GenericMapAdapter asks IsJsonNode() once, when it is made. For a JSON object it then answers MethodsFirst and HasAttributes() == false. Any other host map, such as a reflected struct, keeps keys-first lookup.
  • IsJsonNode() caches its answer by the accessor's most-derived type, in two relaxed atomic slots (one for the last JSON type, one for the last other type). Without the cache, the cross-cast on every lent map item cost up to +19% on reflect renders (verifier round 1).
  • Everything that asks those two questions now follows for JSON views. That includes tuple unpacking: {% set x, y = j %} unpacks the keys, which was the third bullet of 0212.

Tests

  • The six reflect.* lines naming 0370 are deleted from test/parity/divergences/reflect.txt.
  • New DictMethods group in test/binding/conformance.h. It runs on every binding, in all four modes, narrow and wide. Its rows are shadowed_method, attr_filter, tests and unpack_keys, with expectations pasted from conformance_expect.py --cpp under Python 3.12 and Jinja2 3.1.6.
  • New ExpressionTest.ReflectedStructFieldComesBeforeADictMethod: a reflected struct keeps its fields before dict methods.

Validation

  • Debug build: full ctest 6/6. ParityReflect/* plus *JsonConformance* with JINJA2CPP_PARITY_CONTEXT=reflect: 3115/3115.
  • The verifier also ran:
    • Clang address+undefined (RelWithDebInfo): ctest 6/6, reflect pass 3115/3115;
    • a shared-library build (GCC): ctest 6/6; the cast across the .so works;
    • a 76-template driver against Python. No regression; 17 templates became exact Python matches. Host data was never written, and mutating methods still go through the copy-on-write cell.
  • scripts/preflight.sh --perf: all PASS, including the instruction gate on the default data.
  • bench/count.py --data reflect, head against master: worst case Render/for_filter_if +0.45%. Everything else is at or below +0.38%. Allocation counts are unchanged; bytes grow slightly (one bool per adapter).
  • Not run here: the MSVC and Apple Clang builds. The dynamic_cast goes to an exported empty struct, the same pattern as detail::OwnedAccessor. CI checks these platforms.
  • Not run: the boost_json binding (the CI bindings job runs the conformance suite on it).

Open

  • Decision D6 of the 0212 plan (/mnt/project-files/release-2.0/0212-plan.md): should IJsonNode get the NodeIdentity() virtual before this merges, so the marker never ships empty? This waits for Ruslan.
  • A JSON module export now takes the dict path in template_module.cpp (CopyMap) instead of keeping the view. The verifier flagged it for a reviewer's look; the same size caps apply.

Roles: architect (0212 plan), verifier, 1 round. Round 1 had two blocking findings, both fixed: the instruction cost of the cast and an include-cleaner hit. Red CI pushes: none yet. Codex: pending.


Generated by Claude Code

A JSON object viewed through jinja2::Reflect now looks names up as the dict
Python's json.loads gives: dict methods before keys, attr() does not see the
keys, it is a sequence, and tuple unpacking takes its keys. The JSON adapter's
accessors derive from a new exported marker, jinja2::IJsonNode, which
GenericMapAdapter detects once when it is made; reflected structs keep
keys-first lookup.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AWNL7mCuJcu8xmBFskqHPP
The cross-cast on every lent map item cost up to 19% of a reflect render;
a two-slot cache keyed by the most-derived type answers with one compare.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AWNL7mCuJcu8xmBFskqHPP
@rmorozov rmorozov self-assigned this Nov 3, 2026

rmorozov commented Nov 3, 2026

Copy link
Copy Markdown
Member Author

@codex review


Generated by Claude Code

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-03T15:23:38.738215Z c6ead2a Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c6ead2a26c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/internal_value.cpp Resolved
…0370)

A JSON view now exports through ExportCopier::CopyMap, which read out every
entry without checking maxSequenceSize.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AWNL7mCuJcu8xmBFskqHPP
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.

2 participants