-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement custom (de)serialization for json and msgpack
- Loading branch information
1 parent
efac08a
commit 752a5e6
Showing
4 changed files
with
93 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#!/usr/bin/env python3 | ||
|
||
__author__ = "Radical.Utils Development Team (Andre Merzky)" | ||
__copyright__ = "Copyright 2024, RADICAL@Rutgers" | ||
__license__ = "MIT" | ||
|
||
import os | ||
|
||
import radical.utils as ru | ||
|
||
|
||
# ------------------------------------------------------------------------------ | ||
# | ||
def test_serialization(): | ||
|
||
class Complex(object): | ||
|
||
def __init__(self, real, imag): | ||
self.real = real | ||
self.imag = imag | ||
|
||
def __eq__(self, other): | ||
return self.real == other.real and self.imag == other.imag | ||
|
||
def serialize(self): | ||
return {'real': self.real, 'imag': self.imag} | ||
|
||
@classmethod | ||
def deserialize(cls, data): | ||
return cls(data['real'], data['imag']) | ||
|
||
|
||
ru.register_serialization(Complex, encode=Complex.serialize, | ||
decode=Complex.deserialize) | ||
|
||
data = {'foo': {'complex_number': Complex(1, 2)}} | ||
json_str = ru.to_json(data) | ||
new_data = ru.from_json(json_str) | ||
|
||
assert data == new_data | ||
|
||
msgpack_str = ru.to_msgpack(data) | ||
new_data = ru.from_msgpack(msgpack_str) | ||
|
||
assert data == new_data | ||
|
||
|
||
# ------------------------------------------------------------------------------ | ||
# | ||
if __name__ == '__main__': | ||
|
||
test_serialization() | ||
|
||
|
||
# ------------------------------------------------------------------------------ | ||
|