Struct serde_with::PickFirst
source · [−]pub struct PickFirst<T>(_);
Expand description
Try multiple deserialization options until one succeeds.
This adapter allows you to specify a list of deserialization options. They are tried in order and the first one working is applied. Serialization always picks the first option.
PickFirst
has one type parameter which must be instantiated with a tuple of two, three, or four elements.
For example, PickFirst<(_, DisplayFromStr)>
on a field of type u32
allows deserializing from a number or from a string via the FromStr
trait.
The value will be serialized as a number, since that is what the first type _
indicates.
Examples
Deserialize a number from either a number or a string.
#[serde_as]
#[derive(Deserialize, Serialize)]
struct Data {
#[serde_as(as = "PickFirst<(_, DisplayFromStr)>")]
as_number: u32,
#[serde_as(as = "PickFirst<(DisplayFromStr, _)>")]
as_string: u32,
}
let data = Data {
as_number: 123,
as_string: 456
};
// Both fields can be deserialized from numbers:
let j = json!({
"as_number": 123,
"as_string": 456,
});
assert_eq!(data, serde_json::from_value(j).unwrap());
// or from a string:
let j = json!({
"as_number": "123",
"as_string": "456",
});
assert_eq!(data, serde_json::from_value(j).unwrap());
// For serialization the first type in the tuple determines the behavior.
// The `as_number` field will use the normal `Serialize` behavior and produce a number,
// while `as_string` used `Display` to produce a string.
let expected = json!({
"as_number": 123,
"as_string": "456",
});
assert_eq!(expected, serde_json::to_value(&data).unwrap());
Trait Implementations
sourceimpl<'de, T, TAs1> DeserializeAs<'de, T> for PickFirst<(TAs1,)> where
TAs1: DeserializeAs<'de, T>,
impl<'de, T, TAs1> DeserializeAs<'de, T> for PickFirst<(TAs1,)> where
TAs1: DeserializeAs<'de, T>,
sourcefn deserialize_as<D>(deserializer: D) -> Result<T, D::Error> where
D: Deserializer<'de>,
fn deserialize_as<D>(deserializer: D) -> Result<T, D::Error> where
D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer.
sourceimpl<'de, T, TAs1, TAs2> DeserializeAs<'de, T> for PickFirst<(TAs1, TAs2)> where
TAs1: DeserializeAs<'de, T>,
TAs2: DeserializeAs<'de, T>,
impl<'de, T, TAs1, TAs2> DeserializeAs<'de, T> for PickFirst<(TAs1, TAs2)> where
TAs1: DeserializeAs<'de, T>,
TAs2: DeserializeAs<'de, T>,
sourcefn deserialize_as<D>(deserializer: D) -> Result<T, D::Error> where
D: Deserializer<'de>,
fn deserialize_as<D>(deserializer: D) -> Result<T, D::Error> where
D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer.
sourceimpl<'de, T, TAs1, TAs2, TAs3> DeserializeAs<'de, T> for PickFirst<(TAs1, TAs2, TAs3)> where
TAs1: DeserializeAs<'de, T>,
TAs2: DeserializeAs<'de, T>,
TAs3: DeserializeAs<'de, T>,
impl<'de, T, TAs1, TAs2, TAs3> DeserializeAs<'de, T> for PickFirst<(TAs1, TAs2, TAs3)> where
TAs1: DeserializeAs<'de, T>,
TAs2: DeserializeAs<'de, T>,
TAs3: DeserializeAs<'de, T>,
sourcefn deserialize_as<D>(deserializer: D) -> Result<T, D::Error> where
D: Deserializer<'de>,
fn deserialize_as<D>(deserializer: D) -> Result<T, D::Error> where
D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer.
sourceimpl<'de, T, TAs1, TAs2, TAs3, TAs4> DeserializeAs<'de, T> for PickFirst<(TAs1, TAs2, TAs3, TAs4)> where
TAs1: DeserializeAs<'de, T>,
TAs2: DeserializeAs<'de, T>,
TAs3: DeserializeAs<'de, T>,
TAs4: DeserializeAs<'de, T>,
impl<'de, T, TAs1, TAs2, TAs3, TAs4> DeserializeAs<'de, T> for PickFirst<(TAs1, TAs2, TAs3, TAs4)> where
TAs1: DeserializeAs<'de, T>,
TAs2: DeserializeAs<'de, T>,
TAs3: DeserializeAs<'de, T>,
TAs4: DeserializeAs<'de, T>,
sourcefn deserialize_as<D>(deserializer: D) -> Result<T, D::Error> where
D: Deserializer<'de>,
fn deserialize_as<D>(deserializer: D) -> Result<T, D::Error> where
D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer.
sourceimpl<T, TAs1> SerializeAs<T> for PickFirst<(TAs1,)> where
TAs1: SerializeAs<T>,
impl<T, TAs1> SerializeAs<T> for PickFirst<(TAs1,)> where
TAs1: SerializeAs<T>,
sourcefn serialize_as<S>(source: &T, serializer: S) -> Result<S::Ok, S::Error> where
S: Serializer,
fn serialize_as<S>(source: &T, serializer: S) -> Result<S::Ok, S::Error> where
S: Serializer,
Serialize this value into the given Serde serializer.
sourceimpl<T, TAs1, TAs2> SerializeAs<T> for PickFirst<(TAs1, TAs2)> where
TAs1: SerializeAs<T>,
impl<T, TAs1, TAs2> SerializeAs<T> for PickFirst<(TAs1, TAs2)> where
TAs1: SerializeAs<T>,
sourcefn serialize_as<S>(source: &T, serializer: S) -> Result<S::Ok, S::Error> where
S: Serializer,
fn serialize_as<S>(source: &T, serializer: S) -> Result<S::Ok, S::Error> where
S: Serializer,
Serialize this value into the given Serde serializer.
sourceimpl<T, TAs1, TAs2, TAs3> SerializeAs<T> for PickFirst<(TAs1, TAs2, TAs3)> where
TAs1: SerializeAs<T>,
impl<T, TAs1, TAs2, TAs3> SerializeAs<T> for PickFirst<(TAs1, TAs2, TAs3)> where
TAs1: SerializeAs<T>,
sourcefn serialize_as<S>(source: &T, serializer: S) -> Result<S::Ok, S::Error> where
S: Serializer,
fn serialize_as<S>(source: &T, serializer: S) -> Result<S::Ok, S::Error> where
S: Serializer,
Serialize this value into the given Serde serializer.
sourceimpl<T, TAs1, TAs2, TAs3, TAs4> SerializeAs<T> for PickFirst<(TAs1, TAs2, TAs3, TAs4)> where
TAs1: SerializeAs<T>,
impl<T, TAs1, TAs2, TAs3, TAs4> SerializeAs<T> for PickFirst<(TAs1, TAs2, TAs3, TAs4)> where
TAs1: SerializeAs<T>,
sourcefn serialize_as<S>(source: &T, serializer: S) -> Result<S::Ok, S::Error> where
S: Serializer,
fn serialize_as<S>(source: &T, serializer: S) -> Result<S::Ok, S::Error> where
S: Serializer,
Serialize this value into the given Serde serializer.
impl<T: Copy> Copy for PickFirst<T>
Auto Trait Implementations
impl<T> RefUnwindSafe for PickFirst<T> where
T: RefUnwindSafe,
impl<T> Send for PickFirst<T> where
T: Send,
impl<T> Sync for PickFirst<T> where
T: Sync,
impl<T> Unpin for PickFirst<T> where
T: Unpin,
impl<T> UnwindSafe for PickFirst<T> where
T: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more