{ "info": { "author": "sg495", "author_email": "sg495@users.noreply.github.com", "bugtrack_url": null, "classifiers": [ "Development Status :: 3 - Alpha", "License :: OSI Approved :: MIT License", "Natural Language :: English", "Operating System :: OS Independent", "Programming Language :: Python :: 3.7", "Typing :: Typed" ], "description": "# typing-json\n[![Build Status](https://api.travis-ci.com/sg495/typing-json.svg?branch=master)](https://travis-ci.com/sg495/typing-json)\n[![codecov](https://codecov.io/gh/sg495/typing-json/graph/badge.svg)](https://codecov.io/gh/sg495/typing-json/)\n[![Generic badge](https://img.shields.io/badge/python-3.7.4+-green.svg)](https://shields.io/)\n[![Checked with mypy](http://www.mypy-lang.org/static/mypy_badge.svg)](http://mypy-lang.org/)\n[![PyPI version shields.io](https://img.shields.io/pypi/v/typing-json.svg)](https://pypi.python.org/pypi/typing-json/)\n[![PyPI status](https://img.shields.io/pypi/status/typing-json.svg)](https://pypi.python.org/pypi/typing-json/)\n[![Generic badge](https://img.shields.io/badge/license-MIT-green.svg)](https://choosealicense.com/licenses/mit/)\n\nAdd typing support to python JSON serialization.\n\n## typechecking\n\nThe typechecking module contains utilities for dynamic typechecking which support relevant types from the typing and typing_extensions libraries.\n\n### is_hashable\n\n```python\ndef is_hashable(t: Any, failure_callback: Optional[Callable[[str], None]] = None) -> bool:\n ...\n````\n\nThe function `is_hashable(t)` returns `True` if and only if the `t` argument is one of the types considered to be hashable for the purposes of dynamic typechecking in this package. Types currently deemed to be hashable by this function:\n\n- the standard types `bool`, `int`, `float`, `complex`, `str`, `bytes`, `range`, `slice`, `type` and `NoneType` (as well as the value `None`, treated equivalently to `NoneType`);\n- the `typing` types `Tuple[_T1,...]` (single-type variadic tuple), `Tuple[_T1,...,_TN]` (multi-type fixed arity tuple), `FrozenSet[_T]`, `Optional[_T]`, `Union[_T1,...,_TN]` where `_T`, `_S`, `_T1`, ..., `_TN` are themselves hashable types;\n- the `typing_extensions` type `Literal[_v1, ..., _vn]` where `_v1`, ..., `_vn` are of hashable standard type (see above);\n- types created using `typing.NamedTuple` using hashable field types;\n\nSupport for enum types and NewType is planned (see pull request [#5](https://github.com/sg495/typing-json/pull/5)).\n\nIf present, `failure_callback` is called to log all reasons why `t` fails to be hashable, in the order in which they arise.\n\n\n\n### is_typecheckable\n\n```python\ndef is_typecheckable(t: Any, failure_callback: Optional[Callable[[str], None]] = None) -> bool:\n ...\n````\n\nThe function `is_typecheckable(t)` returns `True` if and only if the `t` argument is one of the types supported for dynamic typechecking using the `is_instance(obj, t)` function from the same module. Currently supported types:\n\n- the standard types `bool`, `int`, `float`, `complex`, `str`, `bytes`, `bytearray`, `memoryview`, `list`, `tuple`, `range`, `slice`, `set`, `frozenset`, `dict`, `type`, `NoneType` and `object` (as well as the value `None`, treated equivalently to `NoneType`);\n- the `collections` types `deque` and `OrderedDict`;\n- the `typing` types `Any`, `List[_T]`, `Tuple[_T1,...]` (single-type variadic tuple), `Tuple[_T1,...,_TN]` (multi-type fixed arity tuple), `Set[_T]`, `FrozenSet[_T]`, `Dict[_T, _S]`, `OrderedDict[_T, _S]`, `Mapping[_T, _S]`, `Deque[_T]`, `Optional[_T]`, `Union[_T1,...,_TN]` where `_T`, `_S`, `_T1`, ..., `_TN` are themselves supported types;\n- the `typing_extensions` type `Literal[_v1, ..., _vn]` where `_v1`, ..., `_vn` are of typecheckable standard of `collections` type (see above);\n- types created using `typing.NamedTuple` using typecheckable field types;\n\nArbitrary classes are currently not supported, regardless of type annotations. Support for types created using `collections.namedtuple` is not planned. Support for enum types and NewType is planned (see pull request [#5](https://github.com/sg495/typing-json/pull/5)).\n\nIf present, `failure_callback` is called to log all reasons why `t` fails to be typecheckable, in the order in which they arise.\n\n\n\n### is_instance\n\n```python\ndef is_instance(obj: Any, t: Any, failure_callback: Optional[Callable[[str], None]] = None) -> bool:\n ...\n````\n\nThe function `is_instance(obj, t)` returns `True` if and only if the `obj` argument is of type `t`. If `t` is not typecheckable according to `is_typecheckable(t)` then `TypeError` is raised.\n\nIf present, `failure_callback` is called to log all reasons why `obj` fails to be an instance of `t`, in the order in which they arise.\n\n\n\n### is_namedtuple\n\n```python\ndef is_namedtuple(t: Any) -> bool:\n ...\n```\n\nThe function `is_namedtuple(t)` returns `True` if the `obj` argument was created using `typing.NamedTuple` and all field types are typecheckable. It is currently possible to fool this method by using `collections.namedtuple` and manually adding a `_field_types:` dictionary with string keys and typecheckable types.\n\nIf present, `failure_callback` is called to log all reasons why `t` fails to be a NamedTuple, in the order in which they arise.\n\n\n## encoding\n\n### is_json_encodable\n\n```python\ndef is_json_encodable(t: Any, failure_callback: Optional[Callable[[str], None]] = None) -> bool:\n ...\n````\n\nThe function `is_json_encodable(t)` returns `True` if and only if `t` is a json-encodable type according to this package. At present, the following are json-encodable types:\n\n- the standard types `bool`, `int`, `float`, `str`, and `NoneType` (as well as the value `None`, treated equivalently to `NoneType`);\n- any `t` such that `is_namedtuple(t)` and such that all field types are json-encodable themselves;\n- the `typing` types `List[_T]`, `Set[_T]`, `FrozenSet[_T]`, `Deque[_T]`, `Tuple[_T,...]`, `Tuple[_T1,...,_TN]`, `Dict[str, _T]`, `OrderedDict[str, _T]`, `Mapping[str, _T]`, `Union[_T1,...,_TN]`, `Optional[_T]`where `_T`, `_T1`, ..., `_TN` are themselves json-encodable types;\n- the `typing_extensions` type `Literal[_v1,...,_vn]`, where where each `_vj in [_v1,...,_vn]` is of type `bool`, `int`, `float`, `str` or `NoneType`.\n\nFuture support is planned for more `typing` and `typing_extensions` types, including enum types and NewType (see pull request [#5](https://github.com/sg495/typing-json/pull/5)).\n\nIf present, `failure_callback` is called to log all reasons why `t` fails to be json-encodable, in the order in which they arise.\n\n\n### to_json_obj\n\n```python\ndef to_json_obj(obj: Any, t: Any) -> Any:\n ...\n````\n\nThe function `to_json_obj(obj, t)` takes an object `obj` and a json encodable type `t` and converts `obj` into a natively--json-compatible object with the same fields and types. The conversion goes as follows:\n\n- if `t in (bool, int, float, str)`, `obj` is returned unaltered;\n- if `t in (None, NoneType, ...)`, `None` is returned;\n- if `is_namedtuple(t)`, a `collections.OrderedDict` is returned with the fields of the named tuple as keys and respective values recursively converted to natively--json-compatible;\n- if `t` is `Union[_T1,...,_TN]`, `obj` is converted to natively--json-compatible type according to the first type `_Tj` in the sequence `_T1`,...,`_TN` such that `is_instance(obj, _Tj)`;\n- the type `Optional[_T]` is treated as `Union[_T, NoneType]`;\n- if `t` is `Literal[_v1,...,_vN]`, `obj` is returned unaltered;\n- if `t` is one of `List[_T]`, `Set[_T]`, `FrozenSet[_T]`, `Deque[_T]`, `Tuple[_T,...]` a list is returned, containing all elements of `obj` recursively converted to natively--json-compatible objects using type `_T` for the conversion;\n- if `t` is `Tuple[_T1,...,_TN]`, a list is returned, containing all elements of `obj` recursively converted to natively--json-compatible objects using types `_T1`,...,`_TN` for the conversion of the elements `x1`,...,`xN` of `obj`;\n- if `t` is `Dict[_S, _T]` or `Mapping[_S, _T]`, a dictionary is returned with keys and values recursively converted to natively--json-compatible type according to types `_S` and `_T` respectively, and the string version of the keys json is used if they are not one of `bool`, `int`, `float`, `str` or `NoneType`;\n- if `t` is `OrderedDict[_S, _T]`, an dictionary is returned with keys and values recursively converted to natively--json-compatible type according to types `_S` and `_T` respectively, and the string version of the keys json is used if they are not one of `bool`, `int`, `float`, `str` or `NoneType`;\n\nIf `t` is not json-encodable according to `is_json_encodable(t)` then `TypeError` is raised. If `obj` is not of type `t` according to `is_instance(obj, t)` then `TypeError` is raised.\n\nFor the purposes of this library, natively--json-compatible types are: `bool`, `int`, `float`, `str`, `NoneType`, `list`, `dict` and `collections.OrderedDict`.\n\n\n### dump and dumps\n\n```python\ndef dump(obj: Any, encoded_type: Any, ...):\n ...\n\ndef dumps(obj: Any, encoded_type: Any, ...):\n ...\n````\n\nThe functions `dump` and `dumps` of `typing_json` mimic the functions `dump` and `dumps` of the standard `json` library, first performing a conversion of `obj` from json-encodable type `encoded_type` to a json object before dumping.\n\n\n## decoding\n\n### from_json_obj\n\n```python\ndef from_json_obj(obj: Any, t: Any) -> Any:\n ...\n````\n\nThe function `to_json_obj(obj, t)` takes an object `obj` and a json encodable type `t` and converts `obj` into an equivalent object of type `t`. The conversion goes as follows:\n\n- if `t in (bool, int, float, str)`, `obj` is returned unaltered (`TypeError` is raised if `not isinstance(obj, t)`);\n- if `t in (None, NoneType)`, `None` is returned (`TypeError` is raised if `obj is not None`);\n- if `t is ...`, `...` is returned (`TypeError` is raised if `obj is not None`);\n- if `is_namedtuple(t)`, the key values of the (ordered) dictionary `obj` are recursively converted to the types of the fields of `t` with the same keys and any missing key is replaced with it default value in `t`, if present (`TypeError` is raised if `obj` is not a (ordered) dictionary, if `obj` contains keys which are not fields of `t`, or if a non-default field of `t` does not appear as a key in `obj`);\n- if `t` is `Union[_T1,..._TN]` then conversion of `obj` to each type `_Tj` listed in the union is attempted in order and the first successful result is returned (`TypeError` is raised if no conversion is successful);\n- if `t` is `Literal` then `obj` is returned unaltered (`TypeError` is raised if `not is_instance(obj, t)`);\n- if `t` is `List[_T]` then all members of `obj` are recursively converted to type `_T` and a list of the results is returned (`TypeError` is raised if `obj` is not a list);\n- if `t` is `Deque[_T]` then all members of `obj` are recursively converted to type `_T` and a deque of the results is returned (`TypeError` is raised if `obj` is not a list);\n- if `t` is `Set[_T]` then all members of `obj` are recursively converted to type `_T` and a set of the results is returned (`TypeError` is raised if `obj` is not a list, order preservation is not guaranteed);\n- if `t` is `FrozenSet[_T]` then all members of `obj` are recursively converted to type `_T` and a frozenset of the results is returned (`TypeError` is raised if `obj` is not a list, order preservation is not guaranteed);\n- if `t` is `Tuple[_T,...]` then all members of `obj` are recursively converted to type `_T` and a tuple of the results is returned (`TypeError` is raised if `obj` is not a list);\n- if `t` is `Tuple[_TN,...,_TN]` then all members of `obj` are recursively converted to the respective types `_Tj` and a tuple of the results is returned (`TypeError` is raised if `obj` is not a list or if the length does not match the required length for the tuple);\n- if `t` is `Dict[_S, _T]` or `Mapping[_S, _T]` then all keys and values of `obj` are recursively converted to types `_S` and `_T` respectively (if `_S` is not one of `bool`, `int`, `float`, `str`, `NoneType` or `None`, the keys are first json-parsed from strings), and a dict of the results is returned (`TypeError` is raised if `obj` is not a (ordered) dictionary);\n- if `t` is `OrderedDict[_S, _T]` then all keys and values of `obj` are recursively converted to types `_S` and `_T` respectively (if `_S` is not one of `bool`, `int`, `float`, `str`, `NoneType` or `None`, the keys are first json-parsed from strings), and an ordered dict of the results is returned (`TypeError` is raised if `obj` is not an ordered dictionary);\n\nIf `t` is not json-encodable according to `is_json_encodable(t)` then `TypeError` is raised.\n\n\n### load and loads\n\n```python\ndef load(fp, decoded_type: Any, ...) -> Any:\n ...\n\ndef dumps(s: str, decoded_type: Any, ...) -> Any:\n ...\n````\n\nThe functions `load` and `loads` of `typing_json` mimic the functions `load` and `loads` of the standard `json` library, performing a conversion of the loaded json object to json-encodable type `decoded_type` before returning.\n\n\n", "description_content_type": "text/markdown", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/sg495/typing-json", "keywords": "", "license": "", "maintainer": "", "maintainer_email": "", "name": "typing-json", "package_url": "https://pypi.org/project/typing-json/", "platform": "", "project_url": "https://pypi.org/project/typing-json/", "project_urls": { "Homepage": "https://github.com/sg495/typing-json" }, "release_url": "https://pypi.org/project/typing-json/0.0.7/", "requires_dist": null, "requires_python": "", "summary": "Add typing support to python JSON serialization.", "version": "0.0.7" }, "last_serial": 5808326, "releases": { "0.0.1": [ { "comment_text": "", "digests": { "md5": "f5969a71a17c5021d599938866048f51", "sha256": "40a1d9bd0ccbf956b7f6e4606b456d29cfd869829c1e440cfc13ad2249a0b973" }, "downloads": -1, "filename": "typing_json-0.0.1-py3-none-any.whl", "has_sig": false, "md5_digest": "f5969a71a17c5021d599938866048f51", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 8604, "upload_time": "2019-09-02T12:20:25", "url": "https://files.pythonhosted.org/packages/30/14/c972d69fe1f2c30f43cb0d88ee9841766e73848f42fe4cac4b9a48460e3a/typing_json-0.0.1-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "577247c988f6034ec023eb25af755c40", "sha256": "5bdd8c4e5f602e21844c4d22dbd9d3e656b57b66b767687aa06e7f8f0dbe7a47" }, "downloads": -1, "filename": "typing-json-0.0.1.tar.gz", "has_sig": false, "md5_digest": "577247c988f6034ec023eb25af755c40", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 11563, "upload_time": "2019-09-02T12:20:28", "url": "https://files.pythonhosted.org/packages/a9/8a/bd7eb436cc66dd9f8e7ddb1714ff7231765d46631c0775a1c44de1f682a0/typing-json-0.0.1.tar.gz" } ], "0.0.4": [ { "comment_text": "", "digests": { "md5": "0f5a527e746940c41d3b115f2c1a4f2c", "sha256": "72ea7b3ee38bd7da01d6c4977338e1eb71555e2cd891afc004c042cef1df1bae" }, "downloads": -1, "filename": "typing_json-0.0.4-py3-none-any.whl", "has_sig": false, "md5_digest": "0f5a527e746940c41d3b115f2c1a4f2c", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 8768, "upload_time": "2019-09-02T17:15:24", "url": "https://files.pythonhosted.org/packages/ac/82/2743b96c8f98be7c297f597ff97e95e76f16539d5b3180cbc37e77e9d055/typing_json-0.0.4-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "c28f041b0f8268123bbe1ab5190d1209", "sha256": "2062723e4f9268d343817899745f302fc5524d1e457c9cc16c3c727fb652adfd" }, "downloads": -1, "filename": "typing-json-0.0.4.tar.gz", "has_sig": false, "md5_digest": "c28f041b0f8268123bbe1ab5190d1209", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 11649, "upload_time": "2019-09-02T17:15:26", "url": "https://files.pythonhosted.org/packages/7e/0c/4688c2138ffd5c78f7606fc18012ec761b0d9771b01781a6fa3388d8e61b/typing-json-0.0.4.tar.gz" } ], "0.0.5": [ { "comment_text": "", "digests": { "md5": "b2ee1e76366c2ae815e1c8a3f0d63787", "sha256": "29cdc8e28c366912a8aecd34dad4d97212383bef29b8aa784d6b283cc87fc02c" }, "downloads": -1, "filename": "typing_json-0.0.5-py3-none-any.whl", "has_sig": false, "md5_digest": "b2ee1e76366c2ae815e1c8a3f0d63787", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 9019, "upload_time": "2019-09-03T17:42:12", "url": "https://files.pythonhosted.org/packages/ee/43/edddc80e62dc330c97eb27f990cd4f9b192a48e624a7680cd7a8fc3f79f6/typing_json-0.0.5-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "3bea2e5a148fc26d573331e5c13dc4c1", "sha256": "9f942b345f4f9717a5659d5960d8866f74d131157945f596c6fa214ed26ee6dd" }, "downloads": -1, "filename": "typing-json-0.0.5.tar.gz", "has_sig": false, "md5_digest": "3bea2e5a148fc26d573331e5c13dc4c1", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 11986, "upload_time": "2019-09-03T17:42:14", "url": "https://files.pythonhosted.org/packages/2f/f4/553184e122fccf61257f1550732809828adb645b26a4409ab8ac3236066f/typing-json-0.0.5.tar.gz" } ], "0.0.6": [ { "comment_text": "", "digests": { "md5": "7cb0112e23dfc78c80307ab8f95790ef", "sha256": "4b6c3f6e7a5499e80e344b21733f2e8cb6757bd8a1101dbe76572080abe214b7" }, "downloads": -1, "filename": "typing_json-0.0.6-py3-none-any.whl", "has_sig": false, "md5_digest": "7cb0112e23dfc78c80307ab8f95790ef", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 11629, "upload_time": "2019-09-07T15:44:31", "url": "https://files.pythonhosted.org/packages/3d/a8/c05b4cd84250d657373f81aed9a7993f353873a266118fbd34f9376b750c/typing_json-0.0.6-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "a1ba3096866eec3f3b76e9fce5788079", "sha256": "9710a42bf1e17325cca2cf1f8e85dc592cc6b160683a95fc1a84499120f5f1e3" }, "downloads": -1, "filename": "typing-json-0.0.6.tar.gz", "has_sig": false, "md5_digest": "a1ba3096866eec3f3b76e9fce5788079", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 15723, "upload_time": "2019-09-07T15:44:33", "url": "https://files.pythonhosted.org/packages/11/6e/caf3c554a0fff2f410adea5db7483d57aa103ee05fa6382c0aafa2347e1d/typing-json-0.0.6.tar.gz" } ], "0.0.7": [ { "comment_text": "", "digests": { "md5": "c1e1e45a8ba8907e25c7a5b71b9874fe", "sha256": "15d683be4be67a71f328b27285b0b1dd636ca92839e3ba87ce9726b273334750" }, "downloads": -1, "filename": "typing_json-0.0.7-py3-none-any.whl", "has_sig": false, "md5_digest": "c1e1e45a8ba8907e25c7a5b71b9874fe", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 11690, "upload_time": "2019-09-10T11:13:14", "url": "https://files.pythonhosted.org/packages/de/3e/b76dbd2ff1bf7af37574779cf955b9a85345c7b8cfcb8b00c70090cb7d34/typing_json-0.0.7-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "880d49e351164072ccdfe8e5e374ff1f", "sha256": "b2f20a81a976470b54b6e0cfe1b5e570a42a827e74c73d20a6856b54d333438c" }, "downloads": -1, "filename": "typing-json-0.0.7.tar.gz", "has_sig": false, "md5_digest": "880d49e351164072ccdfe8e5e374ff1f", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 15899, "upload_time": "2019-09-10T11:13:15", "url": "https://files.pythonhosted.org/packages/cd/2a/6e3f15464ff807a59bf9e2da940b7c31d033612e6ab072bf6e41646252c7/typing-json-0.0.7.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "c1e1e45a8ba8907e25c7a5b71b9874fe", "sha256": "15d683be4be67a71f328b27285b0b1dd636ca92839e3ba87ce9726b273334750" }, "downloads": -1, "filename": "typing_json-0.0.7-py3-none-any.whl", "has_sig": false, "md5_digest": "c1e1e45a8ba8907e25c7a5b71b9874fe", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 11690, "upload_time": "2019-09-10T11:13:14", "url": "https://files.pythonhosted.org/packages/de/3e/b76dbd2ff1bf7af37574779cf955b9a85345c7b8cfcb8b00c70090cb7d34/typing_json-0.0.7-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "880d49e351164072ccdfe8e5e374ff1f", "sha256": "b2f20a81a976470b54b6e0cfe1b5e570a42a827e74c73d20a6856b54d333438c" }, "downloads": -1, "filename": "typing-json-0.0.7.tar.gz", "has_sig": false, "md5_digest": "880d49e351164072ccdfe8e5e374ff1f", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 15899, "upload_time": "2019-09-10T11:13:15", "url": "https://files.pythonhosted.org/packages/cd/2a/6e3f15464ff807a59bf9e2da940b7c31d033612e6ab072bf6e41646252c7/typing-json-0.0.7.tar.gz" } ] }