models: Support serialization of lists of strings
This commit is contained in:
parent
1ea83803a5
commit
af10e642f2
@ -87,7 +87,9 @@ class ImmutableObject(object):
|
|||||||
public_key = key.lstrip('_')
|
public_key = key.lstrip('_')
|
||||||
value = self.__dict__[key]
|
value = self.__dict__[key]
|
||||||
if isinstance(value, (set, frozenset, list, tuple)):
|
if isinstance(value, (set, frozenset, list, tuple)):
|
||||||
value = [o.serialize() for o in value]
|
value = [
|
||||||
|
v.serialize() if isinstance(v, ImmutableObject) else v
|
||||||
|
for v in value]
|
||||||
elif isinstance(value, ImmutableObject):
|
elif isinstance(value, ImmutableObject):
|
||||||
value = value.serialize()
|
value = value.serialize()
|
||||||
if value:
|
if value:
|
||||||
|
|||||||
@ -249,6 +249,13 @@ class AlbumTest(unittest.TestCase):
|
|||||||
'artists': [artist.serialize()]},
|
'artists': [artist.serialize()]},
|
||||||
Album(uri='uri', name='name', artists=[artist]).serialize())
|
Album(uri='uri', name='name', artists=[artist]).serialize())
|
||||||
|
|
||||||
|
def test_serialize_with_images(self):
|
||||||
|
image = 'data:foobar'
|
||||||
|
self.assertDictEqual(
|
||||||
|
{'__model__': 'Album', 'uri': 'uri', 'name': 'name',
|
||||||
|
'images': [image]},
|
||||||
|
Album(uri='uri', name='name', images=[image]).serialize())
|
||||||
|
|
||||||
def test_to_json_and_back(self):
|
def test_to_json_and_back(self):
|
||||||
album1 = Album(uri='uri', name='name', artists=[Artist(name='foo')])
|
album1 = Album(uri='uri', name='name', artists=[Artist(name='foo')])
|
||||||
serialized = json.dumps(album1, cls=ModelJSONEncoder)
|
serialized = json.dumps(album1, cls=ModelJSONEncoder)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user