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
| #[derive(Debug, PartialEq)]
pub enum Json {
Null,
Number(f64),
Str(String),
Array(Vec<Json>),
Object(HashMap<String, Json>),
}
impl From<String> for Json {
fn from(value: String) -> Self {
Str(value)
}
}
impl From<&str> for Json {
fn from(value: &str) -> Self {
Str(value.into())
}
}
impl From<i32> for Json {
fn from(value: i32) -> Self {
Number(value as f64)
}
}
#[macro_export]
macro_rules! json {
(null) => {Json::Null};
([ $( $value:tt ),* ]) => {
Json::Array(
vec![$( json!($value) ),*]
)
};
({ $( $key:tt : $value:tt ),* }) => {
Json::Object(
vec![$( ($key.to_string(), json!($value)) ),*].into_iter().collect()
)
};
($value:tt) => {
Json::from($value)
}
}
|