Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

return JSON items in OrderedDict + tests #124

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion slumber/serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@
except ImportError:
_SERIALIZERS["yaml"] = False

ordered_dict_defined = True
try:
from collections import OrderedDict
except ImportError:
try:
from ordereddict import OrderedDict
except ImportError:
ordered_dict_defined = False

class BaseSerializer(object):

Expand Down Expand Up @@ -45,7 +53,10 @@ class JsonSerializer(BaseSerializer):
key = "json"

def loads(self, data):
return json.loads(data)
if ordered_dict_defined:
return json.loads(data, object_pairs_hook=OrderedDict)
else:
return json.loads(data)

def dumps(self, data):
return json.dumps(data)
Expand Down
31 changes: 30 additions & 1 deletion tests/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,29 @@
import slumber
import slumber.serialize

ordered_dict_defined = True
try:
from collections import OrderedDict
except ImportError:
try:
from ordereddict import OrderedDict
except ImportError:
ordered_dict_defined = False


class ResourceTestCase(unittest.TestCase):
def setUp(self):
self.data = {
"foo": "bar",
}
if ordered_dict_defined:
self.data1 = OrderedDict((
("foo", "bar"),
("foo2", "bar"),
("foo1", "bar"),
))

def test_json_get_serializer(self):
def prepare_json_serializer(self):
s = slumber.serialize.Serializer()

serializer = None
Expand All @@ -23,11 +38,25 @@ def test_json_get_serializer(self):
serializer = s.get_serializer(content_type=content_type)
self.assertEqual(type(serializer), slumber.serialize.JsonSerializer,
"content_type %s should produce a JsonSerializer")
return serializer

def test_json_get_serializer(self):
serializer = self.prepare_json_serializer()

result = serializer.dumps(self.data)
self.assertEqual(result, '{"foo": "bar"}')
self.assertEqual(self.data, serializer.loads(result))

def test_json_get_serializer_ordereddict(self):
if not ordered_dict_defined: # skip this test if OrderedDict is not loaded
return

serializer = self.prepare_json_serializer()

result = serializer.dumps(self.data1)
self.assertEqual(result, '{"foo": "bar", "foo2": "bar", "foo1": "bar"}')
self.assertEqual(self.data1, serializer.loads(result))

def test_yaml_get_serializer(self):
s = slumber.serialize.Serializer()

Expand Down