s2json_core/impls/
json.rs1use crate::{JSONProperties, JSONValue, Map, PrimitiveValue};
2use alloc::{string::String, vec::Vec};
3
4impl Default for JSONValue {
6 fn default() -> Self {
7 JSONValue::Primitive(PrimitiveValue::Null)
8 }
9}
10impl JSONValue {
11 pub fn to_prim(&self) -> Option<&PrimitiveValue> {
13 match self {
14 JSONValue::Primitive(v) => Some(v),
15 _ => None,
16 }
17 }
18
19 pub fn to_vec(&self) -> Option<&Vec<JSONValue>> {
21 match self {
22 JSONValue::Array(v) => Some(v),
23 _ => None,
24 }
25 }
26
27 pub fn to_nested(&self) -> Option<&Map<String, JSONValue>> {
29 match self {
30 JSONValue::Object(v) => Some(v),
31 _ => None,
32 }
33 }
34}
35impl From<&str> for JSONValue {
36 fn from(s: &str) -> Self {
37 JSONValue::Primitive(PrimitiveValue::String(s.into()))
38 }
39}
40impl From<String> for JSONValue {
49 fn from(s: String) -> Self {
50 JSONValue::Primitive(PrimitiveValue::String(s))
51 }
52}
53impl From<&String> for JSONValue {
54 fn from(s: &String) -> Self {
55 JSONValue::Primitive(PrimitiveValue::String(s.into()))
56 }
57}
58impl From<JSONValue> for String {
59 fn from(v: JSONValue) -> Self {
60 match v {
61 JSONValue::Primitive(PrimitiveValue::String(s)) => s,
62 _ => "".into(),
63 }
64 }
65}
66impl From<&JSONValue> for String {
67 fn from(v: &JSONValue) -> Self {
68 match v {
69 JSONValue::Primitive(PrimitiveValue::String(s)) => s.into(),
70 _ => "".into(),
71 }
72 }
73}
74
75macro_rules! impl_from_uint {
77 ($($t:ty),*) => {
78 $(
79 impl From<$t> for JSONValue {
80 fn from(v: $t) -> Self {
81 JSONValue::Primitive(PrimitiveValue::U64(v as u64))
82 }
83 }
84
85 impl From<JSONValue> for $t {
86 fn from(v: JSONValue) -> Self {
87 match v {
88 JSONValue::Primitive(PrimitiveValue::String(v)) => v.parse().unwrap_or_default(),
89 JSONValue::Primitive(PrimitiveValue::U64(v)) => v as $t,
90 _ => 0,
91 }
92 }
93 }
94 )*
95 };
96}
97impl_from_uint!(u8, u16, u32, u64, usize);
98macro_rules! impl_from_uint_ref {
99 ($($t:ty),*) => {
100 $(
101 impl From<&JSONValue> for $t {
102 fn from(v: &JSONValue) -> Self {
103 match v {
104 JSONValue::Primitive(PrimitiveValue::String(v)) => v.parse().unwrap_or_default(),
105 JSONValue::Primitive(PrimitiveValue::U64(v)) => *v as $t,
106 _ => 0,
107 }
108 }
109 }
110 )*
111 };
112}
113impl_from_uint_ref!(u8, u16, u32, u64, usize);
114macro_rules! impl_from_sint {
116 ($($t:ty),*) => {
117 $(
118 impl From<$t> for JSONValue {
119 fn from(v: $t) -> Self {
120 JSONValue::Primitive(PrimitiveValue::I64(v as i64))
121 }
122 }
123
124 impl From<JSONValue> for $t {
125 fn from(v: JSONValue) -> Self {
126 match v {
127 JSONValue::Primitive(PrimitiveValue::String(v)) => v.parse().unwrap_or_default(),
128 JSONValue::Primitive(PrimitiveValue::I64(v)) => v as $t,
129 _ => 0,
130 }
131 }
132 }
133 )*
134 };
135}
136impl_from_sint!(i8, i16, i32, i64, isize);
137macro_rules! impl_from_sint_ref {
138 ($($t:ty),*) => {
139 $(
140 impl From<&JSONValue> for $t {
141 fn from(v: &JSONValue) -> Self {
142 match v {
143 JSONValue::Primitive(PrimitiveValue::String(v)) => v.parse().unwrap_or_default(),
144 JSONValue::Primitive(PrimitiveValue::I64(v)) => *v as $t,
145 _ => 0,
146 }
147 }
148 }
149 )*
150 };
151}
152impl_from_sint_ref!(i8, i16, i32, i64, isize);
153impl From<f32> for JSONValue {
154 fn from(v: f32) -> Self {
155 JSONValue::Primitive(PrimitiveValue::F32(v))
156 }
157}
158impl From<JSONValue> for f32 {
159 fn from(v: JSONValue) -> Self {
160 match v {
161 JSONValue::Primitive(PrimitiveValue::String(v)) => v.parse().unwrap_or_default(),
162 JSONValue::Primitive(PrimitiveValue::F32(v)) => v,
163 _ => 0.0,
164 }
165 }
166}
167impl From<&JSONValue> for f32 {
168 fn from(v: &JSONValue) -> Self {
169 match v {
170 JSONValue::Primitive(PrimitiveValue::String(v)) => v.parse().unwrap_or_default(),
171 JSONValue::Primitive(PrimitiveValue::F32(v)) => *v,
172 _ => 0.0,
173 }
174 }
175}
176impl From<f64> for JSONValue {
177 fn from(v: f64) -> Self {
178 JSONValue::Primitive(PrimitiveValue::F64(v))
179 }
180}
181impl From<JSONValue> for f64 {
182 fn from(v: JSONValue) -> Self {
183 match v {
184 JSONValue::Primitive(PrimitiveValue::String(v)) => v.parse().unwrap_or_default(),
185 JSONValue::Primitive(PrimitiveValue::F64(v)) => v,
186 _ => 0.0,
187 }
188 }
189}
190impl From<&JSONValue> for f64 {
191 fn from(v: &JSONValue) -> Self {
192 match v {
193 JSONValue::Primitive(PrimitiveValue::String(v)) => v.parse().unwrap_or_default(),
194 JSONValue::Primitive(PrimitiveValue::F64(v)) => *v,
195 _ => 0.0,
196 }
197 }
198}
199impl From<bool> for JSONValue {
200 fn from(v: bool) -> Self {
201 JSONValue::Primitive(PrimitiveValue::Bool(v))
202 }
203}
204impl From<JSONValue> for bool {
205 fn from(v: JSONValue) -> Self {
206 match v {
207 JSONValue::Primitive(PrimitiveValue::String(v)) => v == "true",
208 JSONValue::Primitive(PrimitiveValue::Bool(v)) => v,
209 _ => false,
210 }
211 }
212}
213impl From<&JSONValue> for bool {
214 fn from(v: &JSONValue) -> Self {
215 match v {
216 JSONValue::Primitive(PrimitiveValue::String(v)) => v == "true",
217 JSONValue::Primitive(PrimitiveValue::Bool(v)) => *v,
218 _ => false,
219 }
220 }
221}
222impl From<()> for JSONValue {
223 fn from(_: ()) -> Self {
224 JSONValue::Primitive(PrimitiveValue::Null)
225 }
226}
227impl From<JSONValue> for () {
228 fn from(_: JSONValue) -> Self {}
229}
230impl From<&JSONValue> for () {
231 fn from(_: &JSONValue) -> Self {}
232}
233impl<T> From<Vec<T>> for JSONValue
234where
235 T: Into<JSONValue>,
236{
237 fn from(v: Vec<T>) -> Self {
238 JSONValue::Array(v.into_iter().map(Into::into).collect())
239 }
240}
241impl<T> From<&Vec<T>> for JSONValue
242where
243 T: Into<JSONValue>,
244 JSONValue: for<'a> From<&'a T>,
245{
246 fn from(v: &Vec<T>) -> Self {
247 JSONValue::Array(v.iter().map(Into::into).collect())
248 }
249}
250impl<T> From<JSONValue> for Vec<T>
251where
252 T: From<JSONValue>,
253{
254 fn from(v: JSONValue) -> Self {
255 match v {
256 JSONValue::Array(v) => v.into_iter().map(Into::into).collect(),
257 _ => Vec::new(),
258 }
259 }
260}
261impl<T> From<&JSONValue> for Vec<T>
262where
263 T: for<'a> From<&'a JSONValue>,
264{
265 fn from(v: &JSONValue) -> Self {
266 match v {
267 JSONValue::Array(v) => v.iter().map(Into::into).collect(),
268 _ => Vec::new(),
269 }
270 }
271}
272impl<T> From<Option<T>> for JSONValue
273where
274 T: Into<JSONValue>,
275{
276 fn from(v: Option<T>) -> Self {
277 match v {
278 Some(v) => v.into(),
279 None => JSONValue::Primitive(PrimitiveValue::Null),
280 }
281 }
282}
283impl From<&JSONValue> for JSONValue {
284 fn from(v: &JSONValue) -> Self {
285 v.clone()
286 }
287}
288impl From<JSONValue> for JSONProperties {
289 fn from(v: JSONValue) -> Self {
290 match v {
291 JSONValue::Object(v) => v.clone(),
292 _ => Self::new(),
293 }
294 }
295}
296impl From<&JSONValue> for JSONProperties {
297 fn from(v: &JSONValue) -> Self {
298 match v {
299 JSONValue::Object(v) => v.clone(),
300 _ => Self::new(),
301 }
302 }
303}
304impl From<JSONProperties> for JSONValue {
305 fn from(v: JSONProperties) -> Self {
306 JSONValue::Object(v)
307 }
308}
309impl From<&JSONProperties> for JSONValue {
310 fn from(v: &JSONProperties) -> Self {
311 JSONValue::Object(v.clone())
312 }
313}