Skip to content

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 1 commit into from
Mar 30, 2016
Merged

Serde support #24

merged 1 commit into from
Mar 30, 2016

Conversation

calebmer
Copy link
Contributor

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.

@FlashCat
Copy link
Contributor

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

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 😊

@calebmer calebmer force-pushed the serde branch 2 times, most recently from d78df16 to b384820 Compare March 21, 2016 01:18
}

#[allow(missing_docs)]
pub struct LinearMapVisitor<K, V> {
Copy link
Contributor

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.

Copy link
Contributor Author

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?

Copy link
Contributor

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)
}

Copy link
Contributor Author

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 👍

Copy link
Contributor

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.

@dtolnay
Copy link
Contributor

dtolnay commented Mar 22, 2016

r? @apasel422

@calebmer
Copy link
Contributor Author

Ok, added the pub exports back in. @apasel422 look good?

@calebmer
Copy link
Contributor Author

Fixed the build errors, @apasel422 how does contain-rs feel about this?

@apasel422 apasel422 merged commit 44b0ef3 into contain-rs:master Mar 30, 2016
@calebmer calebmer deleted the serde branch March 30, 2016 16:15
@calebmer
Copy link
Contributor Author

calebmer commented Apr 3, 2016

Thanks so much for the merge! Is there a plan on when this will be released to http://crates.io?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants