source: pyson/test/DeserializerTest.py@ 498

Last change on this file since 498 was 490, checked in by wouter, 18 months ago

#152 changed @JsonDeserialize deserialization argument from str to type. It now takes the class as argument. This is attempt to solve some resolvation issues

File size: 2.0 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 MyDeserializer import ValueDeserializer2
21
22class ValueDeserializer(Deserializer):
23 def __hash__(self):
24 return hash(self.geta())
25 def deserialize(self, data:object, clas: object)-> object:
26 if type(data)!=str:
27 raise ValueError("Expected str starting with '$', got "+str(data))
28 return Simple(int(data[1:]))
29
30
31@JsonDeserialize(using =ValueDeserializer)
32class Simple:
33 def __init__(self, a:int):
34 self._a=a
35 def geta(self)->int:
36 return self._a
37 def __eq__(self, other):
38 return isinstance(other, self.__class__) and \
39 self._a==other._a
40 def __str__(self):
41 return self._name+","+str(self._a)
42
43
44
45@JsonDeserialize(using =ValueDeserializer2)
46class Simple2:
47 def __init__(self, a:int):
48 self._a=a
49 def geta(self)->int:
50 return self._a
51 def __eq__(self, other):
52 return isinstance(other, self.__class__) and \
53 self._a==other._a
54 def __str__(self):
55 return self._name+","+str(self._a)
56
57
58
59
60class DeserializerTest(unittest.TestCase):
61 '''
62 Test a lot of back-and-forth cases.
63 FIXME Can we make this a parameterized test?
64 '''
65 pyson=ObjectMapper()
66
67 def testDeserialize(self):
68 objson= "$12"
69 self.assertEqual(Simple(12), self.pyson.parse(objson, Simple))
70
71 def testExternalDeserialize2(self):
72 objson= "$13"
73 self.assertEqual(13, self.pyson.parse(objson, Simple2))
74
Note: See TracBrowser for help on using the repository browser.