@nyx/serde — Unified Multi-Format Serialization Framework
A unified serialization and deserialization architecture supporting JSON, YAML, TOML, MessagePack, and CBOR with zero heap allocations and compile-time type introspection.
📄 Text Formats (JSON, YAML, TOML)
Seamlessly serialize structs to human-readable configs or Web API responses with exact formatting control.
⚡ Binary Formats (MsgPack & CBOR)
Compact, high-density binary serialization for microsecond IPC and network RPC stream pipelines.
🛡️ Zero-Allocation Region Streaming
Parses incoming payloads directly into caller region frames without garbage collection pauses.
🔄 Single Generic API
Write data structures once; serialize across any format with a single method call: serde::to_string(val, format).
1. Installation
# Install via Nyx Package Manager
nypm add @nyx/serde
2. Comprehensive Practical Examples
Load application server settings from a YAML or TOML file, validate settings, and export status payloads as JSON.
use @nyx/serde;
struct DatabaseConfig {
pub host: str,
pub port: u16,
pub max_connections: u32,
pub enable_ssl: bool,
}
struct AppConfig {
pub app_name: str,
pub environment: str,
pub db: DatabaseConfig,
}
fn main() {
// 1. Create a structured configuration object
let config = AppConfig {
app_name: "Sovereign Payment Gateway",
environment: "production",
db: DatabaseConfig {
host: "postgres.internal.cloud",
port: 5432,
max_connections: 250,
enable_ssl: true,
},
};
// 2. Serialize to formatted YAML (for Kubernetes / DevOps configs)
let yaml_out = serde::to_string(config, DataFormat::Yaml);
println("--- YAML Configuration ---");
println(yaml_out);
// 3. Serialize to standard JSON (for REST API responses)
let json_out = serde::to_string(config, DataFormat::Json);
println("\n--- JSON Payload ---");
println(json_out);
// 4. Serialize to TOML (for package manifests)
let toml_out = serde::to_string(config, DataFormat::Toml);
println("\n--- TOML Manifest ---");
println(toml_out);
}
Encode 1,000,000 telemetry packets into dense MessagePack binary format for high-speed inter-process communication.
use @nyx/serde;
struct TelemetryEvent {
pub device_id: u64,
pub timestamp_ns: u64,
pub voltage: f32,
pub temperature_c: f32,
pub is_alert: bool,
}
fn encode_network_packet(event: TelemetryEvent) -> [u8] {
// Zero-heap MessagePack encoding
let binary_bytes = serde::to_bytes(event, DataFormat::MsgPack);
println("Encoded telemetry packet into {binary_bytes.len()} binary bytes.");
return binary_bytes;
}
3. Function Reference
| Signature | Parameters | Return | Description |
|---|---|---|---|
serde::to_string<T>(val: T, format: DataFormat) |
val: T, format: DataFormat |
str |
Serializes generic struct T into UTF-8 text string (JSON, YAML, or TOML). |
serde::to_bytes<T>(val: T, format: DataFormat) |
val: T, format: DataFormat |
[u8] |
Encodes generic struct T into compact binary byte array (MessagePack or CBOR). |
serde::from_string<T>(raw: str, format: DataFormat) |
raw: str, format: DataFormat |
T |
Parses text payload into typed struct T using zero-copy regional allocation. |
serde::from_bytes<T>(bytes: [u8], format: DataFormat) |
bytes: [u8], format: DataFormat |
T |
Decodes binary payload directly into structured representation in memory. |