Skip to content

Commit

Permalink
Merge pull request #49 from issackelly/feature/uuid-serialization
Browse files Browse the repository at this point in the history
Add UUID serialization (helps me with Django 1.8)
  • Loading branch information
issackelly committed Oct 3, 2015
2 parents ec8805c + c0ad478 commit a4994eb
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
4 changes: 3 additions & 1 deletion restless/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import datetime
import decimal
import traceback
import uuid

try:
import json
Expand All @@ -18,12 +19,13 @@ class MoreTypesJSONEncoder(json.JSONEncoder):
* ``datetime.date``
* ``datetime.time``
* ``decimal.Decimal``
* ``uuid.UUID``
"""
def default(self, data):
if isinstance(data, (datetime.datetime, datetime.date, datetime.time)):
return data.isoformat()
elif isinstance(data, decimal.Decimal):
elif isinstance(data, decimal.Decimal) or isinstance(data, uuid.UUID):
return str(data)
else:
return super(MoreTypesJSONEncoder, self).default(data)
Expand Down
2 changes: 2 additions & 0 deletions tests/test_serializers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import datetime
from decimal import Decimal
import unittest
import uuid

from restless.serializers import JSONSerializer

Expand All @@ -17,6 +18,7 @@ def setUp(self):
# Some data the usual JSON encoder can't handle...
'nested': datetime.datetime(2014, 3, 30, 12, 55, 15),
'again': Decimal('18.9'),
'uuid': uuid.uuid4()
},
}

Expand Down

0 comments on commit a4994eb

Please sign in to comment.