serde_with

Struct Map

source
pub struct Map<K, V>(/* private fields */);
Expand description

This serializes a list of tuples into a map

Normally, you want to use a HashMap or a BTreeMap when deserializing a map. However, sometimes this is not possible due to type constraints, e.g., if the type implements neither Hash nor Ord. Another use case is deserializing a map with duplicate keys.

§Examples

Wrapper does not implement Hash nor Ord, thus prohibiting the use HashMap or BTreeMap. The JSON also contains a duplicate key.

#[serde_as]
#[derive(Debug, Deserialize, Serialize, Default)]
struct S {
    #[serde_as(as = "Map<_, _>")]
    s: Vec<(Wrapper<i32>, String)>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(transparent)]
struct Wrapper<T>(T);

let data = S {
    s: vec![
        (Wrapper(1), "a".to_string()),
        (Wrapper(2), "b".to_string()),
        (Wrapper(3), "c".to_string()),
        (Wrapper(2), "d".to_string()),
    ],
};

let json = r#"{
  "s": {
    "1": "a",
    "2": "b",
    "3": "c",
    "2": "d"
  }
}"#;
assert_eq!(json, serde_json::to_string_pretty(&data).unwrap());

Trait Implementations§

source§

impl<'de, K, KAs, V, VAs, const N: usize> DeserializeAs<'de, [(K, V); N]> for Map<KAs, VAs>
where KAs: DeserializeAs<'de, K>, VAs: DeserializeAs<'de, V>,

source§

fn deserialize_as<D>(deserializer: D) -> Result<[(K, V); N], D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer.
source§

impl<'de, K, KAs, V, VAs> DeserializeAs<'de, BTreeSet<(K, V)>> for Map<KAs, VAs>
where KAs: DeserializeAs<'de, K>, VAs: DeserializeAs<'de, V>, (K, V): Ord,

source§

fn deserialize_as<D>(deserializer: D) -> Result<BTreeSet<(K, V)>, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer.
source§

impl<'de, K, KAs, V, VAs> DeserializeAs<'de, BinaryHeap<(K, V)>> for Map<KAs, VAs>
where KAs: DeserializeAs<'de, K>, VAs: DeserializeAs<'de, V>, (K, V): Ord,

source§

fn deserialize_as<D>(deserializer: D) -> Result<BinaryHeap<(K, V)>, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer.
source§

impl<'de, K, KAs, V, VAs> DeserializeAs<'de, Box<[(K, V)]>> for Map<KAs, VAs>
where KAs: DeserializeAs<'de, K>, VAs: DeserializeAs<'de, V>, (K, V):,

source§

fn deserialize_as<D>(deserializer: D) -> Result<Box<[(K, V)]>, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer.
source§

impl<'de, K, KAs, V, VAs, S> DeserializeAs<'de, HashSet<(K, V), S>> for Map<KAs, VAs>
where KAs: DeserializeAs<'de, K>, VAs: DeserializeAs<'de, V>, (K, V): Eq + Hash, S: BuildHasher + Default,

source§

fn deserialize_as<D>(deserializer: D) -> Result<HashSet<(K, V), S>, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer.
source§

impl<'de, K, KAs, V, VAs, S> DeserializeAs<'de, HashSet<(K, V), S>> for Map<KAs, VAs>
where KAs: DeserializeAs<'de, K>, VAs: DeserializeAs<'de, V>, (K, V): Eq + Hash, S: BuildHasher + Default,

source§

fn deserialize_as<D>( deserializer: D, ) -> Result<HashbrownSet014<(K, V), S>, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer.
source§

impl<'de, K, KAs, V, VAs, S> DeserializeAs<'de, HashSet<(K, V), S>> for Map<KAs, VAs>
where KAs: DeserializeAs<'de, K>, VAs: DeserializeAs<'de, V>, (K, V): Eq + Hash, S: BuildHasher + Default,

source§

fn deserialize_as<D>( deserializer: D, ) -> Result<HashbrownSet015<(K, V), S>, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer.
source§

