def test_complex_custom_objecthook(self): class Bar(object): def __init__(self, date, value): self.date = date self.value = value def bar_hook(dct): if u'*bar*' in dct: return Bar(dct.get('date'), dct.get('value')) return None serialiser = jsonte.JsonteSerialiser(custom_objecthook=bar_hook) data = { '*bar*': None, 'date': { '#date': '2001-01-01' }, 'value': { '#num': '42.00' } } bar = serialiser.loads(json.dumps(data)) self.assertTrue(isinstance(bar, Bar)) self.assertEqual(bar.date, datetime.date(2001, 1, 1)) self.assertTrue(isinstance(bar.value, decimal.Decimal)) self.assertEqual(bar.value, decimal.Decimal('42.00'))
def test_custom_objecthook(self): class Foo(object): pass def foo_hook(dct): if u'**foo**' in dct: return Foo() return None serialiser = jsonte.JsonteSerialiser(custom_objecthook=foo_hook) data = [{u'**foo**': 1}, {u'**bar**': 2}] raw_json = json.dumps(data) list_back = serialiser.loads(raw_json) self.assertTrue(isinstance(list_back[0], Foo)) self.assertEqual(list_back[1], {u'**bar**': 2})
def test_order_independance(self): class Foo(object): pass # noinspection PyUnusedLocal def foo_serialiser(foo_inst): dct = jsonte.SerialisationDict() dct[u'#foo'] = u'A foo instance' return dct # noinspection PyUnusedLocal def obj_serialiser(obj_inst): dct = jsonte.SerialisationDict() dct[u'#obj'] = u'A obj instance' return dct serialiser = jsonte.JsonteSerialiser() serialiser.add_type_serialiser(object, obj_serialiser) serialiser.add_type_serialiser(Foo, foo_serialiser) serialiser.finalise_serialisers() f = Foo() jsonte_str = serialiser.dumps(f) self.assertTrue(u'A foo instance' in jsonte_str)
def setUp(self): self.serialiser = jsonte.JsonteSerialiser() self.serialiser.reserved_initial_chars += u'*'
def setUp(self): self.serialiser = jsonte.JsonteSerialiser()
def test_prefix(self): serialiser = jsonte.JsonteSerialiser(array_websafety='prefix') data = [u'a', u'list'] jsonte_str = serialiser.dumps(data) first_line = jsonte_str.splitlines()[0] self.assertEqual(first_line, u")]}',")
def test_raise_exception(self): serialiser = jsonte.JsonteSerialiser(array_websafety='exception') data = [u'a', u'list'] self.assertRaises(RuntimeError, serialiser.dumps, data)
def setUp(self): self.serialiser = jsonte.JsonteSerialiser(escape_char=u'')