json_value/traits/
mod.rs

1use serde_json::Map;
2
3/// Attempt to convert json value to specified type
4pub trait JsonValueWrap
5where
6    Self: Sized,
7{
8    fn as_boolean(&self) -> Option<&bool>;
9    fn as_array(&self) -> Option<&Vec<Self>>;
10    fn as_string(&self) -> Option<&String>;
11    fn as_object(&self) -> Option<&Map<String, Self>>;
12    fn into_boolean(self) -> Option<bool>;
13    fn into_string(self) -> Option<String>;
14    fn into_array(self) -> Option<Vec<Self>>;
15    fn into_object(self) -> Option<Map<String, Self>>;
16}
17
18/// Check the type of json value
19pub trait JsonValueCheck: JsonValueWrap
20where
21    Self: Sized,
22{
23    fn is_null(&self) -> bool;
24    fn is_empty(&self) -> bool;
25    #[inline]
26    fn is_boolean(&self) -> bool {
27        self.as_boolean().is_some()
28    }
29    #[inline]
30    fn is_true(&self) -> bool {
31        matches!(self.as_boolean(), Some(true))
32    }
33    #[inline]
34    fn is_false(&self) -> bool {
35        matches!(self.as_boolean(), Some(false))
36    }
37    #[inline]
38    fn is_string(&self) -> bool {
39        self.as_string().is_some()
40    }
41    #[inline]
42    fn is_array(&self) -> bool {
43        self.as_array().is_some()
44    }
45}
46
47/// Treat json value as array and get data from it according to index
48pub trait JsonMaybeArray: JsonValueWrap
49where
50    Self: Sized,
51{
52    fn get_index(&self, index: usize) -> Option<&Self>;
53    fn mut_index(&mut self, index: usize) -> Option<&mut Self>;
54}
55
56/// Treat json value as object and get data from it according to key
57pub trait JsonMaybeObject: JsonValueWrap
58where
59    Self: Sized,
60{
61    fn get_key(&self, key: &str) -> Option<&Self>;
62    fn mut_key(&mut self, key: &str) -> Option<&mut Self>;
63    fn extract_key(&mut self, key: &str) -> Option<Self>;
64    #[inline]
65    fn get_key_as_boolean(&self, key: &str) -> Option<&bool> {
66        self.get_key(key).and_then(|f| f.as_boolean())
67    }
68    #[inline]
69    fn get_key_as_string(&self, key: &str) -> Option<&String> {
70        self.get_key(key).and_then(|f| f.as_string())
71    }
72    #[inline]
73    fn get_key_as_array(&self, key: &str) -> Option<&Vec<Self>> {
74        self.get_key(key).and_then(|f| f.as_array())
75    }
76    #[inline]
77    fn get_key_as_object(&self, key: &str) -> Option<&Map<String, Self>> {
78        self.get_key(key).and_then(|f| f.as_object())
79    }
80    #[inline]
81    fn extract_key_as_boolean(&mut self, key: &str) -> Option<bool> {
82        self.extract_key(key).and_then(|f| f.into_boolean())
83    }
84    #[inline]
85    fn extract_key_as_string(&mut self, key: &str) -> Option<String> {
86        self.extract_key(key).and_then(|f| f.into_string())
87    }
88    #[inline]
89    fn extract_key_as_array(&mut self, key: &str) -> Option<Vec<Self>> {
90        self.extract_key(key).and_then(|f| f.into_array())
91    }
92    #[inline]
93    fn extract_key_as_object(&mut self, key: &str) -> Option<Map<String, Self>> {
94        self.extract_key(key).and_then(|f| f.into_object())
95    }
96}