Skip to content

Commit

Permalink
Merge pull request #5 from tranvietanh1991/develop
Browse files Browse the repository at this point in the history
add support for variant of dict and list set tuple
  • Loading branch information
0xGosu committed Jun 5, 2020
2 parents 96c8109 + 15d920e commit 1da05c3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
2 changes: 1 addition & 1 deletion nameko_django/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.1.2
1.1.3
4 changes: 3 additions & 1 deletion nameko_django/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ def encode_nondefault_object(obj):
return dict(obj.to_dict())
elif hasattr(obj, 'to_list') and callable(obj.to_list):
return list(obj.to_list())
elif isinstance(obj, tuple): # tuple will be treated as list
elif isinstance(obj, dict): # handle Box, defaultdict and all variant of dictionary
return dict(obj)
elif isinstance(obj, (tuple, set, list)): # tuple,set,list will be treated as list
return list(obj)
elif isinstance(obj, Enum) and hasattr(obj, 'value'):
return obj.value
Expand Down
26 changes: 25 additions & 1 deletion tests/test_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from datetime import datetime, date, time, timedelta
from decimal import Decimal
from aenum import Enum, IntEnum, Constant
from collections import namedtuple
from collections import namedtuple, defaultdict, OrderedDict
from django.utils.timezone import FixedOffset
from django.utils import timezone
from nose import tools
Expand Down Expand Up @@ -97,6 +97,30 @@ def test_simple_dictionary():
assert test_data == dec_data


def test_default_dictionary():
test_data = defaultdict(
type='Mount',
modes=1,
rw=True,
level=1.5
)
enc_data = dumps(test_data)
dec_data = loads(enc_data)
assert dict(test_data) == dec_data


def test_orderred_dictionary():
test_data = OrderedDict(
type='Mount',
rw=True,
level=1.5
)
test_data['modes'] = 1
enc_data = dumps(test_data)
dec_data = loads(enc_data)
assert dict(test_data) == dec_data


def test_simple_boxdict():
test_data = {'a': 1, 'b': 2, 'c': 3, 'd': '', 'e': {'e0': 0, 'e1': -1, 'ee': ['e', 'e']}}
enc_data = dumps(Box(test_data))
Expand Down

0 comments on commit 1da05c3

Please sign in to comment.