pub struct OneOrMany<T, FORMAT: Format = PreferOne>(/* private fields */);
Expand description
Deserialize one or many elements
Sometimes it is desirable 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 deserialize 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(as = "OneOrMany<_, PreferOne>")]
countries: Vec<String>,
#[serde_as(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!(serde_json::to_value(data).unwrap(), j);
Trait Implementations§
source§impl<'de, T, TAs, FORMAT> DeserializeAs<'de, Vec<T>> for OneOrMany<TAs, FORMAT>where
TAs: DeserializeAs<'de, T>,
FORMAT: Format,
impl<'de, T, TAs, FORMAT> DeserializeAs<'de, Vec<T>> for OneOrMany<TAs, FORMAT>where
TAs: DeserializeAs<'de, T>,
FORMAT: Format,
source§fn deserialize_as<D>(deserializer: D) -> Result<Vec<T>, D::Error>where
D: Deserializer<'de>,
fn deserialize_as<D>(deserializer: D) -> Result<Vec<T>, D::Error>where
D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer.
source§impl<T, TA> JsonSchemaAs<Vec<T>> for OneOrMany<TA, PreferOne>where
TA: JsonSchemaAs<T>,
Available on crate feature schemars_0_8
only.
impl<T, TA> JsonSchemaAs<Vec<T>> for OneOrMany<TA, PreferOne>where
TA: JsonSchemaAs<T>,
Available on crate feature
schemars_0_8
only.source§fn schema_name() -> String
fn schema_name() -> String
The name of the generated JSON Schema. Read more
source§fn schema_id() -> Cow<'static, str>
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
fn json_schema(gen: &mut SchemaGenerator) -> Schema
Generates a JSON Schema for this type. Read more
source§fn is_referenceable() -> bool
fn is_referenceable() -> bool
Whether JSON Schemas generated for this type should be re-used where possible using the
$ref
keyword. Read moresource§impl<T, TA> JsonSchemaAs<Vec<T>> for OneOrMany<TA, PreferMany>where
TA: JsonSchemaAs<T>,
Available on crate feature schemars_0_8
only.
impl<T, TA> JsonSchemaAs<Vec<T>> for OneOrMany<TA, PreferMany>where
TA: JsonSchemaAs<T>,
Available on crate feature
schemars_0_8
only.source§fn schema_name() -> String
fn schema_name() -> String
The name of the generated JSON Schema. Read more
source§fn schema_id() -> Cow<'static, str>
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
fn json_schema(gen: &mut SchemaGenerator) -> Schema
Generates a JSON Schema for this type. Read more
source§fn is_referenceable() -> bool
fn is_referenceable() -> bool
Whether JSON Schemas generated for this type should be re-used where possible using the
$ref
keyword. Read moresource§impl<T, U> SerializeAs<Vec<T>> for OneOrMany<U, PreferOne>where
U: SerializeAs<T>,
impl<T, U> SerializeAs<Vec<T>> for OneOrMany<U, PreferOne>where
U: SerializeAs<T>,
source§fn serialize_as<S>(source: &Vec<T>, serializer: S) -> Result<S::Ok, S::Error>where
S: Serializer,
fn serialize_as<S>(source: &Vec<T>, serializer: S) -> Result<S::Ok, S::Error>where
S: Serializer,
Serialize this value into the given Serde serializer.
source§impl<T, U> SerializeAs<Vec<T>> for OneOrMany<U, PreferMany>where
U: SerializeAs<T>,
impl<T, U> SerializeAs<Vec<T>> for OneOrMany<U, PreferMany>where
U: SerializeAs<T>,
source§fn serialize_as<S>(source: &Vec<T>, serializer: S) -> Result<S::Ok, S::Error>where
S: Serializer,
fn serialize_as<S>(source: &Vec<T>, serializer: S) -> Result<S::Ok, S::Error>where
S: Serializer,
Serialize this value into the given Serde serializer.
Auto Trait Implementations§
impl<T, FORMAT> Freeze for OneOrMany<T, FORMAT>
impl<T, FORMAT> RefUnwindSafe for OneOrMany<T, FORMAT>where
T: RefUnwindSafe,
FORMAT: RefUnwindSafe,
impl<T, FORMAT> Send for OneOrMany<T, FORMAT>
impl<T, FORMAT> Sync for OneOrMany<T, FORMAT>
impl<T, FORMAT> Unpin for OneOrMany<T, FORMAT>
impl<T, FORMAT> UnwindSafe for OneOrMany<T, FORMAT>where
T: UnwindSafe,
FORMAT: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more