mirror of
https://github.com/YunoHost/moulinette.git
synced 2024-09-03 20:06:31 +02:00
40 lines
1.2 KiB
Python
40 lines
1.2 KiB
Python
import logging
|
|
from json.encoder import JSONEncoder
|
|
import datetime
|
|
import pytz
|
|
|
|
logger = logging.getLogger('moulinette.utils.serialize')
|
|
|
|
|
|
# JSON utilities -------------------------------------------------------
|
|
|
|
class JSONExtendedEncoder(JSONEncoder):
|
|
|
|
"""Extended JSON encoder
|
|
|
|
Extend default JSON encoder to recognize more types and classes. It will
|
|
never raise an exception if the object can't be encoded and return its repr
|
|
instead.
|
|
|
|
The following objects and types are supported:
|
|
- set: converted into list
|
|
|
|
"""
|
|
|
|
def default(self, o):
|
|
"""Return a serializable object"""
|
|
# Convert compatible containers into list
|
|
if isinstance(o, set) or (
|
|
hasattr(o, '__iter__') and hasattr(o, 'next')):
|
|
return list(o)
|
|
|
|
# Display the date in its iso format ISO-8601 Internet Profile (RFC 3339)
|
|
if isinstance(o, datetime.date):
|
|
if o.tzinfo is None:
|
|
o = o.replace(tzinfo=pytz.utc)
|
|
return o.isoformat()
|
|
|
|
# Return the repr for object that json can't encode
|
|
logger.warning('cannot properly encode in JSON the object %s, '
|
|
'returned repr is: %r', type(o), o)
|
|
return repr(o)
|