Closed glennpierce closed 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());
}
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 ?
struct GlennPierce { i: u8, }
Make sure you are using serde 0.8 and serde_derive 0.8. This crate has not been updated to serde 0.9 yet.
Thanks. That did it.
Hi, I think I just came across this issue with serde 1.0.2. Are there any plans to update this crate?
Yes, that is tracked in #4.
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,
}
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