sdjson


Namesdjson JSON
Version 0.4.0 PyPI version JSON
download
home_pagehttps://github.com/domdfcoding/singledispatch-json
SummaryCustom 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.
upload_time2022-05-01 10:12:12
maintainer
docs_urlNone
author
requires_python>=3.6
licenseMIT
keywords json serialize singledispatch
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            
=======
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.herokuapp.com/github/domdfcoding/singledispatch-json/badge.svg
	:target: https://dependency-dash.herokuapp.com/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.4.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/2022
	: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": "",
    "docs_url": null,
    "requires_python": ">=3.6",
    "maintainer_email": "",
    "keywords": "json,serialize,singledispatch",
    "author": "",
    "author_email": "Dominic Davis-Foster <dominic@davis-foster.co.uk>",
    "download_url": "https://files.pythonhosted.org/packages/60/9d/f651c703fd5d8409af12c552752c28cf771261ac7f3c5ab6fb3b4d049e98/sdjson-0.4.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.herokuapp.com/github/domdfcoding/singledispatch-json/badge.svg\n\t:target: https://dependency-dash.herokuapp.com/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.4.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/2022\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.4.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": "f0ceb350ece5516d17236b811feab559caa0de5b695af9c8acdc1bc510f94c6b",
                "md5": "525315f4e8fd0ed86aaa1bf796380c62",
                "sha256": "679fc0a6c5e6abb76bf6ef9733d2c286f02d960b42e6e9782263b0d565ad5c88"
            },
            "downloads": -1,
            "filename": "sdjson-0.4.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "525315f4e8fd0ed86aaa1bf796380c62",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.6",
            "size": 30594,
            "upload_time": "2022-05-01T10:12:10",
            "upload_time_iso_8601": "2022-05-01T10:12:10.613134Z",
            "url": "https://files.pythonhosted.org/packages/f0/ce/b350ece5516d17236b811feab559caa0de5b695af9c8acdc1bc510f94c6b/sdjson-0.4.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "609df651c703fd5d8409af12c552752c28cf771261ac7f3c5ab6fb3b4d049e98",
                "md5": "947ee59b174aa0fa77b1a9dd022c140f",
                "sha256": "d15e54d3727ee6b175014494a31a2bd6012da3cd3083850ba9cfb58aef0c4f3b"
            },
            "downloads": -1,
            "filename": "sdjson-0.4.0.tar.gz",
            "has_sig": false,
            "md5_digest": "947ee59b174aa0fa77b1a9dd022c140f",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.6",
            "size": 8940,
            "upload_time": "2022-05-01T10:12:12",
            "upload_time_iso_8601": "2022-05-01T10:12:12.268065Z",
            "url": "https://files.pythonhosted.org/packages/60/9d/f651c703fd5d8409af12c552752c28cf771261ac7f3c5ab6fb3b4d049e98/sdjson-0.4.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2022-05-01 10:12:12",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "domdfcoding",
    "github_project": "singledispatch-json",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "tox": true,
    "lcname": "sdjson"
}
        
Elapsed time: 0.13248s