Skip to content

tinyHui/python-serializable-dataclass

Repository files navigation

Serializable Dataclass for Python

codecov

This library provides a very simple API for encoding and decoding Python class to and from a designated serializable type.

Quickstart

pip install py-serializable-dataclass

First, you need to define the data object, e.g.

from serializable_dataclass import serializable_dataclass

@serializable_dataclass
class AnyData:
    field1: int
    field2: Optional[str]
    field3: Text

Now, enjoy the easy accessed serialization

# To serialize
assert AnyData(field1=1, 
               field2="string", 
               field3="another string").serialize() == {
                        "field1": 1,
                        "field2": "string",
                        "field3": "another string",
                    }

# To deserialize
AnyData.deserialize(
            {
                "field1": 1,
                "field2": None,
                "field3": "another string",
            }
        ) == AnyData(field1=1, field2=None, field3="another string")

The language parameter is optional and it's default value is JSON.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published