canonicaljson


Namecanonicaljson JSON
Version 2.0.0 PyPI version JSON
download
home_pagehttps://github.com/matrix-org/python-canonicaljson
SummaryCanonical JSON
upload_time2023-03-15 01:51:52
maintainer
docs_urlNone
authorMatrix.org Team and Contributors
requires_python>=3.7
licenseApache License, Version 2.0
keywords json
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            Canonical JSON
==============

.. image:: https://img.shields.io/pypi/v/canonicaljson.svg
    :target: https://pypi.python.org/pypi/canonicaljson/
    :alt: Latest Version

Features
--------

* Encodes objects and arrays as `RFC 7159`_ JSON.
* Sorts object keys so that you get the same result each time.
* Has no insignificant whitespace to make the output as small as possible.
* Escapes only the characters that must be escaped, U+0000 to U+0019 / U+0022 /
  U+0056, to keep the output as small as possible.
* Uses the shortest escape sequence for each escaped character.
* Encodes the JSON as UTF-8.
* Can be configured to encode custom types unknown to the stdlib JSON encoder.

Supports Python versions 3.7 and newer.

.. _`RFC 7159`: https://tools.ietf.org/html/rfc7159

Installing
----------

.. code:: bash

   pip install canonicaljson

Using
-----

To encode an object into the canonicaljson:

.. code:: python

    import canonicaljson
    assert canonicaljson.encode_canonical_json({}) == b'{}'

There's also an iterator version:

.. code:: python

    import canonicaljson
    assert b''.join(canonicaljson.iterencode_canonical_json({})) == b'{}'

The underlying JSON implementation can be chosen with the following:

.. code:: python

    import json
    import canonicaljson
    canonicaljson.set_json_library(json)

.. note::

    By default canonicaljson uses `simplejson`_ under the hood (except for PyPy,
    which uses the standard library json module).

.. _simplejson: https://simplejson.readthedocs.io/

A preserialisation hook allows you to encode objects which aren't encodable by the
standard library ``JSONEncoder``.

.. code:: python

    import canonicaljson
    from typing import Dict

    class CustomType:
        pass

    def callback(c: CustomType) -> Dict[str, str]:
        return {"Hello": "world!"}

    canonicaljson.register_preserialisation_callback(CustomType, callback)
    assert canonicaljson.encode_canonical_json(CustomType()) == b'{"Hello":"world!"}'

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/matrix-org/python-canonicaljson",
    "name": "canonicaljson",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": "",
    "keywords": "json",
    "author": "Matrix.org Team and Contributors",
    "author_email": "packages@matrix.org",
    "download_url": "https://files.pythonhosted.org/packages/a8/f2/2835b7ab464988d1f85e351a19c4d6b2e4c317ba8484ebd2a311850eab8c/canonicaljson-2.0.0.tar.gz",
    "platform": null,
    "description": "Canonical JSON\n==============\n\n.. image:: https://img.shields.io/pypi/v/canonicaljson.svg\n    :target: https://pypi.python.org/pypi/canonicaljson/\n    :alt: Latest Version\n\nFeatures\n--------\n\n* Encodes objects and arrays as `RFC 7159`_ JSON.\n* Sorts object keys so that you get the same result each time.\n* Has no insignificant whitespace to make the output as small as possible.\n* Escapes only the characters that must be escaped, U+0000 to U+0019 / U+0022 /\n  U+0056, to keep the output as small as possible.\n* Uses the shortest escape sequence for each escaped character.\n* Encodes the JSON as UTF-8.\n* Can be configured to encode custom types unknown to the stdlib JSON encoder.\n\nSupports Python versions 3.7 and newer.\n\n.. _`RFC 7159`: https://tools.ietf.org/html/rfc7159\n\nInstalling\n----------\n\n.. code:: bash\n\n   pip install canonicaljson\n\nUsing\n-----\n\nTo encode an object into the canonicaljson:\n\n.. code:: python\n\n    import canonicaljson\n    assert canonicaljson.encode_canonical_json({}) == b'{}'\n\nThere's also an iterator version:\n\n.. code:: python\n\n    import canonicaljson\n    assert b''.join(canonicaljson.iterencode_canonical_json({})) == b'{}'\n\nThe underlying JSON implementation can be chosen with the following:\n\n.. code:: python\n\n    import json\n    import canonicaljson\n    canonicaljson.set_json_library(json)\n\n.. note::\n\n    By default canonicaljson uses `simplejson`_ under the hood (except for PyPy,\n    which uses the standard library json module).\n\n.. _simplejson: https://simplejson.readthedocs.io/\n\nA preserialisation hook allows you to encode objects which aren't encodable by the\nstandard library ``JSONEncoder``.\n\n.. code:: python\n\n    import canonicaljson\n    from typing import Dict\n\n    class CustomType:\n        pass\n\n    def callback(c: CustomType) -> Dict[str, str]:\n        return {\"Hello\": \"world!\"}\n\n    canonicaljson.register_preserialisation_callback(CustomType, callback)\n    assert canonicaljson.encode_canonical_json(CustomType()) == b'{\"Hello\":\"world!\"}'\n",
    "bugtrack_url": null,
    "license": "Apache License, Version 2.0",
    "summary": "Canonical JSON",
    "version": "2.0.0",
    "split_keywords": [
        "json"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e554346f681c24a9c3a08e2e74dcee2555ccd1081705b46f791f7b228e177d06",
                "md5": "1179236239f9d7350bc59f9d027d2e6d",
                "sha256": "c38a315de3b5a0532f1ec1f9153cd3d716abfc565a558d00a4835428a34fca5b"
            },
            "downloads": -1,
            "filename": "canonicaljson-2.0.0-py3-none-any.whl",
            "has_sig": true,
            "md5_digest": "1179236239f9d7350bc59f9d027d2e6d",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 7921,
            "upload_time": "2023-03-15T01:51:50",
            "upload_time_iso_8601": "2023-03-15T01:51:50.931773Z",
            "url": "https://files.pythonhosted.org/packages/e5/54/346f681c24a9c3a08e2e74dcee2555ccd1081705b46f791f7b228e177d06/canonicaljson-2.0.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "a8f22835b7ab464988d1f85e351a19c4d6b2e4c317ba8484ebd2a311850eab8c",
                "md5": "ea60a3f3c99e3382c62606f86c08caae",
                "sha256": "e2fdaef1d7fadc5d9cb59bd3d0d41b064ddda697809ac4325dced721d12f113f"
            },
            "downloads": -1,
            "filename": "canonicaljson-2.0.0.tar.gz",
            "has_sig": true,
            "md5_digest": "ea60a3f3c99e3382c62606f86c08caae",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 10716,
            "upload_time": "2023-03-15T01:51:52",
            "upload_time_iso_8601": "2023-03-15T01:51:52.883625Z",
            "url": "https://files.pythonhosted.org/packages/a8/f2/2835b7ab464988d1f85e351a19c4d6b2e4c317ba8484ebd2a311850eab8c/canonicaljson-2.0.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-03-15 01:51:52",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "matrix-org",
    "github_project": "python-canonicaljson",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "canonicaljson"
}
        
Elapsed time: 0.05970s