17 releases
Uses new Rust 2024
new 0.2.2 | Apr 12, 2025 |
---|---|
0.2.1 | Apr 12, 2025 |
0.1.15 | Apr 11, 2025 |
#1954 in Encoding
1,762 downloads per month
460KB
10K
SLoC
facet-json
Logo by Misiasart
Thanks to all individual and corporate sponsors, without whom this work could not exist:
JSON serialization and deserialization for facet.
Usage
Serialization Example
use facet::Facet;
use facet_reflect::Peek;
use facet_json::to_json_string;
#[derive(facet::Facet)]
struct Person {
name: String,
age: u32,
}
// Create a struct to serialize
let person = Person {
name: "Alice".to_string(),
age: 30,
};
// Create a Peek object from the struct
let peek = Peek::new(&person);
// Serialize to JSON (true = pretty-print)
let json = to_json_string(peek, true);
println!("{}", json);
// Output:
// {
// "name": "Alice",
// "age": 30
// }
Deserialization Example
use facet::Facet;
use facet_json::from_str;
#[derive(facet::Facet, Debug)]
struct Person {
name: String,
age: u32,
}
// JSON string to deserialize
let json = r#"{"name":"Bob","age":25}"#;
// Deserialize the JSON to a Person struct
let person: Person = from_str(json).unwrap();
println!("{:?}", person);
// Output: Person { name: "Bob", age: 25 }
Dependencies
~240KB