allow custom class json behavior on builtin json object
Project description
What is this?
A 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
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?
pip install json-fix
from json_fix import fix_it; fix_it() # only needs to be done once per runtime, NOT per-file
# 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"
If you want control over that are defined externally (datetime, numpy.array, tensor, etc), use the override_table
from json_fix import fix_it; fix_it() # only needs to be done once per runtime, NOT per-file
import json
import pandas as pd
SomeClassYouDidntDefine = pd.DataFrame
# the key is a lambda checker, the value is the converter if the check==True
class_checker = lambda obj: isinstance(obj, SomeClassYouDidntDefine)
# the lambda here (the converter) needs to return a string
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)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file json_fix-0.1.1.tar.gz.
File metadata
- Download URL: json_fix-0.1.1.tar.gz
- Upload date:
- Size: 2.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.8.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
de80a943860ffc957eb3f7bc750acc1617415f75ea3a7c02a16691dcde320581
|
|
| MD5 |
d0c251963b076c2655ec0fa1cf426726
|
|
| BLAKE2b-256 |
430a9806007eeb291421c9ebdcab0e3600a431e12b3953d100762daaa5389825
|
File details
Details for the file json_fix-0.1.1-py3-none-any.whl.
File metadata
- Download URL: json_fix-0.1.1-py3-none-any.whl
- Upload date:
- Size: 2.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.8.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3e4ad9b00ad18e8d60832d55636970a98f1b1b275ba16c880772508fbc4b3726
|
|
| MD5 |
b7d49854560135652e0a0be2c0b72000
|
|
| BLAKE2b-256 |
8141f2862cdf6e22f2fe1d9c2e4ce6b124c3002bbf6b98756965a7ce71b8e6bf
|