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.pointer("/jedi/name").unwrap().as_str().unwrap(),
    "Saes Rrogon"
);

Required Methods

Provided Methods

Implementors