def test_serialization_default_types_2(self): for x in self.testdata: s = serializer.encode_data(x) xx = serializer.decode_data(s) self.assertEqual(x, xx) for x in self.testdata_arrays: s = serializer.encode_data(x) xx = serializer.decode_data(s) self.assertSequenceEqual(x.tolist(), xx.tolist())
def test_transform_data_with_registered_type(self): def dummy_transform(obj): return {'x': obj.x, 'y': obj.y} serializer.register(CustomType, dummy_transform, '', None) data = {'test': CustomType(13, 37)} transformed = serializer.encode_data(data) self.assertEqual(transformed, {'test': {'x': 13, 'y': 37}})
def encode_numpy_number(item): """ Encode a numpy scalar to JSON """ return serializer.encode_data(item)
def encode_numpy_array(item): """ Encode a numpy array to JSON """ return serializer.encode_data(item)
def test_transform_data_unknown_type(self): data = {'test': CustomType(13, 37)} transformed = serializer.encode_data(data) self.assertNotEqual(transformed, {'test': {'x': 13, 'y': 37}})
def test_encode_decode_complex(self): data = {'results': [{'result_1': np.array([1, 2, 3, 4, 5])}]} new_data = serializer.decode_data(serializer.encode_data(data)) self.assertTrue( np.array_equal(data['results'][0]['result_1'], new_data['results'][0]['result_1']))