jsonway/
object_builder.rsuse serde_json::{Value, to_value};
use serde::{Serialize, Serializer};
pub type Object = ::serde_json::Map<String, Value>;
use array_builder;
pub struct ObjectBuilder {
pub object: Object,
pub null: bool,
pub skip: bool,
pub root: Option<String>
}
impl ObjectBuilder {
pub fn new() -> ObjectBuilder {
ObjectBuilder {
object: ::serde_json::Map::new(),
null: false,
skip: false,
root: None
}
}
pub fn from_json(object: Value) -> Option<ObjectBuilder> {
match object {
Value::Object(object) => Some(ObjectBuilder {
object: object,
null: false,
skip: false,
root: None
}),
_ => None
}
}
pub fn build<F>(builder: F) -> ObjectBuilder where F: FnOnce(&mut ObjectBuilder) {
let mut bldr = ObjectBuilder::new();
builder(&mut bldr);
bldr
}
pub fn null(&mut self) {
self.null = true;
}
pub fn skip(&mut self) {
self.skip = true;
}
pub fn root(&mut self, root: &str) {
self.root = Some(root.to_string());
}
pub fn has_root(&mut self) -> bool {
self.root.is_some()
}
pub fn unwrap(self) -> Value {
if self.root.is_some() {
let mut obj = ::serde_json::Map::new();
let root = self.root.as_ref().unwrap().to_string();
let self_json = self.unwrap_internal();
obj.insert(root, self_json);
Value::Object(obj)
} else {
self.unwrap_internal()
}
}
#[inline]
fn unwrap_internal(self) -> Value {
if self.null {
Value::Null
} else {
Value::Object(self.object)
}
}
}
impl ObjectBuilder {
pub fn set<V: Serialize, N: Into<String>>(&mut self, name: N, value: V) {
self.set_json(name, to_value(&value).unwrap());
}
pub fn call<V: Serialize, N: Into<String>>(&mut self, name: N, value: V) {
self.set(name, value);
}
}
impl ObjectBuilder {
pub fn set_json<N: Into<String>>(&mut self, name: N, value: Value) {
self.object.insert(name.into(), value);
}
pub fn array<N: Into<String>, F>(&mut self, name: N, builder: F) where F: FnOnce(&mut array_builder::ArrayBuilder) {
self.set(name.into(), array_builder::ArrayBuilder::build(builder).unwrap());
}
pub fn object<N: Into<String>, F>(&mut self, name: N, builder: F) where F: FnOnce(&mut ObjectBuilder) {
self.set(name.into(), ObjectBuilder::build(builder).unwrap());
}
}
impl Serialize for ObjectBuilder {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer {
let json_object = if self.null { Value::Null } else { to_value(&self.object).unwrap() };
json_object.serialize(serializer)
}
}