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
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"
If you want control over classes that are defined externally (datetime, numpy.array, tensor, etc), use the override_table
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)
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
json_fix-0.5.1.tar.gz
(3.1 kB
view details)
Built Distribution
File details
Details for the file json_fix-0.5.1.tar.gz
.
File metadata
- Download URL: json_fix-0.5.1.tar.gz
- Upload date:
- Size: 3.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b44167b225a81351c2a4f41d8617688259cf55efb7eea214b741d496631d8519 |
|
MD5 | ca6b21a8092123c62e793c81b8f5b140 |
|
BLAKE2b-256 | d3a24bd02ab380a781b3ba647d6d1c40d960378a93b8b579b349eb5c32fb7a34 |
File details
Details for the file json_fix-0.5.1-py3-none-any.whl
.
File metadata
- Download URL: json_fix-0.5.1-py3-none-any.whl
- Upload date:
- Size: 3.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0e33232753fb3aed4554b47d563590a661c9b9598c0e8097ba3e01915ff0a980 |
|
MD5 | c2e1a147e76fd06a002ac08f636cba99 |
|
BLAKE2b-256 | 8be0f34cbd723f5f558948977e2d771e9cab9ccf93f7d5c8b8344da48f59b94a |