freshcrate
Skin:/
Home > Frameworks > jsonconversion

jsonconversion

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

Why this rank:Release freshnessHealthy release cadenceStrong adoption

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 ``JSONObject``\s 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: .. code:: python 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``: .. code:: python 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.

Release History

VersionChangesUrgencyDate
1.2.1Imported from PyPI (1.2.1)Low4/21/2026
1.2.0Migrated `pkg_resources` to `importlib.metadata` which is new standard for python>=3.7Low10/24/2025
1.1.2Relaxed numpy version for upper dependenciesLow10/14/2025
1.1.1Properly specifying numpy dependency based on python versionLow1/27/2025
1.1.0- Added dependabot configuration - Added github actions to run test on pull requests - Removed pytest dependabilityLow1/23/2025
1.0.3Relieved numpy version for better backwards compatibilityLow1/22/2025
1.0.2Updated minimum python version to 3.9Low1/13/2025
1.0.1Updated build processLow3/7/2024
1.0.0Updated dependency deprecation and moved to python3Low3/5/2024
0.2.7Release 0.2.7Low11/16/2018
0.2.5The constructor of `JSONObjectEncoder` is now more generic in terms of accepted `kwargs`.Low9/27/2017
0.2.4Should now be installable for all 2.6.* and 2.7.* Python interpreters Low4/10/2017
0.2.3* Convert `README.md` to `README.rst` * add `__version__` information * Update `setup.py` * set correct `url` * add `download_url` * set correct `version` * add `classifiers` * parse `README.rst` for `long_description` * add missing copyright header * correct `python_requires` stringLow4/10/2017
0.2.2Release 0.2.2Low4/4/2017
0.2.1Release 0.2.1Low4/4/2017

Dependencies & License Audit

Loading dependencies...

Similar Packages

dataclass-wizardA wizard-like JSON serialization library for Python dataclassesmain@2026-06-01
py-serializableLibrary for serializing and deserializing Python Objects to and from JSON and XML.2.1.0
flask-jwt-extendedExtended JWT integration with Flask4.7.4
avroAvro is a serialization and RPC framework.1.12.1
tqdmFast, Extensible Progress Meterv4.68.1

More from pypi

markitdownUtility tool for converting various files to Markdown
fastapiFastAPI framework, high performance, easy to learn, fast to code, ready for production
djangoA high-level Python web framework that encourages rapid development and clean, pragmatic design.
flaskA simple framework for building complex web applications.

More in Frameworks

langchainThe agent engineering platform
deer-flowAn open-source long-horizon SuperAgent harness that researches, codes, and creates. With the help of sandboxes, memories, tools, skill, subagents and message gateway, it handles different levels of ta
tqdmFast, Extensible Progress Meter
simBuild, deploy, and orchestrate AI agents. Sim is the central intelligence layer for your AI workforce.