===========
json_dunder
===========
Encdoe objects with a __json__ dunder method and decode objects with a __from_json__ dunder method.
.. code-block:: python
import json_dunder
# Use tells json to use this class
coder = json_dunder.JsonDunderTypeCoder().use()
@coder.register
class A:
def __init__(self, x):
self.x = x
def __eq__(self, other):
if isinstance(other, self.__class__):
return self.x == other.x
return False
def __json__(self):
return {"x": self.x}
@classmethod
def __from_json__(cls, d: dict):
return cls(**d)
@coder.register
class B:
def __init__(self, a, y):
self.a = a
self.y = y
def __eq__(self, other):
if isinstance(other, self.__class__):
return self.a == other.a and self.y == other.y
return False
def __json__(self):
return {"a": self.a, "y": self.y}
@classmethod
def __from_json__(cls, d: dict):
return cls(**d)
a = A(1)
b = B(a, 2)
sa = json_dunder.dumps(a)
assert sa == '{"x": 1, "__json_type__": "__main__.A"}'
sb = json_dunder.dumps(b)
assert sb == '{"a": {"x": 1, "__json_type__": "__main__.A"}, "y": 2, "__json_type__": "__main__.B"}'
obj = json_dunder.loads(sa)
assert obj == a
obj = json_dunder.loads(sb)
assert obj.a == a
assert obj == b
Install
=======
.. code-block::
pip install json_dunder
Raw data
{
"_id": null,
"home_page": "https://github.com/justengel/json_dunder",
"name": "json-dunder",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "json decoder encoder dunder",
"author": "Justin Engel",
"author_email": "jtengel08@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/f9/99/d7329304881edb22a5f51344cd1d00c3c7c9fcb278e671e181ed08b3bd4a/json_dunder-0.0.0.tar.gz",
"platform": "any",
"description": "===========\r\njson_dunder\r\n===========\r\n\r\nEncdoe objects with a __json__ dunder method and decode objects with a __from_json__ dunder method.\r\n\r\n\r\n.. code-block:: python\r\n\r\n import json_dunder\r\n\r\n # Use tells json to use this class\r\n coder = json_dunder.JsonDunderTypeCoder().use()\r\n\r\n @coder.register\r\n class A:\r\n def __init__(self, x):\r\n self.x = x\r\n\r\n def __eq__(self, other):\r\n if isinstance(other, self.__class__):\r\n return self.x == other.x\r\n return False\r\n\r\n def __json__(self):\r\n return {\"x\": self.x}\r\n\r\n @classmethod\r\n def __from_json__(cls, d: dict):\r\n return cls(**d)\r\n\r\n @coder.register\r\n class B:\r\n def __init__(self, a, y):\r\n self.a = a\r\n self.y = y\r\n\r\n def __eq__(self, other):\r\n if isinstance(other, self.__class__):\r\n return self.a == other.a and self.y == other.y\r\n return False\r\n\r\n def __json__(self):\r\n return {\"a\": self.a, \"y\": self.y}\r\n\r\n @classmethod\r\n def __from_json__(cls, d: dict):\r\n return cls(**d)\r\n\r\n a = A(1)\r\n b = B(a, 2)\r\n\r\n sa = json_dunder.dumps(a)\r\n assert sa == '{\"x\": 1, \"__json_type__\": \"__main__.A\"}'\r\n\r\n sb = json_dunder.dumps(b)\r\n assert sb == '{\"a\": {\"x\": 1, \"__json_type__\": \"__main__.A\"}, \"y\": 2, \"__json_type__\": \"__main__.B\"}'\r\n\r\n obj = json_dunder.loads(sa)\r\n assert obj == a\r\n\r\n obj = json_dunder.loads(sb)\r\n assert obj.a == a\r\n assert obj == b\r\n\r\n\r\nInstall\r\n=======\r\n\r\n.. code-block::\r\n\r\n pip install json_dunder\r\n",
"bugtrack_url": null,
"license": "Proprietary",
"summary": "Encode objects with a __json__ dunder method and decode objects with a __from_json__ dunder method.",
"version": "0.0.0",
"project_urls": {
"Download": "https://github.com/justengel/json_dunder/archive/refs/tags/v0.0.0.tar.gz",
"Homepage": "https://github.com/justengel/json_dunder"
},
"split_keywords": [
"json",
"decoder",
"encoder",
"dunder"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "5bf720e065e33a038b278deb5d5beb378dc47c8e02fdd77862f9486efb20d82a",
"md5": "ce0e55afebc7b6f4abb381cab47ea445",
"sha256": "2171d4e64ace82f2ea67ed71c9dc48b6c064eac6b5e202365b055a142d9f0184"
},
"downloads": -1,
"filename": "json_dunder-0.0.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "ce0e55afebc7b6f4abb381cab47ea445",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 4364,
"upload_time": "2023-09-23T18:21:02",
"upload_time_iso_8601": "2023-09-23T18:21:02.312830Z",
"url": "https://files.pythonhosted.org/packages/5b/f7/20e065e33a038b278deb5d5beb378dc47c8e02fdd77862f9486efb20d82a/json_dunder-0.0.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "f999d7329304881edb22a5f51344cd1d00c3c7c9fcb278e671e181ed08b3bd4a",
"md5": "500026f2428c644ecb6d4eb587f84e74",
"sha256": "7987cee400ff90497392b1bd5b0534f35248abf6ee4a33ef570abf80a40522c1"
},
"downloads": -1,
"filename": "json_dunder-0.0.0.tar.gz",
"has_sig": false,
"md5_digest": "500026f2428c644ecb6d4eb587f84e74",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 4662,
"upload_time": "2023-09-23T18:21:04",
"upload_time_iso_8601": "2023-09-23T18:21:04.326168Z",
"url": "https://files.pythonhosted.org/packages/f9/99/d7329304881edb22a5f51344cd1d00c3c7c9fcb278e671e181ed08b3bd4a/json_dunder-0.0.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-09-23 18:21:04",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "justengel",
"github_project": "json_dunder",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"requirements": [],
"lcname": "json-dunder"
}