Menu
Introduction
Getting Started
Developer guide
Release
Architecture
CLI
Technical documentations
Version
Publication date

Mar 9, 2022

Confidentiality
Public
Reactions
1
Share

SerializableRField

Resource field for storing custom objects with manual JSON serialization.

SerializableRField stores instances of SerializableObjectJson subclasses, which provide custom serialize() and deserialize() methods for converting between Python objects and JSON-compatible representations.

When a Resource is saved: - The object's serialize() method is called - The resulting JSON data is stored in KV_STORE

When a Resource is loaded: - The JSON data is retrieved - The object's deserialize() class method is called to reconstruct the object

This is useful for: - Custom objects with specific serialization requirements - Legacy objects that need special handling - Objects that can't use Pydantic (use ModelRField for Pydantic models) - Fine-grained control over serialization format

Storage behavior: - Stored in KV_STORE by default - Not included in dict views by default (can be changed) - Uses custom serialize/deserialize methods

Example: ```python class CustomData(SerializableObjectJson): def init(self, value: int = 0): self.value = value

    def serialize(self) -> Dict:
        return {'value': self.value}

    @classmethod
    def deserialize(cls, data: Dict) -> 'CustomData':
        return cls(value=data['value'])

class MyResource(Resource):
    # Default value is object_type() (new instance)
    data = SerializableRField(object_type=CustomData)

    # Included in dict view
    visible_data = SerializableRField(
        object_type=CustomData,
        include_in_dict_view=True
    )

# Usage
resource = MyResource()
resource.data = CustomData(value=42)
```

Note: - The object_type must be a subclass of SerializableObjectJson - Default value is a new instance of object_type (calls object_type()) - The object_type must have a no-argument constructor

Attributes
include_in_dict_view: boolobject_type: typestorage: RFieldStorage
Functions
__init__

Initialize a SerializableRField for storing custom serializable objects.

object_type : type
The SerializableObjectJson subclass that this field will store. Must have a no-argument constructor. A new instance is created as the default value.
include_in_dict_view : bool - False
If True, this field is included in dict/JSON representations. Set to False for large or complex objects. Defaults to False
deserialize

Deserialize JSON data back to a SerializableObjectJson instance.

This method is called when loading the field from storage. It delegates to the object's deserialize() class method to reconstruct the object.

r_field_value : Any
JSON-compatible data produced by serialize(), or None
Return type : SerializableObjectJson
get_default_value

Get the default value for this field.

If the default value is a Type or Callable, it will be called to generate a new default value. Otherwise, the default value is returned directly.

This ensures that mutable defaults (like lists or dicts) are not shared between Resource instances.

Return type : Any
serialize

Serialize a SerializableObjectJson instance to JSON-compatible data.

This method is called when saving the field to storage. It delegates to the object's serialize() method to convert it to JSON-compatible format.

r_field_value : SerializableObjectJson
The SerializableObjectJson instance to serialize, or None
Return type : Any
Shine Logo
Technical bricks to reuse or customize

Have you developed a brick?

Share it to accelerate projects for the entire community.