2012-09-20 16:06:27 +04:00
|
|
|
"""
|
|
|
|
Helper classes for parsers.
|
|
|
|
"""
|
2022-10-05 13:02:00 +03:00
|
|
|
|
|
|
|
import contextlib
|
2015-06-18 16:38:29 +03:00
|
|
|
import datetime
|
2015-06-25 23:55:51 +03:00
|
|
|
import decimal
|
2017-07-11 00:22:55 +03:00
|
|
|
import json # noqa
|
2015-06-25 23:55:51 +03:00
|
|
|
import uuid
|
2015-06-18 16:38:29 +03:00
|
|
|
|
|
|
|
from django.db.models.query import QuerySet
|
2019-04-30 18:53:44 +03:00
|
|
|
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
|
2015-06-18 16:38:29 +03:00
|
|
|
|
2017-11-09 11:03:48 +03:00
|
|
|
from rest_framework.compat import coreapi
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
|
|
|
|
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.
|
2012-09-20 16:06:27 +04:00
|
|
|
"""
|
2014-09-12 14:50:20 +04:00
|
|
|
def default(self, obj):
|
2012-09-20 16:06:27 +04:00
|
|
|
# For Date Time string spec, see ECMA 262
|
2018-01-08 18:22:32 +03:00
|
|
|
# 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):
|
2012-09-20 16:06:27 +04:00
|
|
|
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):
|
2019-04-30 18:53:44 +03:00
|
|
|
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)
|
2015-01-19 17:18:02 +03:00
|
|
|
elif isinstance(obj, uuid.UUID):
|
2019-04-30 18:53:44 +03:00
|
|
|
return str(obj)
|
2014-09-12 14:50:20 +04:00
|
|
|
elif isinstance(obj, QuerySet):
|
2014-10-02 17:44:20 +04:00
|
|
|
return tuple(obj)
|
2018-09-17 11:39:59 +03:00
|
|
|
elif isinstance(obj, bytes):
|
2016-06-13 12:41:50 +03:00
|
|
|
# Best-effort for binary blobs. See #4187.
|
2019-05-01 08:49:17 +03:00
|
|
|
return obj.decode()
|
2014-09-12 14:50:20 +04:00
|
|
|
elif hasattr(obj, 'tolist'):
|
|
|
|
# Numpy arrays and array scalars.
|
|
|
|
return obj.tolist()
|
2017-01-07 01:32:13 +03:00
|
|
|
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__'):
|
2019-07-09 13:41:05 +03:00
|
|
|
cls = (list if isinstance(obj, (list, tuple)) else dict)
|
2022-10-05 13:02:00 +03:00
|
|
|
with contextlib.suppress(Exception):
|
2019-07-09 13:41:05 +03:00
|
|
|
return cls(obj)
|
2014-09-12 14:50:20 +04:00
|
|
|
elif hasattr(obj, '__iter__'):
|
2014-10-02 17:44:20 +04:00
|
|
|
return tuple(item for item in obj)
|
2019-04-30 18:53:44 +03:00
|
|
|
return super().default(obj)
|
2023-06-17 06:18:25 +03:00
|
|
|
|
|
|
|
|
|
|
|
class CustomScalar:
|
|
|
|
"""
|
|
|
|
CustomScalar that knows how to encode timedelta that renderer
|
|
|
|
can understand.
|
|
|
|
"""
|
|
|
|
@classmethod
|
|
|
|
def represent_timedelta(cls, dumper, data):
|
|
|
|
value = str(data.total_seconds())
|
|
|
|
return dumper.represent_scalar('tag:yaml.org,2002:str', value)
|