pub struct MapAsPairs<KA = Same, VA = Same>(/* private fields */);Expand description
A map that is written as a sequence of key and value pairs.
When serializing, the entries are written as a sequence of pairs
([["a", 1], ["b", 2]]). This is useful for maps whose keys cannot be
keys in the format, like tuples or structs in JSON. When deserializing,
a sequence of pairs and a map are accepted. The keys are deserialized
with the adapter KA and the values with VA (both Same by
default). Keys that are given more than once are handled like in maps
(see DuplicateKeys).
Supported are BTreeMap<K, V> and HashMap<K, V>.
use std::collections::BTreeMap;
use deser::adapters::MapAsPairs;
use deser::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
pub struct Grid {
#[deser(as = MapAsPairs)]
cells: BTreeMap<(u32, u32), String>,
}
let grid: Grid = deser_json::from_str(r#"{"cells": [[[0, 1], "x"]]}"#).unwrap();
assert_eq!(grid.cells[&(0, 1)], "x");
assert_eq!(
deser_json::to_string(&grid).unwrap(),
r#"{"cells":[[[0,1],"x"]]}"#
);Trait Implementations§
Source§impl<'de, K: Ord + Send, V: Send, KA: Deserialize<'de, K>, VA: Deserialize<'de, V>> Deserialize<'de, BTreeMap<K, V>> for MapAsPairs<KA, VA>
impl<'de, K: Ord + Send, V: Send, KA: Deserialize<'de, K>, VA: Deserialize<'de, V>> Deserialize<'de, BTreeMap<K, V>> for MapAsPairs<KA, VA>
Source§fn deserialize_into<'out>(
out: &'out mut Option<BTreeMap<K, V>>,
state: &mut State,
) -> SinkHandle<'out, 'de>
fn deserialize_into<'out>( out: &'out mut Option<BTreeMap<K, V>>, state: &mut State, ) -> SinkHandle<'out, 'de>
Creates a sink that deserializes the value into the given slot. Read more
Source§fn expecting() -> Cow<'static, str>
fn expecting() -> Cow<'static, str>
Returns what the value expects, for error messages. Read more
Source§fn deserialize_atom(
slot: &mut Slot<T, Self>,
atom: Atom<'_>,
state: &mut State,
) -> Result<(), Error>
fn deserialize_atom( slot: &mut Slot<T, Self>, atom: Atom<'_>, state: &mut State, ) -> Result<(), Error>
Deserializes an atom into the slot. Read more
Source§fn deserialize_borrowed_atom(
slot: &mut Slot<T, Self>,
atom: Atom<'de>,
state: &mut State,
) -> Result<(), Error>
fn deserialize_borrowed_atom( slot: &mut Slot<T, Self>, atom: Atom<'de>, state: &mut State, ) -> Result<(), Error>
Deserializes an atom that borrows from the data being deserialized
into the slot. Read more
Source§fn describe_type(d: &mut dyn Describe)
fn describe_type(d: &mut dyn Describe)
Describes the Rust shape of the type. Read more
Source§fn initial_value() -> Option<T>
fn initial_value() -> Option<T>
Provides the value of a missing struct field. Read more
Source§fn deserialize_update<'out>(
value: &'out mut T,
state: &mut State,
) -> SinkHandle<'out, 'de>
fn deserialize_update<'out>( value: &'out mut T, state: &mut State, ) -> SinkHandle<'out, 'de>
Creates a sink that updates an existing value. Read more
Source§impl<'de, K: Hash + Eq + Send, V: Send, H: BuildHasher + Default + Send, KA: Deserialize<'de, K>, VA: Deserialize<'de, V>> Deserialize<'de, HashMap<K, V, H>> for MapAsPairs<KA, VA>
impl<'de, K: Hash + Eq + Send, V: Send, H: BuildHasher + Default + Send, KA: Deserialize<'de, K>, VA: Deserialize<'de, V>> Deserialize<'de, HashMap<K, V, H>> for MapAsPairs<KA, VA>
Source§fn deserialize_into<'out>(
out: &'out mut Option<HashMap<K, V, H>>,
state: &mut State,
) -> SinkHandle<'out, 'de>
fn deserialize_into<'out>( out: &'out mut Option<HashMap<K, V, H>>, state: &mut State, ) -> SinkHandle<'out, 'de>
Creates a sink that deserializes the value into the given slot. Read more
Source§fn expecting() -> Cow<'static, str>
fn expecting() -> Cow<'static, str>
Returns what the value expects, for error messages. Read more
Source§fn deserialize_atom(
slot: &mut Slot<T, Self>,
atom: Atom<'_>,
state: &mut State,
) -> Result<(), Error>
fn deserialize_atom( slot: &mut Slot<T, Self>, atom: Atom<'_>, state: &mut State, ) -> Result<(), Error>
Deserializes an atom into the slot. Read more
Source§fn deserialize_borrowed_atom(
slot: &mut Slot<T, Self>,
atom: Atom<'de>,
state: &mut State,
) -> Result<(), Error>
fn deserialize_borrowed_atom( slot: &mut Slot<T, Self>, atom: Atom<'de>, state: &mut State, ) -> Result<(), Error>
Deserializes an atom that borrows from the data being deserialized
into the slot. Read more
Source§fn describe_type(d: &mut dyn Describe)
fn describe_type(d: &mut dyn Describe)
Describes the Rust shape of the type. Read more
Source§fn initial_value() -> Option<T>
fn initial_value() -> Option<T>
Provides the value of a missing struct field. Read more
Source§fn deserialize_update<'out>(
value: &'out mut T,
state: &mut State,
) -> SinkHandle<'out, 'de>
fn deserialize_update<'out>( value: &'out mut T, state: &mut State, ) -> SinkHandle<'out, 'de>
Creates a sink that updates an existing value. Read more
Source§impl<K: Sync, V: Sync, KA: Serialize<K>, VA: Serialize<V>> Serialize<BTreeMap<K, V>> for MapAsPairs<KA, VA>
impl<K: Sync, V: Sync, KA: Serialize<K>, VA: Serialize<V>> Serialize<BTreeMap<K, V>> for MapAsPairs<KA, VA>
Source§fn serialize<'a>(
value: &'a BTreeMap<K, V>,
state: &mut State,
) -> Result<Emit<'a>, Error>
fn serialize<'a>( value: &'a BTreeMap<K, V>, state: &mut State, ) -> Result<Emit<'a>, Error>
Serializes the value.
Source§fn container_shape(value: &BTreeMap<K, V>) -> ContainerShape
fn container_shape(value: &BTreeMap<K, V>) -> ContainerShape
Returns the shape of the value if it’s a map or sequence. Read more
Source§impl<K: Sync, V: Sync, H: BuildHasher + Sync, KA: Serialize<K>, VA: Serialize<V>> Serialize<HashMap<K, V, H>> for MapAsPairs<KA, VA>
impl<K: Sync, V: Sync, H: BuildHasher + Sync, KA: Serialize<K>, VA: Serialize<V>> Serialize<HashMap<K, V, H>> for MapAsPairs<KA, VA>
Source§fn serialize<'a>(
value: &'a HashMap<K, V, H>,
state: &mut State,
) -> Result<Emit<'a>, Error>
fn serialize<'a>( value: &'a HashMap<K, V, H>, state: &mut State, ) -> Result<Emit<'a>, Error>
Serializes the value.
Source§fn container_shape(value: &HashMap<K, V, H>) -> ContainerShape
fn container_shape(value: &HashMap<K, V, H>) -> ContainerShape
Returns the shape of the value if it’s a map or sequence. Read more
Auto Trait Implementations§
impl<KA, VA> Freeze for MapAsPairs<KA, VA>
impl<KA, VA> RefUnwindSafe for MapAsPairs<KA, VA>
impl<KA, VA> Send for MapAsPairs<KA, VA>
impl<KA, VA> Sync for MapAsPairs<KA, VA>
impl<KA, VA> Unpin for MapAsPairs<KA, VA>
impl<KA, VA> UnsafeUnpin for MapAsPairs<KA, VA>
impl<KA, VA> UnwindSafe for MapAsPairs<KA, VA>
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