Struct serde_with::PickFirst

source ·
pub struct PickFirst<T>(/* private fields */);
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§

source§

impl<T: Clone> Clone for PickFirst<T>

source§

fn clone(&self) -> PickFirst<T>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T: Debug> Debug for PickFirst<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T: Default> Default for PickFirst<T>

source§

fn default() -> PickFirst<T>

Returns the “default value” for a type. Read more
source§

impl<'de, T, TAs1> DeserializeAs<'de, T> for PickFirst<(TAs1,)>where TAs1: DeserializeAs<'de, T>,

source§

fn deserialize_as<D>(deserializer: D) -> Result<T, D::Error>where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer.
source§

impl<'de, T, TAs1, TAs2> DeserializeAs<'de, T> for PickFirst<(TAs1, TAs2)>where TAs1: DeserializeAs<'de, T>, TAs2: DeserializeAs<'de, T>,

source§

fn deserialize_as<D>(deserializer: D) -> Result<T, D::Error>where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer.
source§

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>,

source§

fn deserialize_as<D>(deserializer: D) -> Result<T, D::Error>where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer.
source§

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>,

source§

fn deserialize_as<D>(deserializer: D) -> Result<T, D::Error>where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer.
source§

impl<T, TAs1> SerializeAs<T> for PickFirst<(TAs1,)>where TAs1: SerializeAs<T>,

source§

fn serialize_as<S>(source: &T, serializer: S) -> Result<S::Ok, S::Error>where S: Serializer,

Serialize this value into the given Serde serializer.
source§

impl<T, TAs1, TAs2> SerializeAs<T> for PickFirst<(TAs1, TAs2)>where TAs1: SerializeAs<T>,

source§

fn serialize_as<S>(source: &T, serializer: S) -> Result<S::Ok, S::Error>where S: Serializer,

Serialize this value into the given Serde serializer.
source§

impl<T, TAs1, TAs2, TAs3> SerializeAs<T> for PickFirst<(TAs1, TAs2, TAs3)>where TAs1: SerializeAs<T>,

source§

fn serialize_as<S>(source: &T, serializer: S) -> Result<S::Ok, S::Error>where S: Serializer,

Serialize this value into the given Serde serializer.
source§

impl<T, TAs1, TAs2, TAs3, TAs4> SerializeAs<T> for PickFirst<(TAs1, TAs2, TAs3, TAs4)>where TAs1: SerializeAs<T>,

source§

fn serialize_as<S>(source: &T, serializer: S) -> Result<S::Ok, S::Error>where S: Serializer,

Serialize this value into the given Serde serializer.
source§

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§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.