Skip to main content

MapAsPairs

Struct MapAsPairs 

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

Source§

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>

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>

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>

Deserializes an atom that borrows from the data being deserialized into the slot. Read more
Source§

fn describe_type(d: &mut dyn Describe)

Describes the Rust shape of the type. Read more
Source§

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>

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>

Source§

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>

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>

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>

Deserializes an atom that borrows from the data being deserialized into the slot. Read more
Source§

fn describe_type(d: &mut dyn Describe)

Describes the Rust shape of the type. Read more
Source§

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>

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>

Source§

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

Returns the shape of the value if it’s a map or sequence. Read more
Source§

fn finish(value: &T, state: &mut State) -> Result<(), Error>

Invoked after the serialization finished. Read more
Source§

fn is_optional(value: &T) -> bool

Checks if the value represents an optional value. Read more
Source§

fn describe(value: &T, d: &mut dyn Describe)

Describes the Rust shape of the value. 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>

Source§

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

Returns the shape of the value if it’s a map or sequence. Read more
Source§

fn finish(value: &T, state: &mut State) -> Result<(), Error>

Invoked after the serialization finished. Read more
Source§

fn is_optional(value: &T) -> bool

Checks if the value represents an optional value. Read more
Source§

fn describe(value: &T, d: &mut dyn Describe)

Describes the Rust shape of the value. 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>
where PhantomData<fn() -> (KA, VA)>: Send,

§

impl<KA, VA> Sync for MapAsPairs<KA, VA>
where PhantomData<fn() -> (KA, VA)>: Sync,

§

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> 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 = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.