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
RevLine 
[489]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
[490]20from MyDeserializer import ValueDeserializer2
[489]21
[490]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:]))
[489]29
[490]30
31@JsonDeserialize(using =ValueDeserializer)
[489]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
[490]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)
[489]56
57
[490]58
59
[489]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"
[490]69 self.assertEqual(Simple(12), self.pyson.parse(objson, Simple))
[489]70
[490]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.