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
use std::{
fmt::{Debug, Display},
str::FromStr,
};
use crate::{
json_err, json_error::JSONError, lexer::Lexer, parser::Parser, JSONArray, JSONBoolean,
JSONNull, JSONNumber, JSONObject, JSONString,
};
#[derive(Debug, Clone)]
pub enum JSONType {
String(JSONString),
Number(JSONNumber),
Boolean(JSONBoolean),
Null(JSONNull),
Array(JSONArray),
Object(JSONObject),
}
#[derive(Debug, Clone)]
pub struct JSONValue {
data: JSONType,
}
impl FromStr for JSONValue {
type Err = JSONError;
fn from_str(json: &str) -> Result<Self, Self::Err> {
let lexer = Lexer::new(json.to_string());
let tokens = lexer.get_tokens();
if let Ok(tokens) = tokens {
let parser = Parser::new(tokens);
let json_value = parser.parse();
if let Ok(json_value) = json_value {
Ok(json_value)
} else {
json_err!(json_value.unwrap_err())
}
} else {
json_err!(tokens.unwrap_err())
}
}
}
impl Display for JSONValue {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.to_string())
}
}
impl JSONValue {
/// Create a new JSON Value from a JSON String
///
/// # Example
///
/// ```
/// use parson::{JSONString, JSONValue};
///
/// let json_string = JSONString::new(String::new());
/// let json_value = JSONValue::from_string(json_string);
/// ```
pub fn from_string(json_string: JSONString) -> Self {
JSONValue {
data: JSONType::String(json_string),
}
}
/// Create a new JSON Value from a JSON Number
///
/// # Example
///
/// ```
/// use parson::{JSONNumber, JSONValue};
///
/// let json_number = JSONNumber::new(1.0);
/// let json_value = JSONValue::from_number(json_number);
/// ```
pub fn from_number(json_number: JSONNumber) -> Self {
JSONValue {
data: JSONType::Number(json_number),
}
}
/// Create a new JSON Value from a JSON Boolean
///
/// # Example
///
/// ```
/// use parson::{JSONValue, JSONBoolean};
///
/// let json_boolean = JSONBoolean::new(true);
/// let json_value = JSONValue::from_boolean(json_boolean);
/// ```
pub fn from_boolean(json_boolean: JSONBoolean) -> Self {
JSONValue {
data: JSONType::Boolean(json_boolean),
}
}
/// Create a new JSON Value from a JSON Null
///
/// # Example
///
/// ```
/// use parson::{JSONNull, JSONValue};
///
/// let json_null = JSONNull::new();
/// let json_value = JSONValue::from_null(json_null);
/// ```
pub fn from_null(json_null: JSONNull) -> Self {
JSONValue {
data: JSONType::Null(json_null),
}
}
/// Create a new JSON Value from a JSON Array
///
/// # Example
///
/// ```
/// use parson::{JSONArray, JSONValue};
///
/// let json_array = JSONArray::new();
/// let json_value = JSONValue::from_array(json_array);
/// ```
pub fn from_array(json_array: JSONArray) -> Self {
JSONValue {
data: JSONType::Array(json_array),
}
}
/// Create a new JSON Value from a JSON Object
///
/// # Example
///
/// ```
/// use parson::{JSONObject, JSONValue};
///
/// let json_object = JSONObject::new();
/// let json_value = JSONValue::from_object(json_object);
/// ```
pub fn from_object(json_object: JSONObject) -> Self {
JSONValue {
data: JSONType::Object(json_object),
}
}
/// Get the type of the JSON Value
///
/// # Example
///
/// ```
/// use parson::{JSONNull, JSONType, JSONValue};
///
/// let json_value = JSONValue::from_null(JSONNull::new());
/// assert!(match json_value.get_type() {
/// JSONType::Null(json_null) => true,
/// _ => false,
/// });
/// ```
pub fn get_type(&self) -> JSONType {
self.data.clone()
}
/// Cast the JSON Value to a Rust owned string
pub fn get_string(&self) -> Result<String, JSONError> {
match &self.data {
JSONType::String(json_string) => Ok(json_string.get_string()),
_ => json_err!("JSONValue::get_string() called on non-string value"; 0, 0),
}
}
/// Cast the JSON Value to a Rust f64
pub fn get_number(&self) -> Result<f64, JSONError> {
match &self.data {
JSONType::Number(json_number) => Ok(json_number.get_number()),
_ => json_err!("JSONValue::get_number() called on non-number value"; 0, 0),
}
}
/// Cast the JSON Value to a Rust bool
pub fn get_boolean(&self) -> Result<bool, JSONError> {
match &self.data {
JSONType::Boolean(json_boolean) => Ok(json_boolean.get_boolean()),
_ => json_err!("JSONValue::get_boolean() called on non-boolean value"; 0, 0),
}
}
/// Cast the JSON Value to JSON Null
pub fn get_null(&self) -> Result<&JSONNull, JSONError> {
match &self.data {
JSONType::Null(json_null) => Ok(json_null),
_ => json_err!("JSONValue::get_null() called on non-null value"; 0, 0),
}
}
/// Cast the JSON Value to JSON Array
pub fn get_array(&self) -> Result<&JSONArray, JSONError> {
match &self.data {
JSONType::Array(json_array) => Ok(json_array),
_ => json_err!("JSONValue::get_array() called on non-array value"; 0, 0),
}
}
/// Cast the JSON Value to JSON Object
pub fn get_object(&self) -> Result<&JSONObject, JSONError> {
match &self.data {
JSONType::Object(json_object) => Ok(json_object),
_ => json_err!("JSONValue::get_object() called on non-object value"; 0, 0),
}
}
/// If the JSON Value is a JSON String
///
/// # Example
///
/// ```
/// use parson::{JSONString, JSONValue};
///
/// let json_string = JSONString::new(String::new());
/// let json_value = JSONValue::from_string(json_string);
/// assert!(json_value.is_string());
/// ```
pub fn is_string(&self) -> bool {
match self.data {
JSONType::String(_) => true,
_ => false,
}
}
/// If the JSON Value is a JSON Number
///
/// # Example
///
/// ```
/// use parson::{JSONNumber, JSONValue};
///
/// let json_number = JSONNumber::new(0.0);
/// let json_value = JSONValue::from_number(json_number);
/// assert!(json_value.is_number());
/// ```
pub fn is_number(&self) -> bool {
match self.data {
JSONType::Number(_) => true,
_ => false,
}
}
/// If the JSON Value is a JSON Boolean
///
/// # Example
///
/// ```
/// use parson::{JSONBoolean, JSONValue};
///
/// let json_boolean = JSONBoolean::new(true);
/// let json_value = JSONValue::from_boolean(json_boolean);
/// assert!(json_value.is_boolean());
/// ```
pub fn is_boolean(&self) -> bool {
match self.data {
JSONType::Boolean(_) => true,
_ => false,
}
}
/// If the JSON Value is a JSON Null
///
/// # Example
///
/// ```
/// use parson::{JSONNull, JSONValue};
///
/// let json_null = JSONNull::new();
/// let json_value = JSONValue::from_null(json_null);
/// assert!(json_value.is_null());
/// ```
pub fn is_null(&self) -> bool {
match self.data {
JSONType::Null(_) => true,
_ => false,
}
}
/// If the JSON Value is a JSON Array
///
/// # Example
///
/// ```
/// use parson::{JSONArray, JSONValue};
///
/// let json_array = JSONArray::new();
/// let json_value = JSONValue::from_array(json_array);
/// assert!(json_value.is_array());
/// ```
pub fn is_array(&self) -> bool {
match self.data {
JSONType::Array(_) => true,
_ => false,
}
}
/// If the JSON Value is a JSON Object
///
/// # Example
///
/// ```
/// use parson::{JSONObject, JSONValue};
///
/// let json_object = JSONObject::new();
/// let json_value = JSONValue::from_object(json_object);
/// assert!(json_value.is_object());
/// ```
pub fn is_object(&self) -> bool {
match self.data {
JSONType::Object(_) => true,
_ => false,
}
}
/// Convert JSON Value to a Rust owned string
pub fn to_string(&self) -> String {
match &self.data {
JSONType::String(json_string) => json_string.to_string(),
JSONType::Number(json_number) => json_number.to_string(),
JSONType::Boolean(json_boolean) => json_boolean.to_string(),
JSONType::Null(json_null) => json_null.to_string(),
JSONType::Array(json_array) => json_array.to_string(),
JSONType::Object(json_object) => json_object.to_string(),
}
}
pub fn format_string(&self, indents: i32, spaces: i32) -> String {
match &self.data {
JSONType::String(json_string) => json_string.to_string(),
JSONType::Number(json_number) => json_number.to_string(),
JSONType::Boolean(json_boolean) => json_boolean.to_string(),
JSONType::Null(json_null) => json_null.to_string(),
JSONType::Array(json_array) => json_array.format_string(indents, spaces),
JSONType::Object(json_object) => json_object.format_string(indents, spaces),
}
}
}