def test_json_serialization_nested(self):
        obj = Song(Person("Fred"))
        serialized_obj = {'artist': {'name': "Fred"}}

        with self.subTest("Serialize nested dataclass -> JSON"):
            self.assertEqual(serialized_obj, JSONSerializer.serialize(obj))

        with self.subTest("Deserialize JSON -> nested dataclass"):
            self.assertEqual(obj, JSONSerializer.deserialize(Song, serialized_obj))
    def test_json_serialization_basic(self):
        obj = Person("Fred")
        serialized_obj = {'name': "Fred"}

        with self.subTest("Serialize dataclass -> JSON"):
            self.assertEqual(serialized_obj, JSONSerializer.serialize(obj))

        with self.subTest("Deserialize JSON -> dataclass"):
            self.assertEqual(obj, JSONSerializer.deserialize(Person, serialized_obj))
Esempio n. 3
0
    def setLabelmap(self, path=None):
        # clear labelmap
        self.labelmap.clear()

        if not path:
            path = os.path.join(os.getcwd(), 'resources/labelmap.json')

        with open(path, 'r') as file:
            data = json.loads(file.read())

        if not isinstance(data, list):
            raise Exception("label map needs to be array!")

        for label in data:
            self.labelmap.append(JSONSerializer.deserialize(LabelMap, label))
Esempio n. 4
0
    def read_file(self, i):
        assert self.files

        if i >= len(self.files):
            self.logger.warning(
                'mock jobs are over. using the last one forever...')
            i = len(self.files) - 1

        with open(self.files[i], 'r') as file:
            jobs_raw_arr = json.load(file)
            jobs = [
                JSONSerializer.deserialize(JobTxInfo, item)
                for item in jobs_raw_arr
            ]

            self.logger.info(
                f'mock job was read: {self.files[i]} ({len(jobs)}) jobs loaded.'
            )
            return jobs
Esempio n. 5
0
    def test_json_serialization_types(self):
        test_cases = [
            (int, 1, 1),
            (float, 1.0, 1.0),
            (str, "Fred", "Fred"),
            (bool, True, True),
            (dict, {'name': "Fred"}, {'name': "Fred"}),
            (Dict, {'name': "Fred"}, {'name': "Fred"}),
            (Dict[str, Person], {'abc123': Person("Fred")}, {'abc123': {'name': "Fred"}}),
            (list, [{'name': "Fred"}], [{'name': "Fred"}]),
            (List, [{'name': "Fred"}], [{'name': "Fred"}]),
            (List[Person], [Person("Fred")], [{'name': "Fred"}]),
            (type(None), None, None)
        ]

        for type_, obj, serialized_obj in test_cases:
            with self.subTest("Serialize object", obj=obj):
                self.assertEqual(serialized_obj, JSONSerializer.serialize(obj))

            with self.subTest("Deserialize object", obj=obj):
                self.assertEqual(obj, JSONSerializer.deserialize(type_, serialized_obj))
from dataclasses_serialization.json import JSONSerializer

from services.models.job import BridgeTxInfo, JobTxInfo

job = JobTxInfo('123', JobTxInfo.STATUS_ACTIVE, chain='BNB', intx_time=0,
                in_tx=BridgeTxInfo(
                    'BNB', 66312, 100.4, 'bnb123', '', '49394034930434093409343', 2302303
                ),
                out_tx=BridgeTxInfo(
                    'ETH', 5454343, 100.1, '', '0xC349309232ABC', '3432423292832983', 2302305
                ))


print(JSONSerializer.serialize(job))

j = JSONSerializer.serialize(job)
out_job = JSONSerializer.deserialize(JobTxInfo, j)
print(out_job)
assert job == out_job