json-fix


Namejson-fix JSON
Version 1.0.2 PyPI version JSON
download
home_pagehttps://github.com/jeff-hykin/json_fix.git
Summaryallow custom class json behavior on builtin json object
upload_time2025-10-23 22:04:58
maintainerNone
docs_urlNone
authorJeff Hykin
requires_python>=3.6
licenseMIT
keywords
VCS
bugtrack_url
requirements certifi charset-normalizer colorama docutils file-system-py idna importlib_metadata jaraco.classes jaraco.context jaraco.functools keyring keyrings.alt more-itertools nh3 pkginfo Pygments readme_renderer requests requests-toolbelt rfc3986 setuptools toml tqdm twine urllib3 zipp
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # What is this?

A pip module that lets you define a `__json__` method, that works like `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 someone eles's code (e.g. a pip module) tries to serialize your object, like
```python
import json
json.dumps(list_containing_your_object)
```
And you'd have to fork their code to make it not throw an error.

# How do I use this tool?

`pip install json-fix`

```python
import json_fix # import this any time (any where) 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 imported classes (numpy array, dataframe, etc) are jsonified?

There's 2 ways; the aggressive `override_table` or the more collaboration-friendly `fallback_table`. Note: some really powerful stuff can be done safely with the fallback table!

## Override Table

**CAUTION!**
- The override table is so powerful it even lets you change how built in objects are serialized. You can screw yourself (infinite loop) if you're using (abusing) the override table to change giant swaths of objects instead of specific classes.
- Even if a class defines a `.__json__()` method, the `json.override_table` will take priority.
- The order of keys matters a lot. The last entry takes the highest priority (this lets us override pip modules even if they try using the override table).


The override table is a dictionary.
It has "check functions" as keys, and jsonifiers as values. 

