8000 Make serialize_meta() to properly reduce set/like-like types other than built-in set/list by dahlia · Pull Request #91 · nirum-lang/nirum-python · GitHub
[go: up one dir, main page]

Skip to content

Make serialize_meta() to properly reduce set/like-like types other than built-in set/list #91

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

Merged
merged 2 commits into from
Jul 6, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Fix bug that list-like types aren't serialized
  • Loading branch information
dahlia committed Jul 5, 2017
commit ae440349dc02b4f25d30a350dbfae418af024c05
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ To be released.
- Fixed a serialization bug that other set-like (i.e. ``collections.Set``) types
than Python built-in ``set`` hadn't been reduced to simpler forms so that they
can be encoded to JSON.
- Fixed a serialization bug that other list-like (i.e. ``collections.Sequence``)
types than Python built-in ``list`` hadn't been reduced to simpler forms so
that they can be encoded to JSON.



Expand Down
10 changes: 8 additions & 2 deletions nirum/serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import decimal
import uuid

from six import string_types

__all__ = (
'serialize_boxed_type', 'serialize_meta',
'serialize_record_type', 'serialize_unboxed_type',
Expand Down Expand Up @@ -59,14 +61,18 @@ def serialize_union_type(data):
def serialize_meta(data):
if hasattr(data, '__nirum_serialize__'):
d = data.__nirum_serialize__()
elif type(data) in {str, float, bool, int}:
elif isinstance(data, (string_types, bool, int, float)):
# FIXME: str in py2 represents binary string as well as text string.
# It should be refactored so that the function explicitly takes
# an expected type as like deserialize_meta() does.
d = data
elif (isinstance(data, datetime.datetime) or
isinstance(data, datetime.date)):
d = data.isoformat()
elif isinstance(data, decimal.Decimal) or isinstance(data, uuid.UUID):
d = str(data)
elif isinstance(data, collections.Set) or isinstance(data, list):
elif (isinstance(data, collections.Set) or
isinstance(data, collections.Sequence)):
d = [serialize_meta(e) for e in data]
elif isinstance(data, collections.Mapping):
d = [
Expand Down
12 changes: 12 additions & 0 deletions tests/serialize_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from pytest import mark

from nirum._compat import utc
from nirum.datastructures import List
from nirum.serialize import (serialize_unboxed_type, serialize_record_type,
serialize_meta, serialize_union_type)
from .nirum_schema import import_nirum_fixture
Expand Down Expand Up @@ -94,6 +95,17 @@ def test_serialize_meta_set(d, expect):
e in serialized


def test_serialize_meta_list(fx_record_type, fx_unboxed_type, fx_offset):
record = fx_record_type(fx_offset, fx_offset)
record2 = fx_record_type(fx_unboxed_type(1.1), fx_unboxed_type(1.2))
serialize_result = serialize_meta([record, record2])
assert serialize_result == [
{'_type': 'point', 'x': 1.2, 'top': 1.2},
{'_type': 'point', 'x': 1.1, 'top': 1.2},
]
assert serialize_meta(List([record, record2])) == serialize_result


def test_serialize_meta_set_of_record(fx_record_type, fx_unboxed_type,
fx_offset):
record = fx_record_type(fx_offset, fx_offset)
Expand Down
0