source: pyson/test/DeserializerTest.py@ 489

Last change on this file since 489 was 489, checked in by wouter, 17 months ago

#152 added test showing an issue with @JsonDeserialize

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