jsonway::serializer

Trait ObjectSerializer

Source
pub trait ObjectSerializer<T> {
    // Required method
    fn build(&self, _: &T, _: &mut ObjectBuilder);

    // Provided methods
    fn root(&self) -> Option<&str> { ... }
    fn serialize(&mut self, obj: &T, include_root: bool) -> Value { ... }
}
Expand description

Provides functionality to create custom JSON presenters for your structs.

§Example

use jsonway::{self, ObjectSerializer};

struct Jedi {
    name: String
}

struct JediSerializer;

impl jsonway::ObjectSerializer<Jedi> for JediSerializer {
    fn root(&self) -> Option<&str> { Some("jedi") }
    fn build(&self, jedi: &Jedi, json: &mut jsonway::ObjectBuilder) {
        json.set("name", &jedi.name);
    }
}

let jedi = Jedi { name: "Saes Rrogon".to_string() };
let json = JediSerializer.serialize(&jedi, true);

assert_eq!(
    json.pointer("/jedi/name").unwrap().as_str().unwrap(),
    "Saes Rrogon"
);

Required Methods§

Source

fn build(&self, _: &T, _: &mut ObjectBuilder)

Provided Methods§

Source

fn root(&self) -> Option<&str>

Source

fn serialize(&mut self, obj: &T, include_root: bool) -> Value

Implementors§