Comparison with Serde
A side-by-side comparison of facet and serde derive macro attributes.
Produce an error when an unknown field is encountered during deserialization. The default behaviour
is to ignore field that are not known.
| Facet |
Serde |
#[derive(facet::Facet)]
#[facet(deny_unknown_fields)]
struct MyStruct {
field1: i32,
field2: Option<i32>,
}
|
#[derive(serde::Deserialize)]
#[serde(deny_unknown_fields)]
struct MyStruct {
field1: i32,
field2: Option<i32>,
}
|
Only allowed for structs, not for enums. During deserialization, any fields that are missing
from the input will be taken from the Default::default implementation of the struct. This is not
possible for enums because they can only have a single Default implementation producing a single
variant.
| Facet |
Serde |
#[derive(facet::Facet)]
#[facet(default)]
struct MyStruct {
field1: i32,
field2: Option<i32>,
}
impl Default for MyStruct {
fn default() -> Self {
Self {
field1: 1,
field2: Some(2),
}
}
}
|
#[derive(serde::Deserialize)]
#[serde(default)]
struct MyStruct {
field1: i32,
field2: Option<i32>,
}
impl Default for MyStruct {
fn default() -> Self {
Self {
field1: 1,
field2: Some(2),
}
}
}
|
Rename all fields at once using a casing convention. Supported values are
"PascalCase"
"camelCase"
"snake_case"
"SCREAMING_SNAKE_CASE"
"kebab-case"
"SCREAMING-KEBAB-CASE"
| Facet |
Serde |
#[derive(facet::Facet)]
#[facet(rename_all = "camelCase")]
struct MyStruct {
field_one: i32,
}
|
#[derive(serde::Deserialize)]
#[serde(rename_all = "camelCase")]
struct MyStruct {
field_one: i32,
}
|
Skip this field during serialization.
| Facet |
Serde |
#[derive(facet::Facet)]
struct MyStruct {
field1: i32,
#[facet(skip_serializing)]
field2: String,
}
|
#[derive(serde::Serialize)]
struct MyStruct {
field1: i32,
#[serde(skip_serializing)]
field2: String,
}
|
Skip serializing this field when a condition is met. Typically used for Option fields when you
want to omit the field entirely from serialized output when the value is None.
| Facet |
Serde |
#[derive(facet::Facet)]
struct MyStruct {
#[facet(skip_serializing_if = |n| n % 2 == 0)]
field1: i32,
#[facet(skip_serializing_if = Option::is_none)]
field2: Option<i32>,
}
|
#[derive(serde::Serialize)]
struct MyStruct {
#[serde(skip_serializing_if = is_even)]
field1: i32,
#[serde(skip_serializing_if = "Option::is_none")]
field2: Option<i32>,
}
fn is_even(n: i32) -> bool {
n % 2 == 0
}
|
Use a specified function to provide a default value when deserializing if the field is missing from
input. You can either use default alone to use Default::default() for the field, or provide an
expression producing the default value.
| Facet |
Serde |
#[derive(facet::Facet)]
struct MyStruct {
field1: i32,
#[facet(default)]
field2: Vec<String>,
#[facet(default = 42))]
field3: i32,
#[facet(default = rand::random())]
field4: i32,
}
|
#[derive(serde::Deserialize)]
struct MyStruct {
field1: i32,
#[serde(default)]
field2: Vec<String>,
#[serde(default = "default_value")]
field3: i32,
#[serde(default = "rand::random")]
field4: i32,
}
fn default_value() -> i32 {
42
}
|