json_ops 0.1.0

Implement json pointer following the json pointer syntax, with type Option<&toml::Value>. Overload / as path operator to point into a node in json tree, as well as some other meaningfull operator overload.
Documentation
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
// Not sub mod but seperate file for operator overload interface.
// Used by include! macro in valueptr mod.

use std::ops::{Div, BitOr, Shl, Deref, DerefMut};

/* ------------------------------------------------------------ */

/// Overload `*` deref operator to treate pointer as `Option<&json::Value>`.
impl<'tr, Value> Deref for ValuePtr<'tr, Value>
where Value: ValuePath + ValueReader
{
    type Target = Option<&'tr Value>;
    fn deref(&self) -> &Self::Target {
        &self.ptr
    }
}

/// Path operator `/`, visit sub-node by string key for object or index for array.
/// 
/// First try directly json index, then try json pointer syntax, otherwise
/// return `None` if both fail.
///
/// ```rust
/// # use serde_json::json;
/// # use json_ops::ValuePath;
/// let v = json!({"i":1,"f":3.14,"a":["pi",null,true]});
/// let p = v.path() / "i";
/// assert_eq!(p.unwrap(), &v["i"]);
/// let p = v.path() / "a" / 0;
/// assert_eq!(p.unwrap(), &v["a"][0]);
/// let p = v.path() / "a/0";
/// assert_eq!(p.unwrap(), &v["a"][0]);
/// ```
impl<'tr, Value> Div<usize> for ValuePtr<'tr, Value>
where Value: ValuePath + ValueReader
{
    type Output = Self;
    fn div(self, rhs: usize) -> Self::Output {
        self.path_index(rhs)
    }
}

impl<'tr, Value> Div<&str> for ValuePtr<'tr, Value>
where Value: ValuePath + ValueReader
{
    type Output = Self;
    fn div(self, rhs: &str) -> Self::Output {
        self.path_str(rhs)
    }
}

impl<'tr, Value> Div<String> for ValuePtr<'tr, Value>
where Value: ValuePath + ValueReader
{
    type Output = Self;
    fn div(self, rhs: String) -> Self::Output {
        self.path_str(rhs.as_str())
    }
}

/// Pipe operator `|` to get string refer or default `rhs`
/// when invalid pointer or the json type is not string.
/// Usually used with literal `|"default"` or just simple `|""`.
///
/// ```rust
/// # use serde_json::json;
/// # use json_ops::ValuePath;
/// let v = json!({"sub":{"key":"val"}});
/// assert_eq!(v.path()/"sub" | "", "");
/// assert_eq!(v.path()/"sub"/"key" | "", "val");
/// assert_eq!(v.path()/"sub"/"any" | "xxx", "xxx");
/// ```
impl<'tr, Value> BitOr<&'tr str> for ValuePtr<'tr, Value>
where Value: ValuePath + ValueReader
{
    type Output = &'tr str;
    fn bitor(self, rhs: &'tr str) -> Self::Output {
        self.get_str(rhs)
    }
}

/// Pipe operator `|` try to get string from a json node or default `rhs`.
///
/// * if `lhs` is really string node, return the string content.
/// * if `rhs` is empty, stringfy the json for any other type.
/// * if `rhs` is "0", only stringfy node for i64 or u64 type.
/// * if `rhs` is "0.0", only stringfy node for float type.
/// * if `rhs` is "bool", only stringfy node for bool type.
/// * if `rhs` is "[]", only stringfy node for array type.
/// * if `rhs` is "{}", only stringfy node for object type.
/// * otherwise, return `rhs` as default.
///
/// ```rust
/// # use serde_json::json;
/// # use json_ops::ValuePath;
/// let v = json!({"int":3, "float":3.14, "str":"null", "array":[1,null,"null"]});
/// 
/// assert_eq!(v.path()/"str" | "".to_string(), "null");
/// assert_eq!(v.path()/"int" | "".to_string(), "3");
/// assert_eq!(v.path()/"int" | "0".to_string(), "3");
/// assert_eq!(v.path()/"int" | "0.0".to_string(), "0.0");
/// assert_eq!(v.path()/"array" | "".to_string(), "[1,null,\"null\"]");
/// assert_eq!(v.path()/"array" | "[]".to_string(), "[1,null,\"null\"]");
/// ```
///
/// Note that the `rhs` string would be moved.
/// If want to only get content from string node, use `| &str` version instead.
impl<'tr, Value> BitOr<String> for ValuePtr<'tr, Value>
where Value: ValuePath + ValueReader
{
    type Output = String;
    fn bitor(self, rhs: String) -> Self::Output {
        self.get_string(rhs)
    }
}

