I use a custom JSONEncoder to alter some values in the dict
I want to dump.
import json
class CustomJSONEncoder(json.JSONEncoder):
def encode(self, obj):
#obj = dict(obj)
obj['__key'] += 1
return super().encode(obj)
data = dict(__key=1)
print('dumps')
print(json.dumps(data, cls=CustomJSONEncoder))
print('dumps', json.dumps(data, cls=CustomJSONEncoder))
json.dump(data, open('test.json', 'w'), cls=CustomJSONEncoder)
print('dump', json.load(open('test.json', 'r')))
Running this gives the expected result:
dumps {"__key": 2}
dump {'__key': 2}
But when I uncomment the commented-out line (which is required for what I ultimately want to do because I don't want to change the original data), dumps
and dump
behave differently:
dumps {"__key": 2}
dump {'__key': 1}
Why does this happen? Is there a workaround for this?