def test_serialization_default_types(self): for x in self.testdata: s = serialize(x) xx = unserialize(s) self.assertEqual(x, xx) for x in self.testdata_arrays: s = serialize(x) xx = unserialize(s) self.assertSequenceEqual(x.tolist(), xx.tolist())
def test_serialization_container_types(self): """ Creates a PythonJsonStucture with all the data-types. Serializes the object and directly afterwards unserializes. The unserialized object should equal the original created PythonJsonStructure. Note: Currently a PythonJsonStructure with a tuple is not tested. A tuple object can only be serialized to a list. """ settable_containers = { 'list': [1, 2, 3], 'dict': { 'a': 1, 'b': 2, 'c': 3 }, 'ndarray': np.random.rand(3, 2, 4, 5).astype(np.cfloat), # 'tuple': (1, 2, 3), 'json_object': PythonJsonStructure() } for key, expected in settable_containers.items(): json_object = PythonJsonStructure({key: expected}) serialized_object = serialize(json_object) unserialized_object = unserialize(serialized_object) np.testing.assert_equal(json_object, unserialized_object)
def load_configuration(file_path: str) -> PythonJsonStructure: """ Loads the instrument configuration from disk storage. Args: file_path: The store file location on disk. Returns: The loaded configuration from disk. """ with open(file_path, 'rb') as file_pointer: serialized_configuration = file_pointer.readlines() unserialized_configuration = dict( serialization.unserialize(serialized_configuration[0])) return PythonJsonStructure(unserialized_configuration)
def test_serialization_data_types(self): settable_objects = { 'none': None, 'bool': False, 'int': 25, 'float': 3.141592, 'str': 'some_string', 'bytes': b'1010101', 'np.float32': np.float32(3.1415), 'np.float64': np.float64(-3.1415), 'np.int32': np.int32(4), 'np.in64': np.int64(-4), 'np.cfloat': np.random.rand(2, 4, 5, 3).astype(np.cfloat), } for key, expected in settable_objects.items(): json_object = PythonJsonStructure({key: expected}) serialized_object = serialize(json_object) unserialized_object = unserialize(serialized_object) np.testing.assert_equal(json_object, unserialized_object)
def test_unserialize(self): data = b'{"hello":"world"}' unserialized = unserialize(data) self.assertDictEqual(unserialized, {'hello': 'world'})
def test_serialize(self): data = {'hello': 'world'} serialized = serialize(data) self.assertEqual(type(serialized), bytes) self.assertEqual(data, unserialize(serialize(data)))
def test_encode_decode_json_dataclass(self): serializer.register_dataclass(CustomDataClass) custom_dataclass = CustomDataClass(1.25, 'my_dummy_value') new_dataclass = unserialize(serialize(custom_dataclass)) self.assertEqual(new_dataclass, custom_dataclass)
def test_serialize_non_string_keys(self): data = PythonJsonStructure({'a': 1, 1: 'a'}) self.assertDictEqual(unserialize(serialize(data)), {'a': 1, '1': 'a'})