sql_json_path/json/
serde_json.rs

1// Copyright 2023 RisingWave Labs
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use super::*;
16use ::serde_json::{Map, Value};
17
18impl Json for Value {
19    type Borrowed<'a> = &'a Value;
20
21    fn as_ref(&self) -> Self::Borrowed<'_> {
22        self
23    }
24
25    fn null() -> Self {
26        Value::Null
27    }
28
29    fn bool(b: bool) -> Self {
30        Value::Bool(b)
31    }
32
33    fn from_u64(v: u64) -> Self {
34        Self::Number(Number::from(v))
35    }
36
37    fn from_i64(v: i64) -> Self {
38        Self::Number(Number::from(v))
39    }
40
41    fn from_f64(v: f64) -> Self {
42        Self::Number(Number::from_f64(v).unwrap())
43    }
44
45    fn from_number(n: Number) -> Self {
46        Self::Number(n)
47    }
48
49    fn from_string(s: &str) -> Self {
50        Self::String(s.to_owned())
51    }
52
53    fn object<'a, I: IntoIterator<Item = (&'a str, Self)>>(iter: I) -> Self {
54        Self::Object(iter.into_iter().map(|(k, v)| (k.to_owned(), v)).collect())
55    }
56}
57
58impl<'a> JsonRef<'a> for &'a Value {
59    type Owned = Value;
60    type Array = &'a Vec<Value>;
61    type Object = &'a serde_json::Map<String, Value>;
62
63    fn to_owned(self) -> Self::Owned {
64        self.clone()
65    }
66
67    fn null() -> Self {
68        &Value::Null
69    }
70
71    fn is_null(self) -> bool {
72        self.is_null()
73    }
74
75    fn as_bool(self) -> Option<bool> {
76        self.as_bool()
77    }
78
79    fn as_number(self) -> Option<Number> {
80        self.as_number().cloned()
81    }
82
83    fn as_str(self) -> Option<&'a str> {
84        self.as_str()
85    }
86
87    fn as_array(self) -> Option<Self::Array> {
88        self.as_array()
89    }
90
91    fn as_object(self) -> Option<Self::Object> {
92        self.as_object()
93    }
94
95    fn is_number(self) -> bool {
96        self.is_number()
97    }
98
99    fn is_string(self) -> bool {
100        self.is_string()
101    }
102
103    fn is_array(self) -> bool {
104        self.is_array()
105    }
106
107    fn is_object(self) -> bool {
108        self.is_object()
109    }
110}
111
112impl<'a> ArrayRef<'a> for &'a Vec<Value> {
113    type JsonRef = &'a Value;
114
115    fn len(self) -> usize {
116        self.len()
117    }
118
119    fn get(self, index: usize) -> Option<Self::JsonRef> {
120        (**self).get(index)
121    }
122
123    fn list(self) -> Vec<Self::JsonRef> {
124        self.iter().collect()
125    }
126}
127
128impl<'a> ObjectRef<'a> for &'a Map<String, Value> {
129    type JsonRef = &'a Value;
130
131    fn len(self) -> usize {
132        self.len()
133    }
134
135    fn get(self, key: &str) -> Option<Self::JsonRef> {
136        self.get(key)
137    }
138
139    fn list(self) -> Vec<(&'a str, Self::JsonRef)> {
140        self.iter().map(|(k, v)| (k.as_str(), v)).collect()
141    }
142
143    fn list_value(self) -> Vec<Self::JsonRef> {
144        self.values().collect()
145    }
146}