dtolnay / erased-serde

Type-erased Serialize, Serializer and Deserializer traits
Apache License 2.0
709 stars 36 forks source link

Example of using with serde macros #5

Closed glennpierce closed 7 years ago

glennpierce commented 7 years ago

Hi

So I have a trait defined like

pub trait Element: Send + Sync

and it in included in another type like

[derive(Serialize, Deserialize)]

pub struct Pipeline { name: String,

elements: Vec<Box<Element>>,

}

I am a little confused with the examples how I could get serde with erased-serde to serialise this. Would I add the trait to Element and implement for all the concrete types of element ? (There may eventually be hundreds of concrete types)

Sorry if it is not meant to be used in this way.

Thanks

dtolnay commented 7 years ago

You need to provide a Serialize implementation for your trait object (impl serde::Serialize for Element). Something like this:

#[macro_use]
extern crate serde_derive;

extern crate serde;
extern crate serde_json;
extern crate erased_serde;

trait Element: erased_serde::Serialize + Send + Sync {
    /* ... */
}

impl serde::Serialize for Element {
    fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
        where S: serde::Serializer
    {
        self.erased_serialize(serializer).map_err(|err| {
            serde::ser::Error::custom(err.to_string())
        })
    }
}

#[derive(Serialize)]
struct GlennPierce {
    i: u8,
}

impl Element for GlennPierce {
    /* ... */
}

#[derive(Serialize)]
struct Pipeline {
    name: String,
    elements: Vec<Box<Element>>,
}

fn main() {
    let pipeline = Pipeline {
        name: "glennpierce".to_owned(),
        elements: vec![
            Box::new(GlennPierce { i: 0 }),
            Box::new(GlennPierce { i: 1 }),
        ],
    };
    println!("{}", serde_json::to_string(&pipeline).unwrap());
}
glennpierce commented 7 years ago

Thanks for this.

Although for this I get something like

error[E0277]: the trait bound GlennPierce: serde::ser::Serialize is not satisfied

impl Element for GlennPierce { | ^^^^^^^ the trait serde::ser::Serialize is not implemented for `GlennPierce

Should not the GlennPierce struct implement the erased_serde::Serialize and not serde::Serialize ?

[derive(Serialize)]

struct GlennPierce { i: u8, }

dtolnay commented 7 years ago

Make sure you are using serde 0.8 and serde_derive 0.8. This crate has not been updated to serde 0.9 yet.

glennpierce commented 7 years ago

Thanks. That did it.

adaszko commented 7 years ago

Hi, I think I just came across this issue with serde 1.0.2. Are there any plans to update this crate?

dtolnay commented 7 years ago

Yes, that is tracked in #4.