<h1>
<picture>
<source srcset="https://github.com/facet-rs/facet/raw/main/static/logo-v2/logo-only.webp">
<img src="https://github.com/facet-rs/facet/raw/main/static/logo-v2/logo-only.png" height="35" alt="Facet logo - a reflection library for Rust">
</picture> facet-json
</h1>
[](https://coveralls.io/github/facet-rs/facet?branch=main)
[](https://github.com/fasterthanlime/free-of-syn)
[](https://crates.io/crates/facet-json)
[](https://docs.rs/facet-json)
[](./LICENSE)
_Logo by [Misiasart](https://misiasart.com/)_
Thanks to all individual and corporate sponsors, without whom this work could not exist:
<p> <a href="https://ko-fi.com/fasterthanlime">
<picture>
<source media="(prefers-color-scheme: dark)" srcset="https://github.com/facet-rs/facet/raw/main/static/sponsors-v2/ko-fi-dark.svg">
<img src="https://github.com/facet-rs/facet/raw/main/static/sponsors-v2/ko-fi-light.svg" height="40" alt="Ko-fi">
</picture>
</a> <a href="https://github.com/sponsors/fasterthanlime">
<picture>
<source media="(prefers-color-scheme: dark)" srcset="https://github.com/facet-rs/facet/raw/main/static/sponsors-v2/github-dark.svg">
<img src="https://github.com/facet-rs/facet/raw/main/static/sponsors-v2/github-light.svg" height="40" alt="GitHub Sponsors">
</picture>
</a> <a href="https://patreon.com/fasterthanlime">
<picture>
<source media="(prefers-color-scheme: dark)" srcset="https://github.com/facet-rs/facet/raw/main/static/sponsors-v2/patreon-dark.svg">
<img src="https://github.com/facet-rs/facet/raw/main/static/sponsors-v2/patreon-light.svg" height="40" alt="Patreon">
</picture>
</a> <a href="https://zed.dev">
<picture>
<source media="(prefers-color-scheme: dark)" srcset="https://github.com/facet-rs/facet/raw/main/static/sponsors-v2/zed-dark.svg">
<img src="https://github.com/facet-rs/facet/raw/main/static/sponsors-v2/zed-light.svg" height="40" alt="Zed">
</picture>
</a> <a href="https://depot.dev?utm_source=facet">
<img src="https://depot.dev/badges/built-with-depot.svg" alt="built with depot">
</a> </p>
JSON serialization and deserialization for [facet](https://crates.io/crates/facet).
## Usage
### Serialization Example
```rust
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
```rust
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 }
```