/// Pipe operator `|` to get integer value if the json node don't hold a integer
/// or string which can parse to integer, or bool which conver to 1 or 0,
/// otherwise return default `rhs`. 
///
/// ```rust
/// # use serde_json::json;
/// # use json_ops::ValuePath;
/// let v = json!({"a":1, "b":"2", "c":"nan", "e":true});
/// assert_eq!(v.path()/"a" | 0, 1);
/// assert_eq!(v.path()/"b" | 0, 2);
/// assert_eq!(v.path()/"c" | 0, 0);
/// assert_eq!(v.path()/"d" | -1, -1);
/// assert_eq!(v.path()/"e" | -1, 1);
/// ```
///
/// Not support `| u64` overload, only support `| i64`
/// to make `| 0` as simple as possible in most use case.
impl<'tr, Value> BitOr<i64> for ValuePtr<'tr, Value>
where Value: ValuePath + ValueReader
{
    type Output = i64;
    fn bitor(self, rhs: i64) -> Self::Output {
        self.get_i64(rhs)
    }
}

/// Pipe operator `|` to get float value if the json node hold a float
/// or string which can parse to float, otherwise default `rhs`. 
///
/// ```rust
/// # use serde_json::json;
/// # use json_ops::ValuePath;
/// let v = json!({"a":1.0, "b":"2.0", "c":"not"});
/// assert_eq!(v.path()/"a" | 0.0, 1.0);
/// assert_eq!(v.path()/"b" | 0.0, 2.0);
/// assert_eq!(v.path()/"c" | 0.0, 0.0);
/// assert_eq!(v.path()/"d" | -1.0, -1.0);
/// ```
impl<'tr, Value> BitOr<f64> for ValuePtr<'tr, Value>
where Value: ValuePath + ValueReader
{
    type Output = f64;
    fn bitor(self, rhs: f64) -> Self::Output {
        self.get_f64(rhs)
    }
}

/// Pipe operator `|` to get bool value if the json node hold a bool
/// or string which can parse to bool, otherwise default `rhs`. 
/// And for integer node, non-zero value is treated as true, zero is false.
///
/// ```rust
/// # use serde_json::json;
/// # use json_ops::ValuePath;
/// let v = json!({"a":1, "b":"2", "c":"nan", "e":true});
/// assert_eq!(v.path()/"a" | false, true);
/// assert_eq!(v.path()/"b" | false, false);
/// assert_eq!(v.path()/"c" | false, false);
/// assert_eq!(v.path()/"e" | false, true);
/// ```
impl<'tr, Value> BitOr<bool> for ValuePtr<'tr, Value>
where Value: ValuePath + ValueReader
{
    type Output = bool;
    fn bitor(self, rhs: bool) -> Self::Output {
        self.get_bool(rhs)
    }
}

/* ------------------------------------------------------------ */

/// Overload `*` deref operator to treate pointer as `Option<&mut json::Value>`.
impl<'tr, Value> Deref for ValuePtrMut<'tr, Value>
where Value: ValuePath + ValueReader + ValueWriter
{
    type Target = Option<&'tr mut Value>;
    fn deref(&self) -> &Self::Target {
        &self.ptr
    }
}

/// Overload `*` deref operator to treate pointer as `Option<&mut json::Value>`.
impl<'tr, Value> DerefMut for ValuePtrMut<'tr, Value>
where Value: ValuePath + ValueReader + ValueWriter
{
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.ptr
    }
}

