Trait jsonway::serializer::Serializer [] [src]

pub trait Serializer {
    fn build(&self, &mut ObjectBuilder);

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

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.to_string());
    }
}

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

assert_eq!(
    json.find_path(&[
        "jedi",
        "name",
    ]).unwrap().as_string().unwrap(), 
    "Saes Rrogon"
);

Required Methods

fn build(&self, &mut ObjectBuilder)

Provided Methods

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

fn serialize(&mut self, include_root: bool) -> Json

Implementors