jangada.serialization.Serializable.serialize#
- static Serializable.serialize(obj: Any, is_copy: bool = False) Any#
Recursively serialize an object to a dictionary structure.
Converts Python objects to a nested dictionary structure suitable for JSON, HDF5 attributes, or other storage formats. Handles Serializable objects, collections, primitives, and dataset types.
- Parameters:
- objAny
The object to serialize.
- is_copybool, optional
If True, only serialize copiable properties. If False, serialize all properties. Default is False.
- Returns:
- Any
The serialized representation: - Serializable → dict with ‘__class__’ key - list/tuple/set → list (recursively serialized) - dict → dict (recursively serialized values) - Primitives → unchanged - None → None
- Raises:
- TypeError
If the object’s type is not registered as primitive, dataset, or Serializable.
See also
deserializeReconstruct objects from serialized data
copyCreate a copy using serialize with is_copy=True
Notes
The serialization format for Serializable objects is: ```python {
‘__class__’: ‘module.ClassName’, ‘property1’: value1, ‘property2’: value2, …
}#
Tuples and sets are converted to lists (Python’s JSON doesn’t distinguish these).
Examples
Serialize a simple object:
>>> class MyClass(Serializable): ... value = SerializableProperty(default=0) ... >>> obj = MyClass(value=42) >>> data = Serializable.serialize(obj) >>> '__class__' in data True >>> data['value'] 42
Serialize with copy mode (only copiable properties):
>>> class MyClass2(Serializable): ... important = SerializableProperty(default=0, copiable=True) ... cache = SerializableProperty(default=0, copiable=False) ... >>> obj = MyClass2(important=10, cache=20) >>> data = Serializable.serialize(obj, is_copy=True) >>> 'important' in data True >>> 'cache' in data False
Collections are handled recursively:
>>> data = Serializable.serialize([1, "two", 3.0]) >>> data [1, 'two', 3.0]