use serde::{Deserialize, Serialize};
use serde_toon::to_string_pretty;
use std::error::Error;
#[derive(Debug, Serialize, Deserialize)]
struct User {
id: u32,
name: String,
email: String,
active: bool,
}
#[derive(Debug, Serialize, Deserialize)]
struct ApiResponse {
users: Vec<User>,
total: u32,
page: u32,
}
fn main() -> Result<(), Box<dyn Error>> {
let response = ApiResponse {
users: vec![
User {
id: 1,
name: "Alice Johnson".into(),
email: "alice@example.com".into(),
active: true,
},
User {
id: 2,
name: "Bob Smith".into(),
email: "bob@example.com".into(),
active: true,
},
User {
id: 3,
name: "Charlie Brown".into(),
email: "charlie@example.com".into(),
active: false,
},
],
total: 3,
page: 1,
};
let json = serde_json::to_string_pretty(&response)?;
println!("JSON ({} chars):\n{}\n", json.len(), json);
let toon = to_string_pretty(&response)?;
println!("TOON ({} chars):\n{}\n", toon.len(), toon);
let savings = ((json.len() - toon.len()) as f64 / json.len() as f64) * 100.0;
println!(
"✓ Token savings: {:.1}% ({} → {} chars)",
savings,
json.len(),
toon.len()
);
Ok(())
}