Skip to content

Commit 685706f

Browse files
committed
Merge pull request #584 from radiosilence/master
Adding timedelta support to JSONEncoder, and an example of how to add decode support to a serializer.
2 parents e32aaa2 + 4fc3b1b commit 685706f

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

rest_framework/utils/encoders.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
class JSONEncoder(json.JSONEncoder):
1414
"""
15-
JSONEncoder subclass that knows how to encode date/time,
15+
JSONEncoder subclass that knows how to encode date/time/timedelta,
1616
decimal types, and generators.
1717
"""
1818
def default(self, o):
@@ -34,6 +34,8 @@ def default(self, o):
3434
if o.microsecond:
3535
r = r[:12]
3636
return r
37+
elif isinstance(o, datetime.timedelta):
38+
return str(o.total_seconds())
3739
elif isinstance(o, decimal.Decimal):
3840
return str(o)
3941
elif hasattr(o, '__iter__'):

0 commit comments

Comments
 (0)