Skip to main content

simd_json/serde/
de.rs

1use crate::serde_ext::de::IntoDeserializer;
2use crate::{Deserializer, Error, ErrorType, Node, Result, StaticNode, macros::stry};
3use serde_ext::de::{self, DeserializeSeed, MapAccess, SeqAccess, Visitor};
4use serde_ext::forward_to_deserialize_any;
5use std::str;
6
7impl<'a, 'de> de::Deserializer<'de> for &'a mut Deserializer<'de>
8where
9    'de: 'a,
10{
11    type Error = Error;
12
13    // Look at the input data to decide what Serde data model type to
14    // deserialize as. Not all data formats are able to support this operation.
15    // Formats that support `deserialize_any` are known as self-describing.
16    #[cfg_attr(not(feature = "no-inline"), inline)]
17    fn deserialize_any<V>(self, visitor: V) -> Result<V::Value>
18    where
19        V: Visitor<'de>,
20    {
21        match stry!(self.next()) {
22            Node::String(s) => visitor.visit_borrowed_str(s),
23            Node::Static(StaticNode::Null) => visitor.visit_unit(),
24            Node::Static(StaticNode::Bool(b)) => visitor.visit_bool(b),
25            #[allow(clippy::useless_conversion)] // .into() required by ordered-float
26            Node::Static(StaticNode::F64(n)) => visitor.visit_f64(n.into()),
27            Node::Static(StaticNode::I64(n)) => visitor.visit_i64(n),
28            #[cfg(feature = "128bit")]
29            Node::Static(StaticNode::I128(n)) => visitor.visit_i128(n),
30            Node::Static(StaticNode::U64(n)) => visitor.visit_u64(n),
31            #[cfg(feature = "128bit")]
32            Node::Static(StaticNode::U128(n)) => visitor.visit_u128(n),
33            Node::Array { len, count: _ } => visit_array(self, len, visitor),
34            Node::Object { len, count: _ } => visit_object(self, len, visitor),
35        }
36    }
37
38    // Uses the `parse_bool` parsing function defined above to read the JSON
39    // identifier `true` or `false` from the input.
40    //
41    // Parsing refers to looking at the input and deciding that it contains the
42    // JSON value `true` or `false`.
43    //
44    // Deserialization refers to mapping that JSON value into Serde's data
45    // model by invoking one of the `Visitor` methods. In the case of JSON and
46    // bool that mapping is straightforward so the distinction may seem silly,
47    // but in other cases Deserializers sometimes perform non-obvious mappings.
48    // For example the TOML format has a Datetime type and Serde's data model
49    // does not. In the `toml` crate, a Datetime in the input is deserialized by
50    // mapping it to a Serde data model "struct" type with a special name and a
51    // single field containing the Datetime represented as a string.
52    #[cfg_attr(not(feature = "no-inline"), inline)]
53    fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value>
54    where
55        V: Visitor<'de>,
56    {
57        match stry!(self.next()) {
58            Node::Static(StaticNode::Bool(b)) => visitor.visit_bool(b),
59            _c => Err(Deserializer::error(ErrorType::ExpectedBoolean)),
60        }
61    }
62
63    // Refer to the "Understanding deserializer lifetimes" page for information
64    // about the three deserialization flavors of strings in Serde.
65    #[cfg_attr(not(feature = "no-inline"), inline)]
66    fn deserialize_str<V>(self, visitor: V) -> Result<V::Value>
67    where
68        V: Visitor<'de>,
69    {
70        if let Ok(Node::String(s)) = self.next() {
71            visitor.visit_borrowed_str(s)
72        } else {
73            Err(Deserializer::error(ErrorType::ExpectedString))
74        }
75    }
76
77    #[cfg_attr(not(feature = "no-inline"), inline)]
78    fn deserialize_string<V>(self, visitor: V) -> Result<V::Value>
79    where
80        V: Visitor<'de>,
81    {
82        if let Ok(Node::String(s)) = self.next() {
83            visitor.visit_str(s)
84        } else {
85            Err(Deserializer::error(ErrorType::ExpectedString))
86        }
87    }
88
89    // The `parse_signed` function is generic over the integer type `T` so here
90    // it is invoked with `T=i8`. The next 8 methods are similar.
91    #[cfg_attr(not(feature = "no-inline"), inline)]
92    #[allow(clippy::cast_possible_truncation)]
93    fn deserialize_i8<V>(self, visitor: V) -> Result<V::Value>
94    where
95        V: Visitor<'de>,
96    {
97        visitor.visit_i8(stry!(self.parse_i8()))
98    }
99
100    #[cfg_attr(not(feature = "no-inline"), inline)]
101    #[allow(clippy::cast_possible_truncation)]
102    fn deserialize_i16<V>(self, visitor: V) -> Result<V::Value>
103    where
104        V: Visitor<'de>,
105    {
106        visitor.visit_i16(stry!(self.parse_i16()))
107    }
108
109    #[cfg_attr(not(feature = "no-inline"), inline)]
110    #[allow(clippy::cast_possible_truncation)]
111    fn deserialize_i32<V>(self, visitor: V) -> Result<V::Value>
112    where
113        V: Visitor<'de>,
114    {
115        visitor.visit_i32(stry!(self.parse_i32()))
116    }
117
118    #[cfg_attr(not(feature = "no-inline"), inline)]
119    fn deserialize_i64<V>(self, visitor: V) -> Result<V::Value>
120    where
121        V: Visitor<'de>,
122    {
123        visitor.visit_i64(stry!(self.parse_i64()))
124    }
125
126    #[cfg_attr(not(feature = "no-inline"), inline)]
127    fn deserialize_i128<V>(self, visitor: V) -> Result<V::Value>
128    where
129        V: Visitor<'de>,
130    {
131        visitor.visit_i128(stry!(self.parse_i128()))
132    }
133
134    #[cfg_attr(not(feature = "no-inline"), inline)]
135    #[allow(clippy::cast_possible_truncation)]
136    fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value>
137    where
138        V: Visitor<'de>,
139    {
140        visitor.visit_u8(stry!(self.parse_u8()))
141    }
142
143    #[cfg_attr(not(feature = "no-inline"), inline)]
144    #[allow(clippy::cast_possible_truncation)]
145    fn deserialize_u16<V>(self, visitor: V) -> Result<V::Value>
146    where
147        V: Visitor<'de>,
148    {
149        visitor.visit_u16(stry!(self.parse_u16()))
150    }
151
152    #[cfg_attr(not(feature = "no-inline"), inline)]
153    #[allow(clippy::cast_possible_truncation)]
154    fn deserialize_u32<V>(self, visitor: V) -> Result<V::Value>
155    where
156        V: Visitor<'de>,
157    {
158        visitor.visit_u32(stry!(self.parse_u32()))
159    }
160
161    #[cfg_attr(not(feature = "no-inline"), inline)]
162    fn deserialize_u64<V>(self, visitor: V) -> Result<V::Value>
163    where
164        V: Visitor<'de>,
165    {
166        visitor.visit_u64(stry!(self.parse_u64()))
167    }
168
169    #[cfg_attr(not(feature = "no-inline"), inline)]
170    fn deserialize_u128<V>(self, visitor: V) -> Result<V::Value>
171    where
172        V: Visitor<'de>,
173    {
174        visitor.visit_u128(stry!(self.parse_u128()))
175    }
176
177    #[cfg_attr(not(feature = "no-inline"), inline)]
178    #[allow(clippy::cast_possible_truncation)]
179    fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value>
180    where
181        V: Visitor<'de>,
182    {
183        let v: f64 = stry!(self.parse_double());
184        visitor.visit_f32(v as f32)
185    }
186
187    #[cfg_attr(not(feature = "no-inline"), inline)]
188    fn deserialize_f64<V>(self, visitor: V) -> Result<V::Value>
189    where
190        V: Visitor<'de>,
191    {
192        visitor.visit_f64(stry!(self.parse_double()))
193    }
194
195    // An absent optional is represented as the JSON `null` and a present
196    // optional is represented as just the contained value.
197    //
198    // As commented in `Serializer` implementation, this is a lossy
199    // representation. For example the values `Some(())` and `None` both
200    // serialize as just `null`. Unfortunately this is typically what people
201    // expect when working with JSON. Other formats are encouraged to behave
202    // more intelligently if possible.
203
204    #[cfg_attr(not(feature = "no-inline"), inline)]
205    fn deserialize_option<V>(self, visitor: V) -> Result<V::Value>
206    where
207        V: Visitor<'de>,
208    {
209        if stry!(self.peek()) == Node::Static(StaticNode::Null) {
210            self.skip();
211            visitor.visit_unit()
212        } else {
213            visitor.visit_some(self)
214        }
215    }
216
217    // In Serde, unit means an anonymous value containing no data.
218    #[cfg_attr(not(feature = "no-inline"), inline)]
219    fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value>
220    where
221        V: Visitor<'de>,
222    {
223        if stry!(self.next()) != Node::Static(StaticNode::Null) {
224            return Err(Deserializer::error(ErrorType::ExpectedNull));
225        }
226        visitor.visit_unit()
227    }
228
229    // Deserialization of compound types like sequences and maps happens by
230    // passing the visitor an "Access" object that gives it the ability to
231    // iterate through the data contained in the sequence.
232    #[cfg_attr(not(feature = "no-inline"), inline)]
233    fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value>
234    where
235        V: Visitor<'de>,
236    {
237        // Parse the opening bracket of the sequence.
238        if let Ok(Node::Array { len, count: _ }) = self.next() {
239            // Give the visitor access to each element of the sequence.
240            visit_array(self, len, visitor)
241        } else {
242            Err(Deserializer::error(ErrorType::ExpectedArray))
243        }
244    }
245
246    // Tuples look just like sequences in JSON. Some formats may be able to
247    // represent tuples more efficiently.
248    //
249    // As indicated by the length parameter, the `Deserialize` implementation
250    // for a tuple in the Serde data model is required to know the length of the
251    // tuple before even looking at the input data.
252
253    #[cfg_attr(not(feature = "no-inline"), inline)]
254    fn deserialize_tuple<V>(self, _len: usize, visitor: V) -> Result<V::Value>
255    where
256        V: Visitor<'de>,
257    {
258        self.deserialize_seq(visitor)
259        // tuples have a known length damn you serde ...
260        //self.skip();
261        // r
262    }
263
264    // Tuple structs look just like sequences in JSON.
265    fn deserialize_tuple_struct<V>(
266        self,
267        _name: &'static str,
268        _len: usize,
269        visitor: V,
270    ) -> Result<V::Value>
271    where
272        V: Visitor<'de>,
273    {
274        self.deserialize_seq(visitor)
275    }
276
277    // Unit struct means a named value containing no data.
278    fn deserialize_unit_struct<V>(self, _name: &'static str, visitor: V) -> Result<V::Value>
279    where
280        V: Visitor<'de>,
281    {
282        self.deserialize_unit(visitor)
283    }
284
285    // As is done here, serializers are encouraged to treat newtype structs as
286    // insignificant wrappers around the data they contain. That means not
287    // parsing anything other than the contained value.
288    fn deserialize_newtype_struct<V>(self, _name: &'static str, visitor: V) -> Result<V::Value>
289    where
290        V: Visitor<'de>,
291    {
292        visitor.visit_newtype_struct(self)
293    }
294
295    #[cfg_attr(not(feature = "no-inline"), inline)]
296    fn deserialize_map<V>(self, visitor: V) -> Result<V::Value>
297    where
298        V: Visitor<'de>,
299    {
300        // Parse the opening bracket of the sequence.
301        if let Ok(Node::Object { len, count: _ }) = self.next() {
302            // Give the visitor access to each element of the sequence.
303            visit_object(self, len, visitor)
304        } else {
305            Err(Deserializer::error(ErrorType::ExpectedMap))
306        }
307    }
308
309    #[cfg_attr(not(feature = "no-inline"), inline)]
310    fn deserialize_struct<V>(
311        self,
312        _name: &'static str,
313        _fields: &'static [&'static str],
314        visitor: V,
315    ) -> Result<V::Value>
316    where
317        V: Visitor<'de>,
318    {
319        match self.next() {
320            // Give the visitor access to each element of the sequence.
321            Ok(Node::Object { len, count: _ }) => visit_object(self, len, visitor),
322            Ok(Node::Array { len, count: _ }) => visit_array(self, len, visitor),
323            _ => Err(Deserializer::error(ErrorType::ExpectedMap)),
324        }
325    }
326
327    #[cfg_attr(not(feature = "no-inline"), inline)]
328    fn deserialize_enum<V>(
329        self,
330        _name: &'static str,
331        _variants: &'static [&'static str],
332        visitor: V,
333    ) -> Result<V::Value>
334    where
335        V: Visitor<'de>,
336    {
337        // Parse the opening bracket of the sequence.
338        match self.next() {
339            Ok(Node::Object { len: 1, .. }) => {
340                // Give the visitor access to each element of the sequence.
341                // let value = ri!(visitor.visit_enum(VariantAccess::new(self)));
342                visitor.visit_enum(VariantAccess::new(self))
343            }
344            Ok(Node::String(s)) => visitor.visit_enum(s.into_deserializer()),
345            _ => Err(Deserializer::error(ErrorType::ExpectedEnum)),
346        }
347    }
348
349    forward_to_deserialize_any! {
350            char
351            bytes byte_buf
352            identifier ignored_any
353    }
354}
355
356// From  https://github.com/serde-rs/json/blob/2d81cbd11302bd246db248dfb335110d1827e893/src/de.rs
357struct VariantAccess<'a, 'de> {
358    de: &'a mut Deserializer<'de>,
359}
360
361impl<'a, 'de> VariantAccess<'a, 'de> {
362    fn new(de: &'a mut Deserializer<'de>) -> Self {
363        VariantAccess { de }
364    }
365}
366
367impl<'de> de::EnumAccess<'de> for VariantAccess<'_, 'de> {
368    type Error = Error;
369    type Variant = Self;
370
371    fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self)>
372    where
373        V: de::DeserializeSeed<'de>,
374    {
375        let val = stry!(seed.deserialize(&mut *self.de));
376        Ok((val, self))
377    }
378}
379
380impl<'de> de::VariantAccess<'de> for VariantAccess<'_, 'de> {
381    type Error = Error;
382
383    fn unit_variant(self) -> Result<()> {
384        de::Deserialize::deserialize(self.de)
385    }
386
387    fn newtype_variant_seed<T>(self, seed: T) -> Result<T::Value>
388    where
389        T: de::DeserializeSeed<'de>,
390    {
391        seed.deserialize(self.de)
392    }
393
394    fn tuple_variant<V>(self, _len: usize, visitor: V) -> Result<V::Value>
395    where
396        V: de::Visitor<'de>,
397    {
398        de::Deserializer::deserialize_seq(self.de, visitor)
399    }
400
401    fn struct_variant<V>(self, fields: &'static [&'static str], visitor: V) -> Result<V::Value>
402    where
403        V: de::Visitor<'de>,
404    {
405        de::Deserializer::deserialize_struct(self.de, "", fields, visitor)
406    }
407}
408
409/// Hands the `len` elements of an array to `visitor`. A visitor of fixed length (tuple, array,
410/// tuple struct, struct from an array) stops asking for elements once it has enough; the elements
411/// it left unread would stay on the tape and be read as the values that follow, so they are an
412/// error, as in `serde_json` ("trailing characters") and `serde::de::value::SeqDeserializer`.
413fn visit_array<'de, V>(de: &mut Deserializer<'de>, len: usize, visitor: V) -> Result<V::Value>
414where
415    V: Visitor<'de>,
416{
417    let mut seq = CommaSeparated::new(de, len);
418    let value = stry!(visitor.visit_seq(&mut seq));
419    if seq.len == 0 {
420        Ok(value)
421    } else {
422        Err(de::Error::invalid_length(len, &"fewer elements in array"))
423    }
424}
425
426/// Like [`visit_array`], for the `len` members of an object.
427fn visit_object<'de, V>(de: &mut Deserializer<'de>, len: usize, visitor: V) -> Result<V::Value>
428where
429    V: Visitor<'de>,
430{
431    let mut map = CommaSeparated::new(de, len);
432    let value = stry!(visitor.visit_map(&mut map));
433    if map.len == 0 {
434        Ok(value)
435    } else {
436        Err(de::Error::invalid_length(len, &"fewer elements in map"))
437    }
438}
439
440// In order to handle commas correctly when deserializing a JSON array or map,
441// we need to track whether we are on the first element or past the first
442// element.
443struct CommaSeparated<'a, 'de: 'a> {
444    de: &'a mut Deserializer<'de>,
445    len: usize,
446}
447impl<'a, 'de> CommaSeparated<'a, 'de> {
448    #[cfg_attr(not(feature = "no-inline"), inline)]
449    fn new(de: &'a mut Deserializer<'de>, len: usize) -> Self {
450        CommaSeparated { de, len }
451    }
452}
453
454// `SeqAccess` is provided to the `Visitor` to give it the ability to iterate
455// through elements of the sequence.
456impl<'de> SeqAccess<'de> for CommaSeparated<'_, 'de> {
457    type Error = Error;
458
459    #[cfg_attr(not(feature = "no-inline"), inline)]
460    fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>>
461    where
462        T: DeserializeSeed<'de>,
463    {
464        if self.len == 0 {
465            Ok(None)
466        } else {
467            self.len -= 1;
468            seed.deserialize(&mut *self.de).map(Some)
469        }
470    }
471    #[cfg_attr(not(feature = "no-inline"), inline)]
472    fn size_hint(&self) -> Option<usize> {
473        Some(self.len)
474    }
475}
476
477// `MapAccess` is provided to the `Visitor` to give it the ability to iterate
478// through entries of the map.
479impl<'de> MapAccess<'de> for CommaSeparated<'_, 'de> {
480    type Error = Error;
481
482    #[cfg_attr(not(feature = "no-inline"), inline)]
483    fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>>
484    where
485        K: DeserializeSeed<'de>,
486    {
487        if self.len == 0 {
488            Ok(None)
489        } else {
490            self.len -= 1;
491            seed.deserialize(MapKey { de: &mut *self.de }).map(Some)
492        }
493    }
494
495    #[cfg_attr(not(feature = "no-inline"), inline)]
496    fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value>
497    where
498        V: DeserializeSeed<'de>,
499    {
500        if self.de.idx >= self.de.tape.len() {
501            return Err(Deserializer::error(ErrorType::Eof));
502        }
503        // read the value
504        seed.deserialize(&mut *self.de)
505    }
506
507    #[cfg_attr(not(feature = "no-inline"), inline)]
508    fn size_hint(&self) -> Option<usize> {
509        Some(self.len)
510    }
511}
512
513// `MapKey` is provided to the `Visitor` to give it the ability to parse integers
514// from string as JSON keys are always string
515struct MapKey<'de: 'a, 'a> {
516    de: &'a mut Deserializer<'de>,
517}
518
519macro_rules! deserialize_integer_key {
520    ($method:ident => $visit:ident; $type:ty) => {
521        fn $method<V>(self, visitor: V) -> Result<V::Value>
522        where
523            V: de::Visitor<'de>,
524        {
525            visitor.$visit(stry!(match stry!(self.de.next()) {
526                Node::String(s) => crate::serde::parse_integer_key::<$type>(s)
527                    .ok_or_else(|| Deserializer::error(ErrorType::InvalidNumber)),
528                _ => Err(Deserializer::error(ErrorType::ExpectedString)),
529            }))
530        }
531    };
532}
533
534impl<'de> de::Deserializer<'de> for MapKey<'de, '_> {
535    type Error = Error;
536
537    #[cfg_attr(not(feature = "no-inline"), inline)]
538    fn deserialize_any<V>(self, visitor: V) -> Result<V::Value>
539    where
540        V: de::Visitor<'de>,
541    {
542        match stry!(self.de.next()) {
543            Node::String(s) => visitor.visit_borrowed_str(s),
544            _ => Err(Deserializer::error(ErrorType::ExpectedString)),
545        }
546    }
547
548    deserialize_integer_key!(deserialize_i8 => visit_i8; i8);
549    deserialize_integer_key!(deserialize_i16 => visit_i16; i16);
550    deserialize_integer_key!(deserialize_i32 => visit_i32; i32);
551    deserialize_integer_key!(deserialize_i64 => visit_i64; i64);
552    deserialize_integer_key!(deserialize_u8 => visit_u8; u8);
553    deserialize_integer_key!(deserialize_u16 => visit_u16; u16);
554    deserialize_integer_key!(deserialize_u32 => visit_u32; u32);
555    deserialize_integer_key!(deserialize_u64 => visit_u64; u64);
556
557    // Like i128/u128 values, keys don't need the `128bit` feature: the key text is parsed directly.
558    deserialize_integer_key!(deserialize_i128 => visit_i128; i128);
559    deserialize_integer_key!(deserialize_u128 => visit_u128; u128);
560
561    #[cfg_attr(not(feature = "no-inline"), inline)]
562    fn deserialize_option<V>(self, visitor: V) -> Result<V::Value>
563    where
564        V: de::Visitor<'de>,
565    {
566        // Map keys cannot be null.
567        visitor.visit_some(self)
568    }
569
570    #[cfg_attr(not(feature = "no-inline"), inline)]
571    fn deserialize_newtype_struct<V>(self, _name: &'static str, visitor: V) -> Result<V::Value>
572    where
573        V: de::Visitor<'de>,
574    {
575        visitor.visit_newtype_struct(self)
576    }
577
578    #[cfg_attr(not(feature = "no-inline"), inline)]
579    fn deserialize_enum<V>(
580        self,
581        name: &'static str,
582        variants: &'static [&'static str],
583        visitor: V,
584    ) -> Result<V::Value>
585    where
586        V: de::Visitor<'de>,
587    {
588        self.de.deserialize_enum(name, variants, visitor)
589    }
590
591    #[cfg_attr(not(feature = "no-inline"), inline)]
592    fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value>
593    where
594        V: de::Visitor<'de>,
595    {
596        self.de.deserialize_bytes(visitor)
597    }
598
599    #[cfg_attr(not(feature = "no-inline"), inline)]
600    fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value>
601    where
602        V: de::Visitor<'de>,
603    {
604        self.de.deserialize_bytes(visitor)
605    }
606
607    forward_to_deserialize_any! {
608        bool f32 f64 char str string unit unit_struct seq tuple tuple_struct map
609        struct identifier ignored_any
610    }
611}
612
613#[cfg(test)]
614mod tests;