serialite


Nameserialite JSON
Version 0.3.4 PyPI version JSON
download
home_pagehttps://github.com/drhagen/serialite
SummaryA serialization library for Python
upload_time2024-03-27 17:43:38
maintainerNone
docs_urlNone
authorDavid Hagen
requires_python<4.0,>=3.10
licenseMIT
keywords serialization deserialization serde pydantic fastapi
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Serialite

Serialite is a library serializing and deserializing arbitrarily complex objects in Python. You
apply the `@serializable` decorator to a dataclass to automatically create `to_data` and `from_data`
methods using the type annotations. Or for more control, inherit from the `SerializableMixin` and
implement the class attribute `__fields_serializer__`. For even more control, inherit from the 
abstract base class `Serializer` and implement the `to_data` and `from_data` methods directly. 

## Basics

The abstract base class is `Serializer`:

```python
class Serializer(Generic[Output]):
    def from_data(self, data: Json) -> DeserializationSuccess[Output]: ...
    def to_data(self, value: Output) -> Json: ...
```

The class is generic in the type of the object that it serializes. The two abstract methods
`from_data` and `to_data` are the key to the whole design, which revolves around getting objects to
and from JSON-serializable data, which are objects constructed entirely of `bool`s, `int`s, 
`float`s, `list`s, and `dict`s. Such structures can be consumed by `json.dumps` to produce a string
and produced by `json.loads` after consuming a string. By basing the serialization around JSON
serializable data, complex structures can be built up or torn down piece by piece while
alternatively building up complex error messages during deserialization which pinpoint the location
in the structure where the bad data exist.

For new classes, it is recommended that the `Serializer` be implemented on the class itself. There is
an abstract base class `Serializable` that classes can inherit from to indicate this. There is a mixin
`SerializableMixin` that provides an implementation of `from_data` and `to_data` for any class that
implements the `__fields_serializer` class attribute.

For `dataclass`es, it is even easier. There is a decorator `serializable` that inserts
`SerializableMixin` into the list of base classes after the `dataclass` decorator has run and also
generates `__fields_serializer__` from the data class attributes.

Finding the correct serializer for each type can be a pain, so
`serializer(cls: type) -> Serializer` is provided as a convenience function. This is a single
dispatch function, which looks up the serializer registered for a particular type. For example,
`serializer(list[float])` will return `ListSerializer(FloatSerializer)`.


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/drhagen/serialite",
    "name": "serialite",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.10",
    "maintainer_email": null,
    "keywords": "serialization, deserialization, serde, pydantic, fastapi",
    "author": "David Hagen",
    "author_email": "david@drhagen.com",
    "download_url": "https://files.pythonhosted.org/packages/d1/a3/812b7760b308cf8873b1dcc6a729402cd8a3f9e3c1265201de1f7d928d8e/serialite-0.3.4.tar.gz",
    "platform": null,
    "description": "# Serialite\n\nSerialite is a library serializing and deserializing arbitrarily complex objects in Python. You\napply the `@serializable` decorator to a dataclass to automatically create `to_data` and `from_data`\nmethods using the type annotations. Or for more control, inherit from the `SerializableMixin` and\nimplement the class attribute `__fields_serializer__`. For even more control, inherit from the \nabstract base class `Serializer` and implement the `to_data` and `from_data` methods directly. \n\n## Basics\n\nThe abstract base class is `Serializer`:\n\n```python\nclass Serializer(Generic[Output]):\n    def from_data(self, data: Json) -> DeserializationSuccess[Output]: ...\n    def to_data(self, value: Output) -> Json: ...\n```\n\nThe class is generic in the type of the object that it serializes. The two abstract methods\n`from_data` and `to_data` are the key to the whole design, which revolves around getting objects to\nand from JSON-serializable data, which are objects constructed entirely of `bool`s, `int`s, \n`float`s, `list`s, and `dict`s. Such structures can be consumed by `json.dumps` to produce a string\nand produced by `json.loads` after consuming a string. By basing the serialization around JSON\nserializable data, complex structures can be built up or torn down piece by piece while\nalternatively building up complex error messages during deserialization which pinpoint the location\nin the structure where the bad data exist.\n\nFor new classes, it is recommended that the `Serializer` be implemented on the class itself. There is\nan abstract base class `Serializable` that classes can inherit from to indicate this. There is a mixin\n`SerializableMixin` that provides an implementation of `from_data` and `to_data` for any class that\nimplements the `__fields_serializer` class attribute.\n\nFor `dataclass`es, it is even easier. There is a decorator `serializable` that inserts\n`SerializableMixin` into the list of base classes after the `dataclass` decorator has run and also\ngenerates `__fields_serializer__` from the data class attributes.\n\nFinding the correct serializer for each type can be a pain, so\n`serializer(cls: type) -> Serializer` is provided as a convenience function. This is a single\ndispatch function, which looks up the serializer registered for a particular type. For example,\n`serializer(list[float])` will return `ListSerializer(FloatSerializer)`.\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A serialization library for Python",
    "version": "0.3.4",
    "project_urls": {
        "Homepage": "https://github.com/drhagen/serialite",
        "Repository": "https://github.com/drhagen/serialite"
    },
    "split_keywords": [
        "serialization",
        " deserialization",
        " serde",
        " pydantic",
        " fastapi"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "a6a68df467773cdf213cda8edf4a3a12aba204f703230c60beebe3eb35fd6e5f",
                "md5": "4cc647fd75d04ec87d3e6f86b3793b4c",
                "sha256": "c5a1570e8a14d69f3556f206324b0d7309098fe58b21a9d0782d06cf1433bdbf"
            },
            "downloads": -1,
            "filename": "serialite-0.3.4-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "4cc647fd75d04ec87d3e6f86b3793b4c",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.10",
            "size": 35988,
            "upload_time": "2024-03-27T17:43:36",
            "upload_time_iso_8601": "2024-03-27T17:43:36.132436Z",
            "url": "https://files.pythonhosted.org/packages/a6/a6/8df467773cdf213cda8edf4a3a12aba204f703230c60beebe3eb35fd6e5f/serialite-0.3.4-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "d1a3812b7760b308cf8873b1dcc6a729402cd8a3f9e3c1265201de1f7d928d8e",
                "md5": "b365737eab809716e8e9d918284f6adc",
                "sha256": "4d2a7c67aaccb077210f0e7d7352db19cdd7b39358c1d06891db0b56139fe17d"
            },
            "downloads": -1,
            "filename": "serialite-0.3.4.tar.gz",
            "has_sig": false,
            "md5_digest": "b365737eab809716e8e9d918284f6adc",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.10",
            "size": 24187,
            "upload_time": "2024-03-27T17:43:38",
            "upload_time_iso_8601": "2024-03-27T17:43:38.963698Z",
            "url": "https://files.pythonhosted.org/packages/d1/a3/812b7760b308cf8873b1dcc6a729402cd8a3f9e3c1265201de1f7d928d8e/serialite-0.3.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-03-27 17:43:38",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "drhagen",
    "github_project": "serialite",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "serialite"
}
        
Elapsed time: 0.23261s