{"id":48811701,"url":"https://github.com/twwd/dict2dc","last_synced_at":"2026-04-14T08:01:33.476Z","repository":{"id":334251685,"uuid":"1140448851","full_name":"twwd/dict2dc","owner":"twwd","description":"A Python library with helpers to parse dicts, e.g., from JSON deserialization, to dataclasses.","archived":false,"fork":false,"pushed_at":"2026-03-27T20:45:27.000Z","size":42,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-03-29T17:24:47.432Z","etag":null,"topics":["python-dataclasses","python-dict","python-json"],"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/twwd.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2026-01-23T09:42:05.000Z","updated_at":"2026-01-26T10:09:16.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/twwd/dict2dc","commit_stats":null,"previous_names":["twwd/dict2dc"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/twwd/dict2dc","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twwd%2Fdict2dc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twwd%2Fdict2dc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twwd%2Fdict2dc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twwd%2Fdict2dc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/twwd","download_url":"https://codeload.github.com/twwd/dict2dc/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twwd%2Fdict2dc/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31787263,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-14T02:24:21.117Z","status":"ssl_error","status_checked_at":"2026-04-14T02:24:20.627Z","response_time":153,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["python-dataclasses","python-dict","python-json"],"created_at":"2026-04-14T08:01:32.577Z","updated_at":"2026-04-14T08:01:33.469Z","avatar_url":"https://github.com/twwd.png","language":"Python","readme":"# dict2dc - dictionary to dataclass parsing\n\n![Python](https://img.shields.io/pypi/pyversions/dict2dc.svg)\n[![PyPI version](https://img.shields.io/pypi/v/dict2dc.svg)](https://pypi.python.org/pypi/dict2dc)\n[![Downloads](https://pepy.tech/badge/dict2dc)](https://pepy.tech/projects/dict2dc)\n[![GitHub stars](https://img.shields.io/github/stars/twwd/dict2dc?style=flat)](https://github.com/twwd/dict2dc/stargazers)\n[![last release status](https://github.com/twwd/dict2dc/actions/workflows/publish.yaml/badge.svg)](https://github.com/twwd/dict2dc/actions/workflows/publish.yaml)\n\n`dict2dc` is a small Python library that helps to parse Python dicts to dataclass structures.\nE.g., these dicts could originate from JSON deserialization.\n\nThe library supports nested dataclasses, collections and union types.\nIt always tries to initiate the best matching class.\n\n## 🚀 Getting started\n\nInstall it in your Python project:\n\n```shell\npip install dict2dc # or uv add or poetry add...\n```\n\n## 💻 Usage Examples\n\n### Deserialization/Parsing\n\n```python\nimport datetime\nimport dataclasses\nfrom collections.abc import Collection\nfrom dict2dc.dict2dc import Dict2Dc\n\n\nclass MyDataSubClass1:\n    a: str\n\n\n@dataclasses.dataclass(frozen=True)\nclass MyDataSubClass2:\n    a: int\n\n\n@dataclasses.dataclass\nclass MyDataClass:\n    a: int\n    b: set[str]\n    c: Collection[MyDataSubClass1 | MyDataSubClass2] = dataclasses.field(default_factory=list)\n    d: datetime.datetime | None = None\n\n\ndata = {\n    \"a\": 1,\n    \"b\": [\"Hello\", \"World\"],  # JSON does not know sets thus they probably come as list\n    \"c\": [{\"a\": \"Hello\"}, {\"a\": 42}],\n    \"d\": \"2026-01-26T10:33:48.703386\",\n}  # e.g., from json.load() or response.json()\n\nDict2Dc().from_dict_enforced(data, cls=MyDataClass)\n```\n\n### Serialization\n\n#### JSON\n\n```python\nimport dataclasses\nimport json\nfrom dict2dc.dc2json import Dc2Json, DcJsonEncoder\nfrom dict2dc.models.base import UNTOUCHED, UNTOUCHED_TYPE\n\n\n@dataclasses.dataclass\nclass MyDataClass:\n    a: str\n    b: str | None = None\n    c: int | UNTOUCHED_TYPE = UNTOUCHED  # Unmodified, the key will be omitted in the serializable dict\n\n\nmy_obj: MyDataClass = MyDataClass(a=\"Hello World\")\nserializable = Dc2Json().as_serializable(my_obj)\njson.dumps(serializable)  # {\"a\": \"Hello World\", \"b\": None}\n\n# Alternative\njson.dumps(serializable, cls=DcJsonEncoder)\n```\n\n#### Query Parameters\n\n```python\nfrom dict2dc.dc2query import Dc2Query\n\nmy_obj: MyDataClass = MyDataClass(...)\nquery_params = Dc2Query().as_query_params(my_obj)\nrequests.get(\"https://example.com\", params=query_params)\n```\n\n### 🛠️ Advanced Usage\n\nThe parsing has some opinionated defaults, e.g., regarding datetime representation.\nIf you want to adjust them or add your own parsing methods,\nyou can pass a mapping from target type to method to the constructor:\n\n```python\nimport datetime\nfrom dict2dc.dict2dc import Dict2Dc\n\n\ndef convert(v: str) -\u003e datetime.datetime:\n    return datetime.datetime.fromtimestamp(float(v), tz=datetime.UTC)\n\n\ndict2dc = Dict2Dc(\n    special_conversions={\n        datetime.datetime: convert\n    }\n)\n\ndict2dc.from_dict_enforced(\n    {\n        \"created\": \"1769416853\"\n    }, cls=MyDataClass\n)\n```\n\nThe default conversions can be found in [dict2dc.py](src/dict2dc/dict2dc.py#L22).\nYou can also use the optional `replace` parameter of the constructor to omit the default conversions entirely.  \n*Note: These conversions are currently only triggered if the value to parse is a string.*\n\nTo adjust the serialization helpers (`Dc2Json`, `DcJsonEncoder`, `Dc2Query`),\nyou need to create your own subclasses.\nThere you can override the existing methods or add your own for custom conversions.","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftwwd%2Fdict2dc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftwwd%2Fdict2dc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftwwd%2Fdict2dc/lists"}