ion-python icon indicating copy to clipboard operation
ion-python copied to clipboard

IonToJSONEncoder does not correctly handle native Python types

Open tgregg opened this issue 3 years ago • 0 comments

As reported by a user, the following fails:

import json
from amazon.ion.json_encoder import IonToJSONEncoder
from amazon.ion import simpleion

ion_str = u'{hello: "world"}'
ion_obj = simpleion.loads(ion_str)
ion_obj['foo'] = 'bar' # Here's the problem

json_str = json.dumps(ion_obj, cls=IonToJSONEncoder)
print(json_str)

The error is “Object of type str is not JSON serializable”. thrown during dumps by Python's built-in JSON encoder here: https://github.com/python/cpython/blob/main/Lib/json/encoder.py#L179

This can probably be fixed by adding a check for dict in IonToJSONEncoder.default, but we need to investigate whether that's the right solution.

tgregg avatar Aug 10 '21 21:08 tgregg