Trait jsonway::serializer::ObjectSerializer [] [src]

pub trait ObjectSerializer<T> {
    fn build(&self, &T, &mut ObjectBuilder);

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

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.find_path(&[
        "jedi",
        "name",
    ]).unwrap().as_string().unwrap(),
    "Saes Rrogon"
);

Required Methods

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

Provided Methods

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

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

Implementors