Class JsonStoreSerializationStrategy<T>

java.lang.Object
dev.langchain4j.community.store.embedding.memfile.serialization.JsonStoreSerializationStrategy<T>
Type Parameters:
T - the type of metadata associated with embedded text segments
All Implemented Interfaces:
StoreSerializationStrategy<T>

public class JsonStoreSerializationStrategy<T> extends Object implements StoreSerializationStrategy<T>
JSON-based implementation of 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: