pub trait Serializer {
// Required method
fn build(&self, _: &mut ObjectBuilder);
// Provided methods
fn root(&self) -> Option<&str> { ... }
fn serialize(&mut self, include_root: bool) -> Value { ... }
}
Expand description
Provides functionality to create custom JSON presenters for your structs.
§Example
use jsonway::{self, Serializer};
struct Jedi {
name: String
}
struct JediSerializer<'a> {
jedi: &'a Jedi
}
impl<'a> jsonway::Serializer for JediSerializer<'a> {
fn root(&self) -> Option<&str> { Some("jedi") }
fn build(&self, json: &mut jsonway::ObjectBuilder) {
json.set("name", &self.jedi.name);
}
}
let jedi = Jedi { name: "Saes Rrogon".to_string() };
let json = JediSerializer{jedi: &jedi}.serialize(true);
assert_eq!(
json.pointer("/jedi/name").unwrap().as_str().unwrap(),
"Saes Rrogon"
);