{"id":1519,"library":"jsonconversion","title":"jsonconversion Library","description":"This Python module facilitates the conversion of arbitrary Python objects into JSON strings and back. It extends the foundational features of the native `json` package's `JSONEncoder` and `JSONDecoder` classes. Currently at version 1.2.1, it maintains an active release cadence with recent updates focusing on dependency relaxation and modernization, requiring Python 3.9 or newer.","status":"active","version":"1.2.1","language":"en","source_language":"en","source_url":"https://github.com/DLR-RM/python-jsonconversion","tags":["JSON","serialization","deserialization","object conversion","python objects","custom classes"],"install":[{"cmd":"pip install jsonconversion","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Required for certain internal operations and object types.","package":"numpy","optional":false}],"imports":[{"symbol":"JSONObject","correct":"from jsonconversion import JSONObject"},{"symbol":"JSONObjectEncoder","correct":"from jsonconversion import JSONObjectEncoder"},{"note":"The primary classes are directly available under the top-level `jsonconversion` package.","wrong":"from jsonconversion.json_conversion import JSONObjectDecoder","symbol":"JSONObjectDecoder","correct":"from jsonconversion import JSONObjectDecoder"}],"quickstart":{"code":"import json\nfrom jsonconversion import JSONObject, JSONObjectEncoder, JSONObjectDecoder\n\n# 1. Define a class that inherits from JSONObject\nclass MyCustomObject(JSONObject):\n    def __init__(self, name: str, value: int):\n        self.name = name\n        self.value = value\n\n    # Required: Convert object to a dictionary for serialization\n    def to_dict(self) -> dict:\n        return {\"name\": self.name, \"value\": self.value}\n\n    # Required: Create an object from a dictionary for deserialization\n    @classmethod\n    def from_dict(cls, data: dict):\n        return cls(name=data[\"name\"], value=data[\"value\"])\n\n    def __eq__(self, other):\n        if not isinstance(other, MyCustomObject):\n            return NotImplemented\n        return self.name == other.name and self.value == other.value\n\n    def __repr__(self):\n        return f\"MyCustomObject(name='{self.name}', value={self.value})\"\n\n# 2. Create an instance of your custom object\noriginal_obj = MyCustomObject(\"example\", 123)\nprint(f\"Original object: {original_obj}\")\n\n# 3. Encode the object to a JSON string\n# Use the JSONObjectEncoder with json.dumps\nencoded_json = json.dumps(original_obj, cls=JSONObjectEncoder, indent=2)\nprint(f\"\\nEncoded JSON:\\n{encoded_json}\")\n\n# 4. Decode the JSON string back to an object\n# Use the JSONObjectDecoder with json.loads\ndecoded_obj = json.loads(encoded_json, cls=JSONObjectDecoder)\nprint(f\"\\nDecoded object: {decoded_obj}\")\n\n# 5. Verify the conversion\nprint(f\"Objects are equal (by value): {original_obj == decoded_obj}\")\nprint(f\"Objects are identical (memory address): {original_obj is decoded_obj}\")\n","lang":"python","description":"This quickstart demonstrates how to define a custom class inheriting from `JSONObject`, implement the required `to_dict` and `from_dict` methods, and then use `JSONObjectEncoder` and `JSONObjectDecoder` with the standard `json` module to serialize and deserialize instances of your custom class."},"warnings":[{"fix":"Upgrade your Python environment to Python 3 and update your codebase to be Python 3 compatible before upgrading to jsonconversion >= 1.0.0.","message":"Version 1.0.0 introduced a significant breaking change by moving to Python 3. This means that versions prior to 1.0.0 are Python 2 compatible, while 1.0.0 and later are exclusively for Python 3.","severity":"breaking","affected_versions":"<1.0.0"},{"fix":"Ensure your project's Python interpreter is version 3.9 or higher.","message":"Starting with version 1.0.2, the minimum required Python version for the library is Python 3.9. Attempting to use it with older Python 3 versions (e.g., 3.7, 3.8) will result in installation or runtime errors.","severity":"breaking","affected_versions":">=1.0.2"},{"fix":"Ensure your custom classes follow the `JSONObject` interface by inheriting and implementing `to_dict()` (to convert to a dictionary) and a `@classmethod from_dict()` (to reconstruct from a dictionary).","message":"For custom Python objects to be serializable/deserializable by `jsonconversion`, they must inherit from `jsonconversion.JSONObject` and implement both the `to_dict()` and `from_dict()` methods. Failure to do so will result in `TypeError` during encoding or decoding.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure that the Python environment where decoding happens has access to the modules defining your `JSONObject` subclasses (e.g., by being in the `PYTHONPATH` or properly installed).","message":"When decoding JSON strings back into custom Python objects, `jsonconversion` relies on importing the original module path stored in the JSON. If the module containing your custom `JSONObject` subclass is not discoverable within your `PYTHONPATH` during deserialization, an `ImportError` or `ModuleNotFoundError` will occur.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}