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:
- Pass serializer/deserializer functions to init
- 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
```
include_in_dict_view: boolstorage: RFieldStorageInitialize an RField with optional custom serialization logic.
collections.abc.Callable[[typing.Any], typing.Any] | Nonecollections.abc.Callable[[typing.Any], typing.Any] | Nonetype | collections.abc.Callable[[], typing.Any] | int | float | str | bool | Nonebool - FalseRFieldStorage - RFieldStorage.KV_STOREDeserialize 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.
AnyAnyGet 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.
AnySerialize 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.
AnyAny