JSONable

raw JSON →
0.3.1 verified Fri May 01 auth: no python maintenance

A Python library providing an abstract base class for JSON serialization/deserialization. Current version: 0.3.1. Release cadence appears sparse; last release in 2015.

pip install jsonable
error AttributeError: 'Person' object has no attribute 'to_json'
cause Using an older version where method name was different, or not inheriting from JSONable.
fix
Ensure you inherit from JSONable and are using version 0.3.1: from jsonable import JSONable.
error TypeError: Object of type 'Person' is not JSON serializable
cause Trying to use json.dumps directly on a JSONable instance instead of calling to_json().
fix
Call instance.to_json() which returns a JSON string.
error ImportError: cannot import name 'JSONable' from 'jsonable'
cause Version mismatch; older versions had different class names.
fix
Upgrade to latest version: pip install --upgrade jsonable. If using v0.2.1, import Type or Base.
breaking In v0.3.0, fields with None values or empty dicts are omitted from serialized JSON. This can break code expecting all fields to be present.
fix Ensure downstream consumers handle missing keys when deserializing.
deprecated The class names 'Type' and 'Base' used in v0.2.1 are deprecated. Import JSONable directly.
fix Use 'from jsonable import JSONable'.
gotcha JSONable only serializes attributes that are JSON-serializable by default. Custom types require implementing _to_json() and _from_json() methods.
fix Override _to_json() and add classmethod _from_json() for custom field types.

Define a class inheriting from JSONable, call to_json() to serialize and from_json() to deserialize.

from jsonable import JSONable

class Person(JSONable):
    def __init__(self, name, age):
        self.name = name
        self.age = age

p = Person(name="Alice", age=30)
json_str = p.to_json()
print(json_str)
recovered = Person.from_json(json_str)
print(recovered.name, recovered.age)