| 1 |
/// Support deserializing from flattened and non-flattened representation |
| 2 |
/// |
| 3 |
/// When working with different serialization formats, sometimes it is more idiomatic to flatten |
| 4 |
/// fields, while other formats prefer nesting. Using `#[serde(flatten)]` only the flattened form |
| 5 |
/// is supported. |
| 6 |
/// |
| 7 |
/// This helper creates a function, which support deserializing from either the flattened or the |
| 8 |
/// nested form. It gives an error, when both forms are provided. The `flatten` attribute is |
| 9 |
/// required on the field such that the helper works. The serialization format will always be |
| 10 |
/// flattened. |
| 11 |
/// |
| 12 |
/// # Examples |
| 13 |
/// |
| 14 |
/// ```rust |
| 15 |
/// # use serde::Deserialize; |
| 16 |
/// # |
| 17 |
/// // Setup the types |
| 18 |
/// #[derive(Deserialize, Debug)] |
| 19 |
/// struct S { |
| 20 |
/// #[serde(flatten, deserialize_with = "deserialize_t")] |
| 21 |
/// t: T, |
| 22 |
/// } |
| 23 |
/// |
| 24 |
/// #[derive(Deserialize, Debug)] |
| 25 |
/// struct T { |
| 26 |
/// i: i32, |
| 27 |
/// } |
| 28 |
/// |
| 29 |
/// // The macro creates custom deserialization code. |
| 30 |
/// // You need to specify a function name and the field name of the flattened field. |
| 31 |
/// serde_with::flattened_maybe!(deserialize_t, "t"); |
| 32 |
/// |
| 33 |
/// # fn main() { |
| 34 |
/// // Supports both flattened |
| 35 |
/// let j = r#" {"i":1} "#; |
| 36 |
/// assert!(serde_json::from_str::<S>(j).is_ok()); |
| 37 |
/// # // Ensure the t field is not dead code |
| 38 |
/// # assert_eq!(serde_json::from_str::<S>(j).unwrap().t.i, 1); |
| 39 |
/// |
| 40 |
/// // and non-flattened versions. |
| 41 |
/// let j = r#" {"t":{"i":1}} "#; |
| 42 |
/// assert!(serde_json::from_str::<S>(j).is_ok()); |
| 43 |
/// |
| 44 |
/// // Ensure that the value is given |
| 45 |
/// let j = r#" {} "#; |
| 46 |
/// assert!(serde_json::from_str::<S>(j).is_err()); |
| 47 |
/// |
| 48 |
/// // and only occurs once, not multiple times. |
| 49 |
/// let j = r#" {"i":1,"t":{"i":1}} "#; |
| 50 |
/// assert!(serde_json::from_str::<S>(j).is_err()); |
| 51 |
/// # } |
| 52 |
/// ``` |
| 53 |
#[macro_export] |
| 54 |
macro_rules! flattened_maybe { |
| 55 |
($fn:ident, $field:tt) => { |
| 56 |
fn $fn<'de, T, D>(deserializer: D) -> $crate::__private__::Result<T, D::Error> |
| 57 |
where |
| 58 |
T: $crate::serde::Deserialize<'de>, |
| 59 |
D: $crate::serde::Deserializer<'de>, |
| 60 |
{ |
| 61 |
use $crate::{ |
| 62 |
__private__::{ |
| 63 |
Option::{self, None, Some}, |
| 64 |
Result::{self, Err, Ok}, |
| 65 |
}, |
| 66 |
serde, |
| 67 |
}; |
| 68 |
|
| 69 |
#[derive($crate::serde_derive::Deserialize)] |
| 70 |
#[serde(crate = "serde")] |
| 71 |
pub struct Both<T> { |
| 72 |
#[serde(flatten)] |
| 73 |
flat: Option<T>, |
| 74 |
#[serde(rename = $field)] |
| 75 |
not_flat: Option<T>, |
| 76 |
} |
| 77 |
|
| 78 |
let both: Both<T> = $crate::serde::Deserialize::deserialize(deserializer)?; |
| 79 |
match (both.flat, both.not_flat) { |
| 80 |
(Some(t), None) | (None, Some(t)) => Ok(t), |
| 81 |
(None, None) => Err($crate::serde::de::Error::missing_field($field)), |
| 82 |
(Some(_), Some(_)) => Err($crate::serde::de::Error::custom(concat!( |
| 83 |
"`", |
| 84 |
$field, |
| 85 |
"` is both flattened and not" |
| 86 |
))), |
| 87 |
} |
| 88 |
} |
| 89 |
}; |
| 90 |
} |