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 |
|
---|
21 |
|
---|
22 | @JsonDeserialize(using ="ValueDeserializer")
|
---|
23 | class 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 |
|
---|
35 | class 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 |
|
---|
45 | class 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 |
|
---|