/// Path operator `/`, visit sub-node by string key for object or index for array.
/// Can chained as `jsonptr / "path" / "to" / "node"` or `jsonptr / "path/to/node"`.
/// First try directly json index, then try json pointer syntax, then
/// return `None` if both fail.
/// Hope to change the node it point to, otherwise better to use immutable `ValuePtr`.
///
/// ```rust
/// # use serde_json::json;
/// # use json_ops::ValuePath;
/// let mut v = json!({"i":1,"f":3.14,"a":["pi",null,true]});
/// let p = v.path_mut() / "i";
/// assert_eq!(p | 0, 1);
/// let p = v.path_mut() / "a" / 0;
/// assert_eq!(p | "", "pi");
/// let p = v.path_mut() / "a/0";
/// assert_eq!(p | "", "pi");
/// ```
impl<'tr, Value> Div<usize> for ValuePtrMut<'tr, Value>
where Value: ValuePath + ValueReader + ValueWriter
{
    type Output = Self;
    fn div(mut self, rhs: usize) -> Self::Output {
        self.path_index(rhs)
    }
}

impl<'tr, Value> Div<&str> for ValuePtrMut<'tr, Value>
where Value: ValuePath + ValueReader + ValueWriter
{
    type Output = Self;
    fn div(mut self, rhs: &str) -> Self::Output {
        self.path_str(rhs)
    }
}

impl<'tr, Value> Div<String> for ValuePtrMut<'tr, Value>
where Value: ValuePath + ValueReader + ValueWriter
{
    type Output = Self;
    fn div(mut self, rhs: String) -> Self::Output {
        self.path_str(rhs.as_str())
    }
}

/// Pipe operator `|` to get string refer or default `rhs`.
/// 
/// Behaves the same as `ValuePtr | &str`, except that
/// the `ValuePtrMut` in `lhs` would be moved and cannot used any more.
impl<'tr, Value> BitOr<&'tr str> for ValuePtrMut<'tr, Value>
where Value: ValuePath + ValueReader + ValueWriter
{
    type Output = &'tr str;
    fn bitor(mut self, rhs: &'tr str) -> Self::Output {
        self.immut().bitor(rhs)
    }
}

/// Proxy of `|` operator overload for mutable json pointer.
/// Would expand for String, i64, f64, bool.
macro_rules! bitor_mut {
    ($rhs:ty) => {
        impl<'tr, Value> BitOr<$rhs> for ValuePtrMut<'tr, Value>
            where Value: ValuePath + ValueReader + ValueWriter
        {
            type Output = $rhs;
            fn bitor(mut self, rhs: $rhs) -> Self::Output {
                self.immut().bitor(rhs)
            }
        }
    };
}

bitor_mut!(String);
bitor_mut!(i64);
bitor_mut!(f64);
bitor_mut!(bool);

/// Operator `<<` to put a scalar value into json node, what supported type inclde:
/// &str, String, i64, f64, bool, and unit() for json null.
/// It will consume the `lhs` pointer and return a new one point to the same node
/// after modify it's content and may type.
///
/// ```rust
/// # use serde_json::json;
/// # use json_ops::ValuePath;
/// let mut v = json!({});
/// 
/// let _ = v.path_mut() << "pi";
/// assert!(v.is_string());
/// let _ = v.path_mut() << 1;
/// assert!(v.is_i64());
/// let _ = v.path_mut() << 3.14;
/// assert!(v.is_f64());
/// let _ = v.path_mut() << true;
/// assert!(v.is_boolean());
/// let _ = v.path_mut() << ();
/// assert!(v.is_null());
///
/// let pi = String::from("PI");
/// let _ = v.path_mut() << "pi" << 3.14 << pi;
/// assert_eq!(v, "PI");
/// ```
///
/// Though put operator `<<` can be chained, the later one overwrite the previous value.
impl<'tr, Value, Rhs> Shl<Rhs> for ValuePtrMut<'tr, Value>
where Rhs: ScalarValue, Value: From<Rhs> + ValuePath + ValueReader + ValueWriter
{
    type Output = Self;
    fn shl(mut self, rhs: Rhs) -> Self::Output {
        self.put_value(rhs)
    }
}

