models: Explicitly define which models can be deserialized

This commit is contained in:
Thomas Adamcik 2015-05-04 22:36:27 +02:00
parent 07159f69c2
commit 7f6809aebb
2 changed files with 7 additions and 11 deletions

View File

@ -2,7 +2,9 @@ from __future__ import absolute_import, unicode_literals
import json
from mopidy.models.immutable import ImmutableObject
from mopidy.models import immutable
_MODELS = ['Ref', 'Artist', 'Album', 'Track', 'TlTrack', 'Playlist']
class ModelJSONEncoder(json.JSONEncoder):
@ -19,7 +21,7 @@ class ModelJSONEncoder(json.JSONEncoder):
"""
def default(self, obj):
if isinstance(obj, ImmutableObject):
if isinstance(obj, immutable.ImmutableObject):
return obj.serialize()
return json.JSONEncoder.default(self, obj)
@ -38,8 +40,8 @@ def model_json_decoder(dct):
"""
if '__model__' in dct:
models = {c.__name__: c for c in ImmutableObject.__subclasses__()}
from mopidy import models
model_name = dct.pop('__model__')
if model_name in models:
return models[model_name](**dct)
if model_name in _MODELS:
return getattr(models, model_name)(**dct)
return dct

View File

@ -1168,9 +1168,3 @@ class SearchResultTest(unittest.TestCase):
self.assertDictEqual(
{'__model__': 'SearchResult', 'uri': 'uri'},
SearchResult(uri='uri').serialize())
def test_to_json_and_back(self):
result1 = SearchResult(uri='uri')
serialized = json.dumps(result1, cls=ModelJSONEncoder)
result2 = json.loads(serialized, object_hook=model_json_decoder)
self.assertEqual(result1, result2)