Skip to content

WIP: preserve duplicate-key semantics in parse - #10

Draft
uvlad7 wants to merge 5 commits into
masterfrom
fixes/duplicate-keys
Draft

uvlad7 wants to merge 5 commits into
masterfrom
fixes/duplicate-keys

Conversation

@uvlad7

@uvlad7 uvlad7 commented Sep 10, 2026

Copy link
Copy Markdown
Owner

What this changes

  • JsonScanner.scan continues to return every matching occurrence. It cannot choose one duplicate-key value without changing its low-level matching semantics.
  • JsonScanner.parse now follows JSON.parse object behavior: for repeated keys, the last source occurrence wins.
  • Result assembly walks matches in reverse source order and inserts only a destination that has not already been populated. This preserves the last occurrence while avoiding parsing values that an overlapping matcher or an earlier duplicate would discard.
  • Overlapping selectors, including a specific key selector together with ANY_KEY, therefore parse the same destination only once.
  • Custom parsing is a block on JsonScanner.parse. The block receives the selected JSON fragment plus quirks_mode and symbolize_names; this permits callers to opt into parser-specific behavior such as allow_duplicate_key: true.

Documentation and tests

  • Documents duplicate-key behavior, including the need to inspect all scan matches or use .last.
  • Documents the custom parsing block with an allow_duplicate_key example.
  • Covers duplicate keys, overlapping selectors, lazy single parsing, and custom parser behavior.

Verification

  • Ruby 4.1: 59 examples, 0 failures.
  • Ruby 2.3: 58 examples, 0 failures, 3 expected pending.
  • RuboCop: clean.

This is deliberately a draft while the implementation and API are reviewed.

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.

1 participant