-
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
Serde support #24
Conversation
r? @cgaebel (I've picked a reviewer for you, use r? to override) |
//! - [Serialize][1]. | ||
//! - [Deserialize][2]. | ||
//! | ||
//! [1]: https://github.com/serde-rs/serde/blob/master/serde/src/ser/impls.rs#L601-L611 |
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.
Link to a specific commit instead because the line numbers in "master" will become outdated.
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.
Of course, thanks. Usually I'm good at catching that 😊
d78df16
to
b384820
Compare
} | ||
|
||
#[allow(missing_docs)] | ||
pub struct LinearMapVisitor<K, V> { |
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 for LinearMapVisitor::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 a Value
-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:
#![feature(plugin, custom_derive)]
#![plugin(serde_macros)]
extern crate serde;
extern crate serde_yaml;
extern crate linear_map;
use linear_map::LinearMap;
#[derive(Deserialize, PartialEq, Debug)]
enum Value {
Null,
Map(LinearMap<String, String>),
}
fn main() {
let y = "Map: {k: v}";
let v: Value = serde_yaml::from_str(&y).unwrap();
assert_eq!(v, expected());
}
fn expected() -> Value {
let mut map = LinearMap::new();
map.insert(String::from("k"), String::from("v"));
Value::Map(map)
}
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.
r? @apasel422 |
Ok, added the |
Fixed the build errors, @apasel422 how does |
Thanks so much for the merge! Is there a plan on when this will be released to http://crates.io? |
This PR adds support for optional serialization/deserialization using the serde library.
Pretty much a clone of @dtolnay's PR, contain-rs/linked-hash-map#48, which was reviewed by @apasel422.