django-rest-framework/rest_framework/utils/encoders.py

68 lines
2.5 KiB
Python
Raw Normal View History

"""
Helper classes for parsers.
"""
import datetime
2015-06-25 23:55:51 +03:00
import decimal
import json # noqa
2015-06-25 23:55:51 +03:00
import uuid
from django.db.models.query import QuerySet
from django.utils import timezone
2019-07-30 03:02:43 +03:00
from django.utils.encoding import force_str
2015-06-25 23:55:51 +03:00
from django.utils.functional import Promise
from rest_framework.compat import coreapi
class JSONEncoder(json.JSONEncoder):
"""
2013-01-15 17:02:53 +04:00
JSONEncoder subclass that knows how to encode date/time/timedelta,
2014-09-12 14:50:20 +04:00
decimal types, generators and other basic python objects.
"""
2014-09-12 14:50:20 +04:00
def default(self, obj):
# For Date Time string spec, see ECMA 262
# https://ecma-international.org/ecma-262/5.1/#sec-15.9.1.15
2014-09-12 14:50:20 +04:00
if isinstance(obj, Promise):
2019-07-30 03:02:43 +03:00
return force_str(obj)
2014-09-12 14:50:20 +04:00
elif isinstance(obj, datetime.datetime):
representation = obj.isoformat()
if representation.endswith('+00:00'):
representation = representation[:-6] + 'Z'
return representation
elif isinstance(obj, datetime.date):
return obj.isoformat()
elif isinstance(obj, datetime.time):
if timezone and timezone.is_aware(obj):
raise ValueError("JSON can't represent timezone-aware times.")
2014-09-12 14:50:20 +04:00
representation = obj.isoformat()
return representation
elif isinstance(obj, datetime.timedelta):
return str(obj.total_seconds())
2014-09-12 14:50:20 +04:00
elif isinstance(obj, decimal.Decimal):
# Serializers will coerce decimals to strings by default.
return float(obj)
elif isinstance(obj, uuid.UUID):
return str(obj)
2014-09-12 14:50:20 +04:00
elif isinstance(obj, QuerySet):
return tuple(obj)
elif isinstance(obj, bytes):
# Best-effort for binary blobs. See #4187.
return obj.decode()
2014-09-12 14:50:20 +04:00
elif hasattr(obj, 'tolist'):
# Numpy arrays and array scalars.
return obj.tolist()
elif (coreapi is not None) and isinstance(obj, (coreapi.Document, coreapi.Error)):
raise RuntimeError(
'Cannot return a coreapi object from a JSON view. '
'You should be using a schema renderer instead for this view.'
)
2014-09-12 14:50:20 +04:00
elif hasattr(obj, '__getitem__'):
cls = (list if isinstance(obj, (list, tuple)) else dict)
try:
return cls(obj)
2017-11-26 05:06:13 +03:00
except Exception:
pass
2014-09-12 14:50:20 +04:00
elif hasattr(obj, '__iter__'):
return tuple(item for item in obj)
return super().default(obj)