// This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. use serde::de::{Deserialize, Deserializer, MapAccess, SeqAccess, Visitor}; use serde::ser::{Serialize, SerializeMap, SerializeSeq, Serializer}; use std::fmt; use std::hash::{BuildHasher, Hash}; use std::marker::PhantomData; use std::ops::Deref; use crate::hashmap::HashMap; use crate::hashset::HashSet; use crate::ordmap::OrdMap; use crate::ordset::OrdSet; use crate::vector::Vector; struct SeqVisitor<'de, S, A> where S: From>, A: Deserialize<'de>, { phantom_s: PhantomData, phantom_a: PhantomData, phantom_lifetime: PhantomData<&'de ()>, } impl<'de, S, A> SeqVisitor<'de, S, A> where S: From>, A: Deserialize<'de>, { pub(crate) fn new() -> SeqVisitor<'de, S, A> { SeqVisitor { phantom_s: PhantomData, phantom_a: PhantomData, phantom_lifetime: PhantomData, } } } impl<'de, S, A> Visitor<'de> for SeqVisitor<'de, S, A> where S: From>, A: Deserialize<'de>, { type Value = S; fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { formatter.write_str("a sequence") } fn visit_seq(self, mut access: Access) -> Result where Access: SeqAccess<'de>, { let mut v: Vec = match access.size_hint() { None => Vec::new(), Some(l) => Vec::with_capacity(l), }; while let Some(i) = access.next_element()? { v.push(i) } Ok(From::from(v)) } } struct MapVisitor<'de, S, K, V> where S: From>, K: Deserialize<'de>, V: Deserialize<'de>, { phantom_s: PhantomData, phantom_k: PhantomData, phantom_v: PhantomData, phantom_lifetime: PhantomData<&'de ()>, } impl<'de, S, K, V> MapVisitor<'de, S, K, V> where S: From>, K: Deserialize<'de>, V: Deserialize<'de>, { pub(crate) fn new() -> MapVisitor<'de, S, K, V> { MapVisitor { phantom_s: PhantomData, phantom_k: PhantomData, phantom_v: PhantomData, phantom_lifetime: PhantomData, } } } impl<'de, S, K, V> Visitor<'de> for MapVisitor<'de, S, K, V> where S: From>, K: Deserialize<'de>, V: Deserialize<'de>, { type Value = S; fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { formatter.write_str("a sequence") } fn visit_map(self, mut access: Access) -> Result where Access: MapAccess<'de>, { let mut v: Vec<(K, V)> = match access.size_hint() { None => Vec::new(), Some(l) => Vec::with_capacity(l), }; while let Some(i) = access.next_entry()? { v.push(i) } Ok(From::from(v)) } } // Set impl<'de, A: Deserialize<'de> + Ord + Clone> Deserialize<'de> for OrdSet { fn deserialize(des: D) -> Result where D: Deserializer<'de>, { des.deserialize_seq(SeqVisitor::new()) } } impl Serialize for OrdSet { fn serialize(&self, ser: S) -> Result where S: Serializer, { let mut s = ser.serialize_seq(Some(self.len()))?; for i in self.iter() { s.serialize_element(i.deref())?; } s.end() } } // Map impl<'de, K: Deserialize<'de> + Ord + Clone, V: Deserialize<'de> + Clone> Deserialize<'de> for OrdMap { fn deserialize(des: D) -> Result where D: Deserializer<'de>, { des.deserialize_map(MapVisitor::<'de, OrdMap, K, V>::new()) } } impl Serialize for OrdMap { fn serialize(&self, ser: S) -> Result where S: Serializer, { let mut s = ser.serialize_map(Some(self.len()))?; for (k, v) in self.iter() { s.serialize_entry(k.deref(), v.deref())?; } s.end() } } // HashMap impl<'de, K, V, S> Deserialize<'de> for HashMap where K: Deserialize<'de> + Hash + Eq + Clone, V: Deserialize<'de> + Clone, S: BuildHasher + Default, { fn deserialize(des: D) -> Result where D: Deserializer<'de>, { des.deserialize_map(MapVisitor::<'de, HashMap, K, V>::new()) } } impl Serialize for HashMap where K: Serialize + Hash + Eq + Clone, V: Serialize + Clone, S: BuildHasher + Default, { fn serialize(&self, ser: Ser) -> Result where Ser: Serializer, { let mut s = ser.serialize_map(Some(self.len()))?; for (k, v) in self.iter() { s.serialize_entry(k.deref(), v.deref())?; } s.end() } } // HashSet impl<'de, A: Deserialize<'de> + Hash + Eq + Clone, S: BuildHasher + Default> Deserialize<'de> for HashSet { fn deserialize(des: D) -> Result where D: Deserializer<'de>, { des.deserialize_seq(SeqVisitor::new()) } } impl Serialize for HashSet { fn serialize(&self, ser: Ser) -> Result where Ser: Serializer, { let mut s = ser.serialize_seq(Some(self.len()))?; for i in self.iter() { s.serialize_element(i.deref())?; } s.end() } } // Vector impl<'de, A: Clone + Deserialize<'de>> Deserialize<'de> for Vector { fn deserialize(des: D) -> Result where D: Deserializer<'de>, { des.deserialize_seq(SeqVisitor::<'de, Vector, A>::new()) } } impl Serialize for Vector { fn serialize(&self, ser: S) -> Result where S: Serializer, { let mut s = ser.serialize_seq(Some(self.len()))?; for i in self.iter() { s.serialize_element(i.deref())?; } s.end() } } // Tests #[cfg(test)] mod test { use super::*; use crate::proptest::{hash_map, hash_set, ord_map, ord_set, vector}; use ::proptest::num::i32; use ::proptest::proptest; use serde_json::{from_str, to_string}; proptest! { #[test] fn ser_ordset(ref v in ord_set(i32::ANY, 0..100)) { assert_eq!(v, &from_str::>(&to_string(&v).unwrap()).unwrap()); } #[test] fn ser_ordmap(ref v in ord_map(i32::ANY, i32::ANY, 0..100)) { assert_eq!(v, &from_str::>(&to_string(&v).unwrap()).unwrap()); } #[test] fn ser_hashmap(ref v in hash_map(i32::ANY, i32::ANY, 0..100)) { assert_eq!(v, &from_str::>(&to_string(&v).unwrap()).unwrap()); } #[test] fn ser_hashset(ref v in hash_set(i32::ANY, 0..100)) { assert_eq!(v, &from_str::>(&to_string(&v).unwrap()).unwrap()); } #[test] fn ser_vector(ref v in vector(i32::ANY, 0..100)) { assert_eq!(v, &from_str::>(&to_string(&v).unwrap()).unwrap()); } } }