Repository navigation
fix: preserve native Unicode in JSON Pointers - #140
Open
mayankdhingra02 wants to merge 1 commit into
Open
mayankdhingra02 wants to merge 1 commit into
mayankdhingra02 wants to merge 1 commit into
Conversation
Keep literal Unicode characters out of the byte-oriented escape codec. Decode existing ASCII escape spellings and UTF-16 surrogate units in separate spans so native object keys retain their code points. Share the decoder with from_parts and cover pointer builders, URI handling and JSON Patch operations that otherwise edit a different key. Signed-off-by: Mayank Dhingra <mayankdhingra02@gmail.com>
Owner
|
Hi @mayankdhingra02, I agree that the But I don't think your solution matches the intended behaviour (I have not managed to communicate that well), and there's a lot of noise in These tests should go some way to demonstrating the intended behaviour. @pytest.mark.parametrize(
("pointer", "data", "want", "want_unescaped"),
[
("/café", {"café": 7}, 7, 7),
("/漢字", {"漢字": 7}, 7, 7),
("/😀", {"😀": 7}, 7, 7),
("/e\N{COMBINING ACUTE ACCENT}", {"é": 7}, 7, 7),
("/café~1part~0name", {"café/part~name": 7}, 7, 7),
("/café%2F", {"café%2F": 7}, 7, 7),
('/café"quoted', {'café"quoted': 7}, 7, 7),
("/café\n\t\0", {"café\n\t\0": 7}, 7, 7),
("/café\\😀", {"café\\😀": 7}, 7, 7),
("/café\\\\😀", {"café\\\\😀": 7}, 7, 7),
("/\\uD83D\\uDE00", {"😀": 7, "\\uD83D\\uDE00": 42}, 42, 7),
],
)
def test_unicode_pointer_tokens(
pointer: str,
data: dict[str, object],
want: object,
want_unescaped: object,
) -> None:
p = JSONPointer(pointer, unicode_escape=False)
assert p.resolve(data) == want
assert str(p) == pointer
p_unescaped = JSONPointer(pointer, unicode_escape=True)
assert p_unescaped.resolve(data) == want_unescaped
assert str(p_unescaped) == _unescape(pointer)Where python-jsonpath/jsonpath/unescape.py Line 10 in bd91a15 |
This branch has not been deployed
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.
Native Unicode in a JSON Pointer is currently converted into UTF-8 bytes
and then read as Latin-1 by the escape codec. For example,
/caféresolvesas
/café. If both keys exist, a JSON Patch operation can silently changethe wrong member.
Preserve native characters while decoding the existing compatibility
escape spellings separately.
from_partsuses the same decoder. Unicodenormalization, the
unicode_escapeoption, URI percent-decoding andsurrogate validation retain their existing roles.
The regressions cover pointer builders, mixed native/escaped characters,
astral and combining characters, backslashes, tilde and percent handling,
and all JSON Patch operations from Python objects and JSON text. The
wrong-key cases fail on the unchanged base. An additional bounded oracle
checks ASCII compatibility against the old decoder and native keys
against Python's JSON encoder.
Validation on macOS:
existing skip each.
hatch run lint,hatch run typing,hatch run covandhatch run docs:buildpass. The changed executable lines and brancharcs are covered.
hatch buildand strict Twine checks pass.Linux and Windows runs remain for CI.