mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-06-09 16:13:39 +03:00
Merge pull request #584 from radiosilence/master
Adding timedelta support to JSONEncoder, and an example of how to add decode support to a serializer.
This commit is contained in:
commit
685706ff2b
|
@ -12,7 +12,7 @@ from rest_framework.serializers import DictWithMetadata, SortedDictWithMetadata
|
||||||
|
|
||||||
class JSONEncoder(json.JSONEncoder):
|
class JSONEncoder(json.JSONEncoder):
|
||||||
"""
|
"""
|
||||||
JSONEncoder subclass that knows how to encode date/time,
|
JSONEncoder subclass that knows how to encode date/time/timedelta,
|
||||||
decimal types, and generators.
|
decimal types, and generators.
|
||||||
"""
|
"""
|
||||||
def default(self, o):
|
def default(self, o):
|
||||||
|
@ -34,6 +34,8 @@ class JSONEncoder(json.JSONEncoder):
|
||||||
if o.microsecond:
|
if o.microsecond:
|
||||||
r = r[:12]
|
r = r[:12]
|
||||||
return r
|
return r
|
||||||
|
elif isinstance(o, datetime.timedelta):
|
||||||
|
return str(o.total_seconds())
|
||||||
elif isinstance(o, decimal.Decimal):
|
elif isinstance(o, decimal.Decimal):
|
||||||
return str(o)
|
return str(o)
|
||||||
elif hasattr(o, '__iter__'):
|
elif hasattr(o, '__iter__'):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user