[489] | 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
|
---|
[568] | 20 | from pickle import NONE
|
---|
[567] | 21 | #from test.MyDeserializer import ValueDeserializer2
|
---|
[489] | 22 |
|
---|
[568] | 23 | # deserializer DROPS FIRST CHAR from string and assumes rest is an int.
|
---|
| 24 | # Then returns Simple(int)
|
---|
[490] | 25 | class 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:]))
|
---|
[489] | 32 |
|
---|
[490] | 33 |
|
---|
[567] | 34 | @JsonDeserialize("test.DeserializerTest.ValueDeserializer")
|
---|
[489] | 35 | class 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 |
|
---|
[567] | 48 | @JsonDeserialize("test.MyDeserializer.ValueDeserializer2")
|
---|
[490] | 49 | class 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)
|
---|
[568] | 59 |
|
---|
| 60 | #None cancels out the existing deserializer.
|
---|
| 61 | # parsing with str should now fail.
|
---|
| 62 | @JsonDeserialize(None)
|
---|
| 63 | class Simple3 (Simple):
|
---|
| 64 | pass
|
---|
[489] | 65 |
|
---|
| 66 |
|
---|
[568] | 67 | class 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)
|
---|
[490] | 77 |
|
---|
| 78 |
|
---|
[489] | 79 | class 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"
|
---|
[490] | 88 | self.assertEqual(Simple(12), self.pyson.parse(objson, Simple))
|
---|
[489] | 89 |
|
---|
[490] | 90 | def testExternalDeserialize2(self):
|
---|
| 91 | objson= "$13"
|
---|
| 92 | self.assertEqual(13, self.pyson.parse(objson, Simple2))
|
---|
[568] | 93 |
|
---|
| 94 |
|
---|
| 95 | def testExternalDeserialize3(self):
|
---|
| 96 | objson= "$13"
|
---|
| 97 | self.assertRaises(AttributeError, 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)
|
---|