Expand description
Serialization and deserialization system for Train Station objects
This module provides a robust, zero-dependency serialization framework that enables persistent storage and model checkpointing for all Train Station objects. The system supports both human-readable JSON format for debugging and efficient binary format for production deployment.
§Design Philosophy
The serialization system follows Train Station’s core principles:
- Zero external dependencies: Uses only the standard library
- Maximum performance: Optimized binary format for production use
- Safety first: Comprehensive validation and error handling
- Future-proof: Generic trait-based design for extensibility
§Supported Formats
§JSON Format
Human-readable format suitable for:
- Model inspection and debugging
- Configuration files and version control
- Cross-language interoperability
- Development and testing workflows
§Binary Format
Optimized binary format suitable for:
- Production model deployment
- High-frequency checkpointing
- Network transmission and storage
- Memory and storage-constrained environments
§Organization
core/- Core serialization types, traits, and functionalitybinary/- Binary format serialization and deserializationjson/- JSON format serialization and deserialization
§Examples
Basic serialization usage:
use train_station::serialization::{StructSerializer, StructDeserializer, Format};
use std::collections::HashMap;
// Create a simple data structure
let mut data = HashMap::new();
data.insert("name".to_string(), "test".to_string());
data.insert("value".to_string(), "42".to_string());
// Serialize to JSON
let serializer = StructSerializer::new()
.field("data", &data)
.field("version", &1u32);
let json = serializer.to_json().unwrap();
assert!(json.contains("test"));
// Deserialize from JSON
let mut deserializer = StructDeserializer::from_json(&json).unwrap();
let loaded_data: HashMap<String, String> = deserializer.field("data").unwrap();
let version: u32 = deserializer.field("version").unwrap();
assert_eq!(loaded_data.get("name").unwrap(), "test");
assert_eq!(version, 1);§Thread Safety
All serialization operations are thread-safe and can be performed concurrently on different objects. The underlying file I/O operations use standard library primitives that provide appropriate synchronization.
§Error Handling
The serialization system provides comprehensive error handling through the
SerializationError type, which includes detailed information about what
went wrong during serialization or deserialization. All operations return
Result types to ensure errors are handled explicitly.
Structs§
- Struct
Deserializer - Deserializer for structured data extraction and type conversion
- Struct
Serializer - Builder for serializing structured data with fluent interface
Enums§
- Field
Value - Universal type-safe container for all serializable field values
- Format
- Serialization format options for saving and loading objects
- Serialization
Error - Main error type for serialization operations
Traits§
- From
Field Value - Trait for converting FieldValue back to concrete types during deserialization
- Serializable
- Core serialization trait for Train Station objects
- Struct
Serializable - Trait for structs that can be serialized and deserialized using the struct builder pattern
- ToField
Value - Trait for converting values to FieldValue for serialization
Type Aliases§
- Serialization
Result - Result type for serialization operations