{"id":20852055,"url":"https://github.com/osom8979/type-serialize","last_synced_at":"2025-05-12T04:32:26.955Z","repository":{"id":41282787,"uuid":"508191904","full_name":"osom8979/type-serialize","owner":"osom8979","description":"Serialize with type annotations","archived":false,"fork":false,"pushed_at":"2025-03-05T01:29:25.000Z","size":88,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-15T15:49:38.120Z","etag":null,"topics":["python3","python38","python39","serialize"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/osom8979.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-06-28T07:05:27.000Z","updated_at":"2025-03-05T01:29:28.000Z","dependencies_parsed_at":"2022-07-06T16:04:49.303Z","dependency_job_id":null,"html_url":"https://github.com/osom8979/type-serialize","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/osom8979%2Ftype-serialize","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/osom8979%2Ftype-serialize/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/osom8979%2Ftype-serialize/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/osom8979%2Ftype-serialize/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/osom8979","download_url":"https://codeload.github.com/osom8979/type-serialize/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253675714,"owners_count":21945956,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["python3","python38","python39","serialize"],"created_at":"2024-11-18T03:16:07.902Z","updated_at":"2025-05-12T04:32:26.938Z","avatar_url":"https://github.com/osom8979.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# type-serialize\n\n[![PyPI](https://img.shields.io/pypi/v/type-serialize?style=flat-square)](https://pypi.org/project/type-serialize/)\n![PyPI - Python Version](https://img.shields.io/pypi/pyversions/type-serialize?style=flat-square)\n[![GitHub](https://img.shields.io/github/license/osom8979/type-serialize?style=flat-square)](https://github.com/osom8979/type-serialize)\n\nSerialize with type annotations\n\n## Features\n\n- Supported in Python 3.9 and later.\n- Serialize classes without additional code.\n  - Custom classes\n  - [@dataclas](https://docs.python.org/3/library/dataclasses.html#dataclasses.dataclass)\n  - [NamedTuple](https://docs.python.org/3/library/typing.html#typing.NamedTuple)\n  - [Enum](https://docs.python.org/3/library/enum.html#enum.Enum)\n  - (Experimental) [numpy.ndarray](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy-ndarray)\n- Deserialization using type annotations.\n- No dependencies\n- \u003cdel\u003eCompress serialization results: bz2, gzip, lzma, zlib\u003c/del\u003e - Removed to focus on 'serialization' itself.\n\n## Overview\n\nTo pass a custom object to the [json.dumps](https://docs.python.org/3/library/json.html#json.dumps)\nand [json.loads](https://docs.python.org/3/library/json.html#json.loads) functions,\nthere is the following method.\n\n- Expand [json.JSONEncoder](https://docs.python.org/3/library/json.html#json.JSONEncoder)\n  and [json.JSONDecoder](https://docs.python.org/3/library/json.html#json.JSONDecoder).\n- Convert to built-in Python object supported by\n  [json.JSONEncoder](https://docs.python.org/3/library/json.html#json.JSONEncoder) and\n  [json.JSONDecoder](https://docs.python.org/3/library/json.html#json.JSONDecoder).\n\nBoth methods require additional code and have some problems.\n\n- Problem of not checking symbols when manipulating strings.\n- When adding/deleting/editing a property, all related codes must be changed together.\n- Painful typecasting (as the biggest problem).\n\nAs a way to hide these problems with a library and use serialization and deserialization,\nI chose **[type annotations](https://docs.python.org/3/library/typing.html)**.\n(Although the library is complicated; haha...) There are some additional advantages to using this.\n\n- Static type checking using [mypy](https://mypy.readthedocs.io/en/stable/).\n- Autocomplete in IDE like PyCharm.\n\n### Things to know\n\n- All public fields are serialized.\n- Methods are not serialized.\n- Private fields that start with an underscore (`_`) are not serialized.\n- Members specified with the `@property` decorator are not serialized.\n- When deserializing, all fields must be type-annotated.\n- A value of `None` is ignored by the serialization target.\n- When deserializing, the `__init__` function must have **NO** required arguments.\n- Implement `__serialize__` to override the serialization method.\n- Implement `__deserialize__` to override the deserialization method.\n\n## Installation\n\n```shell\npip install type-serialize\n```\n\n## Usage\n\n### Serializable python object\n\n```python\nfrom dataclasses import dataclass\nfrom type_serialize import deserialize, serialize\n\n\n@dataclass\nclass Sample:\n    field1: str\n    field2: int\n\n\ndata = Sample(field1=\"a\", field2=100)\nobj = serialize(data)\nassert isinstance(obj, dict)\nassert obj[\"field1\"] == \"a\"\nassert obj[\"field2\"] == 100\nprint(obj)\n\nresult = deserialize(obj, Sample)\nassert isinstance(result, Sample)\nassert data == result\nprint(result)\n```\n\n### Override serialize and deserialize\n\n```python\nfrom dataclasses import dataclass\nfrom type_serialize import deserialize, serialize\n\n\n@dataclass\nclass Sample:\n    value: int\n\n    def __serialize__(self):\n        return {\"a\": self.value}\n\n    def __deserialize__(self, data) -\u003e None:\n        self.value = data[\"a\"]\n\n    def __init__(self, value=100):\n        self.value = value\n\n\ntest = Sample(value=200)\nobj = serialize(test)\nassert isinstance(obj, dict)\nassert obj[\"a\"] == 200\nprint(obj)\n\nresult = deserialize(obj, Sample)\nassert isinstance(result, Sample)\nassert test == result\nprint(result)\n```\n\n## License\n\nSee the [LICENSE](./LICENSE) file for details. In summary,\n**type-serialize** is licensed under the **MIT license**.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fosom8979%2Ftype-serialize","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fosom8979%2Ftype-serialize","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fosom8979%2Ftype-serialize/lists"}