# 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/7e/b9/a06f8a1fe0f41be460cff40b66c9f50ffcdb9d14b63fc6a90e1436189721/serialite-0.3.5.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.5",
"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": "ab07a2aa2617b065b4e91bedf3698558139e79c0a9d6059b240bca1108ae9964",
"md5": "97fc0b413782fbd7165d709c6c0e650d",
"sha256": "f93027d0c16ebb8c50210f273766adafed8dabc126ac694558dad0075cc87592"
},
"downloads": -1,
"filename": "serialite-0.3.5-py3-none-any.whl",
"has_sig": false,
"md5_digest": "97fc0b413782fbd7165d709c6c0e650d",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.10",
"size": 35970,
"upload_time": "2024-11-25T17:29:44",
"upload_time_iso_8601": "2024-11-25T17:29:44.016775Z",
"url": "https://files.pythonhosted.org/packages/ab/07/a2aa2617b065b4e91bedf3698558139e79c0a9d6059b240bca1108ae9964/serialite-0.3.5-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "7eb9a06f8a1fe0f41be460cff40b66c9f50ffcdb9d14b63fc6a90e1436189721",
"md5": "ecaf86b59e063d65b50f497678d2977f",
"sha256": "ca593827deb94132eb8200e0215353c8530952ea9c7163be2fd02daa179efe8e"
},
"downloads": -1,
"filename": "serialite-0.3.5.tar.gz",
"has_sig": false,
"md5_digest": "ecaf86b59e063d65b50f497678d2977f",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.10",
"size": 24120,
"upload_time": "2024-11-25T17:29:45",
"upload_time_iso_8601": "2024-11-25T17:29:45.790545Z",
"url": "https://files.pythonhosted.org/packages/7e/b9/a06f8a1fe0f41be460cff40b66c9f50ffcdb9d14b63fc6a90e1436189721/serialite-0.3.5.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-11-25 17:29:45",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "drhagen",
"github_project": "serialite",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "serialite"
}