Skip to main content

This python module helps converting arbitrary Python objects into JSON strings and back.

Project description

The jsonconversion package

This python module helps converting arbitrary Python objects into JSON strings and back. It extends the basic features of the JSONEncoder and JSONDecoder classes provided by the native json package. For this purpose, jsonconversion ships with these four classes:

The JSONObject class

Your serializable classes should inherit from this class. Hereby, they must implement the methods from_dict and to_dict. The example further down describes how to do so.

The JSONExtendedEncoder class

This is a class used internally by JSONObjectEncoder. However, it can also be used directly, if you do not need the features of JSONObjectEncoder but want to implement your own encoders.

The class is especially helpful, if you want custom handling of builtins (int, dict, …) or classes deriving from builtins. This would not be possible if directly inheriting from JSONEncoder. To do so, override the isinstance method and return False for all types you want to handle in the default method.

If you look at the source code of JSONObjectEncoder, you will see how this can be used.

The JSONObjectEncoder class

Encodes Python objects into JSON strings. Supported objects are:

  • Python builtins: int, float, str, list, set, dict, tuple

  • type objects: isinstance(object, type)

  • All classes deriving from JSONObject

Those objects can of course also be nested!

The JSONObjectDecoder class

Decodes JSON strings converted using the JSONObjectEncoder back to Python objects.

The class adds a custom keyword argument to the load[s] method: substitute_modules. This parameter takes a dict in the form {"old.module.MyClass": "new.module.MyClass"}. It can be used if you have serialized JSONObjects who’s module path has changed.

Usage

Using jsonconversion is easy. You can find further code examples in the test folder.

Encoding and Decoding

In order to encode Python objects with JSON conversion and to later decode them, you have to import the Python module json. The module provides the methods dump/dumps for encoding and load/loads for decoding:

import json

from jsonconversion.decoder import JSONObjectDecoder
from jsonconversion.encoder import JSONObjectEncoder

var = (1, 2, 3)  # variable to be serialized

# "dumps" converts the variable to a string, "dump" directly writes it to a file
str_var = json.dumps(var, cls=JSONObjectEncoder)
# Equivalently, "loads" converts the object back from a string. "load" from a file
var_2 = json.loads(str_var, cls=JSONObjectDecoder)
assert var == var_2

Deriving from JSONObject

In order to serialize arbitrary, self-written classes, they must derive from JSONObject and implement the two methods from_dict and to_dict:

class MyClass(JSONObject):

    def __init__(self, a, b, c):
        self.a = a
        self.b = b
        self.c = c

    @classmethod
    def from_dict(cls, dict_):
        return cls(dict_['a'], dict_['b'], dict_['c'])

    def to_dict(self):
        return {'a': self.a, 'b': self.b, 'c': self.c}

    def __eq__(self, other):
        return self.a == other.a and self.b == other.b and self.c == other.c

General notes

  • jsonconversion stores the class path in the JSON string when serializing a JSONObject. When decoding the object back, it automatically imports the correct module. You only have to ensure that the module is within your PYTHONPATH.

  • The to_dict and from_dict methods only need to specify the elements of a class, needed to recreate the object. Derived attributes of a class (like age from year_born) do not need to be serialized.

  • If you compare the original object with the object obtained from serialization and deserialization using is, they will differ, as these are objects at different locations in memory. Also a comparison of JSONObject with == will fail, if you do not tell Python how to compare two objects. This is why MyClass overrides the __eq__ method.

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

jsonconversion-1.2.0.tar.gz (9.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

jsonconversion-1.2.0-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

File details

Details for the file jsonconversion-1.2.0.tar.gz.

File metadata

  • Download URL: jsonconversion-1.2.0.tar.gz
  • Upload date:
  • Size: 9.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.13

File hashes

Hashes for jsonconversion-1.2.0.tar.gz
Algorithm Hash digest
SHA256 e2b3fb3db929c28b62b8ea5ed0f2bcc5f36a8634fa9258075461eab1883ea074
MD5 b3a50eea184cef9fa8cf08b144228275
BLAKE2b-256 a3fdc49c6065872884f63fc710db1bb0bc27db73ebd2f960cb7a09ceedb530f1

See more details on using hashes here.

File details

Details for the file jsonconversion-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: jsonconversion-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 11.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.13

File hashes

Hashes for jsonconversion-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c646a026fb8acbb3736bbf90a556554f2ba72d30c34f10191fa3ed71fa337fa5
MD5 1935f247011bc9e5ee04062933347c21
BLAKE2b-256 d67e4762ea6c2f49c362de83dd009a6cda67626f80b6290d8635b38ace9b9d08

See more details on using hashes here.

Supported by

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