Skip to main content

allow custom class json behavior on builtin json object

Project description

What is this?

A pip module that let you define a __json__ method, that works like the toJSON from JavaScript.
(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 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.5.2.tar.gz (3.3 kB view details)

Uploaded Source

Built Distribution

json_fix-0.5.2-py3-none-any.whl (3.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: json_fix-0.5.2.tar.gz
  • Upload date:
  • Size: 3.3 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

Hashes for json_fix-0.5.2.tar.gz
Algorithm Hash digest
SHA256 6d85ba545622603e2662afbd5cdb3b4af1f2992aa473b76cf58d7ed768e0efc8
MD5 4be9f073bbf8082de111a486c1d114be
BLAKE2b-256 1fe4b7a7ded06c49a885701e64c19fa026e90a4d710137226854cbaa1b780f92

See more details on using hashes here.

File details

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

File metadata

  • Download URL: json_fix-0.5.2-py3-none-any.whl
  • Upload date:
  • Size: 3.3 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

Hashes for json_fix-0.5.2-py3-none-any.whl
Algorithm Hash digest
SHA256 5e65c8fd5fc674dd7141374968175c660d1af20481ca0d936a16a1633377a583
MD5 c82120dcca5f2215900bc2c8fb6dcf7b
BLAKE2b-256 bd38fc1c28173f2ad5fc6b2cfa981f15a4007c1f4c22eded8beb3051e49eba20

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