impl<'de, K, KAs, V, VAs, S> DeserializeAs<'de, IndexSet<(K, V), S>> for Map<KAs, VAs>
where KAs: DeserializeAs<'de, K>, VAs: DeserializeAs<'de, V>, (K, V): Eq + Hash, S: BuildHasher + Default,

source§

fn deserialize_as<D>(deserializer: D) -> Result<IndexSet<(K, V), S>, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer.
source§

impl<'de, K, KAs, V, VAs, S> DeserializeAs<'de, IndexSet<(K, V), S>> for Map<KAs, VAs>
where KAs: DeserializeAs<'de, K>, VAs: DeserializeAs<'de, V>, (K, V): Eq + Hash, S: BuildHasher + Default,

source§

fn deserialize_as<D>(deserializer: D) -> Result<IndexSet2<(K, V), S>, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer.
source§

impl<'de, K, KAs, V, VAs> DeserializeAs<'de, LinkedList<(K, V)>> for Map<KAs, VAs>
where KAs: DeserializeAs<'de, K>, VAs: DeserializeAs<'de, V>, (K, V):,

source§

fn deserialize_as<D>(deserializer: D) -> Result<LinkedList<(K, V)>, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer.
source§

impl<'de, K, KAs, V, VAs> DeserializeAs<'de, Vec<(K, V)>> for Map<KAs, VAs>
where KAs: DeserializeAs<'de, K>, VAs: DeserializeAs<'de, V>, (K, V):,

source§

fn deserialize_as<D>(deserializer: D) -> Result<Vec<(K, V)>, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer.
source§

impl<'de, K, KAs, V, VAs> DeserializeAs<'de, VecDeque<(K, V)>> for Map<KAs, VAs>
where KAs: DeserializeAs<'de, K>, VAs: DeserializeAs<'de, V>, (K, V):,

source§

fn deserialize_as<D>(deserializer: D) -> Result<VecDeque<(K, V)>, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer.
source§

impl<K, V, KA, VA> JsonSchemaAs<[(K, V)]> for Map<KA, VA>
where VA: JsonSchemaAs<V>,

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<K, V, KA, VA, const N: usize> JsonSchemaAs<[(K, V); N]> for Map<KA, VA>
where VA: JsonSchemaAs<V>,

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<K, V, KA, VA> JsonSchemaAs<BTreeSet<(K, V)>> for Map<KA, VA>
where VA: JsonSchemaAs<V>,

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<K, V, KA, VA> JsonSchemaAs<BinaryHeap<(K, V)>> for Map<KA, VA>
where VA: JsonSchemaAs<V>,

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<K, V, KA, VA> JsonSchemaAs<Box<[(K, V)]>> for Map<KA, VA>
where VA: JsonSchemaAs<V>,

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<K, V, S, KA, VA> JsonSchemaAs<HashSet<(K, V), S>> for Map<KA, VA>
where VA: JsonSchemaAs<V>,

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<K, V, KA, VA> JsonSchemaAs<LinkedList<(K, V)>> for Map<KA, VA>
where VA: JsonSchemaAs<V>,

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<K, V, KA, VA> JsonSchemaAs<Vec<(K, V)>> for Map<KA, VA>
where VA: JsonSchemaAs<V>,

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<K, V, KA, VA> JsonSchemaAs<VecDeque<(K, V)>> for Map<KA, VA>
where VA: JsonSchemaAs<V>,

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<K, KAs, V, VAs> SerializeAs<[(K, V)]> for Map<KAs, VAs>
where KAs: SerializeAs<K>, VAs: SerializeAs<V>,

source§

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

Serialize this value into the given Serde serializer.
source§

impl<K, KAs, V, VAs, const N: usize> SerializeAs<[(K, V); N]> for Map<KAs, VAs>
where KAs: SerializeAs<K>, VAs: SerializeAs<V>,

source§

fn serialize_as<S>( source: &[(K, V); N], serializer: S, ) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer.
source§

impl<K, KAs, V, VAs> SerializeAs<BTreeSet<(K, V)>> for Map<KAs, VAs>
where KAs: SerializeAs<K>, VAs: SerializeAs<V>,

source§

fn serialize_as<S>( source: &BTreeSet<(K, V)>, serializer: S, ) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer.
source§

