Skip to main content

sonic_rs/value/
value_trait.rs

1use std::fmt::{Display, Formatter};
2
3use crate::{index::Index, JsonNumberTrait, Number, RawNumber};
4
5/// JsonType is an enum that represents the type of a JSON value.
6///
7/// # Examples
8///
9/// ```
10/// use sonic_rs::{JsonType, JsonValueTrait, Value};
11///
12/// let json: Value = sonic_rs::from_str(r#"{"a": 1, "b": true}"#).unwrap();
13///
14/// assert_eq!(json.get(&"a").unwrap().get_type(), JsonType::Number);
15/// ```
16#[derive(Copy, Clone, PartialEq, Eq, Debug)]
17#[repr(u8)]
18pub enum JsonType {
19    Null = 0,
20    Boolean = 1,
21    Number = 2,
22    String = 3,
23    Object = 4,
24    Array = 5,
25}
26
27impl From<u8> for JsonType {
28    fn from(value: u8) -> Self {
29        match value {
30            0 => JsonType::Null,
31            1 => JsonType::Boolean,
32            2 => JsonType::Number,
33            3 => JsonType::String,
34            4 => JsonType::Object,
35            5 => JsonType::Array,
36            _ => panic!("Invalid JsonType value from u8 {value}"),
37        }
38    }
39}
40
41impl Display for JsonType {
42    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
43        match self {
44            JsonType::Null => f.write_str("Null"),
45            JsonType::Boolean => f.write_str("Boolean"),
46            JsonType::Number => f.write_str("Number"),
47            JsonType::String => f.write_str("String"),
48            JsonType::Object => f.write_str("Object"),
49            JsonType::Array => f.write_str("Array"),
50        }
51    }
52}
53
54/// A trait for all JSON values. Used by `Value` and `LazyValue`.
55///
56/// The `Option<V: JsonValueTrait>` and `Result<V: JsonValueTrait, E>` also implement this trait.
57/// The `Option::None` or `Result::Err(_)` will be viewed as a null value.
58pub trait JsonValueTrait {
59    type ValueType<'v>
60    where
61        Self: 'v;
62
63    /// Gets the type of the value. Returns `JsonType::Null` as default if `self` is `Option::None`
64    /// or `Result::Err(_)`.
65    ///
66    /// # Examples
67    /// ```
68    /// use sonic_rs::{
69    ///     value::{JsonType, JsonValueTrait, Value},
70    ///     Result,
71    /// };
72    ///
73    /// let json: Value = sonic_rs::from_str(r#"{"a": 1, "b": true}"#).unwrap();
74    ///
75    /// assert_eq!(json.get_type(), JsonType::Object);
76    ///
77    /// let v: Option<&Value> = json.get("c");
78    /// assert!(v.is_none());
79    /// assert_eq!(v.get_type(), JsonType::Null);
80    ///
81    /// let v: Result<Value> = sonic_rs::from_str("invalid json");
82    /// assert!(v.is_err());
83    /// assert_eq!(v.get_type(), JsonType::Null);
84    /// ```
85    fn get_type(&self) -> JsonType;
86
87    /// Returns true if the value is a `bool`.
88    ///
89    /// # Examples
90    /// ```
91    /// use sonic_rs::{json, JsonValueTrait};
92    ///
93    /// let val = json!(true);
94    /// assert!(val.is_boolean());
95    /// ```
96    #[inline]
97    fn is_boolean(&self) -> bool {
98        self.get_type() == JsonType::Boolean
99    }
100
101    /// Returns true if the value is true.
102    ///
103    /// # Examples
104    /// ```
105    /// use sonic_rs::{json, JsonValueTrait};
106    ///
107    /// let val = json!(true);
108    /// assert!(val.is_true());
109    /// ```
110    #[inline]
111    fn is_true(&self) -> bool {
112        self.as_bool().unwrap_or_default()
113    }
114
115    /// Returns true if the value is false.
116    ///
117    /// # Examples
118    /// ```
119    /// use sonic_rs::{json, JsonValueTrait};
120    ///
121    /// let val = json!(false);
122    /// assert!(val.is_false());
123    /// ```
124    #[inline]
125    fn is_false(&self) -> bool {
126        !self.is_true()
127    }
128
129    /// Returns true if the `self` value is `null`.
130    ///
131    /// # Notes
132    ///
133    /// It will Returns true if `self` is `Option::None` or `Result::Err(_)`.
134    ///
135    /// # Examples
136    /// ```
137    /// use sonic_rs::{json, JsonValueTrait, Result, Value};
138    ///
139    /// let val = json!(null);
140    /// assert!(val.is_null());
141    ///
142    /// let val: Option<&Value> = val.get("unknown");
143    /// assert!(val.is_none());
144    /// assert!(val.is_null());
145    ///
146    /// let val: Result<Value> = sonic_rs::from_str("invalid json");
147    /// assert!(val.is_err());
148    /// assert!(val.is_null());
149    /// ```
150    #[inline]
151    fn is_null(&self) -> bool {
152        self.get_type() == JsonType::Null
153    }
154
155    /// Returns true if the value is a `number`.
156    ///
157    /// # Examples
158    ///
159    /// ```
160    /// use sonic_rs::{json, JsonValueTrait};
161    ///
162    /// assert!(json!(1).is_number());
163    /// assert!(Option::Some(json!(1.23)).is_number());
164    /// ```
165    #[inline]
166    fn is_number(&self) -> bool {
167        self.get_type() == JsonType::Number
168    }
169
170    /// Returns true if the value is a `string`.
171    ///  
172    /// # Examples
173    ///
174    /// ```
175    /// use sonic_rs::{json, JsonValueTrait};
176    /// assert!(json!("foo").is_str());
177    /// ```
178    #[inline]
179    fn is_str(&self) -> bool {
180        self.get_type() == JsonType::String
181    }
182
183    /// Returns true if the value is an `array`.
184    ///
185    /// # Examples
186    ///
187    /// ```
188    /// use sonic_rs::{json, JsonValueTrait};
189    /// assert!(json!([]).is_array());
190    /// ```
191    #[inline]
192    fn is_array(&self) -> bool {
193        self.get_type() == JsonType::Array
194    }
195
196    /// Returns true if the value is an `object`.
197    ///
198    /// # Examples
199    ///
200    /// ```
201    /// use sonic_rs::{json, JsonValueTrait};
202    /// assert!(json!({}).is_object());
203    /// ```
204    #[inline]
205    fn is_object(&self) -> bool {
206        self.get_type() == JsonType::Object
207    }
208
209    /// Returns true if the value is a number and it is an `f64`.
210    /// It will returns false if the value is a `u64` or `i64`.
211    ///
212    /// # Examples
213    ///
214    /// ```
215    /// use sonic_rs::{json, JsonValueTrait};
216    /// assert!(!json!(123).is_f64()); // false
217    /// assert!(!json!(-123).is_f64()); // false
218    ///
219    /// assert!(json!(-1.23).is_f64());
220    /// ```
221    #[inline]
222    fn is_f64(&self) -> bool {
223        self.as_number().map(|f| f.is_f64()).unwrap_or_default()
224    }
225
226    /// Returns true if the value is a integer number and it between `i64::MIN` and `i64::MAX`
227    ///
228    /// # Examples
229    ///
230    /// ```
231    /// use sonic_rs::{json, JsonValueTrait};
232    ///
233    /// assert!(json!(-123).is_i64());
234    /// assert!(json!(0).is_i64());
235    /// assert!(json!(123).is_i64());
236    /// assert!(json!(i64::MIN).is_i64());
237    /// assert!(json!(i64::MAX).is_i64());
238    ///
239    /// assert!(!json!(u64::MAX).is_i64()); // overflow for i64
240    /// assert!(!json!(-1.23).is_i64()); // false
241    /// ```
242    #[inline]
243    fn is_i64(&self) -> bool {
244        self.as_i64().is_some()
245    }
246
247    /// Returns true if the value is a integer number and it between `0` and `i64::MAX`
248    ///
249    /// # Examples
250    ///
251    /// ```
252    /// use sonic_rs::{json, JsonValueTrait};
253    ///
254    /// assert!(json!(123).is_u64());
255    /// assert!(json!(0).is_u64());
256    /// assert!(json!(u64::MAX).is_u64());
257    ///
258    /// assert!(!json!(-123).is_u64());
259    /// assert!(!json!(1.23).is_u64());
260    /// ```
261    #[inline]
262    fn is_u64(&self) -> bool {
263        self.as_u64().is_some()
264    }
265
266    /// If `self` meets `is_i64`, represent it as i64 if possible. Returns None otherwise.
267    ///
268    /// # Examples
269    ///
270    /// ```
271    /// use sonic_rs::{json, JsonValueTrait};
272    ///
273    /// assert_eq!(json!(123).as_i64(), Some(123));
274    /// assert_eq!(json!(-123).as_i64(), Some(-123));
275    /// assert_eq!(json!(i64::MAX).as_i64(), Some(i64::MAX));
276    ///
277    /// assert_eq!(json!(u64::MAX).as_i64(), None);
278    /// ```
279    #[inline]
280    fn as_i64(&self) -> Option<i64> {
281        self.as_number().and_then(|n| n.as_i64())
282    }
283
284    /// If `self` meets `is_i64`, represent it as u64 if possible. Returns None otherwise.
285    ///
286    /// # Examples
287    ///
288    /// ```
289    /// use sonic_rs::{json, JsonValueTrait};
290    ///
291    /// assert_eq!(json!(123).as_u64(), Some(123));
292    /// assert_eq!(json!(-123).as_u64(), None);
293    /// assert_eq!(json!(i64::MAX).as_u64(), Some(i64::MAX as u64));
294    ///
295    /// assert_eq!(json!(u64::MAX).as_u64(), Some(u64::MAX));
296    /// ```
297    #[inline]
298    fn as_u64(&self) -> Option<u64> {
299        self.as_number().and_then(|n| n.as_u64())
300    }
301
302    /// If `self` is a number, represent it as f64 if possible. Returns None otherwise.
303    /// The integer number will be converted to f64.
304    ///
305    /// # Examples
306    ///
307    /// ```
308    /// use sonic_rs::{json, JsonValueTrait};
309    ///
310    /// assert_eq!(json!(123).as_f64(), Some(123 as f64));
311    /// assert_eq!(json!(-123).as_f64(), Some(-123 as f64));
312    /// assert_eq!(json!(0.123).as_f64(), Some(0.123));
313    ///
314    /// assert_eq!(json!("hello").as_f64(), None);
315    /// ```
316    #[inline]
317    fn as_f64(&self) -> Option<f64> {
318        self.as_number().and_then(|n| n.as_f64())
319    }
320
321    /// Returns the `Number` if `self` is a `Number`.
322    ///
323    /// # Examples
324    ///
325    /// ```
326    /// use sonic_rs::{json, JsonValueTrait, Number};
327    ///
328    /// assert_eq!(json!(123).as_number(), Some(Number::from(123)));
329    /// ```
330    fn as_number(&self) -> Option<Number>;
331
332    /// Returns the [`RawNumber`] without precision loss if `self` is a `Number`.
333    fn as_raw_number(&self) -> Option<RawNumber>;
334
335    /// Returns the str if `self` is a `string`.
336    ///
337    /// # Examples
338    ///
339    /// ```
340    /// use sonic_rs::{json, JsonValueTrait};
341    /// assert_eq!(json!("foo").as_str(), Some("foo"));
342    /// ```
343    fn as_str(&self) -> Option<&str>;
344
345    /// Returns the bool if `self` is a `boolean`.
346    ///
347    ///
348    /// # Examples
349    ///
350    /// ```
351    /// use sonic_rs::{json, JsonValueTrait};
352    /// assert_eq!(json!(true).as_bool(), Some(true));
353    /// ```
354    fn as_bool(&self) -> Option<bool>;
355
356    /// Index into a JSON array or map. A string-like index can be used to access a
357    /// value in a map, and a usize index can be used to access an element of an
358    /// array.
359    ///
360    /// Returns `None` if the type of `self` does not match the type of the
361    /// index, for example if the index is a string and `self` is an array or a
362    /// number. Also returns `None` if the given key does not exist in the map
363    /// or the given index is not within the bounds of the array.
364    ///
365    /// # Examples
366    /// ```
367    /// use sonic_rs::value::{JsonType, JsonValueTrait, Value};
368    ///
369    /// let json: Value = sonic_rs::from_str(r#"{"a": 1, "b": true}"#).unwrap();
370    ///
371    /// assert!(json.get("a").is_number());
372    /// assert!(json.get("unknown").is_none());
373    /// ```
374    fn get<I: Index>(&self, index: I) -> Option<Self::ValueType<'_>>;
375
376    /// Looks up a value by a path.
377    ///
378    /// The path is an iterator of multiple keys or indexes. It can be a `&[&str]`, `&[usize]`
379    /// or a `JsonPointer`.
380    ///
381    /// # Examples
382    ///
383    /// ```
384    /// use sonic_rs::{json, pointer, JsonValueTrait};
385    ///
386    /// let data = json!({
387    ///     "x": {
388    ///         "y": ["z", "zz"]
389    ///     }
390    /// });
391    ///
392    /// assert_eq!(data.pointer(&["x", "y"] ).unwrap(), &json!(["z", "zz"]));
393    /// assert_eq!(data.pointer(&pointer!["x", "y", 1] ).unwrap(), &json!("zz"));
394    /// assert_eq!(data.pointer(&["a", "b"]), None);
395    /// ```
396    fn pointer<P: IntoIterator>(&self, path: P) -> Option<Self::ValueType<'_>>
397    where
398        P::Item: Index;
399}
400
401/// A trait for all JSON object or array values. Used by `Value`.
402///
403/// The `Option<V: JsonContainerTrait>` and `Result<V: JsonContainerTrait, E>` also implement this
404/// trait. The `Option::None` or `Result::Err(_)` will be viewed as a null value.
405pub trait JsonContainerTrait {
406    type ObjectType;
407    type ArrayType;
408
409    /// Returns the object if `self` is an `object`.
410    ///
411    /// # Examples
412    ///
413    /// ```
414    /// use sonic_rs::{json, object, JsonContainerTrait, JsonValueTrait, Value};
415    ///
416    /// let value: Value = sonic_rs::from_str(r#"{"a": 1, "b": true}"#).unwrap();
417    ///
418    /// assert!(value.is_object());
419    /// assert_eq!(value.as_object(), Some(&object! {"a": 1, "b": true}));
420    /// ```
421    fn as_object(&self) -> Option<&Self::ObjectType>;
422
423    /// Returns the array if `self` is an `array`.
424    ///
425    /// # Examples
426    ///
427    /// ```
428    /// use sonic_rs::{array, json, JsonContainerTrait, JsonValueTrait, Value};
429    ///
430    /// let value: Value = sonic_rs::from_str(r#"[1, 2, 3]"#).unwrap();
431    ///
432    /// assert!(value.is_array());
433    /// assert_eq!(value.as_array(), Some(&array![1, 2, 3]));
434    /// ```
435    fn as_array(&self) -> Option<&Self::ArrayType>;
436}
437
438/// A trait for all mutable JSON values. Used by mutable `Value`.
439///
440/// The `Option<V: JsonValueMutTrait>` and `Result<V: JsonValueMutTrait, E>` also implement this
441/// trait. The `Option::None` or `Result::Err(_)` will be viewed as a null value.
442pub trait JsonValueMutTrait {
443    type ValueType;
444    type ObjectType;
445    type ArrayType;
446
447    /// Returns the mutable object if `self` is an `object`.
448    ///
449    /// # Examples
450    ///
451    /// ```
452    /// use sonic_rs::{JsonValueMutTrait, json};
453    /// use sonic_rs::Value;
454    ///
455    /// let mut value: Value = sonic_rs::from_str(r#"{"a": 1, "b": true}"#).unwrap();
456    /// let obj = value.as_object_mut().unwrap();
457    /// obj["a"] = json!(2);
458    /// assert_eq!(value, json!({"a": 2, "b": true}));
459    /// ```
460    fn as_object_mut(&mut self) -> Option<&mut Self::ObjectType>;
461
462    /// Returns the mutable array if `self` is an `array`.
463    ///
464    /// # Examples
465    ///
466    /// ```
467    /// use sonic_rs::{json, JsonValueMutTrait, Value};
468    ///
469    /// let mut value: Value = sonic_rs::from_str(r#"[1, 2, 3]"#).unwrap();
470    /// let arr = value.as_array_mut().unwrap();
471    /// arr[0] = json!(2);
472    /// assert_eq!(value, json!([2, 2, 3]));
473    /// ```
474    fn as_array_mut(&mut self) -> Option<&mut Self::ArrayType>;
475
476    /// Looks up a value by a path.
477    ///
478    /// The path is an iterator of multiple keys or indexes. It can be a `&[&str]`, `&[usize]`
479    /// or a `JsonPointer`.
480    ///
481    /// # Examples
482    ///
483    /// ```
484    /// # use sonic_rs::{json, pointer, JsonValueTrait};
485    /// #
486    /// let data = json!({
487    ///     "x": {
488    ///         "y": ["z", "zz"]
489    ///     }
490    /// });
491    ///
492    /// assert_eq!(data.pointer(&["x", "y"] ).unwrap(), &json!(["z", "zz"]));
493    /// assert_eq!(data.pointer(&pointer!["x", "y", 1] ).unwrap(), &json!("zz"));
494    /// assert_eq!(data.pointer(&["a", "b"]), None);
495    /// ```
496    fn pointer_mut<P: IntoIterator>(&mut self, path: P) -> Option<&mut Self::ValueType>
497    where
498        P::Item: Index;
499
500    /// Mutably index into a JSON array or map. A string-like index can be used to
501    /// access a value in a map, and a usize index can be used to access an
502    /// element of an array.
503    ///
504    /// Returns `None` if the type of `self` does not match the type of the
505    /// index, for example if the index is a string and `self` is an array or a
506    /// number. Also returns `None` if the given key does not exist in the map
507    /// or the given index is not within the bounds of the array.
508    ///
509    /// ```
510    /// use sonic_rs::{json, JsonValueMutTrait};
511    ///
512    /// let mut object = json!({ "A": 65, "B": 66, "C": 67 });
513    /// *object.get_mut("A").unwrap() = json!(69);
514    ///
515    /// let mut array = json!([ "A", "B", "C" ]);
516    /// *array.get_mut(2).unwrap() = json!("D");
517    /// ```
518    fn get_mut<I: Index>(&mut self, index: I) -> Option<&mut Self::ValueType>;
519}
520
521impl<V: JsonValueTrait> JsonValueTrait for Option<V> {
522    type ValueType<'v>
523        = V::ValueType<'v>
524    where
525        V: 'v,
526        Self: 'v;
527
528    fn as_bool(&self) -> Option<bool> {
529        self.as_ref().and_then(|v| v.as_bool())
530    }
531
532    fn as_f64(&self) -> Option<f64> {
533        self.as_ref().and_then(|v| v.as_f64())
534    }
535
536    fn as_i64(&self) -> Option<i64> {
537        self.as_ref().and_then(|v| v.as_i64())
538    }
539
540    fn as_u64(&self) -> Option<u64> {
541        self.as_ref().and_then(|v| v.as_u64())
542    }
543
544    fn as_number(&self) -> Option<Number> {
545        self.as_ref().and_then(|v| v.as_number())
546    }
547
548    fn as_raw_number(&self) -> Option<RawNumber> {
549        self.as_ref().and_then(|v| v.as_raw_number())
550    }
551
552    fn get_type(&self) -> JsonType {
553        self.as_ref().map_or(JsonType::Null, |v| v.get_type())
554    }
555
556    fn as_str(&self) -> Option<&str> {
557        self.as_ref().and_then(|v| v.as_str())
558    }
559
560    fn get<I: Index>(&self, index: I) -> Option<Self::ValueType<'_>> {
561        self.as_ref().and_then(|v| v.get(index))
562    }
563
564    fn pointer<P: IntoIterator>(&self, path: P) -> Option<Self::ValueType<'_>>
565    where
566        P::Item: Index,
567    {
568        self.as_ref().and_then(|v| v.pointer(path))
569    }
570}
571
572impl<V: JsonContainerTrait> JsonContainerTrait for Option<V> {
573    type ArrayType = V::ArrayType;
574    type ObjectType = V::ObjectType;
575
576    fn as_array(&self) -> Option<&Self::ArrayType> {
577        self.as_ref().and_then(|v| v.as_array())
578    }
579
580    fn as_object(&self) -> Option<&Self::ObjectType> {
581        self.as_ref().and_then(|v| v.as_object())
582    }
583}
584
585impl<V: JsonValueMutTrait> JsonValueMutTrait for Option<V> {
586    type ValueType = V::ValueType;
587    type ArrayType = V::ArrayType;
588    type ObjectType = V::ObjectType;
589
590    fn as_array_mut(&mut self) -> Option<&mut Self::ArrayType> {
591        self.as_mut().and_then(|v| v.as_array_mut())
592    }
593    fn as_object_mut(&mut self) -> Option<&mut Self::ObjectType> {
594        self.as_mut().and_then(|v| v.as_object_mut())
595    }
596
597    fn pointer_mut<P: IntoIterator>(&mut self, path: P) -> Option<&mut Self::ValueType>
598    where
599        P::Item: Index,
600    {
601        self.as_mut().and_then(|v| v.pointer_mut(path))
602    }
603
604    fn get_mut<I: Index>(&mut self, index: I) -> Option<&mut Self::ValueType> {
605        self.as_mut().and_then(|v| v.get_mut(index))
606    }
607}
608
609impl<V: JsonValueTrait, E> JsonValueTrait for Result<V, E> {
610    type ValueType<'v>
611        = V::ValueType<'v>
612    where
613        V: 'v,
614        Self: 'v;
615
616    fn as_bool(&self) -> Option<bool> {
617        self.as_ref().ok().and_then(|v| v.as_bool())
618    }
619
620    fn as_f64(&self) -> Option<f64> {
621        self.as_ref().ok().and_then(|v| v.as_f64())
622    }
623
624    fn as_i64(&self) -> Option<i64> {
625        self.as_ref().ok().and_then(|v| v.as_i64())
626    }
627
628    fn as_u64(&self) -> Option<u64> {
629        self.as_ref().ok().and_then(|v| v.as_u64())
630    }
631
632    fn as_number(&self) -> Option<Number> {
633        self.as_ref().ok().and_then(|v| v.as_number())
634    }
635
636    fn as_raw_number(&self) -> Option<RawNumber> {
637        self.as_ref().ok().and_then(|v| v.as_raw_number())
638    }
639
640    fn get_type(&self) -> JsonType {
641        self.as_ref().ok().map_or(JsonType::Null, |v| v.get_type())
642    }
643
644    fn as_str(&self) -> Option<&str> {
645        self.as_ref().ok().and_then(|v| v.as_str())
646    }
647
648    fn get<I: Index>(&self, index: I) -> Option<Self::ValueType<'_>> {
649        self.as_ref().ok().and_then(|v| v.get(index))
650    }
651
652    fn pointer<P: IntoIterator>(&self, path: P) -> Option<Self::ValueType<'_>>
653    where
654        P::Item: Index,
655    {
656        self.as_ref().ok().and_then(|v| v.pointer(path))
657    }
658}
659
660impl<V: JsonContainerTrait, E> JsonContainerTrait for Result<V, E> {
661    type ArrayType = V::ArrayType;
662    type ObjectType = V::ObjectType;
663    fn as_array(&self) -> Option<&Self::ArrayType> {
664        self.as_ref().ok().and_then(|v| v.as_array())
665    }
666
667    fn as_object(&self) -> Option<&Self::ObjectType> {
668        self.as_ref().ok().and_then(|v| v.as_object())
669    }
670}
671
672impl<V: JsonValueMutTrait, E> JsonValueMutTrait for Result<V, E> {
673    type ValueType = V::ValueType;
674    type ArrayType = V::ArrayType;
675    type ObjectType = V::ObjectType;
676
677    fn as_array_mut(&mut self) -> Option<&mut Self::ArrayType> {
678        self.as_mut().ok().and_then(|v| v.as_array_mut())
679    }
680
681    fn as_object_mut(&mut self) -> Option<&mut Self::ObjectType> {
682        self.as_mut().ok().and_then(|v| v.as_object_mut())
683    }
684
685    fn pointer_mut<P: IntoIterator>(&mut self, path: P) -> Option<&mut Self::ValueType>
686    where
687        P::Item: Index,
688    {
689        self.as_mut().ok().and_then(|v| v.pointer_mut(path))
690    }
691
692    fn get_mut<I: Index>(&mut self, index: I) -> Option<&mut Self::ValueType> {
693        self.as_mut().ok().and_then(|v| v.get_mut(index))
694    }
695}
696
697impl<V: JsonValueTrait> JsonValueTrait for &V {
698    type ValueType<'v>
699        = V::ValueType<'v>
700    where
701        V: 'v,
702        Self: 'v;
703
704    fn as_bool(&self) -> Option<bool> {
705        (*self).as_bool()
706    }
707
708    fn as_f64(&self) -> Option<f64> {
709        (*self).as_f64()
710    }
711
712    fn as_i64(&self) -> Option<i64> {
713        (*self).as_i64()
714    }
715
716    fn as_u64(&self) -> Option<u64> {
717        (*self).as_u64()
718    }
719
720    fn as_number(&self) -> Option<Number> {
721        (*self).as_number()
722    }
723
724    fn as_raw_number(&self) -> Option<RawNumber> {
725        (*self).as_raw_number()
726    }
727
728    fn get_type(&self) -> JsonType {
729        (*self).get_type()
730    }
731
732    fn as_str(&self) -> Option<&str> {
733        (*self).as_str()
734    }
735
736    fn get<I: Index>(&self, index: I) -> Option<Self::ValueType<'_>> {
737        (*self).get(index)
738    }
739
740    fn pointer<P: IntoIterator>(&self, path: P) -> Option<Self::ValueType<'_>>
741    where
742        P::Item: Index,
743    {
744        (*self).pointer(path)
745    }
746}
747
748impl<V: JsonContainerTrait> JsonContainerTrait for &V {
749    type ArrayType = V::ArrayType;
750    type ObjectType = V::ObjectType;
751
752    fn as_array(&self) -> Option<&Self::ArrayType> {
753        (*self).as_array()
754    }
755
756    fn as_object(&self) -> Option<&Self::ObjectType> {
757        (*self).as_object()
758    }
759}
760
761impl<V: JsonValueMutTrait> JsonValueMutTrait for &mut V {
762    type ValueType = V::ValueType;
763    type ArrayType = V::ArrayType;
764    type ObjectType = V::ObjectType;
765
766    fn as_array_mut(&mut self) -> Option<&mut Self::ArrayType> {
767        (*self).as_array_mut()
768    }
769
770    fn as_object_mut(&mut self) -> Option<&mut Self::ObjectType> {
771        (*self).as_object_mut()
772    }
773
774    fn get_mut<I: Index>(&mut self, index: I) -> Option<&mut Self::ValueType> {
775        (**self).get_mut(index)
776    }
777
778    fn pointer_mut<P: IntoIterator>(&mut self, path: P) -> Option<&mut Self::ValueType>
779    where
780        P::Item: Index,
781    {
782        (**self).pointer_mut(path)
783    }
784}