JsonWay

Build Status

JsonWay gives you a simple DSL for declaring JSON structures. This is particularly helpful when the generation process is fraught with conditionals and loops. It is inspired by jbuilder and has similar functionality.

```toml

Cargo.toml

[dependencies.jsonway] git = "https://github.com/rustless/jsonway" ```

API docs

Simple example

``` rust JsonWay::object(|json| { json.set("firstname", "Luke".tostring()); json.set("lastname", "Skywalker".tostring());

json.object("info", |json| {
    json.set("homeworld", "Tatooine".to_string());
    json.set("born", "19 BBY".to_string());
    json.set("died", "Between 45 ABY and 137 ABY".to_string());
});

json.list("masters", |json| {
    json.push("Obi-Wan Kenobi".to_string());
    json.push("Yoda".to_string());
    json.push("Joruus C'baoth (Briefly)".to_string());
    json.push("Darth Sidious (Briefly)".to_string());
});

});

// { // "firstname": "Luke", // "lastname": "Skywalker", // "info": { // "born": "19 BBY", // "died": "Between 45 ABY and 137 ABY", // "homeworld": "Tatooine" // }, // "masters": [ // "Obi-Wan Kenobi", // "Yoda", // "Joruus C'baoth (Briefly)", // "Darth Sidious (Briefly)" // ] // } ```

Build with iterators

~~~rust

[deriving(Show)]

enum Side { Light, Dark }

struct Jedi { name: String, side: Side }

let jedi = vec![ Jedi { name: "Saes Rrogon".tostring(), side: Dark }, Jedi { name: "Qui-Gon Jinn".tostring(), side: Light }, Jedi { name: "Obi-Wan Kenobi".to_string(), side: Light } ];

let lightjediobjectslist = JsonWay::list(|json| { // Use objects method to make list of objects json.objects(&mut jedi.iter(), |jedi, json| { match jedi.side { Light => { json.set("name".tostring(), jedi.name.tostring()); json.set("side".tostring(), jedi.side.to_string()); }, Dark => json.skip() } }) });

// [ // { // "name": "Qui-Gon Jinn", // "side": "Light" // }, // { // "name": "Obi-Wan Kenobi", // "side": "Light" // } // ]

let lightjedituplelist = JsonWay::list(|json| { // Use lists method to make list of lists json.lists(&mut jedi.iter(), |jedi, json| { match jedi.side { Light => { json.push(jedi.name.tostring()); json.push(jedi.side.to_string()); }, Dark => json.skip() } }) });

// [ // [ // "Qui-Gon Jinn", // "Light" // ], // [ // "Obi-Wan Kenobi", // "Light" // ] // ]

~~~

You can explicitly make JsonWay object return null if you want:

~~~rust // .. match jedi.side { Light => { json.push(jedi.name.tostring()); json.push(jedi.side.tostring()); }, Dark => json.null() } ~~~

Serializers

Serializier

Provides convention and functionality to create custom JSON presenters for any struct.

``` use jsonway::{ObjectBuilder, Serializer};

struct Jedi { name: String }

struct JediSerializer<'a> { jedi: &'a Jedi }

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

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

ObjectSerializer

ObjectSerializer<T> is a generic struct for single object:T serialization.

```rust use jsonway::{ObjectBuilder, ObjectSerializer};

struct Jedi { name: String }

struct JediSerializer;

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

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

ObjectScopeSerializer

ObjectScopeSerializer<T, S> is a generic struct for object:T and scope:S serialization.

```rust use jsonway::{ObjectBuilder, ObjectScopeSerializer};

struct User { id: uint, is_admin: bool }

struct Jedi { name: String, secret: String }

struct JediSerializer;

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

    if current_user.is_admin {
        json.set("secret", jedi.secret.to_string());
    }
}

}

let jedi = Jedi { name: "Palpatine".tostring(), secret: "Dark side".tostring() }; let currentuser = User { id: 1, isadmin: true }; let json = JediSerializer.serialize(&jedi, &current_user); ```

ListSerializer

Provides convention and functionality to create custom JSON presenters for the list of resources including meta information.

TODO: Example