I want to parse (json.loads) a json string that contains datetime values sent from a http client.
I know that I can write a custom json encoder by extending the default encoder and overriding the default method
class MyJSONEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, (datetime.datetime,)):
return obj.isoformat()
elif isinstance(obj, (decimal.Decimal,)):
return str(obj)
else:
return json.JSONEncoder.default(self, obj)
My questions are -
- How do I customize the default json decoder? Do I need to override the decode method? Can I in some way, override/add a callback function for every field/value in the json string? (I have seen the code in json.decoder.JSONDecoder and json.scanner but am not sure what to do)
- Is there an easy way to identify a specific value as a datetime string? The date values are strings in ISO format.
Thanks,