No project description provided
Project description
nested dataclass serialization
- lets you (de)serialize nested dataclasses from and to json
- uses custom json-encoder for recursive serialization
- uses
importlib
for deserialization (instantiation of the dataclass-objects)
minimal example
@dataclass
class Bar:
x: str
@dataclass
class Foo:
bars: list[Bar]
bar = Bar(x="test")
foo = Foo(bars=[bar, bar])
json_str = serialize_dataclass(foo)
# this json_str looks like this:
{
"_target_": "test_dataclass_serialization.Foo",
"_id_": "a22ddd36-<some-long-id-here>d2290929",
"bars": [
{
"_target_": "test_dataclass_serialization.Bar",
"_id_": "a22ddd36-<some-long-id-here>ebb0ab925b1bd977208e4",
"x": "test",
},
{
"_target_": "test_dataclass_serialization.Bar",
"_id_": "a22ddd36-<some-long-id-here>ebb0ab925b1bd977208e4",
"x": "test",
},
],
}
# somewhere in a different python-process/container/server
des_foo = deserialize_dataclass(json_str) # getting back the dataclass-object
assert id(des_foo.bars[0]) == id(des_foo.bars[1]) # just to show that deserialization works
similar libs
- madman-bob: looks like you need to explicitly provide the python-class for deserialization:
JSONSerializer.deserialize(InventoryItem, {'name': 'Apple', 'unit_ ...
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Close
Hashes for nested_dataclass_serialization-0.1.5.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | ceeb17134de44db699b5d3555cf2a33bcb16dce56038aa15729b45f78979657c |
|
MD5 | 5d6c1384c7b9db625f0f26d27ef7c261 |
|
BLAKE2b-256 | 5dc5b429c6c7c71c418af7b3aab4fdb38388a838abad4dfe31c6436b50aa206d |