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
use std::collections::{ BTreeMap, HashMap };
use std::mem;
use short::{ self, Short };
use number::Number;
use object::Object;
use { JsonValue, Null };
macro_rules! implement_extras {
($from:ty) => {
impl From<Option<$from>> for JsonValue {
fn from(val: Option<$from>) -> JsonValue {
match val {
Some(value) => value.into(),
None => Null,
}
}
}
impl From<Vec<$from>> for JsonValue {
fn from(mut val: Vec<$from>) -> JsonValue {
JsonValue::Array(
val.drain(..)
.map(|value| value.into())
.collect()
)
}
}
impl From<Vec<Option<$from>>> for JsonValue {
fn from(mut val: Vec<Option<$from>>) -> JsonValue {
JsonValue::Array(
val.drain(..)
.map(|item| item.into())
.collect()
)
}
}
}
}
macro_rules! implement_eq {
($to:ident, $from:ty) => {
impl PartialEq<$from> for JsonValue {
fn eq(&self, other: &$from) -> bool {
match *self {
JsonValue::$to(ref value) => value == other,
_ => false
}
}
}
impl<'a> PartialEq<$from> for &'a JsonValue {
fn eq(&self, other: &$from) -> bool {
match **self {
JsonValue::$to(ref value) => value == other,
_ => false
}
}
}
impl PartialEq<JsonValue> for $from {
fn eq(&self, other: &JsonValue) -> bool {
match *other {
JsonValue::$to(ref value) => value == self,
_ => false
}
}
}
}
}
macro_rules! implement {
($to:ident, $from:ty as num) => {
impl From<$from> for JsonValue {
fn from(val: $from) -> JsonValue {
JsonValue::$to(val.into())
}
}
implement_eq!($to, $from);
implement_extras!($from);
};
($to:ident, $from:ty) => {
impl From<$from> for JsonValue {
fn from(val: $from) -> JsonValue {
JsonValue::$to(val)
}
}
implement_eq!($to, $from);
implement_extras!($from);
}
}
impl<'a> From<&'a str> for JsonValue {
fn from(val: &'a str) -> JsonValue {
if val.len() <= short::MAX_LEN {
JsonValue::Short(unsafe { Short::from_slice(val) })
} else {
JsonValue::String(val.into())
}
}
}
impl<'a> From<Option<&'a str>> for JsonValue {
fn from(val: Option<&'a str>) -> JsonValue {
match val {
Some(value) => value.into(),
None => Null,
}
}
}
impl From<HashMap<String, JsonValue>> for JsonValue {
fn from(mut val: HashMap<String, JsonValue>) -> JsonValue {
let mut object = Object::with_capacity(val.len());
for (key, value) in val.drain() {
object.insert(&key, value);
}
JsonValue::Object(object)
}
}
impl From<Option<HashMap<String, JsonValue>>> for JsonValue {
fn from(val: Option<HashMap<String, JsonValue>>) -> JsonValue {
match val {
Some(value) => value.into(),
None => Null,
}
}
}
impl From<BTreeMap<String, JsonValue>> for JsonValue {
fn from(mut val: BTreeMap<String, JsonValue>) -> JsonValue {
let mut object = Object::with_capacity(val.len());
for (key, value) in val.iter_mut() {
let value = mem::replace(value, Null);
object.insert(key, value);
}
JsonValue::Object(object)
}
}
impl From<Option<BTreeMap<String, JsonValue>>> for JsonValue {
fn from(val: Option<BTreeMap<String, JsonValue>>) -> JsonValue {
match val {
Some(value) => value.into(),
None => Null,
}
}
}
impl From<Option<JsonValue>> for JsonValue {
fn from(val: Option<JsonValue>) -> JsonValue {
match val {
Some(value) => value,
None => Null,
}
}
}
impl<'a> PartialEq<&'a str> for JsonValue {
fn eq(&self, other: &&str) -> bool {
match *self {
JsonValue::Short(ref value) => value == *other,
JsonValue::String(ref value) => value == *other,
_ => false
}
}
}
impl<'a> PartialEq<JsonValue> for &'a str {
fn eq(&self, other: &JsonValue) -> bool {
match *other {
JsonValue::Short(ref value) => value == *self,
JsonValue::String(ref value) => value == *self,
_ => false
}
}
}
impl PartialEq<str> for JsonValue {
fn eq(&self, other: &str) -> bool {
match *self {
JsonValue::Short(ref value) => value == other,
JsonValue::String(ref value) => value == other,
_ => false
}
}
}
impl<'a> PartialEq<JsonValue> for str {
fn eq(&self, other: &JsonValue) -> bool {
match *other {
JsonValue::Short(ref value) => value == self,
JsonValue::String(ref value) => value == self,
_ => false
}
}
}
implement!(String, String);
implement!(Number, isize as num);
implement!(Number, usize as num);
implement!(Number, i8 as num);
implement!(Number, i16 as num);
implement!(Number, i32 as num);
implement!(Number, i64 as num);
implement!(Number, u8 as num);
implement!(Number, u16 as num);
implement!(Number, u32 as num);
implement!(Number, u64 as num);
implement!(Number, f32 as num);
implement!(Number, f64 as num);
implement!(Number, Number);
implement!(Object, Object);
implement!(Array, Vec<JsonValue>);
implement!(Boolean, bool);