Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cant use standard json.dump #19

Open
lexapi opened this issue Apr 11, 2018 · 3 comments
Open

Cant use standard json.dump #19

lexapi opened this issue Apr 11, 2018 · 3 comments

Comments

@lexapi
Copy link

lexapi commented Apr 11, 2018

Example:

a = {
    "dict": {
        "nested_list": [
            {
                "@dict_list_dict_f1": 1,
            },
            {
                "@dict_list_dict_f1": 2,
            },
        ]
    }
}

b = {
    "dict": {
        "nested_list": [
            {
                "@dict_list_dict_f1": 1,
            },
        ]
    }
}

from jsondiff import diff
d=diff(a, b, syntax='symmetric')

print(diff)
{'dict': {'nested_list': {delete: [(1, {'@dict_list_dict_f1': 2})]}}}

print(json.dumps(diff))

Traceback (most recent call last):
    print(json.dumps(diff))
  File "/usr/lib/python3.5/json/__init__.py", line 230, in dumps
    return _default_encoder.encode(obj)
  File "/usr/lib/python3.5/json/encoder.py", line 198, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/python3.5/json/encoder.py", line 256, in iterencode
    return _iterencode(o, 0)
TypeError: keys must be a string


print(diff(a, b,  syntax='symmetric', dump=True))
{"dict": {"nested_list": {"$delete": [[1, {"@dict_list_dict_f1": 2}]]}}}
# internal dump convert nested tuple to list ???
@fzumstein fzumstein changed the title Cant use standart json.dump Cant use standard json.dump May 31, 2018
@fedorov
Copy link

fedorov commented Oct 16, 2018

I think I had a similar issue. I fixed it by passing marshal=True to diff.

@larsks
Copy link

larsks commented Apr 2, 2019

I came here to report the same problem; I wonder if marshal=True should be the default behavior? It seems surprising that the output of jsondiff.diff is not itself JSON serializable.

@richard-jones
Copy link

I also came here for the same reason. I ended up implementing a hacky parser which switches the Symbols for strings, wish I'd know about marshal=True.

The problem with having the insert and delete keywords as strings is that you can't tell them apart from a data structure with insert and delete as legitimate keys, so I can see the reasoning for making them symbols.

Perhaps a better longer term solution would be to adopt the output semantics of JSONPatch, which is completely unambigous, and is native JSON: http://jsonpatch.com/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants