Interface StateMixin
StateMixin provides Jackson annotations to enable flexible JSON serialization and deserialization of State objects using dynamic property handling. Unlike the other mixins which handle polymorphic types, this mixin focuses on enabling State objects to be serialized as flat JSON objects with arbitrary key-value pairs.
The mixin configures Jackson to:
- Serialize the entire state map as top-level JSON properties using
JsonAnyGetter - Deserialize any JSON properties directly into the state map using
JsonAnySetter - Handle dynamic state properties without requiring predefined fields
- Support arbitrary key-value pairs for flexible state management
Key features:
- Flat serialization: State properties appear as top-level JSON fields
- Dynamic properties: No need to predefine state structure
- Type preservation: Object values maintain their types during serialization
- Bidirectional mapping: Seamless serialization and deserialization
This approach enables State objects to function as flexible containers for agent context, configuration, and runtime data without requiring rigid schemas. It's particularly useful for:
- Agent memory and context persistence
- Dynamic configuration management
- Inter-agent state sharing
- Workflow state preservation
Example JSON representation:
// State object with various properties
{
"currentUser": "john.doe",
"sessionId": "sess-123",
"preferences": {
"theme": "dark",
"language": "en"
},
"counters": {
"interactions": 15,
"errors": 0
}
}
The mixin eliminates the need for wrapper objects or nested structures, providing a clean, flat JSON representation that's easy to work with in both client and server environments.
- Author:
- Pascal Wilbrink
-
Method Summary
-
Method Details
-
getState
Gets the entire state as a map for JSON serialization.This method is annotated with
JsonAnyGetterto instruct Jackson to serialize all map entries as top-level properties in the JSON output. This creates a flat JSON structure where each state key becomes a direct property of the JSON object.The method should return the internal state map containing all key-value pairs that represent the current state of the object.
- Returns:
- a map containing all state key-value pairs to be serialized as top-level JSON properties
-
set
Sets a single key-value pair in the state during JSON deserialization.This method is annotated with
JsonAnySetterto instruct Jackson to call this method for each JSON property that doesn't map to a specific field. This enables dynamic deserialization where any JSON property is automatically added to the state map.The method should store the key-value pair in the internal state map, allowing for flexible state reconstruction from JSON without requiring predefined property structures.
- Parameters:
key- the property name from the JSON objectvalue- the property value from the JSON object, which can be any JSON-compatible type (String, Number, Boolean, Map, List, or null)
-