Implement RFC 6901-style tilde escaping for special characters in keys#10
Open
Implement RFC 6901-style tilde escaping for special characters in keys#10
Conversation
Keys containing `.`, `$`, `[`, or `~` previously caused crashes
(ValueError, AttributeError) or silent data corruption on round-trip
through flatten/unflatten. This adds escape sequences (~0 for ~, ~1
for ., ~2 for $, ~3 for [) so all valid JSON keys round-trip correctly.
Also fixes: rsplit("$", 2) -> rsplit("$", 1), _int_key_re.match ->
fullmatch, and guards against AttributeError when root becomes list.
https://claude.ai/code/session_01SwKhkAmPwW1qrMHpJmYT3x
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR implements proper escaping of special characters in dictionary keys using RFC 6901-style tilde sequences. This allows keys containing
.,$,[, and~characters to round-trip correctly throughflatten()andunflatten()operations.Key Changes
Added escaping functions: Implemented
_escape_key()and_unescape_key()functions that handle tilde-based escaping:~0→~(tilde)~1→.(dot)~2→$(dollar sign)~3→[(opening bracket)Updated flatten logic: Modified
_object_to_rows()to escape dictionary keys before building the flattened path, preventing special characters from being misinterpreted as format delimiters.Enhanced unflatten logic: Implemented a three-pass approach:
[N]pattern matching on escaped keysFixed regex patterns:
_int_key_reto usefullmatch()instead ofmatch()and added$anchor to prevent false matches on keys like[0]suffixrsplit()call to use maxsplit=1 instead of 2 for correct type suffix extractionUpdated test case: Changed the existing dollar sign test case from
_$!<home>!$_to use proper escaping, demonstrating the new behavior.Added comprehensive test suite: 150+ lines of new tests covering:
Updated documentation: Added RFC 6901 reference and escaping table to README with examples.
Implementation Details
The escaping order is critical:
~must be escaped first to avoid double-escaping. Similarly, unescaping must reverse this order, processing~3before~0to prevent premature decoding of escape sequences.The three-pass unflatten approach ensures that array index detection (
[N]patterns) works correctly on escaped keys, so literal bracket keys like[0](escaped as~30]) are not confused with actual array indices.https://claude.ai/code/session_01SwKhkAmPwW1qrMHpJmYT3x