#json-parser #json #facet #deserialize #serialization

facet-json

JSON serialization and deserialization support for Facet traits

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

Download history 1758/week @ 2025-04-06

1,762 downloads per month

MIT/Apache

460KB
10K SLoC

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 }

Dependencies

~240KB