```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
check_func = lambda obj: isinstance(obj, SomeClassYouDidntDefine)
# then assign it to a function that does the converting
json.override_table[check_func] = 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

If you want **all python classes to be jsonable by default**, we can easily do that with the fallback table. The logic is `if nothing 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": null,
    "docs_url": null,
    "requires_python": ">=3.6",
    "maintainer_email": null,
    "keywords": null,
    "author": "Jeff Hykin",
    "author_email": "jeff.hykin@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/1e/3e/84a3ef86aea3e7cbb4dca525977a6b2fd07afb1eb16c15ed77036dbe0493/json_fix-1.0.2.tar.gz",
    "platform": null,
    "description": "# What is this?\n\nA pip module that lets you define a `__json__` method, that works like `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 someone eles's code (e.g. a pip module) tries to serialize your object, like\n```python\nimport json\njson.dumps(list_containing_your_object)\n```\nAnd you'd have to fork their code to make it not throw an error.\n\n# How do I use this tool?\n\n`pip install json-fix`\n\n```python\nimport json_fix # import this any time (any where) 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 imported classes (numpy array, dataframe, etc) are jsonified?\n\nThere's 2 ways; the aggressive `override_table` or the more collaboration-friendly `fallback_table`. Note: some really powerful stuff can be done safely with the fallback table!\n\n## Override Table\n\n**CAUTION!**\n- The override table is so powerful it even lets you change how built in objects are serialized. You can screw yourself (infinite loop) if you're using (abusing) the override table to change giant swaths of objects instead of specific classes.\n- Even if a class defines a `.__json__()` method, the `json.override_table` will take priority.\n- The order of keys matters a lot. The last entry takes the highest priority (this lets us override pip modules even if they try using the override table).\n\n\nThe override table is a dictionary.\nIt has \"check functions\" as keys, and jsonifiers as values. \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\ncheck_func = lambda obj: isinstance(obj, SomeClassYouDidntDefine)\n# then assign it to a function that does the converting\njson.override_table[check_func] = 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\nIf you want **all python classes to be jsonable by default**, we can easily do that with the fallback table. The logic is `if nothing 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",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "allow custom class json behavior on builtin json object",
    "version": "1.0.2",
    "project_urls": {
        "Homepage": "https://github.com/jeff-hykin/json_fix.git"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "720f980e714185df717191531157664ce1dd06b6a3c3f042ee947ccddaa8403d",
                "md5": "935a0269c972cf2e603f8b4e9d2bb88f",
                "sha256": "7a5fcc33d2a0f9966f2794ab1705be2983fcb46bdd55b57763c0ab749f757ed5"
            },
            "downloads": -1,
            "filename": "json_fix-1.0.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "935a0269c972cf2e603f8b4e9d2bb88f",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.6",
            "size": 3959,
            "upload_time": "2025-10-23T22:04:57",
            "upload_time_iso_8601": "2025-10-23T22:04:57.582606Z",
            "url": "https://files.pythonhosted.org/packages/72/0f/980e714185df717191531157664ce1dd06b6a3c3f042ee947ccddaa8403d/json_fix-1.0.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "1e3e84a3ef86aea3e7cbb4dca525977a6b2fd07afb1eb16c15ed77036dbe0493",
                "md5": "07a19773a5dd91744070eab899e9e8d5",
                "sha256": "dd775aeba78a5481127b7534cbc0fda0ae7275d812dbdac16fdcce3503f8211a"
            },
            "downloads": -1,
            "filename": "json_fix-1.0.2.tar.gz",
            "has_sig": false,
            "md5_digest": "07a19773a5dd91744070eab899e9e8d5",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.6",
            "size": 4155,
            "upload_time": "2025-10-23T22:04:58",
            "upload_time_iso_8601": "2025-10-23T22:04:58.593377Z",
            "url": "https://files.pythonhosted.org/packages/1e/3e/84a3ef86aea3e7cbb4dca525977a6b2fd07afb1eb16c15ed77036dbe0493/json_fix-1.0.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-10-23 22:04:58",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "jeff-hykin",
    "github_project": "json_fix",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [
        {
            "name": "certifi",
            "specs": [
                [
                    "==",
                    "2025.10.5"
                ]
            ]
        },
        {
            "name": "charset-normalizer",
            "specs": [
                [
                    "==",
                    "3.4.4"
                ]
            ]
        },
        {
            "name": "colorama",
            "specs": [
                [
                    "==",
                    "0.4.6"
                ]
            ]
        },
        {
            "name": "docutils",
            "specs": [
                [
                    "==",
                    "0.22.2"
                ]
            ]
        },
        {
            "name": "file-system-py",
            "specs": [
                [
                    "==",
                    "0.0.11"
                ]
            ]
        },
        {
            "name": "idna",
            "specs": [
                [
                    "==",
                    "3.11"
                ]
            ]
        },
        {
            "name": "importlib_metadata",
            "specs": [
                [
                    "==",
                    "8.7.0"
                ]
            ]
        },
        {
            "name": "jaraco.classes",
            "specs": [
                [
                    "==",
                    "3.4.0"
                ]
            ]
        },
        {
            "name": "jaraco.context",
            "specs": [
                [
                    "==",
                    "6.0.1"
                ]
            ]
        },
        {
            "name": "jaraco.functools",
            "specs": [
                [
                    "==",
                    "4.3.0"
                ]
            ]
        },
        {
            "name": "keyring",
            "specs": [
                [
                    "==",
                    "25.6.0"
                ]
            ]
        },
        {
            "name": "keyrings.alt",
            "specs": [
                [
                    "==",
                    "5.0.2"
                ]
            ]
        },
        {
            "name": "more-itertools",
            "specs": [
                [
                    "==",
                    "10.8.0"
                ]
            ]
        },
        {
            "name": "nh3",
            "specs": [
                [
                    "==",
                    "0.3.1"
                ]
            ]
        },
        {
            "name": "pkginfo",
            "specs": [
                [
                    "==",
                    "1.12.1.2"
                ]
            ]
        },
        {
            "name": "Pygments",
            "specs": [
                [
                    "==",
                    "2.19.2"
                ]
            ]
        },
        {
            "name": "readme_renderer",
            "specs": [
                [
                    "==",
                    "44.0"
                ]
            ]
        },
        {
            "name": "requests",
            "specs": [
                [
                    "==",
                    "2.32.5"
                ]
            ]
        },
        {
            "name": "requests-toolbelt",
            "specs": [
                [
                    "==",
                    "1.0.0"
                ]
            ]
        },
        {
            "name": "rfc3986",
            "specs": [
                [
                    "==",
                    "2.0.0"
                ]
            ]
        },
        {
            "name": "setuptools",
            "specs": [
                [
                    "==",
                    "80.9.0"
                ]
            ]
        },
        {
            "name": "toml",
            "specs": [
                [
                    "==",
                    "0.10.2"
                ]
            ]
        },
        {
            "name": "tqdm",
            "specs": [
                [
                    "==",
                    "4.67.1"
                ]
            ]
        },
        {
            "name": "twine",
            "specs": [
                [
                    "==",
                    "3.4.1"
                ]
            ]
        },
        {
            "name": "urllib3",
            "specs": [
                [
                    "==",
                    "2.5.0"
                ]
            ]
        },
        {
            "name": "zipp",
            "specs": [
                [
                    "==",
                    "3.23.0"
                ]
            ]
        }
    ],
    "lcname": "json-fix"
}
        
Elapsed time: 1.10592s