Class JsonStoreSerializationStrategy<T>
- Type Parameters:
T- the type of metadata associated with embedded text segments
- All Implemented Interfaces:
StoreSerializationStrategy<T>
StoreSerializationStrategy for MemFileEmbeddingStore.
This implementation uses Jackson ObjectMapper to serialize and deserialize embedding store data to and from JSON format. It provides a human-readable serialization format that includes proper formatting and custom handling for embedding vectors.
Serialization Features:
- JSON Pretty Printing: Output is formatted with proper indentation for readability
- Custom Embedding Serialization: Float arrays in embeddings are properly serialized/deserialized
- Robust Error Handling: Wraps Jackson exceptions in RuntimeExceptions with clear error messages
- File Operations: Supports atomic file writes with proper directory creation
JSON Structure: The serialized JSON includes:
- Store entries with embedding vectors, IDs, and chunk file references
- Configuration metadata (chunk storage directory, cache size)
- All necessary data to fully restore the embedding store's state
Thread Safety: This class is thread-safe. The static ObjectMapper is configured once and can be safely used by multiple threads concurrently.
Usage Example:
StoreSerializationStrategy<String> strategy = new JsonStoreSerializationStrategy<>();
MemFileEmbeddingStore<String> store = new MemFileEmbeddingStore<>();
// Serialize to JSON string
String json = strategy.serialize(store);
// Serialize to file
Path file = Paths.get("store.json");
strategy.serializeToFile(store, file);
// Deserialize from JSON string
MemFileEmbeddingStore<String> restoredStore = strategy.deserialize(json, store);
// Deserialize from file
MemFileEmbeddingStore<String> restoredStore2 = strategy.deserializeFromFile(file, store);
- Since:
- 1.0.0
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiondeserialize(String json) Deserializes an embedding store from its string representation.deserializeFromFile(Path filePath) Deserializes an embedding store from a file.serialize(MemFileEmbeddingStore<T> store) Serializes the given embedding store to a string representation.voidserializeToFile(MemFileEmbeddingStore<T> store, Path filePath) Serializes the given embedding store directly to a file.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface dev.langchain4j.community.store.embedding.memfile.serialization.StoreSerializationStrategy
deserializeFromFile, serializeToFile
-
Constructor Details
-
JsonStoreSerializationStrategy
public JsonStoreSerializationStrategy()
-
-
Method Details
-
serialize
Serializes the given embedding store to a string representation.This method converts the complete state of the embedding store into a string format that can be persisted, transmitted, or cached. The exact format depends on the implementation (JSON, XML, etc.).
Serialization Content: The serialized string should include:
- All embedding entries with their IDs and vector data
- References to chunk files (not the actual content)
- Store configuration (chunk directory, cache size)
- Any metadata required for complete restoration
Serializes the embedding store to a JSON string with pretty printing enabled. The resulting JSON includes all store entries with their embedding vectors, IDs, chunk file references, and configuration metadata.
- Specified by:
serializein interfaceStoreSerializationStrategy<T>- Parameters:
store- the embedding store to serialize; must not be null- Returns:
- a formatted JSON string representation of the store
- Throws:
IllegalArgumentException- if store is nullRuntimeException- if JSON serialization fails- See Also:
-
serializeToFile
Serializes the given embedding store directly to a file.This method writes the serialized representation of the embedding store directly to the specified file path. The file will be created if it doesn't exist, or overwritten if it does exist. Parent directories will be created as needed. File Operations: The implementation should handle:
- Creating parent directories if they don't exist
- Overwriting existing files atomically where possible
- Proper cleanup in case of write failures
Serializes the embedding store to a JSON file with atomic write operations. If the target directory does not exist, it will be created. If the file already exists, it will be overwritten.
- Specified by:
serializeToFilein interfaceStoreSerializationStrategy<T>- Parameters:
store- the embedding store to serialize; must not be nullfilePath- the path where the JSON file will be written; must not be null- Throws:
IllegalArgumentException- if store or filePath is nullRuntimeException- if file I/O operations fail- See Also:
-
deserialize
Deserializes an embedding store from its string representation.This method reconstructs a
MemFileEmbeddingStorefrom data that was previously created byStoreSerializationStrategy.serialize(MemFileEmbeddingStore). The deserialized store will have the same configuration and embedding entries as the original.Restoration Process: Deserialization typically involves:
- Parsing the serialized format to extract metadata
- Recreating the store with original configuration
- Restoring all embedding entries and their references
- Setting up internal structures (cache, etc.) but not preloading content
Dependencies: The deserialized store requires:
- Access to the original chunk storage directory
- All referenced chunk files must exist and be readable
- Proper file permissions for the chunk directory and files
Deserializes a JSON string back into a
MemFileEmbeddingStoreinstance. The JSON is parsed and validated, then used to reconstruct the store with all its entries, embeddings, and configuration settings.- Specified by:
deserializein interfaceStoreSerializationStrategy<T>- Parameters:
json- the JSON string representation of a serialized store; must not be null or blank- Returns:
- a new MemFileEmbeddingStore instance restored from the JSON data
- Throws:
IllegalArgumentException- if json is null or blankRuntimeException- if JSON parsing or deserialization fails- See Also:
-
deserializeFromFile
Deserializes an embedding store from a file.This method reads the serialized data from the specified file and reconstructs the embedding store. The file must contain data that was previously created by
StoreSerializationStrategy.serializeToFile(MemFileEmbeddingStore, Path)or compatible serialization method.File Requirements: The file must:
- Exist and be readable
- Contain valid serialized store data
- Be in the format expected by this strategy implementation
- Not be corrupted or partially written
Deserializes a
MemFileEmbeddingStorefrom a JSON file. The entire file is read into memory as a string, then passed todeserialize(String)for processing.- Specified by:
deserializeFromFilein interfaceStoreSerializationStrategy<T>- Parameters:
filePath- the path to the JSON file containing serialized store data; must not be null- Returns:
- a new MemFileEmbeddingStore instance restored from the file data
- Throws:
IllegalArgumentException- if filePath is nullRuntimeException- if file I/O or JSON deserialization fails- See Also:
-