2012-09-20 16:06:27 +04:00
|
|
|
"""
|
|
|
|
Helper classes for parsers.
|
|
|
|
"""
|
2013-02-05 00:55:35 +04:00
|
|
|
from __future__ import unicode_literals
|
2014-01-17 16:05:10 +04:00
|
|
|
from django.db.models.query import QuerySet
|
2014-10-01 16:09:14 +04:00
|
|
|
from django.utils import six, timezone
|
2014-12-04 05:11:42 +03:00
|
|
|
from django.utils.encoding import force_text
|
2013-05-25 02:44:23 +04:00
|
|
|
from django.utils.functional import Promise
|
2015-01-19 18:16:57 +03:00
|
|
|
from rest_framework.compat import total_seconds
|
2012-09-20 16:06:27 +04:00
|
|
|
import datetime
|
|
|
|
import decimal
|
2013-01-05 16:40:02 +04:00
|
|
|
import json
|
2015-01-19 17:18:02 +03:00
|
|
|
import uuid
|
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
|
|
|
|
# http://ecma-international.org/ecma-262/5.1/#sec-15.9.1.15
|
2014-09-12 14:50:20 +04:00
|
|
|
if isinstance(obj, Promise):
|
|
|
|
return force_text(obj)
|
|
|
|
elif isinstance(obj, datetime.datetime):
|
|
|
|
representation = obj.isoformat()
|
|
|
|
if obj.microsecond:
|
|
|
|
representation = representation[:23] + representation[26:]
|
|
|
|
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()
|
|
|
|
if obj.microsecond:
|
|
|
|
representation = representation[:12]
|
|
|
|
return representation
|
|
|
|
elif isinstance(obj, datetime.timedelta):
|
2015-01-19 12:57:26 +03:00
|
|
|
return six.text_type(total_seconds(obj))
|
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):
|
|
|
|
return six.text_type(obj)
|
2014-09-12 14:50:20 +04:00
|
|
|
elif isinstance(obj, QuerySet):
|
2014-10-02 17:44:20 +04:00
|
|
|
return tuple(obj)
|
2014-09-12 14:50:20 +04:00
|
|
|
elif hasattr(obj, 'tolist'):
|
|
|
|
# Numpy arrays and array scalars.
|
|
|
|
return obj.tolist()
|
|
|
|
elif hasattr(obj, '__getitem__'):
|
2013-11-22 00:09:48 +04:00
|
|
|
try:
|
2014-09-12 14:50:20 +04:00
|
|
|
return dict(obj)
|
2013-12-09 13:24:10 +04:00
|
|
|
except:
|
2013-11-22 00:09:48 +04:00
|
|
|
pass
|
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)
|
2014-09-12 14:50:20 +04:00
|
|
|
return super(JSONEncoder, self).default(obj)
|