impl<K, KAs, V, VAs> SerializeAs<BinaryHeap<(K, V)>> for Map<KAs, VAs>
where KAs: SerializeAs<K>, VAs: SerializeAs<V>,

source§

fn serialize_as<S>( source: &BinaryHeap<(K, V)>, serializer: S, ) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer.
source§

impl<K, KAs, V, VAs> SerializeAs<Box<[(K, V)]>> for Map<KAs, VAs>
where KAs: SerializeAs<K>, VAs: SerializeAs<V>,

source§

fn serialize_as<S>( source: &Box<[(K, V)]>, serializer: S, ) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer.
source§

impl<K, KAs, V, VAs, H> SerializeAs<HashSet<(K, V), H>> for Map<KAs, VAs>
where KAs: SerializeAs<K>, VAs: SerializeAs<V>, H: ?Sized + Sized,

source§

fn serialize_as<S>( source: &HashSet<(K, V), H>, serializer: S, ) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer.
source§

impl<K, KAs, V, VAs, H> SerializeAs<HashSet<(K, V), H>> for Map<KAs, VAs>
where KAs: SerializeAs<K>, VAs: SerializeAs<V>, H: ?Sized + Sized,

source§

fn serialize_as<S>( source: &HashbrownSet014<(K, V), H>, serializer: S, ) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer.
source§

impl<K, KAs, V, VAs, H> SerializeAs<HashSet<(K, V), H>> for Map<KAs, VAs>
where KAs: SerializeAs<K>, VAs: SerializeAs<V>, H: ?Sized + Sized,

source§

fn serialize_as<S>( source: &HashbrownSet015<(K, V), H>, serializer: S, ) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer.
source§

impl<K, KAs, V, VAs, H> SerializeAs<IndexSet<(K, V), H>> for Map<KAs, VAs>
where KAs: SerializeAs<K>, VAs: SerializeAs<V>, H: ?Sized + Sized,

source§

fn serialize_as<S>( source: &IndexSet<(K, V), H>, serializer: S, ) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer.
source§

impl<K, KAs, V, VAs, H> SerializeAs<IndexSet<(K, V), H>> for Map<KAs, VAs>
where KAs: SerializeAs<K>, VAs: SerializeAs<V>, H: ?Sized + Sized,

source§

fn serialize_as<S>( source: &IndexSet2<(K, V), H>, serializer: S, ) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer.
source§

impl<K, KAs, V, VAs> SerializeAs<LinkedList<(K, V)>> for Map<KAs, VAs>
where KAs: SerializeAs<K>, VAs: SerializeAs<V>,

source§

fn serialize_as<S>( source: &LinkedList<(K, V)>, serializer: S, ) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer.
source§

impl<K, KAs, V, VAs> SerializeAs<Option<(K, V)>> for Map<KAs, VAs>
where KAs: SerializeAs<K>, VAs: SerializeAs<V>,

source§

fn serialize_as<S>( source: &Option<(K, V)>, serializer: S, ) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer.
source§

impl<K, KAs, V, VAs> SerializeAs<Vec<(K, V)>> for Map<KAs, VAs>
where KAs: SerializeAs<K>, VAs: SerializeAs<V>,

source§

fn serialize_as<S>( source: &Vec<(K, V)>, serializer: S, ) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer.
source§

impl<K, KAs, V, VAs> SerializeAs<VecDeque<(K, V)>> for Map<KAs, VAs>
where KAs: SerializeAs<K>, VAs: SerializeAs<V>,

source§

fn serialize_as<S>( source: &VecDeque<(K, V)>, serializer: S, ) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer.

Auto Trait Implementations§

§

impl<K, V> Freeze for Map<K, V>

§

impl<K, V> RefUnwindSafe for Map<K, V>

§

impl<K, V> Send for Map<K, V>
where K: Send, V: Send,

§

impl<K, V> Sync for Map<K, V>
where K: Sync, V: Sync,

§

impl<K, V> Unpin for Map<K, V>
where K: Unpin, V: Unpin,

§

impl<K, V> UnwindSafe for Map<K, V>
where K: UnwindSafe, V: 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>,

source§

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>,

source§

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.