sonic_rs/value/
value_trait.rs

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