from abc import ABC from datetime import datetime from decimal import Decimal import json import re import sys, traceback from typing import Dict, List, Set, Any, Union, Optional import unittest from uuid import uuid4, UUID from pyson.Deserializer import Deserializer from pyson.JsonDeserialize import JsonDeserialize from pyson.JsonGetter import JsonGetter from pyson.JsonSubTypes import JsonSubTypes from pyson.JsonTypeInfo import Id, As from pyson.JsonTypeInfo import JsonTypeInfo from pyson.JsonValue import JsonValue from pyson.ObjectMapper import ObjectMapper @JsonDeserialize(using ="ValueDeserializer") class Simple: def __init__(self, a:int): self._a=a def geta(self)->int: return self._a def __eq__(self, other): return isinstance(other, self.__class__) and \ self._a==other._a def __str__(self): return self._name+","+str(self._a) class ValueDeserializer(Deserializer): def __hash__(self): return hash(self.geta()) def deserialize(self, data:object, clas: object)-> object: if type(data)!=str: raise ValueError("Expected str starting with '$', got "+str(data)) return int(data[1:]) class DeserializerTest(unittest.TestCase): ''' Test a lot of back-and-forth cases. FIXME Can we make this a parameterized test? ''' pyson=ObjectMapper() def testDeserialize(self): objson= "$12" self.assertEqual(12, self.pyson.parse(objson, Simple))