1#[derive(Debug, Clone, PartialEq)]
2pub enum JSON {
3 Int(i128),
4 Float(f64),
5 Bool(bool),
6 Str(String),
7 Array(Vec<JSON>),
8 Dict(Vec<(String, JSON)>),
9 Null,
10}
11
12impl JSON {
13 pub fn stringify(&self) -> String {
14 use JSON::*;
15 match self {
16 Int(x) => format!("{}", x),
17 Float(x) => format!("{}", x),
18 Bool(x) => format!("{:?}", x),
19 Str(x) => format!("{:?}", x),
20 Array(xs) => format!(
21 "[{}]",
22 xs.iter()
23 .map(|j| j.stringify())
24 .collect::<Vec<_>>()
25 .join(",")
26 ),
27 Dict(d) => format!(
28 "{{{}}}",
29 d.iter()
30 .map(|(key, val)| format!("\"{}\":{}", key, val.stringify()))
31 .collect::<Vec<_>>()
32 .join(",")
33 ),
34 Null => "null".to_string(),
35 }
36 }
37}