| Crates.io | serde_rson |
| lib.rs | serde_rson |
| version | 1.0.0 |
| created_at | 2025-05-31 21:13:19.04619+00 |
| updated_at | 2025-05-31 21:24:11.115418+00 |
| description | Serde integration for RSON |
| homepage | https://rson.dev |
| repository | https://github.com/RSON-Rust-Serialized-Object-Notation/RSON-core |
| max_upload_size | |
| id | 1696647 |
| size | 14,060 |
Serde integration for RSON (Rust Serialized Object Notation)
serde_rson provides seamless Serde integration for RSON, allowing you to serialize and deserialize Rust structs and enums to/from RSON format with zero boilerplate.
Key Features:
serde_json[dependencies]
serde_rson = "1.0.0"
serde = { version = "1.0", features = ["derive"] }
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)]
struct Config {
name: String,
version: String,
database: DatabaseConfig,
features: Vec<String>,
}
#[derive(Serialize, Deserialize, Debug)]
struct DatabaseConfig {
host: String,
port: Option<u16>,
ssl: bool,
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Parse RSON with comments and rich types
let rson_text = r#"
// Application configuration
Config(
name: "My App",
version: "1.0.0",
database: DatabaseConfig(
host: "localhost",
port: Some(5432), // Optional port
ssl: true,
),
features: ["auth", "logging"], // Trailing comma OK
)
"#;
// Deserialize from RSON
let config: Config = serde_rson::from_str(rson_text)?;
println!("Config: {:#?}", config);
// Serialize to RSON
let rson_output = serde_rson::to_string(&config)?;
println!("RSON output:\n{}", rson_output);
Ok(())
}
use serde_rson;
// From string
let value: MyStruct = serde_rson::from_str(rson_text)?;
// From reader (file, network, etc.)
let value: MyStruct = serde_rson::from_reader(reader)?;
// From bytes
let value: MyStruct = serde_rson::from_slice(rson_bytes)?;
use serde_rson;
// To string
let rson_text = serde_rson::to_string(&my_struct)?;
// To string with pretty printing
let rson_text = serde_rson::to_string_pretty(&my_struct)?;
// To writer (file, network, etc.)
serde_rson::to_writer(writer, &my_struct)?;
// To bytes
let rson_bytes = serde_rson::to_vec(&my_struct)?;
#[derive(Serialize, Deserialize)]
enum Message {
Text(String),
Image { url: String, width: u32, height: u32 },
Video { url: String, duration: f64 },
}
// RSON representation
let rson = r#"
Message::Image(
url: "https://example.com/image.jpg",
width: 800,
height: 600,
)
"#;
// JSON would need workarounds like:
// {"Image": {"url": "...", "width": 800, "height": 600}}
#[derive(Serialize, Deserialize)]
struct User {
name: String,
email: Option<String>,
phone: Option<String>,
}
// RSON with explicit optionals
let rson = r#"
User(
name: "John Doe",
email: Some("john@example.com"),
phone: None, // Explicit null
)
"#;
#[derive(Serialize, Deserialize)]
struct ServerConfig {
host: String,
port: u16,
workers: u8,
timeout: u64,
}
// RSON with documentation
let rson = r#"
// Production server configuration
ServerConfig(
host: "0.0.0.0", // Bind to all interfaces
port: 8080, // HTTP port
workers: 4, // Number of worker threads
timeout: 30, // Request timeout in seconds
)
"#;
use serde_rson::Serializer;
use serde::Serialize;
let mut serializer = Serializer::new();
serializer.pretty(true);
serializer.comments(true);
my_struct.serialize(&mut serializer)?;
let rson_text = serializer.into_string();
use serde_rson::Deserializer;
use serde::Deserialize;
let mut deserializer = Deserializer::new(rson_text);
deserializer.strict_mode(false); // Allow trailing commas
let value = MyStruct::deserialize(&mut deserializer)?;
use serde_rson::StreamDeserializer;
// Parse multiple RSON values from a stream
let stream = StreamDeserializer::new(reader);
for value in stream {
let config: Config = value?;
// Process each config...
}
# Before
[dependencies]
serde_json = "1.0"
# After
[dependencies]
serde_rson = "0.1.0"
// Before
use serde_json;
// After
use serde_rson;
// Before
let value: MyStruct = serde_json::from_str(json_text)?;
let json_text = serde_json::to_string(&value)?;
// After
let value: MyStruct = serde_rson::from_str(rson_text)?;
let rson_text = serde_rson::to_string(&value)?;
// Now you can use RSON-specific features
let rson_with_comments = r#"
// This is a comment!
Config(
name: "My App",
debug: true, // Trailing comma
)
"#;
let config: Config = serde_rson::from_str(rson_with_comments)?;
RSON performance is comparable to JSON for typical use cases:
| Operation | serde_json | serde_rson | Notes |
|---|---|---|---|
| Parse | Baseline | ~95% | Slightly slower due to richer parsing |
| Serialize | Baseline | ~100% | Same performance |
| File Size | Baseline | ~95% | More compact with unquoted keys |
Benchmarks on typical configuration files
use serde_rson::ser::{Serializer, Config};
let config = Config {
pretty: true, // Pretty-print output
indent: " ".to_string(), // Indentation string
comments: false, // Include comments in output
trailing_commas: true, // Add trailing commas
};
let serializer = Serializer::with_config(config);
use serde_rson::de::{Deserializer, Config};
let config = Config {
strict_mode: false, // Allow RSON extensions
max_depth: 128, // Maximum nesting depth
allow_comments: true, // Parse comments
allow_trailing_commas: true, // Allow trailing commas
};
let deserializer = Deserializer::with_config(rson_text, config);
#[cfg(test)]
mod tests {
use super::*;
use serde_rson;
#[test]
fn test_roundtrip() {
let original = Config {
name: "Test".to_string(),
version: "1.0".to_string(),
// ... other fields
};
// Serialize to RSON
let rson_text = serde_rson::to_string(&original).unwrap();
// Deserialize back
let parsed: Config = serde_rson::from_str(&rson_text).unwrap();
// Should be identical
assert_eq!(original, parsed);
}
}
We welcome contributions! Please see our Contributing Guide.
Areas we need help with:
This project is licensed under the MIT License - see the LICENSE file for details.
Made with ๐ฆ by the RSON community