Crate facet_json

Source
Expand description

Facet logo - a reflection library for Rust   facet-json

Coverage Status 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:

Ko-fi GitHub Sponsors Patreon Zed built with depot

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 }

Functions§

from_str
Deserializes a JSON string into a value of type T that implements Facet.
from_str_opaque
Deserialize a Poke object from a JSON string.
to_json
Serializes any Facet type to JSON
to_json_string
Serializes any Facet type to JSON and returns it as a String