=======
sdjson
=======
.. start short_desc
**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.**
.. end short_desc
.. start shields
.. list-table::
:stub-columns: 1
:widths: 10 90
* - Docs
- |docs| |docs_check|
* - Tests
- |actions_linux| |actions_windows| |actions_macos| |coveralls|
* - PyPI
- |pypi-version| |supported-versions| |supported-implementations| |wheel|
* - Anaconda
- |conda-version| |conda-platform|
* - Activity
- |commits-latest| |commits-since| |maintained| |pypi-downloads|
* - QA
- |codefactor| |actions_flake8| |actions_mypy|
* - Other
- |license| |language| |requires|
.. |docs| image:: https://img.shields.io/readthedocs/singledispatch-json/latest?logo=read-the-docs
:target: https://singledispatch-json.readthedocs.io/en/latest
:alt: Documentation Build Status
.. |docs_check| image:: https://github.com/domdfcoding/singledispatch-json/workflows/Docs%20Check/badge.svg
:target: https://github.com/domdfcoding/singledispatch-json/actions?query=workflow%3A%22Docs+Check%22
:alt: Docs Check Status
.. |actions_linux| image:: https://github.com/domdfcoding/singledispatch-json/workflows/Linux/badge.svg
:target: https://github.com/domdfcoding/singledispatch-json/actions?query=workflow%3A%22Linux%22
:alt: Linux Test Status
.. |actions_windows| image:: https://github.com/domdfcoding/singledispatch-json/workflows/Windows/badge.svg
:target: https://github.com/domdfcoding/singledispatch-json/actions?query=workflow%3A%22Windows%22
:alt: Windows Test Status
.. |actions_macos| image:: https://github.com/domdfcoding/singledispatch-json/workflows/macOS/badge.svg
:target: https://github.com/domdfcoding/singledispatch-json/actions?query=workflow%3A%22macOS%22
:alt: macOS Test Status
.. |actions_flake8| image:: https://github.com/domdfcoding/singledispatch-json/workflows/Flake8/badge.svg
:target: https://github.com/domdfcoding/singledispatch-json/actions?query=workflow%3A%22Flake8%22
:alt: Flake8 Status
.. |actions_mypy| image:: https://github.com/domdfcoding/singledispatch-json/workflows/mypy/badge.svg
:target: https://github.com/domdfcoding/singledispatch-json/actions?query=workflow%3A%22mypy%22
:alt: mypy status
.. |requires| image:: https://dependency-dash.repo-helper.uk/github/domdfcoding/singledispatch-json/badge.svg
:target: https://dependency-dash.repo-helper.uk/github/domdfcoding/singledispatch-json/
:alt: Requirements Status
.. |coveralls| image:: https://img.shields.io/coveralls/github/domdfcoding/singledispatch-json/master?logo=coveralls
:target: https://coveralls.io/github/domdfcoding/singledispatch-json?branch=master
:alt: Coverage
.. |codefactor| image:: https://img.shields.io/codefactor/grade/github/domdfcoding/singledispatch-json?logo=codefactor
:target: https://www.codefactor.io/repository/github/domdfcoding/singledispatch-json
:alt: CodeFactor Grade
.. |pypi-version| image:: https://img.shields.io/pypi/v/sdjson
:target: https://pypi.org/project/sdjson/
:alt: PyPI - Package Version
.. |supported-versions| image:: https://img.shields.io/pypi/pyversions/sdjson?logo=python&logoColor=white
:target: https://pypi.org/project/sdjson/
:alt: PyPI - Supported Python Versions
.. |supported-implementations| image:: https://img.shields.io/pypi/implementation/sdjson
:target: https://pypi.org/project/sdjson/
:alt: PyPI - Supported Implementations
.. |wheel| image:: https://img.shields.io/pypi/wheel/sdjson
:target: https://pypi.org/project/sdjson/
:alt: PyPI - Wheel
.. |conda-version| image:: https://img.shields.io/conda/v/domdfcoding/sdjson?logo=anaconda
:target: https://anaconda.org/domdfcoding/sdjson
:alt: Conda - Package Version
.. |conda-platform| image:: https://img.shields.io/conda/pn/domdfcoding/sdjson?label=conda%7Cplatform
:target: https://anaconda.org/domdfcoding/sdjson
:alt: Conda - Platform
.. |license| image:: https://img.shields.io/github/license/domdfcoding/singledispatch-json
:target: https://github.com/domdfcoding/singledispatch-json/blob/master/LICENSE
:alt: License
.. |language| image:: https://img.shields.io/github/languages/top/domdfcoding/singledispatch-json
:alt: GitHub top language
.. |commits-since| image:: https://img.shields.io/github/commits-since/domdfcoding/singledispatch-json/v0.5.0
:target: https://github.com/domdfcoding/singledispatch-json/pulse
:alt: GitHub commits since tagged version
.. |commits-latest| image:: https://img.shields.io/github/last-commit/domdfcoding/singledispatch-json
:target: https://github.com/domdfcoding/singledispatch-json/commit/master
:alt: GitHub last commit
.. |maintained| image:: https://img.shields.io/maintenance/yes/2024
:alt: Maintenance
.. |pypi-downloads| image:: https://img.shields.io/pypi/dm/sdjson
:target: https://pypi.org/project/sdjson/
:alt: PyPI - Downloads
.. end shields
|
Usage
#########
Creating and registering a custom encoder is as easy as:
>>> import sdjson
>>>
>>> @sdjson.dump.register(MyClass)
>>> def encode_myclass(obj):
... return dict(obj)
>>>
In this case, ``MyClass`` can be made JSON-serializable simply by calling
``dict()`` on it. If your class requires more complicated logic
to make it JSON-serializable, do that here.
Then, to dump the object to a string:
>>> class_instance = MyClass()
>>> print(sdjson.dumps(class_instance))
'{"menu": ["egg and bacon", "egg sausage and bacon", "egg and spam", "egg bacon and spam"],
"today\'s special": "Lobster Thermidor au Crevette with a Mornay sauce served in a Provencale
manner with shallots and aubergines garnished with truffle pate, brandy and with a fried egg
on top and spam."}'
>>>
Or to dump to a file:
>>> with open("spam.json", "w") as fp:
... sdjson.dumps(class_instance, fp)
...
>>>
``sdjson`` also provides access to ``load``, ``loads``, ``JSONDecoder``,
``JSONDecodeError``, and ``JSONEncoder`` from the ``json`` module,
allowing you to use ``sdjson`` as a drop-in replacement
for ``json``.
If you wish to dump an object without using the custom encoders, you
can pass a different ``JSONEncoder`` subclass, or indeed ``JSONEncoder``
itself to get the stock functionality.
>>> sdjson.dumps(class_instance, cls=sdjson.JSONEncoder)
>>>
|
When you've finished, if you want to unregister the encoder you can call:
>>> sdjson.encoders.unregister(MyClass)
>>>
to remove the encoder for ``MyClass``. If you want to replace the encoder with a
different one it is not necessary to call this function: the
``@sdjson.encoders.register`` decorator will replace any existing decorator for
the given class.
Note that this module cannot be used to create custom encoders for any object
``json`` already knows about; that is: ``dict``, ``list``, ``tuple``, ``str``,
``int``, ``float``, ``bool``, and ``None``.
TODO
######
1. Add support for custom decoders.
Raw data
{
"_id": null,
"home_page": "https://github.com/domdfcoding/singledispatch-json",
"name": "sdjson",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.6",
"maintainer_email": null,
"keywords": "json, serialize, singledispatch",
"author": null,
"author_email": "Dominic Davis-Foster <dominic@davis-foster.co.uk>",
"download_url": "https://files.pythonhosted.org/packages/1f/37/e4688b0d818e43809c8587aff64c1c940310a69eb7d6164dc604d5349ddf/sdjson-0.5.0.tar.gz",
"platform": "Windows",
"description": "\n=======\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&logoColor=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/2024\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>>> import sdjson\n>>>\n>>> @sdjson.dump.register(MyClass)\n>>> def encode_myclass(obj):\n... return dict(obj)\n>>>\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>>> class_instance = MyClass()\n>>> 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>>>\n\nOr to dump to a file:\n\n>>> with open(\"spam.json\", \"w\") as fp:\n... sdjson.dumps(class_instance, fp)\n...\n>>>\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>>> sdjson.dumps(class_instance, cls=sdjson.JSONEncoder)\n>>>\n\n|\n\nWhen you've finished, if you want to unregister the encoder you can call:\n\n>>> sdjson.encoders.unregister(MyClass)\n>>>\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",
"bugtrack_url": null,
"license": "MIT",
"summary": "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.",
"version": "0.5.0",
"project_urls": {
"Documentation": "https://singledispatch-json.readthedocs.io/en/latest",
"Homepage": "https://github.com/domdfcoding/singledispatch-json",
"Issue Tracker": "https://github.com/domdfcoding/singledispatch-json/issues",
"Source Code": "https://github.com/domdfcoding/singledispatch-json"
},
"split_keywords": [
"json",
" serialize",
" singledispatch"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "d40a086439278bf5aac9c9706bdaba4e1a68cd903b90da479c0aa421eb1c92a7",
"md5": "fc34909bbea27502cf3217cbbad8fc48",
"sha256": "9e31c8075ec65671668dfaa065267fe565790aeee5d31f44b9c1408c57d8054c"
},
"downloads": -1,
"filename": "sdjson-0.5.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "fc34909bbea27502cf3217cbbad8fc48",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.6",
"size": 11369,
"upload_time": "2024-07-15T15:57:29",
"upload_time_iso_8601": "2024-07-15T15:57:29.510341Z",
"url": "https://files.pythonhosted.org/packages/d4/0a/086439278bf5aac9c9706bdaba4e1a68cd903b90da479c0aa421eb1c92a7/sdjson-0.5.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "1f37e4688b0d818e43809c8587aff64c1c940310a69eb7d6164dc604d5349ddf",
"md5": "265a87757c20357f3b5e45c17c32397e",
"sha256": "1cb9fc9316b2fab479575f9a3102190da49f151be25a87e296a2316707b9e46e"
},
"downloads": -1,
"filename": "sdjson-0.5.0.tar.gz",
"has_sig": false,
"md5_digest": "265a87757c20357f3b5e45c17c32397e",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.6",
"size": 9039,
"upload_time": "2024-07-15T15:57:34",
"upload_time_iso_8601": "2024-07-15T15:57:34.430775Z",
"url": "https://files.pythonhosted.org/packages/1f/37/e4688b0d818e43809c8587aff64c1c940310a69eb7d6164dc604d5349ddf/sdjson-0.5.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-07-15 15:57:34",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "domdfcoding",
"github_project": "singledispatch-json",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [
{
"name": "domdf-python-tools",
"specs": [
[
">=",
"2.5.2"
]
]
},
{
"name": "typing-extensions",
"specs": [
[
">=",
"3.7.4.3"
]
]
}
],
"tox": true,
"lcname": "sdjson"
}