Struct OneOrMany Copy item path Source pub struct OneOrMany<A = Same , F = PreferOne >();Expand description A collection that also accepts a single element.
When deserializing, a sequence is the collection and every other value
(an atom or a map) is a single element, which becomes a collection of
one. The elements are deserialized with the adapter A (by default
Same ). This is for data that writes a list with one element as the
element itself ("tags": "a" and "tags": ["a", "b"]). As a sequence
is always the collection, elements cannot be sequences themselves.
When serializing, F decides the form: PreferOne (the default)
writes a collection with exactly one element as that element and
PreferMany always writes a sequence.
Supported are Vec<T>, VecDeque<T>, BTreeSet<T> and HashSet<T>.
use deser::adapters::{OneOrMany, PreferMany};
use deser::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct Rule {
#[deser(as = OneOrMany)]
hosts: Vec<String>,
#[deser(as = OneOrMany<_ , PreferMany>)]
ports: Vec<u16>,
}
let rule: Rule = deser_json::from_str(r#"{"hosts": "a", "ports": 80}"# ).unwrap();
assert_eq! (rule.hosts, ["a" ]);
assert_eq! (rule.ports, [80 ]);
assert_eq! (
deser_json::to_string(& rule).unwrap(),
r#"{"hosts":"a","ports":[80]}"#
);
In formats where keys can be given more than once (like query strings,
see ContainerShape::set_multimap ) the values of all occurrences of
the key are collected, as without the adapter.
Creates a sink that deserializes the value into the given slot.
Read more Returns what the value expects, for error messages.
Read more Provides the value of a missing struct field.
Read more Deserializes an atom into the slot.
Read more Deserializes an atom that borrows from the data being deserialized
into the slot.
Read more Describes the Rust shape of the type.
Read more Creates a sink that updates an existing value.
Read more Creates a sink that deserializes the value into the given slot.
Read more Returns what the value expects, for error messages.
Read more Provides the value of a missing struct field.
Read more Deserializes an atom into the slot.
Read more Deserializes an atom that borrows from the data being deserialized
into the slot.
Read more Describes the Rust shape of the type.
Read more Creates a sink that updates an existing value.
Read more Creates a sink that deserializes the value into the given slot.
Read more Returns what the value expects, for error messages.
Read more Provides the value of a missing struct field.
Read more Deserializes an atom into the slot.
Read more Deserializes an atom that borrows from the data being deserialized
into the slot.
Read more Describes the Rust shape of the type.
Read more Creates a sink that updates an existing value.
Read more Creates a sink that deserializes the value into the given slot.
Read more Returns what the value expects, for error messages.
Read more Provides the value of a missing struct field.
Read more Deserializes an atom into the slot.
Read more Deserializes an atom that borrows from the data being deserialized
into the slot.
Read more Describes the Rust shape of the type.
Read more Creates a sink that updates an existing value.
Read more Serializes the value.
Describes the Rust shape of the value.
Read more Invoked after the serialization finished.
Read more Checks if the value represents an optional value.
Read more Returns the shape of the value if it’s a map or sequence.
Read more Serializes the value.
Describes the Rust shape of the value.
Read more Invoked after the serialization finished.
Read more Checks if the value represents an optional value.
Read more Returns the shape of the value if it’s a map or sequence.
Read more Serializes the value.
Describes the Rust shape of the value.
Read more Invoked after the serialization finished.
Read more Checks if the value represents an optional value.
Read more Returns the shape of the value if it’s a map or sequence.
Read more Serializes the value.
Describes the Rust shape of the value.
Read more Invoked after the serialization finished.
Read more Checks if the value represents an optional value.
Read more Returns the shape of the value if it’s a map or sequence.
Read more Immutably borrows from an owned value.
Read more Mutably borrows from an owned value.
Read more Returns the argument unchanged.
Calls U::from(self).
That is, this conversion is whatever the implementation of
From <T> for U chooses to do.
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.