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
use std::collections::BTreeMap;
use serde::ser::Serialize;
use serde_json::value::{Value, to_value};
#[derive(Debug, Clone)]
pub struct Context {
data: BTreeMap<String, Value>,
}
impl Context {
pub fn new() -> Context {
Context {
data: BTreeMap::new()
}
}
pub fn add<T: Serialize>(&mut self, key: &str, val: &T) {
self.data.insert(key.to_owned(), to_value(val));
}
#[doc(hidden)]
pub fn as_json(&self) -> Value {
to_value(&self.data)
}
}
impl Default for Context {
fn default() -> Context {
Context::new()
}
}
pub trait ValueRender {
fn render(&self) -> String;
}
impl ValueRender for Value {
fn render(&self) -> String {
match *self {
Value::String(ref s) => s.clone(),
Value::I64(i) => i.to_string(),
Value::U64(i) => i.to_string(),
Value::F64(f) => f.to_string(),
Value::Bool(i) => i.to_string(),
Value::Null => "".to_owned(),
Value::Array(ref a) => {
let mut buf = String::new();
buf.push('[');
for i in a.iter() {
buf.push_str(i.render().as_ref());
buf.push_str(", ");
}
buf.push(']');
buf
},
Value::Object(_) => "[object]".to_owned()
}
}
}
pub trait ValueNumber {
fn to_number(&self) -> Result<f64, ()>;
}
impl ValueNumber for Value {
fn to_number(&self) -> Result<f64, ()> {
match *self {
Value::I64(i) => Ok(i as f64),
Value::U64(i) => Ok(i as f64),
Value::F64(f) => Ok(f as f64),
_ => Err(())
}
}
}
pub trait ValueTruthy {
fn is_truthy(&self) -> bool;
}
impl ValueTruthy for Value {
fn is_truthy(&self) -> bool {
match *self {
Value::I64(i) => i != 0,
Value::U64(i) => i != 0,
Value::F64(i) => i != 0.0 || !i.is_nan(),
Value::Bool(ref i) => *i,
Value::Null => false,
Value::String(ref i) => !i.is_empty(),
Value::Array(ref i) => !i.is_empty(),
Value::Object(ref i) => !i.is_empty()
}
}
}
#[inline]
pub fn get_json_pointer(key: &str) -> String {
["/", &key.replace(".", "/")].join("")
}