Metadata-Version: 2.1
Name: sdjson
Version: 0.4.0
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.
Author-email: Dominic Davis-Foster <dominic@davis-foster.co.uk>
License: MIT
Keywords: json,serialize,singledispatch
Home-page: https://github.com/domdfcoding/singledispatch-json
Project-URL: Issue Tracker, https://github.com/domdfcoding/singledispatch-json/issues
Project-URL: Source Code, https://github.com/domdfcoding/singledispatch-json
Project-URL: Documentation, https://singledispatch-json.readthedocs.io/en/latest
Platform: Windows
Platform: macOS
Platform: Linux
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.6
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: Implementation :: CPython
Classifier: Programming Language :: Python :: Implementation :: PyPy
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Topic :: Utilities
Classifier: Typing :: Typed
Requires-Python: >=3.6
Requires-Dist: domdf-python-tools>=2.5.2
Requires-Dist: typing-extensions>=3.7.4.3
Description-Content-Type: text/x-rst


=======
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.
