aboutsummaryrefslogtreecommitdiffstats
path: root/rest_framework/utils/encoders.py
diff options
context:
space:
mode:
authorJosé Padilla2014-11-28 12:14:40 -0400
committerJosé Padilla2014-11-28 12:14:40 -0400
commit0cc990792c63caa8fa8fea62cea53b0d28157b55 (patch)
tree7ea80a203cc8718150cd55e4403f3f4771160281 /rest_framework/utils/encoders.py
parent1aa77830955dcdf829f65a9001b6b8900dfc8755 (diff)
parent3a5b3772fefc3c2f2c0899947cbc07bfe6e6b5d2 (diff)
downloaddjango-rest-framework-0cc990792c63caa8fa8fea62cea53b0d28157b55.tar.bz2
Merge branch 'version-3.1' into oauth_as_package
Conflicts: requirements-test.txt rest_framework/compat.py tests/settings.py tox.ini
Diffstat (limited to 'rest_framework/utils/encoders.py')
-rw-r--r--rest_framework/utils/encoders.py21
1 files changed, 10 insertions, 11 deletions
diff --git a/rest_framework/utils/encoders.py b/rest_framework/utils/encoders.py
index 174b08b8..4d6bb3a3 100644
--- a/rest_framework/utils/encoders.py
+++ b/rest_framework/utils/encoders.py
@@ -2,11 +2,10 @@
Helper classes for parsers.
"""
from __future__ import unicode_literals
-from django.utils import timezone
from django.db.models.query import QuerySet
-from django.utils.datastructures import SortedDict
+from django.utils import six, timezone
from django.utils.functional import Promise
-from rest_framework.compat import force_text
+from rest_framework.compat import force_text, OrderedDict
import datetime
import decimal
import types
@@ -40,12 +39,12 @@ class JSONEncoder(json.JSONEncoder):
representation = representation[:12]
return representation
elif isinstance(obj, datetime.timedelta):
- return str(obj.total_seconds())
+ return six.text_type(obj.total_seconds())
elif isinstance(obj, decimal.Decimal):
# Serializers will coerce decimals to strings by default.
return float(obj)
elif isinstance(obj, QuerySet):
- return list(obj)
+ return tuple(obj)
elif hasattr(obj, 'tolist'):
# Numpy arrays and array scalars.
return obj.tolist()
@@ -55,7 +54,7 @@ class JSONEncoder(json.JSONEncoder):
except:
pass
elif hasattr(obj, '__iter__'):
- return [item for item in obj]
+ return tuple(item for item in obj)
return super(JSONEncoder, self).default(obj)
@@ -68,11 +67,11 @@ else:
class SafeDumper(yaml.SafeDumper):
"""
Handles decimals as strings.
- Handles SortedDicts as usual dicts, but preserves field order, rather
+ Handles OrderedDicts as usual dicts, but preserves field order, rather
than the usual behaviour of sorting the keys.
"""
def represent_decimal(self, data):
- return self.represent_scalar('tag:yaml.org,2002:str', str(data))
+ return self.represent_scalar('tag:yaml.org,2002:str', six.text_type(data))
def represent_mapping(self, tag, mapping, flow_style=None):
value = []
@@ -82,7 +81,7 @@ else:
best_style = True
if hasattr(mapping, 'items'):
mapping = list(mapping.items())
- if not isinstance(mapping, SortedDict):
+ if not isinstance(mapping, OrderedDict):
mapping.sort()
for item_key, item_value in mapping:
node_key = self.represent_data(item_key)
@@ -104,7 +103,7 @@ else:
SafeDumper.represent_decimal
)
SafeDumper.add_representer(
- SortedDict,
+ OrderedDict,
yaml.representer.SafeRepresenter.represent_dict
)
# SafeDumper.add_representer(
@@ -112,7 +111,7 @@ else:
# yaml.representer.SafeRepresenter.represent_dict
# )
# SafeDumper.add_representer(
- # SortedDictWithMetadata,
+ # OrderedDictWithMetadata,
# yaml.representer.SafeRepresenter.represent_dict
# )
SafeDumper.add_representer(