Skip to main content

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


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.3.0.tar.gz (2.7 kB view details)

Uploaded Source

Built Distribution

json_fix-0.3.0-py3-none-any.whl (2.6 kB view details)

Uploaded Python 3

File details

Details for the file json_fix-0.3.0.tar.gz.

File metadata

  • Download URL: json_fix-0.3.0.tar.gz
  • Upload date:
  • Size: 2.7 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.6

File hashes

Hashes for json_fix-0.3.0.tar.gz
Algorithm Hash digest
SHA256 78292352f9754ee17a60e6d3a687b14c9ca2dc5de03a999c9c57ac2f5c92a17d
MD5 9daf25cc0c8368bc8fed74b6866f93fc
BLAKE2b-256 f8d257f4ee7c8064705a18994b0741d990d4ff6571272c025f061c10529f4531

See more details on using hashes here.

File details

Details for the file json_fix-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: json_fix-0.3.0-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.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.6

File hashes

Hashes for json_fix-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8edc2c8c316c2aef612e37ba93e675227ec9ddba0a7a6f43c6493235be803dd0
MD5 c25750791b854381c73a2e40432ab0ed
BLAKE2b-256 c6288daa7644693a83bd66bad7d270ee7bb299d926ab24df961ddc2941e2fa31

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page