facet-json 0.2.0

JSON serialization and deserialization support for Facet traits
Documentation

experimental free of syn crates.io documentation MIT/Apache-2.0 licensed

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,
}

fn main() {
    // 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,
}

fn main() {
    // 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 }
}

License

Licensed under either of:

at your option.