Struct serde_with::PickFirst[][src]

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

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

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

Deserialize this value from the given Serde deserializer.

Deserialize this value from the given Serde deserializer.

Deserialize this value from the given Serde deserializer.

Deserialize this value from the given Serde deserializer.

Serialize this value into the given Serde serializer.

Serialize this value into the given Serde serializer.

Serialize this value into the given Serde serializer.

Serialize this value into the given Serde serializer.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.