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
|
---|
9 | import unittest
|
---|
10 | from uuid import uuid4, UUID
|
---|
11 |
|
---|
12 | from pyson.Deserializer import Deserializer
|
---|
13 | from pyson.JsonDeserialize import JsonDeserialize
|
---|
14 | from pyson.JsonGetter import JsonGetter
|
---|
15 | from pyson.JsonSubTypes import JsonSubTypes
|
---|
16 | from pyson.JsonTypeInfo import Id, As
|
---|
17 | from pyson.JsonTypeInfo import JsonTypeInfo
|
---|
18 | from pyson.JsonValue import JsonValue
|
---|
19 | from pyson.ObjectMapper import ObjectMapper
|
---|
20 | from pickle import NONE
|
---|
21 | #from test.MyDeserializer import ValueDeserializer2
|
---|
22 |
|
---|
23 | # deserializer DROPS FIRST CHAR from string and assumes rest is an int.
|
---|
24 | # Then returns Simple(int)
|
---|
25 | class ValueDeserializer(Deserializer):
|
---|
26 | def __hash__(self):
|
---|
27 | return hash(self.geta())
|
---|
28 | def deserialize(self, data:object, clas: object)-> object:
|
---|
29 | if type(data)!=str:
|
---|
30 | raise ValueError("Expected str starting with '$', got "+str(data))
|
---|
31 | return Simple(int(data[1:]))
|
---|
32 |
|
---|
33 |
|
---|
34 | @JsonDeserialize("test.DeserializerTest.ValueDeserializer")
|
---|
35 | class Simple:
|
---|
36 | def __init__(self, a:int):
|
---|
37 | self._a=a
|
---|
38 | def geta(self)->int:
|
---|
39 | return self._a
|
---|
40 | def __eq__(self, other):
|
---|
41 | return isinstance(other, self.__class__) and \
|
---|
42 | self._a==other._a
|
---|
43 | def __str__(self):
|
---|
44 | return self._name+","+str(self._a)
|
---|
45 |
|
---|
46 |
|
---|
47 |
|
---|
48 | @JsonDeserialize("test.MyDeserializer.ValueDeserializer2")
|
---|
49 | class Simple2:
|
---|
50 | def __init__(self, a:int):
|
---|
51 | self._a=a
|
---|
52 | def geta(self)->int:
|
---|
53 | return self._a
|
---|
54 | def __eq__(self, other):
|
---|
55 | return isinstance(other, self.__class__) and \
|
---|
56 | self._a==other._a
|
---|
57 | def __str__(self):
|
---|
58 | return self._name+","+str(self._a)
|
---|
59 |
|
---|
60 | #None cancels out the existing deserializer.
|
---|
61 | # parsing with str should now fail.
|
---|
62 | @JsonDeserialize(None)
|
---|
63 | class Simple3 (Simple):
|
---|
64 | pass
|
---|
65 |
|
---|
66 |
|
---|
67 | class MyDict:
|
---|
68 | def __init__(self, data ):
|
---|
69 | self._data=data
|
---|
70 | def getData(self)->int:
|
---|
71 | return self._data
|
---|
72 | def __eq__(self, other):
|
---|
73 | return isinstance(other, self.__class__) and \
|
---|
74 | self._data==other._data
|
---|
75 | def __str__(self):
|
---|
76 | return self._name+","+str(self._data)
|
---|
77 |
|
---|
78 |
|
---|
79 | class DeserializerTest(unittest.TestCase):
|
---|
80 | '''
|
---|
81 | Test a lot of back-and-forth cases.
|
---|
82 | FIXME Can we make this a parameterized test?
|
---|
83 | '''
|
---|
84 | pyson=ObjectMapper()
|
---|
85 |
|
---|
86 | def testDeserialize(self):
|
---|
87 | objson= "$12"
|
---|
88 | self.assertEqual(Simple(12), self.pyson.parse(objson, Simple))
|
---|
89 |
|
---|
90 | def testExternalDeserialize2(self):
|
---|
91 | objson= "$13"
|
---|
92 | self.assertEqual(13, self.pyson.parse(objson, Simple2))
|
---|
93 |
|
---|
94 |
|
---|
95 | def testExternalDeserialize3(self):
|
---|
96 | objson= "$13"
|
---|
97 | self.assertRaises(ValueError, lambda:self.pyson.parse(objson, Simple3))
|
---|
98 |
|
---|
99 | def testDeserializeMap(self):
|
---|
100 | objson='{ "Simple(1)": Simple(12), "Simple(2)": Simple(22) }'
|
---|
101 | self.pyson.parse(objson, MyDict)
|
---|