๐ฆ serde_rson
Serde integration for RSON (Rust Serialized Object Notation)
๐ฏ What is serde_rson?
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:
- Drop-in replacement for
serde_json - Rich type support - Structs, enums, options, tuples
- Comments and trailing commas - Developer-friendly format
- JSON compatibility - Every JSON is valid RSON
- High performance - Built on efficient Rust parsing
๐ Quick Start
Installation
[]
= "1.0.0"
= { = "1.0", = ["derive"] }
Basic Usage
use ;
๐ API Reference
Deserialization
use serde_rson;
// From string
let value: MyStruct = from_str?;
// From reader (file, network, etc.)
let value: MyStruct = from_reader?;
// From bytes
let value: MyStruct = from_slice?;
Serialization
use serde_rson;
// To string
let rson_text = to_string?;
// To string with pretty printing
let rson_text = to_string_pretty?;
// To writer (file, network, etc.)
to_writer?;
// To bytes
let rson_bytes = to_vec?;
๐จ RSON vs JSON Examples
Rich Enums
// 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}}
Optional Fields
// RSON with explicit optionals
let rson = r#"
User(
name: "John Doe",
email: Some("john@example.com"),
phone: None, // Explicit null
)
"#;
Comments in Configuration
// 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
)
"#;
๐ง Advanced Features
Custom Serialization
use Serializer;
use Serialize;
let mut serializer = new;
serializer.pretty;
serializer.comments;
my_struct.serialize?;
let rson_text = serializer.into_string;
Custom Deserialization
use Deserializer;
use Deserialize;
let mut deserializer = new;
deserializer.strict_mode; // Allow trailing commas
let value = deserialize?;
Streaming
use StreamDeserializer;
// Parse multiple RSON values from a stream
let stream = new;
for value in stream
๐ฏ Migration from serde_json
1. Update Dependencies
# Before
[]
= "1.0"
# After
[]
= "0.1.0"
2. Update Imports
// Before
use serde_json;
// After
use serde_rson;
3. Replace Function Calls
// Before
let value: MyStruct = from_str?;
let json_text = to_string?;
// After
let value: MyStruct = from_str?;
let rson_text = to_string?;
4. Enjoy RSON Features
// 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 = from_str?;
๐ Performance
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
๐ ๏ธ Configuration
Serializer Options
use ;
let config = Config ;
let serializer = with_config;
Deserializer Options
use ;
let config = Config ;
let deserializer = with_config;
๐งช Testing
๐ Documentation
- API Documentation - Complete API reference
- Serde Documentation - Learn about Serde
- RSON Specification - Language specification
- Examples - Usage examples
๐ค Contributing
We welcome contributions! Please see our Contributing Guide.
Areas we need help with:
- Performance optimizations
- Additional Serde features
- Better error messages
- Documentation and examples
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
๐ Related Projects
- rson-core - Core RSON parsing library
- rson-cli - Command-line tools
- rson-schema - Schema validation
- RSON Website - Documentation and playground
Made with ๐ฆ by the RSON community