-
Notifications
You must be signed in to change notification settings - Fork 12
Serde support #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Serde support #24
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
//! An optional implementation of serialization/deserialization. Reference | ||
//! implementations used: | ||
//! | ||
//! - [Serialize][1]. | ||
//! - [Deserialize][2]. | ||
//! | ||
//! [1]: https://github.com/serde-rs/serde/blob/97856462467db2e90cf368e407c7ebcc726a01a9/serde/src/ser/impls.rs#L601-L611 | ||
//! [2]: https://github.com/serde-rs/serde/blob/97856462467db2e90cf368e407c7ebcc726a01a9/serde/src/de/impls.rs#L694-L746 | ||
|
||
extern crate serde; | ||
|
||
use super::LinearMap; | ||
|
||
use self::serde::{Serialize, Serializer, Deserialize, Deserializer}; | ||
use self::serde::ser::impls::MapIteratorVisitor; | ||
use self::serde::de::{Visitor, MapVisitor, Error}; | ||
|
||
use std::marker::PhantomData; | ||
|
||
impl<K, V> Serialize for LinearMap<K, V> | ||
where K: Serialize + Ord, | ||
V: Serialize, | ||
{ | ||
#[inline] | ||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error> | ||
where S: Serializer, | ||
{ | ||
serializer.serialize_map(MapIteratorVisitor::new(self.iter(), Some(self.len()))) | ||
} | ||
} | ||
|
||
#[allow(missing_docs)] | ||
pub struct LinearMapVisitor<K, V> { | ||
marker: PhantomData<LinearMap<K, V>>, | ||
} | ||
|
||
impl<K, V> LinearMapVisitor<K, V> { | ||
#[allow(missing_docs)] | ||
pub fn new() -> Self { | ||
LinearMapVisitor { | ||
marker: PhantomData, | ||
} | ||
} | ||
} | ||
|
||
impl<K, V> Visitor for LinearMapVisitor<K, V> | ||
where K: Deserialize + Eq, | ||
V: Deserialize, | ||
{ | ||
type Value = LinearMap<K, V>; | ||
|
||
#[inline] | ||
fn visit_unit<E>(&mut self) -> Result<Self::Value, E> | ||
where E: Error, | ||
{ | ||
Ok(LinearMap::new()) | ||
} | ||
|
||
#[inline] | ||
fn visit_map<Visitor>(&mut self, mut visitor: Visitor) -> Result<Self::Value, Visitor::Error> | ||
where Visitor: MapVisitor, | ||
{ | ||
let mut values = LinearMap::with_capacity(visitor.size_hint().0); | ||
|
||
while let Some((key, value)) = try!(visitor.visit()) { | ||
values.insert(key, value); | ||
} | ||
|
||
try!(visitor.end()); | ||
|
||
Ok(values) | ||
} | ||
} | ||
|
||
impl<K, V> Deserialize for LinearMap<K, V> | ||
where K: Deserialize + Eq, | ||
V: Deserialize, | ||
{ | ||
fn deserialize<D>(deserializer: &mut D) -> Result<LinearMap<K, V>, D::Error> | ||
where D: Deserializer, | ||
{ | ||
deserializer.deserialize_map(LinearMapVisitor::new()) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#![cfg(feature = "serde_impl")] | ||
|
||
extern crate linear_map; | ||
extern crate serde; | ||
extern crate serde_json; | ||
|
||
use linear_map::LinearMap; | ||
|
||
#[test] | ||
fn test_ser_empty() { | ||
let map = LinearMap::<String, u32>::new(); | ||
let j = serde_json::to_string(&map).unwrap(); | ||
let expected = "{}"; | ||
assert_eq!(j, expected); | ||
} | ||
|
||
#[test] | ||
fn test_ser() { | ||
let mut map = LinearMap::new(); | ||
map.insert("b", 20); | ||
map.insert("a", 10); | ||
map.insert("c", 30); | ||
|
||
let j = serde_json::to_string(&map).unwrap(); | ||
let expected = r#"{"b":20,"a":10,"c":30}"#; | ||
assert_eq!(j, expected); | ||
} | ||
|
||
#[test] | ||
fn test_de_empty() { | ||
let j = "{}"; | ||
let map: LinearMap<String, u32> = serde_json::from_str(j).unwrap(); | ||
assert_eq!(map.len(), 0); | ||
} | ||
|
||
#[test] | ||
fn test_de() { | ||
let j = r#"{"b":20,"a":10,"c":30}"#; | ||
let map: LinearMap<String, u32> = serde_json::from_str(j).unwrap(); | ||
let items: Vec<_> = map.iter().map(|(k, v)| (k.clone(), *v)).collect(); | ||
assert_eq!(items, [("b".to_owned(), 20), | ||
("a".to_owned(), 10), | ||
("c".to_owned(), 30)]); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This does not need to be
pub
- then you don't need the#[allow(missing_docs)]
. Same forLinearMapVisitor::new
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I use
LinearMap
in aValue
-like enum in my code base. I'd like to serialize/deserialize that value. Is there a good way to deserialize a map of its visitor is not public?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even so, why does the visitor need to be public? All you should need are the Serialize and Deserialize traits implemented for LinearMap. Here is a working example that deserializes a Value-like enum containing a LinearMap:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a requirement to be on stable, so no custom derives, and we really don't want to use syntex because the interface is very undesirable 😐
serde_json
uses the visitor implementation here, so as that was my reference I also did it that way. I thought it that was the canonical way of deserializing in this way.It's not too hard for me to reimplement this functionality, I'll make it private 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay I understand now why it makes sense for it to be public. You can change it back.