I'm trying to use serde-reflection
to create a JSON structure explaining the struct structure in order to transfer and receive structs as JavaScript an ArrayBuffer
and then de-serialize that into a JavaScript Object in the browser.
I want a way to "register" the structs I'm interested in and then keeping track of all those registered types so that they can be returned from a web server.
extern crate serde_json;
use serde_json::json;
use serde_reflection::{Samples, Tracer, TracerConfig};
pub struct MessageRegistry {
tracer: Tracer,
samples: Samples
}
impl MessageRegistry {
pub fn new() -> MessageRegistry {
MessageRegistry {
tracer: Tracer::new(TracerConfig::default()),
samples: Samples::new()
}
}
pub fn add_type<T>(&mut self) {
self.tracer.trace_type::<T>(&self.samples).unwrap();
//^^^^^^^^^^ the trait `serde::Deserialize<'_>` is not implemented for `T`
}
pub fn to_json_string(&mut self) -> String {
let registry = self.tracer.registry().unwrap();
serde_json::to_string(®istry).unwrap()
}
}
I'm stuck on using the tracer.trace_type::<T>(&self.samples)
line. If I try it out with pub fn add<'de, T: serde::Deserialize<'de>>(&mut self) {
instead I get into problems with the lifetime instead:
cannot infer an appropriate lifetime for borrow expression due to conflicting requirements
expected `serde::Deserialize<'_>`
found `serde::Deserialize<'de>`