1 |
|
---|
2 | from abc import ABC
|
---|
3 | from datetime import datetime
|
---|
4 | from decimal import Decimal
|
---|
5 | import json
|
---|
6 | import re
|
---|
7 | import sys, traceback
|
---|
8 | from typing import Dict, List, Set, Any, Union, Optional, Collection
|
---|
9 | import unittest
|
---|
10 | from uuid import uuid4, UUID
|
---|
11 |
|
---|
12 | from pyson.Deserializer import Deserializer
|
---|
13 | from pyson.Serializer import Serializer
|
---|
14 | from pyson.JsonSerialize import JsonSerialize
|
---|
15 | from pyson.JsonDeserialize import JsonDeserialize
|
---|
16 | from pyson.JsonGetter import JsonGetter
|
---|
17 | from pyson.JsonSubTypes import JsonSubTypes
|
---|
18 | from pyson.JsonTypeInfo import Id, As
|
---|
19 | from pyson.JsonTypeInfo import JsonTypeInfo
|
---|
20 | from pyson.JsonValue import JsonValue
|
---|
21 | from pyson.ObjectMapper import ObjectMapper
|
---|
22 | from pickle import NONE
|
---|
23 | from test.MyDeserializer import ValueDeserializer2
|
---|
24 |
|
---|
25 | # deserializer DROPS FIRST CHAR from string and assumes rest is an int.
|
---|
26 | # Then returns Simple(int)
|
---|
27 | class ValueDeserializer(Deserializer):
|
---|
28 | def __hash__(self):
|
---|
29 | return hash(self.geta())
|
---|
30 | def deserialize(self, data:object, clas: object)-> object:
|
---|
31 | if type(data)!=str:
|
---|
32 | raise ValueError("Expected str starting with '$', got "+str(data))
|
---|
33 | return Simple(int(data[1:]))
|
---|
34 |
|
---|
35 | # serializes Simple object, just prefixing its value (as string) with "$"
|
---|
36 | class ValueSerializer(Serializer):
|
---|
37 | def serialize(self, obj:object)-> object:
|
---|
38 | if not isinstance(obj, Simple):
|
---|
39 | raise ValueError("Expected Dimple object")
|
---|
40 | return "$" + str(obj.geta())
|
---|
41 |
|
---|
42 | class Basic:
|
---|
43 | def __init__(self, v:float):
|
---|
44 | self._v=v
|
---|
45 | def __eq__(self, other):
|
---|
46 | return isinstance(other, self.__class__) and \
|
---|
47 | self._v==other._v
|
---|
48 | def getV(self):
|
---|
49 | return self._v
|
---|
50 | def __repr__(self):
|
---|
51 | return "Basic:"+str(self._v)
|
---|
52 | def __hash__(self):
|
---|
53 | return hash(self._v)
|
---|
54 |
|
---|
55 | @JsonDeserialize(ValueDeserializer)
|
---|
56 | @JsonSerialize(ValueSerializer)
|
---|
57 | class Simple:
|
---|
58 | def __init__(self, a:int):
|
---|
59 | self._a=a
|
---|
60 | def geta(self)->int:
|
---|
61 | return self._a
|
---|
62 | def __eq__(self, other):
|
---|
63 | return isinstance(other, self.__class__) and \
|
---|
64 | self._a==other._a
|
---|
65 | def __repr__(self):
|
---|
66 | return "Simple:"+str(self._a)
|
---|
67 | def __hash__(self):
|
---|
68 | return hash(self._a)
|
---|
69 |
|
---|
70 |
|
---|
71 |
|
---|
72 | @JsonDeserialize(ValueDeserializer2)
|
---|
73 | class Simple2:
|
---|
74 | def __init__(self, a:int):
|
---|
75 | self._a=a
|
---|
76 | def geta(self)->int:
|
---|
77 | return self._a
|
---|
78 | def __eq__(self, other):
|
---|
79 | return isinstance(other, self.__class__) and \
|
---|
80 | self._a==other._a
|
---|
81 | def __repr__(self):
|
---|
82 | return self._name+","+str(self._a)
|
---|
83 |
|
---|
84 | #None cancels out the existing deserializer.
|
---|
85 | # parsing with str should now fail.
|
---|
86 | @JsonDeserialize(None)
|
---|
87 | class Simple3 (Simple):
|
---|
88 | pass
|
---|
89 |
|
---|
90 |
|
---|
91 | class MyList:
|
---|
92 | def __init__(self, data: List[Simple] ):
|
---|
93 | self.data=data
|
---|
94 | def getData(self)->List[Simple]:
|
---|
95 | return self.data
|
---|
96 |
|
---|
97 | class BasicDict:
|
---|
98 | def __init__(self, data: Dict[Basic,Basic] ):
|
---|
99 | self.data=data
|
---|
100 | def getData(self)->Dict[Basic,Basic]:
|
---|
101 | return self.data
|
---|
102 | def __repr__(self):
|
---|
103 | return "BasicDict:"+str(self.data)
|
---|
104 | def __eq__(self, other):
|
---|
105 | return isinstance(other, self.__class__) and \
|
---|
106 | self.data==other.data
|
---|
107 |
|
---|
108 |
|
---|
109 | class SimpleDict:
|
---|
110 | def __init__(self, data: Dict[Simple,Simple] ):
|
---|
111 | self.data=data
|
---|
112 | def getData(self)->Dict[Simple,Simple]:
|
---|
113 | return self.data
|
---|
114 | def __eq__(self, other):
|
---|
115 | return isinstance(other, self.__class__) and \
|
---|
116 | self.data==other.data
|
---|
117 | def __repr__(self):
|
---|
118 | return "SimpleDict:"+str(self.data)
|
---|
119 |
|
---|
120 |
|
---|
121 | class MyOptionalString:
|
---|
122 | def __init__(self, s:Optional[str]):
|
---|
123 | self.data:Optional[str] = s
|
---|
124 | def getData(self)->Optional[str]:
|
---|
125 | return self.data
|
---|
126 | def __eq__(self, other):
|
---|
127 | return isinstance(other, self.__class__) and \
|
---|
128 | self.data==other.data
|
---|
129 | def __repr__(self):
|
---|
130 | return "MyOptionalString:"+str(self.data)
|
---|
131 |
|
---|
132 |
|
---|
133 | class SimpleA(Simple):
|
---|
134 | pass
|
---|
135 |
|
---|
136 | class SimpleB(Simple):
|
---|
137 | pass
|
---|
138 |
|
---|
139 | class DeserializerTest(unittest.TestCase):
|
---|
140 | '''
|
---|
141 | Test a lot of back-and-forth cases.
|
---|
142 | FIXME Can we make this a parameterized test?
|
---|
143 | '''
|
---|
144 | pyson=ObjectMapper()
|
---|
145 |
|
---|
146 | def testDeserialize(self):
|
---|
147 | objson= "$12"
|
---|
148 | self.assertEqual(Simple(12), self.pyson.parse(objson, Simple))
|
---|
149 |
|
---|
150 | def testExternalDeserialize2(self):
|
---|
151 | objson= "$13"
|
---|
152 | self.assertEqual(13, self.pyson.parse(objson, Simple2))
|
---|
153 |
|
---|
154 |
|
---|
155 | def testExternalDeserialize3(self):
|
---|
156 | objson= "$13"
|
---|
157 | self.assertRaises(ValueError, lambda:self.pyson.parse(objson, Simple3))
|
---|
158 |
|
---|
159 | def testDeserializeMyList(self):
|
---|
160 | print(self.pyson.toJson(MyList([12,13])))
|
---|
161 |
|
---|
162 | # the json we provide is NOT the proper json but a STRING.
|
---|
163 | # This triggers a fallback mechanism that tries to parse the string as json.
|
---|
164 | objson={"data": ["$12", "$13"]}
|
---|
165 | res = self.pyson.parse(objson, MyList)
|
---|
166 | print(res)
|
---|
167 | self.assertEqual([Simple(12),Simple(13)], res.data)
|
---|
168 |
|
---|
169 | def testDeserializeCollection(self):
|
---|
170 | objson=[1,2,3]
|
---|
171 | # this checks that pyson can parse a list as Collection.
|
---|
172 | # The result is a simple list like in java/jackson.
|
---|
173 | # Collection without typing info means Collectino[Any]
|
---|
174 | res = self.pyson.parse(objson, Collection)
|
---|
175 | print(res)
|
---|
176 | self.assertEqual([1,2,3], res)
|
---|
177 |
|
---|
178 |
|
---|
179 | def testSerializeBasicDict(self):
|
---|
180 | '''
|
---|
181 | Basic object keys. Special (de)serializer should kick in #190
|
---|
182 | '''
|
---|
183 | d= BasicDict( { Basic(1.):Basic(2.), Basic(3.): Basic(4.) } )
|
---|
184 | objson={"data": {"{\"v\": 1.0}": {"v": 2.0}, "{\"v\": 3.0}": {"v": 4.0}}}
|
---|
185 | dump=json.dumps(self.pyson.toJson(d));
|
---|
186 | print(dump)
|
---|
187 | # self.assertEqual(objson, dump);
|
---|
188 | res=self.pyson.parse(objson, BasicDict)
|
---|
189 | print("res="+str(res))
|
---|
190 | self.assertEqual(d, res)
|
---|
191 |
|
---|
192 |
|
---|
193 | def testSerializeSimpleDictCustomSerializer(self):
|
---|
194 | d= SimpleDict( { Simple(1):Simple(2), Simple(3): Simple(4) } )
|
---|
195 |
|
---|
196 | # The keys need extra quotes, they are deserialied by
|
---|
197 | # our special deserializer that parses the string as json,
|
---|
198 | # and json requires double quotes around its strings.
|
---|
199 | objson = {"data": {'"$1"': "$2", '"$3"': "$4"}}
|
---|
200 |
|
---|
201 | obj=self.pyson.toJson(d)
|
---|
202 | print(json.dumps(obj))
|
---|
203 | self.assertEqual(objson, obj)
|
---|
204 |
|
---|
205 | res = self.pyson.parse(objson, SimpleDict)
|
---|
206 | print("res="+str(res))
|
---|
207 | self.assertEqual(d, res)
|
---|
208 |
|
---|
209 | def testDeserializeOptString(self):
|
---|
210 | objson:dict={'s':None}
|
---|
211 | res = self.pyson.parse(objson, MyOptionalString)
|
---|
212 | print(res)
|
---|
213 | self.assertEqual(MyOptionalString(None), res)
|
---|
214 |
|
---|
215 | objson={'s':"something"}
|
---|
216 | res = self.pyson.parse(objson, MyOptionalString)
|
---|
217 | print(res)
|
---|
218 | self.assertEqual(MyOptionalString("something"), res)
|
---|
219 |
|
---|
220 |
|
---|
221 | def testSerializeMixedList(self):
|
---|
222 | # see #296
|
---|
223 | list:List[Simple] = [SimpleA(1), SimpleB(1)]
|
---|
224 | print(self.pyson.toJson(list))
|
---|
225 |
|
---|
226 | |
---|