def default_decoder(cls, value): """Default decoder. Recursively deserialize into immutable types ( :class:`josepy.util.frozendict` instead of :func:`dict`, :func:`tuple` instead of :func:`list`). """ # bases cases for different types returned by json.loads if isinstance(value, list): return tuple(cls.default_decoder(subvalue) for subvalue in value) elif isinstance(value, dict): return util.frozendict( dict((cls.default_decoder(key), cls.default_decoder(value)) for key, value in six.iteritems(value))) else: # integer or string return value
def test_init_dict(self): from josepy.util import frozendict self.assertEqual(self.fdict, frozendict({'x': 1, 'y': '2'}))
def setUp(self): from josepy.util import frozendict self.fdict = frozendict(x=1, y='2')
def test_default_decoder_dict_to_frozendict(self): from josepy.json_util import Field obj = Field.default_decoder({'x': 2}) self.assertIsInstance(obj, util.frozendict) self.assertEqual(obj, util.frozendict(x=2))