• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //! An optional implementation of serialization/deserialization.
2 
3 extern crate serde;
4 
5 use std::fmt::{Formatter, Result as FmtResult};
6 use std::hash::{BuildHasher, Hash};
7 use std::marker::PhantomData;
8 
9 use super::LinkedHashMap;
10 
11 use self::serde::de::{Error, MapAccess, Visitor};
12 use self::serde::ser::SerializeMap;
13 use self::serde::{Deserialize, Deserializer, Serialize, Serializer};
14 
15 impl<K, V, S> Serialize for LinkedHashMap<K, V, S>
16 where
17     K: Serialize + Eq + Hash,
18     V: Serialize,
19     S: BuildHasher,
20 {
21     #[inline]
serialize<T>(&self, serializer: T) -> Result<T::Ok, T::Error> where T: Serializer,22     fn serialize<T>(&self, serializer: T) -> Result<T::Ok, T::Error>
23     where
24         T: Serializer,
25     {
26         let mut map_serializer = serializer.serialize_map(Some(self.len()))?;
27         for (k, v) in self {
28             map_serializer.serialize_key(k)?;
29             map_serializer.serialize_value(v)?;
30         }
31         map_serializer.end()
32     }
33 }
34 
35 #[derive(Debug)]
36 /// `serde::de::Visitor` for a linked hash map.
37 pub struct LinkedHashMapVisitor<K, V> {
38     marker: PhantomData<LinkedHashMap<K, V>>,
39 }
40 
41 impl<K, V> LinkedHashMapVisitor<K, V> {
42     /// Creates a new visitor for a linked hash map.
new() -> Self43     pub fn new() -> Self {
44         LinkedHashMapVisitor {
45             marker: PhantomData,
46         }
47     }
48 }
49 
50 impl<K, V> Default for LinkedHashMapVisitor<K, V> {
default() -> Self51     fn default() -> Self {
52         LinkedHashMapVisitor::new()
53     }
54 }
55 
56 impl<'de, K, V> Visitor<'de> for LinkedHashMapVisitor<K, V>
57 where
58     K: Deserialize<'de> + Eq + Hash,
59     V: Deserialize<'de>,
60 {
61     type Value = LinkedHashMap<K, V>;
62 
expecting(&self, formatter: &mut Formatter) -> FmtResult63     fn expecting(&self, formatter: &mut Formatter) -> FmtResult {
64         write!(formatter, "a map")
65     }
66 
67     #[inline]
visit_unit<E>(self) -> Result<Self::Value, E> where E: Error,68     fn visit_unit<E>(self) -> Result<Self::Value, E>
69     where
70         E: Error,
71     {
72         Ok(LinkedHashMap::new())
73     }
74 
75     #[inline]
visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error> where M: MapAccess<'de>,76     fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
77     where
78         M: MapAccess<'de>,
79     {
80         let mut values = LinkedHashMap::with_capacity(map.size_hint().unwrap_or(0));
81 
82         while let Some((key, value)) = map.next_entry()? {
83             values.insert(key, value);
84         }
85 
86         Ok(values)
87     }
88 }
89 
90 impl<'de, K, V> Deserialize<'de> for LinkedHashMap<K, V>
91 where
92     K: Deserialize<'de> + Eq + Hash,
93     V: Deserialize<'de>,
94 {
deserialize<D>(deserializer: D) -> Result<LinkedHashMap<K, V>, D::Error> where D: Deserializer<'de>,95     fn deserialize<D>(deserializer: D) -> Result<LinkedHashMap<K, V>, D::Error>
96     where
97         D: Deserializer<'de>,
98     {
99         deserializer.deserialize_map(LinkedHashMapVisitor::new())
100     }
101 }
102