use serde_json::{Value, to_value};
use serde::{Serialize, Serializer};
pub type JsonArray = Vec<Value>;
use object_builder;
pub struct ArrayBuilder {
pub array: JsonArray,
pub null: bool,
pub skip: bool,
pub root: Option<String>
}
impl ArrayBuilder {
pub fn new() -> ArrayBuilder {
ArrayBuilder {
array: vec![],
null: false,
skip: false,
root: None
}
}
pub fn from_json(array: Value) -> Option<ArrayBuilder> {
match array {
Value::Array(array) => Some(ArrayBuilder {
array: array,
null: false,
skip: false,
root: None
}),
_ => None
}
}
pub fn build<F>(builder: F) -> ArrayBuilder where F: FnOnce(&mut ArrayBuilder) {
let mut bldr = ArrayBuilder::new();
builder(&mut bldr);
bldr
}
pub fn push_json(&mut self, value: Value) {
self.array.push(value);
}
pub fn array<F>(&mut self, builder: F) where F: FnOnce(&mut ArrayBuilder) {
self.push(ArrayBuilder::build(builder).unwrap());
}
pub fn object<F>(&mut self, builder: F) where F: FnOnce(&mut object_builder::ObjectBuilder) {
self.push(object_builder::ObjectBuilder::build(builder).unwrap());
}
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::Array(self.array)
}
}
}
impl ArrayBuilder {
pub fn push<T: Serialize>(&mut self, value: T) {
self.push_json(to_value(&value).unwrap());
}
}
impl ArrayBuilder {
pub fn objects<A, T: Iterator<Item=A>, F>(&mut self, iter: T, func: F) where F: Fn(A, &mut object_builder::ObjectBuilder) {
for a in iter {
let mut bldr = object_builder::ObjectBuilder::new();
func(a, &mut bldr);
if !bldr.skip {
self.push(bldr.unwrap())
}
}
}
pub fn arrays<A, T: Iterator<Item=A>, F>(&mut self, iter: T, func: F) where F: Fn(A, &mut ArrayBuilder) {
for a in iter {
let mut bldr = ArrayBuilder::new();
func(a, &mut bldr);
if !bldr.skip {
self.push(bldr.unwrap())
}
}
}
pub fn map<A, T: Iterator<Item=A>, F>(&mut self, iter: T, func: F) where F: Fn(A) -> Value {
for a in iter {
self.push(func(a))
}
}
}
impl Serialize for ArrayBuilder {
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.array).unwrap() };
json_object.serialize(serializer)
}
}