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

Mar 9, 2022

Confidentiality
Public
Reactions
1
Share

RField

Resource field with custom serialization/deserialization support.

RField extends BaseRField to provide custom serialization and deserialization logic. This is useful when the field value needs special handling for storage (e.g., complex objects, encrypted data, compressed content).

The field is automatically persisted when a Resource is output from a task and automatically restored when the Resource is loaded as input to another task.

Custom serialization can be implemented in two ways:

  1. Pass serializer/deserializer functions to init
  2. Subclass RField and override the serialize/deserialize methods

Attributes: _deserializer: Optional function to convert stored values to runtime values _serializer: Optional function to convert runtime values to storable values

Example: ```python import json

class MyResource(Resource):
    # Using serializer/deserializer functions
    metadata = RField(
        serializer=lambda x: json.dumps(x),
        deserializer=lambda x: json.loads(x),
        default_value=dict,
        storage=RFieldStorage.KV_STORE
    )

    # Simple field with no custom serialization
    description = RField(storage=RFieldStorage.KV_STORE)
```

```python
# Alternative: Subclass approach
class JsonRField(RField):
    def serialize(self, value):
        return json.dumps(value) if value is not None else None

    def deserialize(self, value):
        return json.loads(value) if value is not None else None
```
Attributes
include_in_dict_view: boolstorage: RFieldStorage
Functions
__init__

Initialize an RField with optional custom serialization logic.

deserializer : collections.abc.Callable[[typing.Any], typing.Any] | None
Function to deserialize stored values (storage -> runtime). Signature: (stored_value: Any) -> runtime_value: Any If None, no custom deserialization is performed. Defaults to None
serializer : collections.abc.Callable[[typing.Any], typing.Any] | None
Function to serialize runtime values (runtime -> storage). Signature: (runtime_value: Any) -> stored_value: Any If None, no custom serialization is performed. Defaults to None
default_value : type | collections.abc.Callable[[], typing.Any] | int | float | str | bool | None
Default value for the field. Can be a primitive, Type, or Callable. See BaseRField.__init__ for details. Defaults to None
include_in_dict_view : bool - False
Whether to include this field in dict/JSON views. Use False for large or sensitive data. Defaults to False
storage : RFieldStorage - RFieldStorage.KV_STORE
Storage backend for persistence (DATABASE, KV_STORE, or NONE). Defaults to KV_STORE for efficient storage of larger values
deserialize

Deserialize a stored value into the field's runtime representation.

This method is called when loading a field value from storage. If a custom deserializer function was provided during initialization, it will be used. Otherwise, the value is returned as-is.

Can be overridden in subclasses to implement class-level deserialization logic.

r_field_value : Any
The raw value retrieved from storage
Return type : Any
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 the field's runtime value into a storable representation.

This method is called when saving a field value to storage. If a custom serializer function was provided during initialization, it will be used. Otherwise, the value is returned as-is.

Can be overridden in subclasses to implement class-level serialization logic.

r_field_value : Any
The current runtime value in the Resource
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.