-
Notifications
You must be signed in to change notification settings - Fork 215
/
json.rs
58 lines (45 loc) · 1.69 KB
/
json.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
use std::error::Error;
use crate::map::Map;
use crate::value::{Value, ValueKind};
pub fn parse(
uri: Option<&String>,
text: &str,
) -> Result<Map<String, Value>, Box<dyn Error + Send + Sync>> {
// Parse a JSON object value from the text
// TODO: Have a proper error fire if the root of a file is ever not a Table
let value = from_json_value(uri, &serde_json::from_str(text)?);
match value.kind {
ValueKind::Table(map) => Ok(map),
_ => Ok(Map::new()),
}
}
fn from_json_value(uri: Option<&String>, value: &serde_json::Value) -> Value {
match *value {
serde_json::Value::String(ref value) => Value::new(uri, ValueKind::String(value.clone())),
serde_json::Value::Number(ref value) => {
if let Some(value) = value.as_i64() {
Value::new(uri, ValueKind::I64(value))
} else if let Some(value) = value.as_f64() {
Value::new(uri, ValueKind::Float(value))
} else {
unreachable!();
}
}
serde_json::Value::Bool(value) => Value::new(uri, ValueKind::Boolean(value)),
serde_json::Value::Object(ref table) => {
let mut m = Map::new();
for (key, value) in table {
m.insert(key.clone(), from_json_value(uri, value));
}
Value::new(uri, ValueKind::Table(m))
}
serde_json::Value::Array(ref array) => {
let mut l = Vec::new();
for value in array {
l.push(from_json_value(uri, value));
}
Value::new(uri, ValueKind::Array(l))
}
serde_json::Value::Null => Value::new(uri, ValueKind::Nil),
}
}