# What is this?
A pip module that let you define a `__json__` method, that works like the `toJSON` from JavaScript.<br>
(e.g. it magically gets called whenever someone does `json.dumps(your_object)`)
From a technical perspective, this module is a safe, backwards-compatible, reversable patch to the built-in python `json` object that allows classes to specify how they should be serialized.
# Why?
Because sometimes external code uses something like
```python
import json
json.dumps(list_containing_your_object)
```
And it simply throws an error no matter how you customize your object
# How do I use this for my class?
`pip install json-fix`
```python
import json_fix # import this before the JSON.dumps gets called
# same file, or different file
class YOUR_CLASS:
def __json__(self):
# YOUR CUSTOM CODE HERE
# you probably just want to do:
# return self.__dict__
return "a built-in object that is natually json-able"
```
# How do I change how someone elses class is jsonified?
There's 2 ways; the aggressive `override_table` or the more collaboration-friendly `fallback_table`. Some really powerful stuff can be done safely with the fallback table.
## Override Table
If a pip module defines a class, you can control how it is json-dumped, even if they defined a `.__json__()` method, by using `json.override_table`.
- Note! The "when" (in when-a-rule-is-added) can be very important. Whatever rule was most-recently added will have the highest priority. So, even if a pip module uses the override table, you can override their override by doing `import that_module` and THEN adding your rule to the override table.
- Note 2! The override table is capable of changing how built-in types are dumped, be careful!
```python
import json_fix # import this before the JSON.dumps gets called
import json
import pandas as pd
SomeClassYouDidntDefine = pd.DataFrame
# create a boolean function for identifying the class
class_checker = lambda obj: isinstance(obj, SomeClassYouDidntDefine)
# then assign it to a function that does the converting
json.override_table[class_checker] = lambda obj_of_that_class: json.loads(obj_of_that_class.to_json())
json.dumps([ 1, 2, SomeClassYouDidntDefine() ], indent=2) # dumps as expected
```
## Fallback Table
Let's say we want all python classes to be jsonable by default, well we can easily do that with the fallback table. The logic is `if notthing in override table, and no .__json__ method, then check the fallback table`.
```python
import json_fix # import this before the JSON.dumps gets called
import json
# a checker for custom objects
checker = lambda obj: hasattr(obj, "__dict__")
# use the __dict__ when they don't specify a __json__ method
json.fallback_table[checker] = lambda obj_with_dict: obj_with_dict.__dict__
class SomeClass:
def __init__(self):
self.thing = 10
json.dumps([ 1, 2, SomeClass() ], indent=2) # dumps as expected
```
Like the override table, the most recently-added checker will have the highest priority.
Raw data
{
"_id": null,
"home_page": "https://github.com/jeff-hykin/json_fix.git",
"name": "json-fix",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.6",
"maintainer_email": "",
"keywords": "",
"author": "Jeff Hykin",
"author_email": "jeff.hykin@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/33/95/f9721bad4ebd1980cc930cc14170079d01645031abfbf5545473baee0941/json_fix-1.0.0.tar.gz",
"platform": null,
"description": "# What is this?\n\nA pip module that let you define a `__json__` method, that works like the `toJSON` from JavaScript.<br>\n(e.g. it magically gets called whenever someone does `json.dumps(your_object)`)\n\nFrom a technical perspective, this module is a safe, backwards-compatible, reversable patch to the built-in python `json` object that allows classes to specify how they should be serialized.\n\n# Why?\n\nBecause sometimes external code uses something like\n```python\nimport json\njson.dumps(list_containing_your_object)\n```\nAnd it simply throws an error no matter how you customize your object\n\n# How do I use this for my class?\n\n`pip install json-fix`\n\n```python\nimport json_fix # import this before the JSON.dumps gets called\n\n# same file, or different file\nclass YOUR_CLASS:\n def __json__(self):\n # YOUR CUSTOM CODE HERE\n # you probably just want to do:\n # return self.__dict__\n return \"a built-in object that is natually json-able\"\n```\n\n# How do I change how someone elses class is jsonified?\n\nThere's 2 ways; the aggressive `override_table` or the more collaboration-friendly `fallback_table`. Some really powerful stuff can be done safely with the fallback table.\n\n## Override Table\n\nIf a pip module defines a class, you can control how it is json-dumped, even if they defined a `.__json__()` method, by using `json.override_table`.\n- Note! The \"when\" (in when-a-rule-is-added) can be very important. Whatever rule was most-recently added will have the highest priority. So, even if a pip module uses the override table, you can override their override by doing `import that_module` and THEN adding your rule to the override table.\n- Note 2! The override table is capable of changing how built-in types are dumped, be careful! \n\n```python\nimport json_fix # import this before the JSON.dumps gets called\nimport json\nimport pandas as pd\n\nSomeClassYouDidntDefine = pd.DataFrame\n\n# create a boolean function for identifying the class\nclass_checker = lambda obj: isinstance(obj, SomeClassYouDidntDefine)\n# then assign it to a function that does the converting\njson.override_table[class_checker] = lambda obj_of_that_class: json.loads(obj_of_that_class.to_json())\n\njson.dumps([ 1, 2, SomeClassYouDidntDefine() ], indent=2) # dumps as expected\n```\n\n## Fallback Table\n\nLet's say we want all python classes to be jsonable by default, well we can easily do that with the fallback table. The logic is `if notthing in override table, and no .__json__ method, then check the fallback table`. \n\n```python\nimport json_fix # import this before the JSON.dumps gets called\nimport json\n\n# a checker for custom objects\nchecker = lambda obj: hasattr(obj, \"__dict__\")\n# use the __dict__ when they don't specify a __json__ method \njson.fallback_table[checker] = lambda obj_with_dict: obj_with_dict.__dict__\n\nclass SomeClass:\n def __init__(self):\n self.thing = 10\n\njson.dumps([ 1, 2, SomeClass() ], indent=2) # dumps as expected\n```\n\nLike the override table, the most recently-added checker will have the highest priority. \n\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "allow custom class json behavior on builtin json object",
"version": "1.0.0",
"project_urls": {
"Homepage": "https://github.com/jeff-hykin/json_fix.git"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "f6a27ab49b838badff7f43bae3338b90d8f0ebef3672eb12634b619305a9db62",
"md5": "bbd0fbcbb15e21da04682b52b3b4854e",
"sha256": "1b7d622572f3c7dd653ce9e5a87a4c645a437be7ee622bc916bccb145789055b"
},
"downloads": -1,
"filename": "json_fix-1.0.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "bbd0fbcbb15e21da04682b52b3b4854e",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.6",
"size": 3837,
"upload_time": "2024-02-13T18:15:29",
"upload_time_iso_8601": "2024-02-13T18:15:29.802814Z",
"url": "https://files.pythonhosted.org/packages/f6/a2/7ab49b838badff7f43bae3338b90d8f0ebef3672eb12634b619305a9db62/json_fix-1.0.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "3395f9721bad4ebd1980cc930cc14170079d01645031abfbf5545473baee0941",
"md5": "430590f268dfe63bb7b4109863656f29",
"sha256": "625b3fc2f7c7c8855eb3e6669c366163cc9b95dbf8e8568fa07f42a65b8d4672"
},
"downloads": -1,
"filename": "json_fix-1.0.0.tar.gz",
"has_sig": false,
"md5_digest": "430590f268dfe63bb7b4109863656f29",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.6",
"size": 3932,
"upload_time": "2024-02-13T18:15:31",
"upload_time_iso_8601": "2024-02-13T18:15:31.472450Z",
"url": "https://files.pythonhosted.org/packages/33/95/f9721bad4ebd1980cc930cc14170079d01645031abfbf5545473baee0941/json_fix-1.0.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-02-13 18:15:31",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "jeff-hykin",
"github_project": "json_fix",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "json-fix"
}