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

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 classes 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

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

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

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

Hashes for json_fix-0.1.2.tar.gz
Algorithm Hash digest
SHA256 aafda3d02ce6f7be0791143cab446a1fe1fe29100a070cf4677775a89cc208b8
MD5 84a5ca60bf95b5b9489ed2f3b367e91a
BLAKE2b-256 3edb2daba242f834538fc324f89f84c1b358888a0d55e74b61721c60c721b5a0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: json_fix-0.1.2-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

Hashes for json_fix-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 b1aa10d69c7c6a1f30e450e2656625055b5181bd2034a5cec41f18b4fc87fc43
MD5 c97514cfa955cb8a71d94c097b8969c2
BLAKE2b-256 53cde468d1fea8b78ac4f524596813601e10f429554ff43e1d6771d30da998c1

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