jsonway/
array_builder.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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>
}

/// Use ArrayBuilder to produce JSON arrays
impl ArrayBuilder {

    pub fn new() -> ArrayBuilder {
        ArrayBuilder {
            array: vec![],
            null: false,
            skip: false,
            root: None
        }
    }

    /// Initialize builder with initial value.
    pub fn from_json(array: Value) -> Option<ArrayBuilder> {
        match array {
            Value::Array(array) => Some(ArrayBuilder {
                array: array,
                null: false,
                skip: false,
                root: None
            }),
            _ => None
        }
    }

    /// Create new ArrayBuilder, pass it to closure as mutable ref and return.
    pub fn build<F>(builder: F) -> ArrayBuilder where F: FnOnce(&mut ArrayBuilder) {
        let mut bldr = ArrayBuilder::new();
        builder(&mut bldr);

        bldr
    }

    /// Push JSON value to array.
    pub fn push_json(&mut self, value: Value) {
        self.array.push(value);
    }

    /// Create new array and push it.
    pub fn array<F>(&mut self, builder: F) where F: FnOnce(&mut ArrayBuilder) {
        self.push(ArrayBuilder::build(builder).unwrap());
    }

    /// Create new object and push it
    pub fn object<F>(&mut self, builder: F) where F: FnOnce(&mut object_builder::ObjectBuilder) {
        self.push(object_builder::ObjectBuilder::build(builder).unwrap());
    }

    /// It you call `null`, this array will be converted to null when converting
    /// to raw JSON value.
    pub fn null(&mut self) {
        self.null = true;
    }

    /// It you call `skip`, this array will be skipped.
    pub fn skip(&mut self) {
        self.skip = true;
    }

    // Set custom root for result Value object
    pub fn root(&mut self, root: &str) {
        self.root = Some(root.to_string());
    }

    pub fn has_root(&mut self) -> bool {
        self.root.is_some()
    }

    /// Move out internal JSON value.
    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()
        }
    }

    /// Move out internal JSON value.
    #[inline]
    fn unwrap_internal(self) -> Value {
        if self.null {
            Value::Null
        } else {
            Value::Array(self.array)
        }
    }
}

impl ArrayBuilder {
    /// Push to array something that can be converted to JSON.
    pub fn push<T: Serialize>(&mut self, value: T) {
        self.push_json(to_value(&value).unwrap());
    }
}

impl ArrayBuilder {

    /// Fill this array by objects builded from iterator.
    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())
            }
        }
    }

    // Fill this array by arrays builded from iterator.
    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())
            }
        }
    }

    /// Fill this array by JSON values builded from iterator.
    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 {
    /// Copy self to new JSON instance.
    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)
    }
}