{"id":19299679,"url":"https://github.com/jeff-hykin/json_fix","last_synced_at":"2025-04-09T13:06:40.233Z","repository":{"id":44245996,"uuid":"399847152","full_name":"jeff-hykin/json_fix","owner":"jeff-hykin","description":"💾 📦 ✅  A fix for the 1000 upvotes python-json question","archived":false,"fork":false,"pushed_at":"2024-12-18T21:02:34.000Z","size":1461,"stargazers_count":26,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-06T06:33:17.399Z","etag":null,"topics":["json","python"],"latest_commit_sha":null,"homepage":"","language":"Shell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jeff-hykin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-08-25T14:22:58.000Z","updated_at":"2025-01-17T10:47:06.000Z","dependencies_parsed_at":"2024-06-21T07:11:15.854Z","dependency_job_id":"672089be-7bed-4d72-8dcf-0e963a419770","html_url":"https://github.com/jeff-hykin/json_fix","commit_stats":{"total_commits":321,"total_committers":1,"mean_commits":321.0,"dds":0.0,"last_synced_commit":"2dfb447a5386f94b1d8953717810f0ce53839c3d"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jeff-hykin%2Fjson_fix","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jeff-hykin%2Fjson_fix/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jeff-hykin%2Fjson_fix/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jeff-hykin%2Fjson_fix/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jeff-hykin","download_url":"https://codeload.github.com/jeff-hykin/json_fix/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248045231,"owners_count":21038553,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["json","python"],"created_at":"2024-11-09T23:12:23.774Z","updated_at":"2025-04-09T13:06:40.206Z","avatar_url":"https://github.com/jeff-hykin.png","language":"Shell","funding_links":[],"categories":[],"sub_categories":[],"readme":"# What is this?\n\nA pip module that lets you define a `__json__` method, that works like `toJSON` from JavaScript.\u003cbr\u003e\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","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjeff-hykin%2Fjson_fix","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjeff-hykin%2Fjson_fix","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjeff-hykin%2Fjson_fix/lists"}