Skip to content

Instantly share code, notes, and snippets.

@TeamDman
Last active August 6, 2026 04:31
Show Gist options
  • Select an option

  • Save TeamDman/c73f65b714554a507404125ad46ba75e to your computer and use it in GitHub Desktop.

Select an option

Save TeamDman/c73f65b714554a507404125ad46ba75e to your computer and use it in GitHub Desktop.
Facet vs Serde flatten
[package]
name = "serde-facet-flatten-comparison"
version = "0.1.0"
edition = "2024"
[dependencies]
facet = "0.46.5"
facet-json = "0.46.1"
serde = { version = "1.0.229", features = ["derive"] }
serde_json = "1.0.151"
serde-facet-flatten-comparison on  main [?] is 📦 v0.1.0 via 🦀 v1.96.0 took 6s
❯ cargo run --example facet
Compiling serde-facet-flatten-comparison v0.1.0 (G:\Programming\Repos\serde-facet-flatten-comparison)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 1.00s
Running `target\debug\examples\facet.exe`
{
"foo": 0,
"bar": "",
"foo": 0,
"baz": false,
"baz": false,
"quux": [
0,
0
]
}
...but does it parse?
Err(
DeserializeError { span: [0..0), path: <root>, kind: solver error: failed to build schema: Duplicate field name 'foo' from different sources: thingy.foo vs other_thingy.foo. This usually means a parent struct and a flattened struct both define a field with the same name. },
)
#[derive(Debug, Default, facet::Facet)]
pub struct MyThingy {
foo: usize,
bar: String,
}
#[derive(Debug, Default, facet::Facet)]
pub struct MyOtherThingy {
foo: usize,
baz: bool,
}
#[derive(Debug, Default, facet::Facet)]
pub struct MyThirdThingy {
baz: bool,
quux: (usize, usize)
}
#[derive(Debug, Default, facet::Facet)]
pub struct MyContainer {
#[facet(flatten)]
thingy: MyThingy,
#[facet(flatten)]
other_thingy: MyOtherThingy,
#[facet(flatten)]
third_thingy: MyThirdThingy
}
fn main() {
let container = MyContainer::default();
let json = facet_json::to_string_pretty(&container).unwrap();
println!("{json}\n");
println!("...but does it parse?");
println!("{:#?}", facet_json::from_str::<MyContainer>(&json));
}
serde-facet-flatten-comparison on  main [?] is 📦 v0.1.0 via 🦀 v1.96.0
❯ cargo run --example serde
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.11s
Running `target\debug\examples\serde.exe`
{
"foo": 0,
"bar": "",
"foo": 0,
"baz": false,
"baz": false,
"quux": [
0,
0
]
}
...but does it parse?
Err(
Error("duplicate field `foo`", line: 11, column: 1),
)
use serde;
use serde_json;
#[derive(Debug, Default, serde::Serialize, serde::Deserialize)]
pub struct MyThingy {
foo: usize,
bar: String,
}
#[derive(Debug, Default, serde::Serialize, serde::Deserialize)]
pub struct MyOtherThingy {
foo: usize,
baz: bool,
}
#[derive(Debug, Default, serde::Serialize, serde::Deserialize)]
pub struct MyThirdThingy {
baz: bool,
quux: (usize, usize)
}
#[derive(Debug, Default, serde::Serialize, serde::Deserialize)]
pub struct MyContainer {
#[serde(flatten)]
thingy: MyThingy,
#[serde(flatten)]
other_thingy: MyOtherThingy,
#[serde(flatten)]
third_thingy: MyThirdThingy
}
fn main() {
let container = MyContainer::default();
let json = serde_json::to_string_pretty(&container).unwrap();
println!("{json}\n");
println!("...but does it parse?");
println!("{:#?}", serde_json::from_str::<MyContainer>(&json));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment