source: pyson/test/DeserializerTest.py@ 569

Last change on this file since 569 was 569, checked in by wouter, 16 months ago

#174 @JsonDeserialize(None) works

File size: 2.9 KB
Line 
1
2from abc import ABC
3from datetime import datetime
4from decimal import Decimal
5import json
6import re
7import sys, traceback
8from typing import Dict, List, Set, Any, Union, Optional
9import unittest
10from uuid import uuid4, UUID
11
12from pyson.Deserializer import Deserializer
13from pyson.JsonDeserialize import JsonDeserialize
14from pyson.JsonGetter import JsonGetter
15from pyson.JsonSubTypes import JsonSubTypes
16from pyson.JsonTypeInfo import Id, As
17from pyson.JsonTypeInfo import JsonTypeInfo
18from pyson.JsonValue import JsonValue
19from pyson.ObjectMapper import ObjectMapper
20from 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)
25class 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")
35class 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")
49class 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)
63class Simple3 (Simple):
64 pass
65
66
67class 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
79class 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)
Note: See TracBrowser for help on using the repository browser.