JsonWay
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 functional.
# Cargo.toml
[]
= "https://github.com/rustless/jsonway"
Simple example
;
// {
// "first_name": "Luke",
// "last_name": "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)"
// ]
// }
object
Build with iterators
let jedi = vec!;
let light_jedi_objects_list = list;
// [
// {
// "name": "Qui-Gon Jinn",
// "side": "Light"
// },
// {
// "name": "Obi-Wan Kenobi",
// "side": "Light"
// }
// ]
let light_jedi_tuple_list = list;
// [
// [
// "Qui-Gon Jinn",
// "Light"
// ],
// [
// "Obi-Wan Kenobi",
// "Light"
// ]
// ]
You can explicitly make JsonWay
object return null
if you want:
// ..
match jedi.side
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.
use ;
;
let jedi = Jedi ;
let json = JediSerializer.serialize;
ObjectScopeSerializer
ObjectScopeSerializer<T, S>
is a generic struct for object:T
and scope:S
serialization.
use ;
;
let jedi = Jedi ;
let current_user = User ;
let json = JediSerializer.serialize;
ListSerializer
Provides convention and functionality to create custom JSON presenters for the list of resources including meta
information.
TODO: Example