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<'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, A> JsonSchemaAs<T> for PickFirst<(A,)>
where A: JsonSchemaAs<T>,

Available on crate feature schemars_0_8 only.
source§

fn schema_name() -> String

The name of the generated JSON Schema. Read more
source§

fn schema_id() -> Cow<'static, str>

Returns a string that uniquely identifies the schema produced by this type. Read more
source§

fn json_schema(gen: &mut SchemaGenerator) -> Schema

Generates a JSON Schema for this type. Read more
source§

fn is_referenceable() -> bool

Whether JSON Schemas generated for this type should be re-used where possible using the $ref keyword. Read more
source§

impl<T, A, B> JsonSchemaAs<T> for PickFirst<(A, B)>
where A: JsonSchemaAs<T>, B: JsonSchemaAs<T>,

Available on crate feature schemars_0_8 only.
source§

fn schema_name() -> String

The name of the generated JSON Schema. Read more
source§

fn schema_id() -> Cow<'static, str>

Returns a string that uniquely identifies the schema produced by this type. Read more
source§

fn json_schema(gen: &mut SchemaGenerator) -> Schema

Generates a JSON Schema for this type. Read more
source§

fn is_referenceable() -> bool

Whether JSON Schemas generated for this type should be re-used where possible using the $ref keyword. Read more
source§

impl<T, A, B, C> JsonSchemaAs<T> for PickFirst<(A, B, C)>
where A: JsonSchemaAs<T>, B: JsonSchemaAs<T>, C: JsonSchemaAs<T>,

Available on crate feature schemars_0_8 only.
source§

fn schema_name() -> String

The name of the generated JSON Schema. Read more
source§

fn schema_id() -> Cow<'static, str>

Returns a string that uniquely identifies the schema produced by this type. Read more
source§

fn json_schema(gen: &mut SchemaGenerator) -> Schema

Generates a JSON Schema for this type. Read more
source§

fn is_referenceable() -> bool

Whether JSON Schemas generated for this type should be re-used where possible using the $ref keyword. Read more
source§

impl<T, A, B, C, D> JsonSchemaAs<T> for PickFirst<(A, B, C, D)>
where A: JsonSchemaAs<T>, B: JsonSchemaAs<T>, C: JsonSchemaAs<T>, D: JsonSchemaAs<T>,

Available on crate feature schemars_0_8 only.
source§

fn schema_name() -> String

The name of the generated JSON Schema. Read more
source§

fn schema_id() -> Cow<'static, str>

Returns a string that uniquely identifies the schema produced by this type. Read more
source§

fn json_schema(gen: &mut SchemaGenerator) -> Schema

Generates a JSON Schema for this type. Read more
source§

fn is_referenceable() -> bool

Whether JSON Schemas generated for this type should be re-used where possible using the $ref keyword. Read more
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.

Auto Trait Implementations§

§

impl<T> Freeze for PickFirst<T>

§

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 T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where 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 T
where 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, U> TryFrom<U> for T
where 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 T
where 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.