################################################################################ # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: MIT-0 ################################################################################ import json import datetime class PythonObjectEncoder(json.JSONEncoder): """Custom JSON Encoder that allows encoding of un-serializable objects For object types which the json module cannot natively serialize. If its a date, then return isoformat(), if the object type has a __repr__ method, serialize that string instead. Usage: >>> example_unserializable_object = {'example': set([1,2,3])} >>> print(json.dumps(example_unserializable_object, cls=PythonObjectEncoder)) {"example": "set([1, 2, 3])"} """ def default(self, obj): # pylint: disable=E0202 if isinstance(obj, (list, dict, str, int, float, bool, type(None))): return json.JSONEncoder.default(self, obj) elif isinstance(obj, datetime.datetime): return obj.isoformat() elif hasattr(obj, "__repr__"): return obj.__repr__() else: return json.JSONEncoder.default(self, obj.__repr__())