| 1 |
# Copyright (C) 2013 Jason R. Coombs <[email protected]> |
| 2 |
# All rights reserved. |
| 3 |
# |
| 4 |
# This software is licensed as described in the file COPYING, which |
| 5 |
# you should have received as part of this distribution. |
| 6 |
|
| 7 |
import unittest |
| 8 |
|
| 9 |
import pytest |
| 10 |
|
| 11 |
import jsonpickle |
| 12 |
import jsonpickle.handlers |
| 13 |
|
| 14 |
|
| 15 |
class CustomObject: |
| 16 |
"A class to be serialized by a custom handler" |
| 17 |
|
| 18 |
def __init__(self, name=None, creator=None): |
| 19 |
self.name = name |
| 20 |
self.creator = creator |
| 21 |
|
| 22 |
def __eq__(self, other): |
| 23 |
return self.name == other.name |
| 24 |
|
| 25 |
|
| 26 |
class CustomA(CustomObject): |
| 27 |
pass |
| 28 |
|
| 29 |
|
| 30 |
class CustomB(CustomA): |
| 31 |
pass |
| 32 |
|
| 33 |
|
| 34 |
class NullHandler(jsonpickle.handlers.BaseHandler): |
| 35 |
def flatten(self, obj, data): |
| 36 |
data['name'] = obj.name |
| 37 |
return data |
| 38 |
|
| 39 |
def restore(self, obj): |
| 40 |
return CustomObject(obj['name'], creator=type(self)) |
| 41 |
|
| 42 |
|
| 43 |
class DecoratedBase(CustomObject): |
| 44 |
pass |
| 45 |
|
| 46 |
|
| 47 |
class DecoratedChild(DecoratedBase): |
| 48 |
pass |
| 49 |
|
| 50 |
|
| 51 |
@jsonpickle.handlers.register(DecoratedBase, base=True) |
| 52 |
class DecoratedHandler(NullHandler): |
| 53 |
pass |
| 54 |
|
| 55 |
|
| 56 |
class SithHandler(jsonpickle.handlers.BaseHandler): |
| 57 |
"""(evil) serialization handler that rewrites the entire object""" |
| 58 |
|
| 59 |
def flatten(self, obj, data): |
| 60 |
data['name'] = obj.name |
| 61 |
data['py/object'] = 'sith.lord' |
| 62 |
return data |
| 63 |
|
| 64 |
|
| 65 |
class HandlerTestCase(unittest.TestCase): |
| 66 |
def setUp(self): |
| 67 |
jsonpickle.handlers.register(CustomObject, NullHandler) |
| 68 |
|
| 69 |
def tearDown(self): |
| 70 |
jsonpickle.handlers.unregister(CustomObject) |
| 71 |
|
| 72 |
def roundtrip(self, ob): |
| 73 |
encoded = jsonpickle.encode(ob) |
| 74 |
decoded = jsonpickle.decode(encoded) |
| 75 |
assert decoded == ob |
| 76 |
return decoded |
| 77 |
|
| 78 |
def test_custom_handler(self): |
| 79 |
"""Ensure that the custom handler is indeed used""" |
| 80 |
expect = CustomObject('jarjar') |
| 81 |
encoded = jsonpickle.encode(expect) |
| 82 |
actual = jsonpickle.decode(encoded) |
| 83 |
assert expect.name == actual.name |
| 84 |
assert expect.creator is None |
| 85 |
assert actual.creator is NullHandler |
| 86 |
|
| 87 |
def test_references(self): |
| 88 |
""" |
| 89 |
Ensure objects handled by a custom handler are properly dereferenced. |
| 90 |
""" |
| 91 |
ob = CustomObject() |
| 92 |
# create a dictionary which contains several references to ob |
| 93 |
subject = dict(a=ob, b=ob, c=ob) |
| 94 |
# ensure the subject can be roundtripped |
| 95 |
new_subject = self.roundtrip(subject) |
| 96 |
assert new_subject['a'] == new_subject['b'] |
| 97 |
assert new_subject['b'] == new_subject['c'] |
| 98 |
assert new_subject['a'] is new_subject['b'] |
| 99 |
assert new_subject['b'] is new_subject['c'] |
| 100 |
|
| 101 |
def test_invalid_class(self): |
| 102 |
with pytest.raises(TypeError): |
| 103 |
jsonpickle.handlers.register('foo', NullHandler) |
| 104 |
|
| 105 |
def test_base_handler(self): |
| 106 |
a = CustomA('a') |
| 107 |
assert a.creator is None |
| 108 |
assert jsonpickle.decode(jsonpickle.encode(a)).creator is None |
| 109 |
|
| 110 |
b = CustomB('b') |
| 111 |
assert b.creator is None |
| 112 |
assert jsonpickle.decode(jsonpickle.encode(b)).creator is None |
| 113 |
|
| 114 |
OtherHandler = type('OtherHandler', (NullHandler,), {}) |
| 115 |
jsonpickle.handlers.register(CustomA, OtherHandler, base=True) |
| 116 |
assert self.roundtrip(a).creator is OtherHandler |
| 117 |
assert self.roundtrip(b).creator is OtherHandler |
| 118 |
|
| 119 |
SpecializedHandler = type('SpecializedHandler', (NullHandler,), {}) |
| 120 |
jsonpickle.handlers.register(CustomB, SpecializedHandler) |
| 121 |
assert self.roundtrip(a).creator is OtherHandler |
| 122 |
assert self.roundtrip(b).creator is SpecializedHandler |
| 123 |
|
| 124 |
def test_decorated_register(self): |
| 125 |
db = DecoratedBase('db') |
| 126 |
dc = DecoratedChild('dc') |
| 127 |
assert self.roundtrip(db).creator is DecoratedHandler |
| 128 |
assert self.roundtrip(dc).creator is DecoratedHandler |
| 129 |
|
| 130 |
def test_custom_handler_can_rewrite_everything(self): |
| 131 |
"""Test the low-level pickling structures""" |
| 132 |
jsonpickle.handlers.unregister(CustomObject) |
| 133 |
jsonpickle.handlers.register(CustomObject, SithHandler) |
| 134 |
obj = CustomObject('jarjar') # secret sith lord jarjar |
| 135 |
pickler = jsonpickle.pickler.Pickler() |
| 136 |
data = pickler.flatten(obj) |
| 137 |
assert isinstance(data, dict) |
| 138 |
assert len(data) == 2 |
| 139 |
assert data['name'] == 'jarjar' |
| 140 |
assert data['py/object'] == 'sith.lord' |