mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-02-03 05:04:31 +03:00
Tidy up JSONEncoder
This commit is contained in:
parent
5e39e159ee
commit
22af49bf8f
|
@ -7,7 +7,6 @@ from django.db.models.query import QuerySet
|
||||||
from django.utils.datastructures import SortedDict
|
from django.utils.datastructures import SortedDict
|
||||||
from django.utils.functional import Promise
|
from django.utils.functional import Promise
|
||||||
from rest_framework.compat import force_text
|
from rest_framework.compat import force_text
|
||||||
# from rest_framework.serializers import DictWithMetadata, SortedDictWithMetadata
|
|
||||||
import datetime
|
import datetime
|
||||||
import decimal
|
import decimal
|
||||||
import types
|
import types
|
||||||
|
@ -17,45 +16,47 @@ import json
|
||||||
class JSONEncoder(json.JSONEncoder):
|
class JSONEncoder(json.JSONEncoder):
|
||||||
"""
|
"""
|
||||||
JSONEncoder subclass that knows how to encode date/time/timedelta,
|
JSONEncoder subclass that knows how to encode date/time/timedelta,
|
||||||
decimal types, and generators.
|
decimal types, generators and other basic python objects.
|
||||||
"""
|
"""
|
||||||
def default(self, o):
|
def default(self, obj):
|
||||||
# For Date Time string spec, see ECMA 262
|
# For Date Time string spec, see ECMA 262
|
||||||
# http://ecma-international.org/ecma-262/5.1/#sec-15.9.1.15
|
# http://ecma-international.org/ecma-262/5.1/#sec-15.9.1.15
|
||||||
if isinstance(o, Promise):
|
if isinstance(obj, Promise):
|
||||||
return force_text(o)
|
return force_text(obj)
|
||||||
elif isinstance(o, datetime.datetime):
|
elif isinstance(obj, datetime.datetime):
|
||||||
r = o.isoformat()
|
representation = obj.isoformat()
|
||||||
if o.microsecond:
|
if obj.microsecond:
|
||||||
r = r[:23] + r[26:]
|
representation = representation[:23] + representation[26:]
|
||||||
if r.endswith('+00:00'):
|
if representation.endswith('+00:00'):
|
||||||
r = r[:-6] + 'Z'
|
representation = representation[:-6] + 'Z'
|
||||||
return r
|
return representation
|
||||||
elif isinstance(o, datetime.date):
|
elif isinstance(obj, datetime.date):
|
||||||
return o.isoformat()
|
return obj.isoformat()
|
||||||
elif isinstance(o, datetime.time):
|
elif isinstance(obj, datetime.time):
|
||||||
if timezone and timezone.is_aware(o):
|
if timezone and timezone.is_aware(obj):
|
||||||
raise ValueError("JSON can't represent timezone-aware times.")
|
raise ValueError("JSON can't represent timezone-aware times.")
|
||||||
r = o.isoformat()
|
representation = obj.isoformat()
|
||||||
if o.microsecond:
|
if obj.microsecond:
|
||||||
r = r[:12]
|
representation = representation[:12]
|
||||||
return r
|
return representation
|
||||||
elif isinstance(o, datetime.timedelta):
|
elif isinstance(obj, datetime.timedelta):
|
||||||
return str(o.total_seconds())
|
return str(obj.total_seconds())
|
||||||
elif isinstance(o, decimal.Decimal):
|
elif isinstance(obj, decimal.Decimal):
|
||||||
return float(o)
|
# Serializers will coerce decimals to strings by default.
|
||||||
elif isinstance(o, QuerySet):
|
return float(obj)
|
||||||
return list(o)
|
elif isinstance(obj, QuerySet):
|
||||||
elif hasattr(o, 'tolist'):
|
return list(obj)
|
||||||
return o.tolist()
|
elif hasattr(obj, 'tolist'):
|
||||||
elif hasattr(o, '__getitem__'):
|
# Numpy arrays and array scalars.
|
||||||
|
return obj.tolist()
|
||||||
|
elif hasattr(obj, '__getitem__'):
|
||||||
try:
|
try:
|
||||||
return dict(o)
|
return dict(obj)
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
elif hasattr(o, '__iter__'):
|
elif hasattr(obj, '__iter__'):
|
||||||
return [i for i in o]
|
return [item for item in obj]
|
||||||
return super(JSONEncoder, self).default(o)
|
return super(JSONEncoder, self).default(obj)
|
||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user