stefankoegl / python-json-pointer

Resolve JSON Pointers in Python
https://python-json-pointer.readthedocs.org/
Other
140 stars 43 forks source link

jsonpointer will not work with unicode keys in python 2.7 #18

Closed maxsmythe closed 9 years ago

maxsmythe commented 9 years ago

It looks like jsonpointer will not work with unicode keys, both of the following code snippets fail:

data = {u'\xee': u'\xee'}
jsonpatch.make_patch({}, data)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/sendhub/scratch/contacts_v2_scratch/venv/local/lib/python2.7/site-packages/jsonpatch.py", line 171, in make_patch
    return JsonPatch.from_diff(src, dst)
  File "/opt/sendhub/scratch/contacts_v2_scratch/venv/local/lib/python2.7/site-packages/jsonpatch.py", line 322, in from_diff
    return cls(list(compare_values([], src, dst)))
  File "/opt/sendhub/scratch/contacts_v2_scratch/venv/local/lib/python2.7/site-packages/jsonpatch.py", line 293, in compare_values
    for operation in compare_dicts(path, value, other):
  File "/opt/sendhub/scratch/contacts_v2_scratch/venv/local/lib/python2.7/site-packages/jsonpatch.py", line 314, in compare_dicts
    ptr = JsonPointer.from_parts(path + [key])
  File "/opt/sendhub/scratch/contacts_v2_scratch/venv/local/lib/python2.7/site-packages/jsonpointer.py", line 291, in from_parts
    parts = [str(part) for part in parts]
UnicodeEncodeError: 'ascii' codec can't encode character u'\xee' in position 0: ordinal not in range(128)
data2 = {u'\xee'.encode('utf-8'): u'\xee'.encode('utf-8')}
jsonpatch.make_patch({}, data2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/sendhub/scratch/contacts_v2_scratch/venv/local/lib/python2.7/site-packages/jsonpatch.py", line 171, in make_patch
    return JsonPatch.from_diff(src, dst)
  File "/opt/sendhub/scratch/contacts_v2_scratch/venv/local/lib/python2.7/site-packages/jsonpatch.py", line 322, in from_diff
    return cls(list(compare_values([], src, dst)))
  File "/opt/sendhub/scratch/contacts_v2_scratch/venv/local/lib/python2.7/site-packages/jsonpatch.py", line 293, in compare_values
    for operation in compare_dicts(path, value, other):
  File "/opt/sendhub/scratch/contacts_v2_scratch/venv/local/lib/python2.7/site-packages/jsonpatch.py", line 314, in compare_dicts
    ptr = JsonPointer.from_parts(path + [key])
  File "/opt/sendhub/scratch/contacts_v2_scratch/venv/local/lib/python2.7/site-packages/jsonpointer.py", line 292, in from_parts
    parts = [part.replace('~', '~0') for part in parts]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 0: ordinal not in range(128)
stefankoegl commented 9 years ago

Thanks for the report! I've release jsonpointer-1.9 and jsonpatch-1.10 which fix this issue.

maxsmythe commented 9 years ago

Thanks for the quick fix!