1 unstable release
0.1.0 | Feb 24, 2025 |
---|
#1013 in Encoding
127 downloads per month
30KB
658 lines
SerialZero
A minimalist JSON parsing and serialization library for Rust.
Features
- Zero dependencies: No external libraries, keeping your project lean.
- Optimized for speed: Written with performance in mind, for both parsing and serializing JSON.
- Minimal API: Simple, clean API that's easy to use without boilerplate.
- Customizable: Allows customization for use cases like date formatting or naming conventions (camelCase, snake_case).
- Portable: Ideal for projects with strict size limitations (embedded systems, webassembly, etc.).
Installation
Add SerialZero to your Cargo.toml
:
[dependencies]
serialzero = "0.1.0"
Quick Start
use serialzero::{json, parse, to_string_pretty, JsonValueExt};
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Parse JSON
let input = r#"{"name":"SerialZero","version":"0.1.0"}"#;
let parsed = parse(input)?;
// Access values
let name = parsed.get("name").and_then(|v| v.as_string()).unwrap_or_default();
println!("Name: {}", name);
// Create JSON with macro
let data = json!({
"id" => 1234,
"username" => "rustacean",
"tags" => ["json", "rust", "serialization"]
});
// Serialize to pretty-printed JSON
let output = to_string_pretty(&data);
println!("JSON: \n{}", output);
Ok(())
}
Working with JSON Values
SerialZero makes it easy to create, access, and modify JSON data:
use serialzero::{JsonValue, json};
use std::collections::HashMap;
// Create JSON values
let null = JsonValue::Null;
let bool_value = JsonValue::Boolean(true);
let number = JsonValue::Number(42.5);
let string = JsonValue::String("hello".to_string());
let array = JsonValue::Array(vec![JsonValue::Number(1.0), JsonValue::Number(2.0)]);
// Build an object
let mut obj = HashMap::new();
obj.insert("name".to_string(), JsonValue::String("SerialZero".to_string()));
obj.insert("version".to_string(), JsonValue::String("0.1.0".to_string()));
let object = JsonValue::Object(obj);
// Or use the convenient macro
let user = json!({
"id" => 1234,
"name" => "Alice",
"emails" => ["alice@example.com", "alice.work@example.com"]
});
// Access data with the extension trait
if let Some(id) = user.get("id").and_then(|v| v.as_number()) {
println!("User ID: {}", id);
}
// Navigate nested structures
if let Some(emails) = user.get("emails").and_then(|v| v.as_array()) {
for email in emails {
if let Some(email_str) = email.as_string() {
println!("Email: {}", email_str);
}
}
}
Customization Options
SerialZero provides customization options for serialization:
use serialzero::{JsonValue, SerializeOptions, to_string_with_options};
let options = SerializeOptions {
pretty: true,
indent: " ".to_string(), // 4 spaces
use_snake_case: true, // Convert camelCase to snake_case
date_format: Some("%Y-%m-%d".to_string()),
};
let json = to_string_with_options(&value, &options);
Error Handling
SerialZero provides detailed error information:
use serialzero::{parse, JsonError};
match parse("{invalid json}") {
Ok(value) => println!("Parsed: {:?}", value),
Err(JsonError::UnexpectedToken(c)) => println!("Unexpected token: {}", c),
Err(e) => println!("Error: {}", e),
}
Why SerialZero?
SerialZero aims to fill the niche between full-featured libraries like serde_json and minimalist parsers. It offers:
- Zero dependencies for leaner projects
- Simplified API for common JSON operations
- Fast parsing and serialization with minimal overhead
- Format customization (camelCase/snake_case, pretty-printing)
- Helper functions and traits for ergonomic JSON manipulation
Performance Considerations
SerialZero is designed with performance in mind, but it prioritizes a clean API and zero dependencies over absolute maximum performance. For extremely performance-critical applications where you need the fastest possible JSON handling, consider benchmarking against alternatives.