Crates.io | serde_sated |
lib.rs | serde_sated |
version | 0.1.3 |
source | src |
created_at | 2024-04-20 21:56:16.942664 |
updated_at | 2024-05-06 06:19:39.114161 |
description | sane adjacently tagged enum deserialization (with untagged variant) for serde |
homepage | |
repository | https://github.com/muttleyxd/serde_sated |
max_upload_size | |
id | 1214951 |
size | 19,890 |
This crate provides a derive macro to override default serde::Deserialize behavior when deserializing adjacently tagged enum variants with fallback untagged value.
Take a look at following code:
use serde::{Deserialize, Serialize};
use serde_json::json;
#[derive(Debug, Deserialize, Serialize)]
#[serde(tag = "resourceType", content = "resource")]
pub enum ResourceStruct {
Number(u64),
String(String),
Complex(Complex),
#[serde(untagged)]
Unknown(serde_json::Value),
}
#[derive(Debug, Deserialize, Serialize)]
pub struct Complex {
pub a: u64,
pub b: u64,
}
#[test]
fn deserialize_json() {
let missing_field_b_in_complex_variant = json!({
"resourceType": "Complex",
"resource": {
"a": 2000
}
});
let result = serde_json::from_value::<ResourceStruct>(missing_field_b_in_complex_variant).unwrap();
eprintln!("Resource: {:#?}", result);
}
This will print:
Resource: Unknown(
Object {
"resource": Object {
"a": Number(2000),
},
"resourceType": String("Complex"),
},
)
As you can see, missing field "b" in Complex variant caused serde to default to untagged variant.
This may or may not be the desired behavior - this crate allows to change that, instead of defaulting to untagged variant it will return the correct error.
Let's change the ResourceStruct derive attribute to use serde_sated::deserialize_enum_with_untagged_as_fallback
use serde::{Deserialize, Serialize};
use serde_json::json;
use serde_sated::deserialize_enum_with_untagged_as_fallback;
#[derive(Debug, deserialize_enum_with_untagged_as_fallback, Serialize)]
#[serde(tag = "resourceType", content = "resource")]
pub enum ResourceStruct {
Number(u64),
String(String),
Complex(Complex),
#[serde(untagged)]
Unknown(serde_json::Value),
}
#[derive(Debug, Deserialize, Serialize)]
pub struct Complex {
pub a: u64,
pub b: u64,
}
#[test]
fn deserialize_json() {
let missing_field_b_in_complex_variant = json!({
"resourceType": "Complex",
"resource": {
"a": 2000
}
});
let result = serde_json::from_value::<ResourceStruct>(missing_field_b_in_complex_variant);
eprintln!("Resource: {:#?}", result);
}
Now the result will be:
Resource: Err(
Error("missing field `b`", line: 0, column: 0),
)