{"id":17362579,"url":"https://github.com/domdfcoding/singledispatch-json","last_synced_at":"2025-04-15T00:52:57.586Z","repository":{"id":38379079,"uuid":"256262824","full_name":"domdfcoding/singledispatch-json","owner":"domdfcoding","description":"Custom JSON Encoder for Python utilising functools.singledispatch to support custom encoders for both Python's built-in classes and user-created classes, without as much legwork.","archived":false,"fork":false,"pushed_at":"2025-04-07T17:14:55.000Z","size":440,"stargazers_count":0,"open_issues_count":6,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-15T00:52:43.947Z","etag":null,"topics":["json","python","python3","serialization","singledispatch"],"latest_commit_sha":null,"homepage":"https://singledispatch-json.readthedocs.io/en/latest","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/domdfcoding.png","metadata":{"files":{"readme":"README.rst","changelog":null,"contributing":"CONTRIBUTING.rst","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}},"created_at":"2020-04-16T15:58:28.000Z","updated_at":"2025-02-05T14:31:58.000Z","dependencies_parsed_at":"2023-02-16T10:35:25.754Z","dependency_job_id":"72796bbd-14c2-4b48-9bf3-6b0ef40ed192","html_url":"https://github.com/domdfcoding/singledispatch-json","commit_stats":{"total_commits":195,"total_committers":4,"mean_commits":48.75,"dds":"0.15384615384615385","last_synced_commit":"314796554002e8551d2a898a0237b3eb4f362798"},"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/domdfcoding%2Fsingledispatch-json","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/domdfcoding%2Fsingledispatch-json/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/domdfcoding%2Fsingledispatch-json/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/domdfcoding%2Fsingledispatch-json/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/domdfcoding","download_url":"https://codeload.github.com/domdfcoding/singledispatch-json/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248986278,"owners_count":21194025,"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":["json","python","python3","serialization","singledispatch"],"created_at":"2024-10-15T19:39:25.148Z","updated_at":"2025-04-15T00:52:57.570Z","avatar_url":"https://github.com/domdfcoding.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"=======\nsdjson\n=======\n\n.. start short_desc\n\n**Custom JSON Encoder for Python utilising functools.singledispatch to support custom encoders for both Python's built-in classes and user-created classes, without as much legwork.**\n\n.. end short_desc\n\n.. start shields\n\n.. list-table::\n\t:stub-columns: 1\n\t:widths: 10 90\n\n\t* - Docs\n\t  - |docs| |docs_check|\n\t* - Tests\n\t  - |actions_linux| |actions_windows| |actions_macos| |coveralls|\n\t* - PyPI\n\t  - |pypi-version| |supported-versions| |supported-implementations| |wheel|\n\t* - Anaconda\n\t  - |conda-version| |conda-platform|\n\t* - Activity\n\t  - |commits-latest| |commits-since| |maintained| |pypi-downloads|\n\t* - QA\n\t  - |codefactor| |actions_flake8| |actions_mypy|\n\t* - Other\n\t  - |license| |language| |requires|\n\n.. |docs| image:: https://img.shields.io/readthedocs/singledispatch-json/latest?logo=read-the-docs\n\t:target: https://singledispatch-json.readthedocs.io/en/latest\n\t:alt: Documentation Build Status\n\n.. |docs_check| image:: https://github.com/domdfcoding/singledispatch-json/workflows/Docs%20Check/badge.svg\n\t:target: https://github.com/domdfcoding/singledispatch-json/actions?query=workflow%3A%22Docs+Check%22\n\t:alt: Docs Check Status\n\n.. |actions_linux| image:: https://github.com/domdfcoding/singledispatch-json/workflows/Linux/badge.svg\n\t:target: https://github.com/domdfcoding/singledispatch-json/actions?query=workflow%3A%22Linux%22\n\t:alt: Linux Test Status\n\n.. |actions_windows| image:: https://github.com/domdfcoding/singledispatch-json/workflows/Windows/badge.svg\n\t:target: https://github.com/domdfcoding/singledispatch-json/actions?query=workflow%3A%22Windows%22\n\t:alt: Windows Test Status\n\n.. |actions_macos| image:: https://github.com/domdfcoding/singledispatch-json/workflows/macOS/badge.svg\n\t:target: https://github.com/domdfcoding/singledispatch-json/actions?query=workflow%3A%22macOS%22\n\t:alt: macOS Test Status\n\n.. |actions_flake8| image:: https://github.com/domdfcoding/singledispatch-json/workflows/Flake8/badge.svg\n\t:target: https://github.com/domdfcoding/singledispatch-json/actions?query=workflow%3A%22Flake8%22\n\t:alt: Flake8 Status\n\n.. |actions_mypy| image:: https://github.com/domdfcoding/singledispatch-json/workflows/mypy/badge.svg\n\t:target: https://github.com/domdfcoding/singledispatch-json/actions?query=workflow%3A%22mypy%22\n\t:alt: mypy status\n\n.. |requires| image:: https://dependency-dash.repo-helper.uk/github/domdfcoding/singledispatch-json/badge.svg\n\t:target: https://dependency-dash.repo-helper.uk/github/domdfcoding/singledispatch-json/\n\t:alt: Requirements Status\n\n.. |coveralls| image:: https://img.shields.io/coveralls/github/domdfcoding/singledispatch-json/master?logo=coveralls\n\t:target: https://coveralls.io/github/domdfcoding/singledispatch-json?branch=master\n\t:alt: Coverage\n\n.. |codefactor| image:: https://img.shields.io/codefactor/grade/github/domdfcoding/singledispatch-json?logo=codefactor\n\t:target: https://www.codefactor.io/repository/github/domdfcoding/singledispatch-json\n\t:alt: CodeFactor Grade\n\n.. |pypi-version| image:: https://img.shields.io/pypi/v/sdjson\n\t:target: https://pypi.org/project/sdjson/\n\t:alt: PyPI - Package Version\n\n.. |supported-versions| image:: https://img.shields.io/pypi/pyversions/sdjson?logo=python\u0026logoColor=white\n\t:target: https://pypi.org/project/sdjson/\n\t:alt: PyPI - Supported Python Versions\n\n.. |supported-implementations| image:: https://img.shields.io/pypi/implementation/sdjson\n\t:target: https://pypi.org/project/sdjson/\n\t:alt: PyPI - Supported Implementations\n\n.. |wheel| image:: https://img.shields.io/pypi/wheel/sdjson\n\t:target: https://pypi.org/project/sdjson/\n\t:alt: PyPI - Wheel\n\n.. |conda-version| image:: https://img.shields.io/conda/v/domdfcoding/sdjson?logo=anaconda\n\t:target: https://anaconda.org/domdfcoding/sdjson\n\t:alt: Conda - Package Version\n\n.. |conda-platform| image:: https://img.shields.io/conda/pn/domdfcoding/sdjson?label=conda%7Cplatform\n\t:target: https://anaconda.org/domdfcoding/sdjson\n\t:alt: Conda - Platform\n\n.. |license| image:: https://img.shields.io/github/license/domdfcoding/singledispatch-json\n\t:target: https://github.com/domdfcoding/singledispatch-json/blob/master/LICENSE\n\t:alt: License\n\n.. |language| image:: https://img.shields.io/github/languages/top/domdfcoding/singledispatch-json\n\t:alt: GitHub top language\n\n.. |commits-since| image:: https://img.shields.io/github/commits-since/domdfcoding/singledispatch-json/v0.5.0\n\t:target: https://github.com/domdfcoding/singledispatch-json/pulse\n\t:alt: GitHub commits since tagged version\n\n.. |commits-latest| image:: https://img.shields.io/github/last-commit/domdfcoding/singledispatch-json\n\t:target: https://github.com/domdfcoding/singledispatch-json/commit/master\n\t:alt: GitHub last commit\n\n.. |maintained| image:: https://img.shields.io/maintenance/yes/2025\n\t:alt: Maintenance\n\n.. |pypi-downloads| image:: https://img.shields.io/pypi/dm/sdjson\n\t:target: https://pypi.org/project/sdjson/\n\t:alt: PyPI - Downloads\n\n.. end shields\n\n|\n\nUsage\n#########\nCreating and registering a custom encoder is as easy as:\n\n\u003e\u003e\u003e import sdjson\n\u003e\u003e\u003e\n\u003e\u003e\u003e @sdjson.dump.register(MyClass)\n\u003e\u003e\u003e def encode_myclass(obj):\n...     return dict(obj)\n\u003e\u003e\u003e\n\nIn this case, ``MyClass`` can be made JSON-serializable simply by calling\n``dict()`` on it. If your class requires more complicated logic\nto make it JSON-serializable, do that here.\n\nThen, to dump the object to a string:\n\n\u003e\u003e\u003e class_instance = MyClass()\n\u003e\u003e\u003e print(sdjson.dumps(class_instance))\n'{\"menu\": [\"egg and bacon\", \"egg sausage and bacon\", \"egg and spam\", \"egg bacon and spam\"],\n\"today\\'s special\": \"Lobster Thermidor au Crevette with a Mornay sauce served in a Provencale\nmanner with shallots and aubergines garnished with truffle pate, brandy and with a fried egg\non top and spam.\"}'\n\u003e\u003e\u003e\n\nOr to dump to a file:\n\n\u003e\u003e\u003e with open(\"spam.json\", \"w\") as fp:\n...     sdjson.dumps(class_instance, fp)\n...\n\u003e\u003e\u003e\n\n``sdjson`` also provides access to ``load``, ``loads``, ``JSONDecoder``,\n``JSONDecodeError``, and ``JSONEncoder`` from the ``json`` module,\nallowing you to use ``sdjson`` as a drop-in replacement\nfor ``json``.\n\nIf you wish to dump an object without using the custom encoders, you\ncan pass a different ``JSONEncoder`` subclass, or indeed ``JSONEncoder``\nitself to get the stock functionality.\n\n\u003e\u003e\u003e sdjson.dumps(class_instance, cls=sdjson.JSONEncoder)\n\u003e\u003e\u003e\n\n|\n\nWhen you've finished, if you want to unregister the encoder you can call:\n\n\u003e\u003e\u003e sdjson.encoders.unregister(MyClass)\n\u003e\u003e\u003e\n\nto remove the encoder for ``MyClass``. If you want to replace the encoder with a\ndifferent one it is not necessary to call this function: the\n``@sdjson.encoders.register`` decorator will replace any existing decorator for\nthe given class.\n\n\nNote that this module cannot be used to create custom encoders for any object\n``json`` already knows about; that is: ``dict``, ``list``, ``tuple``, ``str``,\n``int``, ``float``, ``bool``, and ``None``.\n\nTODO\n######\n\n1. Add support for custom decoders.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdomdfcoding%2Fsingledispatch-json","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdomdfcoding%2Fsingledispatch-json","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdomdfcoding%2Fsingledispatch-json/lists"}