cozyweboob/tools/jsonwriter.py

28 lines
768 B
Python
Raw Normal View History

2016-09-28 21:38:55 +02:00
import json
from datetime import date, datetime
2016-09-28 21:38:55 +02:00
from decimal import Decimal
class CustomJSONEncoder(json.JSONEncoder):
"""
Custom JSONEncoder to support more types.
"""
def default(self, o):
if isinstance(o, datetime) or isinstance(o, date):
2016-09-28 21:38:55 +02:00
# Serialize datetime objects to ISO dates
return o.isoformat()
elif isinstance(o, Decimal):
# Serialize Decimal objects to string
return str(o)
return json.JSONEncoder.default(self, o)
def pretty_json(foo):
"""
Pretty printing of JSON output, using the custom JSONEncoder.
"""
return json.dumps(foo, sort_keys=True,
indent=4, separators=(',', ': '),
cls=CustomJSONEncoder)