Expand description
Serializer Builder Pattern
Provides a fluent API for configuring serialization options with sensible defaults. The builder pattern allows users to customize various aspects of serialization without needing to understand all the internal configuration types.
§Thread Safety
Both SerializerBuilder and Serializer implement Send + Sync and can be
safely shared between threads. The serializer methods are stateless and can be
called concurrently:
use std::sync::Arc;
use std::thread;
use serializer::{SerializerBuilder, DxDocument, DxLlmValue};
// Create a shared serializer
let serializer = Arc::new(SerializerBuilder::new().build());
let handles: Vec<_> = (0..4).map(|i| {
let serializer = Arc::clone(&serializer);
thread::spawn(move || {
let mut doc = DxDocument::new();
doc.context.insert("id".to_string(), DxLlmValue::Num(i as f64));
serializer.serialize(&doc)
})
}).collect();
for handle in handles {
let result = handle.join().unwrap();
assert!(!result.is_empty());
}§Example
use serializer::{SerializerBuilder, DxDocument, DxLlmValue};
let mut doc = DxDocument::new();
doc.context.insert("name".to_string(), DxLlmValue::Str("MyApp".to_string()));
// Simple usage with defaults
let serializer = SerializerBuilder::new().build();
let text = serializer.serialize(&doc);
// Advanced configuration
let serializer = SerializerBuilder::new()
.indent_size(4)
.preserve_comments(true)
.expand_keys(false)
.validate_output(true)
.build();
let text = serializer.serialize(&doc);Structs§
- Serializer
- Configured Serializer instance
- Serializer
Builder - Builder for configuring serialization options