Struct serde_with::OneOrMany[][src]

pub struct OneOrMany<T, FORMAT: Format = PreferOne>(_);

Deserialize one or many elements

Sometime it is desireable to have a shortcut in writing 1-element lists in a config file. Usually, this is done by either writing a list or the list element itself. This distinction is not semantically important on the Rust side, thus both forms should serialize into the same Vec.

The OneOrMany adapter achieves exactly this use case. The serialization behavior can be tweaked to either always serialize as a list using PreferMany or to serialize as the inner element if possible using PreferOne. By default PreferOne is assumed, which can also be omitted like OneOrMany<_>.

Examples

#[serde_as]
#[derive(Deserialize, serde::Serialize)]
struct Data {
    #[serde_as(deserialize_as = "OneOrMany<_, PreferOne>")]
    countries: Vec<String>,
    #[serde_as(deserialize_as = "OneOrMany<_, PreferMany>")]
    cities: Vec<String>,
}

// The adapter allows deserializing a `Vec` from either
// a single element
let j = json!({
    "countries": "Spain",
    "cities": "Berlin",
});
assert!(serde_json::from_value::<Data>(j).is_ok());

// or from a list.
let j = json!({
    "countries": ["Germany", "France"],
    "cities": ["Amsterdam"],
});
assert!(serde_json::from_value::<Data>(j).is_ok());

// For serialization you can choose how a single element should be encoded.
// Either directly, with `PreferOne` (default), or as a list with `PreferMany`.
let data = Data {
    countries: vec!["Spain".to_string()],
    cities: vec!["Berlin".to_string()],
};
let j = json!({
    "countries": "Spain",
    "cities": ["Berlin"],
});
assert_eq!(data, serde_json::from_value(j).unwrap());

Trait Implementations

impl<T: Clone, FORMAT: Clone + Format> Clone for OneOrMany<T, FORMAT>[src]

fn clone(&self) -> OneOrMany<T, FORMAT>[src]

Returns a copy of the value. Read more

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl<T: Debug, FORMAT: Debug + Format> Debug for OneOrMany<T, FORMAT>[src]

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

Formats the value using the given formatter. Read more

impl<T: Default, FORMAT: Default + Format> Default for OneOrMany<T, FORMAT>[src]

fn default() -> OneOrMany<T, FORMAT>[src]

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

impl<'de, T, U, FORMAT> DeserializeAs<'de, Vec<T, Global>> for OneOrMany<U, FORMAT> where
    U: DeserializeAs<'de, T>,
    FORMAT: Format
[src]

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

Deserialize this value from the given Serde deserializer.

impl<T, U> SerializeAs<Vec<T, Global>> for OneOrMany<U, PreferOne> where
    U: SerializeAs<T>, 
[src]

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

Serialize this value into the given Serde serializer.

impl<T, U> SerializeAs<Vec<T, Global>> for OneOrMany<U, PreferMany> where
    U: SerializeAs<T>, 
[src]

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

Serialize this value into the given Serde serializer.

impl<T: Copy, FORMAT: Copy + Format> Copy for OneOrMany<T, FORMAT>[src]

Auto Trait Implementations

impl<T, FORMAT> RefUnwindSafe for OneOrMany<T, FORMAT> where
    FORMAT: RefUnwindSafe,
    T: RefUnwindSafe

impl<T, FORMAT> Send for OneOrMany<T, FORMAT> where
    FORMAT: Send,
    T: Send

impl<T, FORMAT> Sync for OneOrMany<T, FORMAT> where
    FORMAT: Sync,
    T: Sync

impl<T, FORMAT> Unpin for OneOrMany<T, FORMAT> where
    FORMAT: Unpin,
    T: Unpin

impl<T, FORMAT> UnwindSafe for OneOrMany<T, FORMAT> where
    FORMAT: UnwindSafe,
    T: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

pub fn to_owned(&self) -> T[src]

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

pub fn clone_into(&self, target: &mut T)[src]

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

recently added

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

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

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

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

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

Performs the conversion.