/// Operator `<<` to push key-value pair (tuple) into json object.
///
/// It will consume the `lhs` pointer and return a new one point to the same node
/// after modify it's content and type.
/// If the node is object, the new pair is insert to it,
/// otherwise change the node to object with only one new pair.
/// The key and value will be moved into json node, and key require String conversion.
///
/// ```rust
/// # use serde_json::json;
/// # use json_ops::ValuePath;
/// let mut v = json!("init string node");
/// 
/// let _ = v.path_mut() << ("i", 1) << ("f", 3.14);
/// assert_eq!(v, json!({"i":1,"f":3.14}));
/// ```
///
/// It can chain `<<` to object with several pairs, while it may be not good enough
/// to use in large loop.
impl<'tr, Value, K: ToString, T> Shl<(K, T)> for ValuePtrMut<'tr, Value>
where Value: From<T> + ValuePath + ValueReader + ValueWriter
{
    type Output = Self;
    fn shl(mut self, rhs: (K, T)) -> Self::Output {
        self.push_object(rhs.0, rhs.1)
    }
}

/// Operator `<<` to push one value tuple into json array.
///
/// It will consume the `lhs` pointer and return a new one point to the same node
/// after modify it's content and type, and so is chainable.
/// If the node is array, the new item is push back to it,
/// otherwise change the node to array with only one new item.
///
/// ```rust
/// # use serde_json::json;
/// # use json_ops::ValuePath;
/// let mut v = json!("init string node");
/// 
/// let _ = v.path_mut() << ("i",) << (1,) << ("f",) << (3.14,);
/// assert_eq!(v, json!(["i", 1,"f", 3.14]));
/// ```
///
/// Note that use single tuple to distinguish with pushing one value to node.
/// Can also use the other overload `<< ["val"]` instead of `("val",)` which may be
/// more clear to express the meanning for one item in array.
impl<'tr, Value, T> Shl<(T,)> for ValuePtrMut<'tr, Value>
where Value: From<T> + ValuePath + ValueReader + ValueWriter
{
    type Output = Self;
    fn shl(mut self, rhs: (T,)) -> Self::Output {
        self.push_array(rhs.0)
    }
}

/// Operator `<<` to push one item to json array.
///
/// It will consume the `lhs` pointer and return a new one point to the same node
/// after modify it's content and type, and so is chainable.
/// If the node is array, the new item is push back to it,
/// otherwise change the node to array with only one new item.
///
/// ```rust
/// # use serde_json::json;
/// # use json_ops::ValuePath;
/// let mut v = json!("init string node");
/// 
/// let _ = v.path_mut() << ["i"] << [1] << ["f"] << [3.14];
/// assert_eq!(v, json!(["i", 1,"f", 3.14]));
/// ```
impl<'tr, Value, T: Copy> Shl<[T;1]> for ValuePtrMut<'tr, Value>
where Value: From<T> + ValuePath + ValueReader + ValueWriter
{
    type Output = Self;
    fn shl(mut self, rhs: [T;1]) -> Self::Output {
        self.push_array(rhs[0])
    }
}

/// Operator `<<` to push a slice to json array.
///
/// It will consume the `lhs` pointer and return a new one point to the same node
/// after modify it's content and type, and so can chain further.
/// If the node is array, the new items is append back,
/// otherwise change the node to array with only the new items.
///
/// ```rust
/// # use serde_json::json;
/// # use json_ops::ValuePath;
/// let mut v = json!("init string node");
/// 
/// let vi = vec![1, 2, 3, 4];
/// let _ = v.path_mut() << &vi[..] << [5] << (6,);
/// assert_eq!(v, json!([1,2,3,4,5,6]));
/// ```
impl<'tr, Value, T: Copy> Shl<&[T]> for ValuePtrMut<'tr, Value>
where Value: From<T> + ValuePath + ValueReader + ValueWriter
{
    type Output = Self;
    fn shl(mut self, rhs: &[T]) -> Self::Output {
        for item in rhs {
            self = self.push_array(*item);
        }
        self
    }
}

/* ------------------------------------------------------------ */