Skip to main content

tests pypi License: MIT bear-ified Ruff-ified

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_ ...

Metadata

Release files for nested-dataclass-serialization 0.1.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for nested-dataclass-serialization 0.1.7
File Size Uploaded
nested_dataclass_serialization-0.1.7.tar.gz 9.0 kB Details

Release files / nested_dataclass_serialization-0.1.7.tar.gz

Download URL nested_dataclass_serialization-0.1.7.tar.gz
Size 9.0 kB
Tags Source
SHA-256 checksum
How to use checksums
33d1772bc6f83951d01082d62e48998bfe86c1b7c0797a9968347e93a12324e3
BLAKE2b-256 checksum
How to use checksums
5d6fe1e2675cb0465104e3fba23bac17ad10961bdb066add998913f969391611
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.1.0 CPython/3.12.4

Release history Release notifications | RSS feed

This release

0.1.7 This release

1 release file

0.1.6

1 release file

0.1.5

1 release file

0.1.4

1 release file

0.1.3

1 release file

0.1.2

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page