1 | use crate::error::{Error, ErrorCode}; |
2 | use crate::map::Map; |
3 | use crate::number::Number; |
4 | use crate::value::Value; |
5 | use alloc::borrow::{Cow, ToOwned}; |
6 | use alloc::string::String; |
7 | #[cfg (feature = "raw_value" )] |
8 | use alloc::string::ToString; |
9 | use alloc::vec::{self, Vec}; |
10 | use core::fmt; |
11 | use core::slice; |
12 | use core::str::FromStr; |
13 | use serde::de::{ |
14 | self, Deserialize, DeserializeSeed, EnumAccess, Expected, IntoDeserializer, MapAccess, |
15 | SeqAccess, Unexpected, VariantAccess, Visitor, |
16 | }; |
17 | use serde::forward_to_deserialize_any; |
18 | |
19 | #[cfg (feature = "arbitrary_precision" )] |
20 | use crate::number::NumberFromString; |
21 | |
22 | impl<'de> Deserialize<'de> for Value { |
23 | #[inline ] |
24 | fn deserialize<D>(deserializer: D) -> Result<Value, D::Error> |
25 | where |
26 | D: serde::Deserializer<'de>, |
27 | { |
28 | struct ValueVisitor; |
29 | |
30 | impl<'de> Visitor<'de> for ValueVisitor { |
31 | type Value = Value; |
32 | |
33 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
34 | formatter.write_str("any valid JSON value" ) |
35 | } |
36 | |
37 | #[inline ] |
38 | fn visit_bool<E>(self, value: bool) -> Result<Value, E> { |
39 | Ok(Value::Bool(value)) |
40 | } |
41 | |
42 | #[inline ] |
43 | fn visit_i64<E>(self, value: i64) -> Result<Value, E> { |
44 | Ok(Value::Number(value.into())) |
45 | } |
46 | |
47 | #[inline ] |
48 | fn visit_u64<E>(self, value: u64) -> Result<Value, E> { |
49 | Ok(Value::Number(value.into())) |
50 | } |
51 | |
52 | #[inline ] |
53 | fn visit_f64<E>(self, value: f64) -> Result<Value, E> { |
54 | Ok(Number::from_f64(value).map_or(Value::Null, Value::Number)) |
55 | } |
56 | |
57 | #[cfg (any(feature = "std" , feature = "alloc" ))] |
58 | #[inline ] |
59 | fn visit_str<E>(self, value: &str) -> Result<Value, E> |
60 | where |
61 | E: serde::de::Error, |
62 | { |
63 | self.visit_string(String::from(value)) |
64 | } |
65 | |
66 | #[cfg (any(feature = "std" , feature = "alloc" ))] |
67 | #[inline ] |
68 | fn visit_string<E>(self, value: String) -> Result<Value, E> { |
69 | Ok(Value::String(value)) |
70 | } |
71 | |
72 | #[inline ] |
73 | fn visit_none<E>(self) -> Result<Value, E> { |
74 | Ok(Value::Null) |
75 | } |
76 | |
77 | #[inline ] |
78 | fn visit_some<D>(self, deserializer: D) -> Result<Value, D::Error> |
79 | where |
80 | D: serde::Deserializer<'de>, |
81 | { |
82 | Deserialize::deserialize(deserializer) |
83 | } |
84 | |
85 | #[inline ] |
86 | fn visit_unit<E>(self) -> Result<Value, E> { |
87 | Ok(Value::Null) |
88 | } |
89 | |
90 | #[inline ] |
91 | fn visit_seq<V>(self, mut visitor: V) -> Result<Value, V::Error> |
92 | where |
93 | V: SeqAccess<'de>, |
94 | { |
95 | let mut vec = Vec::new(); |
96 | |
97 | while let Some(elem) = tri!(visitor.next_element()) { |
98 | vec.push(elem); |
99 | } |
100 | |
101 | Ok(Value::Array(vec)) |
102 | } |
103 | |
104 | #[cfg (any(feature = "std" , feature = "alloc" ))] |
105 | fn visit_map<V>(self, mut visitor: V) -> Result<Value, V::Error> |
106 | where |
107 | V: MapAccess<'de>, |
108 | { |
109 | match tri!(visitor.next_key_seed(KeyClassifier)) { |
110 | #[cfg (feature = "arbitrary_precision" )] |
111 | Some(KeyClass::Number) => { |
112 | let number: NumberFromString = tri!(visitor.next_value()); |
113 | Ok(Value::Number(number.value)) |
114 | } |
115 | #[cfg (feature = "raw_value" )] |
116 | Some(KeyClass::RawValue) => { |
117 | let value = tri!(visitor.next_value_seed(crate::raw::BoxedFromString)); |
118 | crate::from_str(value.get()).map_err(de::Error::custom) |
119 | } |
120 | Some(KeyClass::Map(first_key)) => { |
121 | let mut values = Map::new(); |
122 | |
123 | values.insert(first_key, tri!(visitor.next_value())); |
124 | while let Some((key, value)) = tri!(visitor.next_entry()) { |
125 | values.insert(key, value); |
126 | } |
127 | |
128 | Ok(Value::Object(values)) |
129 | } |
130 | None => Ok(Value::Object(Map::new())), |
131 | } |
132 | } |
133 | } |
134 | |
135 | deserializer.deserialize_any(ValueVisitor) |
136 | } |
137 | } |
138 | |
139 | impl FromStr for Value { |
140 | type Err = Error; |
141 | fn from_str(s: &str) -> Result<Value, Error> { |
142 | super::super::de::from_str(s) |
143 | } |
144 | } |
145 | |
146 | macro_rules! deserialize_number { |
147 | ($method:ident) => { |
148 | #[cfg(not(feature = "arbitrary_precision" ))] |
149 | fn $method<V>(self, visitor: V) -> Result<V::Value, Error> |
150 | where |
151 | V: Visitor<'de>, |
152 | { |
153 | match self { |
154 | Value::Number(n) => n.deserialize_any(visitor), |
155 | _ => Err(self.invalid_type(&visitor)), |
156 | } |
157 | } |
158 | |
159 | #[cfg(feature = "arbitrary_precision" )] |
160 | fn $method<V>(self, visitor: V) -> Result<V::Value, Error> |
161 | where |
162 | V: Visitor<'de>, |
163 | { |
164 | match self { |
165 | Value::Number(n) => n.$method(visitor), |
166 | _ => self.deserialize_any(visitor), |
167 | } |
168 | } |
169 | }; |
170 | } |
171 | |
172 | fn visit_array<'de, V>(array: Vec<Value>, visitor: V) -> Result<V::Value, Error> |
173 | where |
174 | V: Visitor<'de>, |
175 | { |
176 | let len = array.len(); |
177 | let mut deserializer = SeqDeserializer::new(array); |
178 | let seq = tri!(visitor.visit_seq(&mut deserializer)); |
179 | let remaining = deserializer.iter.len(); |
180 | if remaining == 0 { |
181 | Ok(seq) |
182 | } else { |
183 | Err(serde::de::Error::invalid_length( |
184 | len, |
185 | &"fewer elements in array" , |
186 | )) |
187 | } |
188 | } |
189 | |
190 | fn visit_object<'de, V>(object: Map<String, Value>, visitor: V) -> Result<V::Value, Error> |
191 | where |
192 | V: Visitor<'de>, |
193 | { |
194 | let len = object.len(); |
195 | let mut deserializer = MapDeserializer::new(object); |
196 | let map = tri!(visitor.visit_map(&mut deserializer)); |
197 | let remaining = deserializer.iter.len(); |
198 | if remaining == 0 { |
199 | Ok(map) |
200 | } else { |
201 | Err(serde::de::Error::invalid_length( |
202 | len, |
203 | &"fewer elements in map" , |
204 | )) |
205 | } |
206 | } |
207 | |
208 | impl<'de> serde::Deserializer<'de> for Value { |
209 | type Error = Error; |
210 | |
211 | #[inline ] |
212 | fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Error> |
213 | where |
214 | V: Visitor<'de>, |
215 | { |
216 | match self { |
217 | Value::Null => visitor.visit_unit(), |
218 | Value::Bool(v) => visitor.visit_bool(v), |
219 | Value::Number(n) => n.deserialize_any(visitor), |
220 | #[cfg (any(feature = "std" , feature = "alloc" ))] |
221 | Value::String(v) => visitor.visit_string(v), |
222 | Value::Array(v) => visit_array(v, visitor), |
223 | Value::Object(v) => visit_object(v, visitor), |
224 | } |
225 | } |
226 | |
227 | deserialize_number!(deserialize_i8); |
228 | deserialize_number!(deserialize_i16); |
229 | deserialize_number!(deserialize_i32); |
230 | deserialize_number!(deserialize_i64); |
231 | deserialize_number!(deserialize_i128); |
232 | deserialize_number!(deserialize_u8); |
233 | deserialize_number!(deserialize_u16); |
234 | deserialize_number!(deserialize_u32); |
235 | deserialize_number!(deserialize_u64); |
236 | deserialize_number!(deserialize_u128); |
237 | deserialize_number!(deserialize_f32); |
238 | deserialize_number!(deserialize_f64); |
239 | |
240 | #[inline ] |
241 | fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Error> |
242 | where |
243 | V: Visitor<'de>, |
244 | { |
245 | match self { |
246 | Value::Null => visitor.visit_none(), |
247 | _ => visitor.visit_some(self), |
248 | } |
249 | } |
250 | |
251 | #[inline ] |
252 | fn deserialize_enum<V>( |
253 | self, |
254 | _name: &str, |
255 | _variants: &'static [&'static str], |
256 | visitor: V, |
257 | ) -> Result<V::Value, Error> |
258 | where |
259 | V: Visitor<'de>, |
260 | { |
261 | let (variant, value) = match self { |
262 | Value::Object(value) => { |
263 | let mut iter = value.into_iter(); |
264 | let (variant, value) = match iter.next() { |
265 | Some(v) => v, |
266 | None => { |
267 | return Err(serde::de::Error::invalid_value( |
268 | Unexpected::Map, |
269 | &"map with a single key" , |
270 | )); |
271 | } |
272 | }; |
273 | // enums are encoded in json as maps with a single key:value pair |
274 | if iter.next().is_some() { |
275 | return Err(serde::de::Error::invalid_value( |
276 | Unexpected::Map, |
277 | &"map with a single key" , |
278 | )); |
279 | } |
280 | (variant, Some(value)) |
281 | } |
282 | Value::String(variant) => (variant, None), |
283 | other => { |
284 | return Err(serde::de::Error::invalid_type( |
285 | other.unexpected(), |
286 | &"string or map" , |
287 | )); |
288 | } |
289 | }; |
290 | |
291 | visitor.visit_enum(EnumDeserializer { variant, value }) |
292 | } |
293 | |
294 | #[inline ] |
295 | fn deserialize_newtype_struct<V>( |
296 | self, |
297 | name: &'static str, |
298 | visitor: V, |
299 | ) -> Result<V::Value, Error> |
300 | where |
301 | V: Visitor<'de>, |
302 | { |
303 | #[cfg (feature = "raw_value" )] |
304 | { |
305 | if name == crate::raw::TOKEN { |
306 | return visitor.visit_map(crate::raw::OwnedRawDeserializer { |
307 | raw_value: Some(self.to_string()), |
308 | }); |
309 | } |
310 | } |
311 | |
312 | let _ = name; |
313 | visitor.visit_newtype_struct(self) |
314 | } |
315 | |
316 | fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, Error> |
317 | where |
318 | V: Visitor<'de>, |
319 | { |
320 | match self { |
321 | Value::Bool(v) => visitor.visit_bool(v), |
322 | _ => Err(self.invalid_type(&visitor)), |
323 | } |
324 | } |
325 | |
326 | fn deserialize_char<V>(self, visitor: V) -> Result<V::Value, Error> |
327 | where |
328 | V: Visitor<'de>, |
329 | { |
330 | self.deserialize_string(visitor) |
331 | } |
332 | |
333 | fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, Error> |
334 | where |
335 | V: Visitor<'de>, |
336 | { |
337 | self.deserialize_string(visitor) |
338 | } |
339 | |
340 | fn deserialize_string<V>(self, visitor: V) -> Result<V::Value, Error> |
341 | where |
342 | V: Visitor<'de>, |
343 | { |
344 | match self { |
345 | #[cfg (any(feature = "std" , feature = "alloc" ))] |
346 | Value::String(v) => visitor.visit_string(v), |
347 | _ => Err(self.invalid_type(&visitor)), |
348 | } |
349 | } |
350 | |
351 | fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value, Error> |
352 | where |
353 | V: Visitor<'de>, |
354 | { |
355 | self.deserialize_byte_buf(visitor) |
356 | } |
357 | |
358 | fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value, Error> |
359 | where |
360 | V: Visitor<'de>, |
361 | { |
362 | match self { |
363 | #[cfg (any(feature = "std" , feature = "alloc" ))] |
364 | Value::String(v) => visitor.visit_string(v), |
365 | Value::Array(v) => visit_array(v, visitor), |
366 | _ => Err(self.invalid_type(&visitor)), |
367 | } |
368 | } |
369 | |
370 | fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Error> |
371 | where |
372 | V: Visitor<'de>, |
373 | { |
374 | match self { |
375 | Value::Null => visitor.visit_unit(), |
376 | _ => Err(self.invalid_type(&visitor)), |
377 | } |
378 | } |
379 | |
380 | fn deserialize_unit_struct<V>(self, _name: &'static str, visitor: V) -> Result<V::Value, Error> |
381 | where |
382 | V: Visitor<'de>, |
383 | { |
384 | self.deserialize_unit(visitor) |
385 | } |
386 | |
387 | fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Error> |
388 | where |
389 | V: Visitor<'de>, |
390 | { |
391 | match self { |
392 | Value::Array(v) => visit_array(v, visitor), |
393 | _ => Err(self.invalid_type(&visitor)), |
394 | } |
395 | } |
396 | |
397 | fn deserialize_tuple<V>(self, _len: usize, visitor: V) -> Result<V::Value, Error> |
398 | where |
399 | V: Visitor<'de>, |
400 | { |
401 | self.deserialize_seq(visitor) |
402 | } |
403 | |
404 | fn deserialize_tuple_struct<V>( |
405 | self, |
406 | _name: &'static str, |
407 | _len: usize, |
408 | visitor: V, |
409 | ) -> Result<V::Value, Error> |
410 | where |
411 | V: Visitor<'de>, |
412 | { |
413 | self.deserialize_seq(visitor) |
414 | } |
415 | |
416 | fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Error> |
417 | where |
418 | V: Visitor<'de>, |
419 | { |
420 | match self { |
421 | Value::Object(v) => visit_object(v, visitor), |
422 | _ => Err(self.invalid_type(&visitor)), |
423 | } |
424 | } |
425 | |
426 | fn deserialize_struct<V>( |
427 | self, |
428 | _name: &'static str, |
429 | _fields: &'static [&'static str], |
430 | visitor: V, |
431 | ) -> Result<V::Value, Error> |
432 | where |
433 | V: Visitor<'de>, |
434 | { |
435 | match self { |
436 | Value::Array(v) => visit_array(v, visitor), |
437 | Value::Object(v) => visit_object(v, visitor), |
438 | _ => Err(self.invalid_type(&visitor)), |
439 | } |
440 | } |
441 | |
442 | fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value, Error> |
443 | where |
444 | V: Visitor<'de>, |
445 | { |
446 | self.deserialize_string(visitor) |
447 | } |
448 | |
449 | fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value, Error> |
450 | where |
451 | V: Visitor<'de>, |
452 | { |
453 | drop(self); |
454 | visitor.visit_unit() |
455 | } |
456 | } |
457 | |
458 | struct EnumDeserializer { |
459 | variant: String, |
460 | value: Option<Value>, |
461 | } |
462 | |
463 | impl<'de> EnumAccess<'de> for EnumDeserializer { |
464 | type Error = Error; |
465 | type Variant = VariantDeserializer; |
466 | |
467 | fn variant_seed<V>(self, seed: V) -> Result<(V::Value, VariantDeserializer), Error> |
468 | where |
469 | V: DeserializeSeed<'de>, |
470 | { |
471 | let variant = self.variant.into_deserializer(); |
472 | let visitor = VariantDeserializer { value: self.value }; |
473 | seed.deserialize(variant).map(|v| (v, visitor)) |
474 | } |
475 | } |
476 | |
477 | impl<'de> IntoDeserializer<'de, Error> for Value { |
478 | type Deserializer = Self; |
479 | |
480 | fn into_deserializer(self) -> Self::Deserializer { |
481 | self |
482 | } |
483 | } |
484 | |
485 | impl<'de> IntoDeserializer<'de, Error> for &'de Value { |
486 | type Deserializer = Self; |
487 | |
488 | fn into_deserializer(self) -> Self::Deserializer { |
489 | self |
490 | } |
491 | } |
492 | |
493 | struct VariantDeserializer { |
494 | value: Option<Value>, |
495 | } |
496 | |
497 | impl<'de> VariantAccess<'de> for VariantDeserializer { |
498 | type Error = Error; |
499 | |
500 | fn unit_variant(self) -> Result<(), Error> { |
501 | match self.value { |
502 | Some(value) => Deserialize::deserialize(value), |
503 | None => Ok(()), |
504 | } |
505 | } |
506 | |
507 | fn newtype_variant_seed<T>(self, seed: T) -> Result<T::Value, Error> |
508 | where |
509 | T: DeserializeSeed<'de>, |
510 | { |
511 | match self.value { |
512 | Some(value) => seed.deserialize(value), |
513 | None => Err(serde::de::Error::invalid_type( |
514 | Unexpected::UnitVariant, |
515 | &"newtype variant" , |
516 | )), |
517 | } |
518 | } |
519 | |
520 | fn tuple_variant<V>(self, _len: usize, visitor: V) -> Result<V::Value, Error> |
521 | where |
522 | V: Visitor<'de>, |
523 | { |
524 | match self.value { |
525 | Some(Value::Array(v)) => { |
526 | if v.is_empty() { |
527 | visitor.visit_unit() |
528 | } else { |
529 | visit_array(v, visitor) |
530 | } |
531 | } |
532 | Some(other) => Err(serde::de::Error::invalid_type( |
533 | other.unexpected(), |
534 | &"tuple variant" , |
535 | )), |
536 | None => Err(serde::de::Error::invalid_type( |
537 | Unexpected::UnitVariant, |
538 | &"tuple variant" , |
539 | )), |
540 | } |
541 | } |
542 | |
543 | fn struct_variant<V>( |
544 | self, |
545 | _fields: &'static [&'static str], |
546 | visitor: V, |
547 | ) -> Result<V::Value, Error> |
548 | where |
549 | V: Visitor<'de>, |
550 | { |
551 | match self.value { |
552 | Some(Value::Object(v)) => visit_object(v, visitor), |
553 | Some(other) => Err(serde::de::Error::invalid_type( |
554 | other.unexpected(), |
555 | &"struct variant" , |
556 | )), |
557 | None => Err(serde::de::Error::invalid_type( |
558 | Unexpected::UnitVariant, |
559 | &"struct variant" , |
560 | )), |
561 | } |
562 | } |
563 | } |
564 | |
565 | struct SeqDeserializer { |
566 | iter: vec::IntoIter<Value>, |
567 | } |
568 | |
569 | impl SeqDeserializer { |
570 | fn new(vec: Vec<Value>) -> Self { |
571 | SeqDeserializer { |
572 | iter: vec.into_iter(), |
573 | } |
574 | } |
575 | } |
576 | |
577 | impl<'de> SeqAccess<'de> for SeqDeserializer { |
578 | type Error = Error; |
579 | |
580 | fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Error> |
581 | where |
582 | T: DeserializeSeed<'de>, |
583 | { |
584 | match self.iter.next() { |
585 | Some(value) => seed.deserialize(value).map(Some), |
586 | None => Ok(None), |
587 | } |
588 | } |
589 | |
590 | fn size_hint(&self) -> Option<usize> { |
591 | match self.iter.size_hint() { |
592 | (lower, Some(upper)) if lower == upper => Some(upper), |
593 | _ => None, |
594 | } |
595 | } |
596 | } |
597 | |
598 | struct MapDeserializer { |
599 | iter: <Map<String, Value> as IntoIterator>::IntoIter, |
600 | value: Option<Value>, |
601 | } |
602 | |
603 | impl MapDeserializer { |
604 | fn new(map: Map<String, Value>) -> Self { |
605 | MapDeserializer { |
606 | iter: map.into_iter(), |
607 | value: None, |
608 | } |
609 | } |
610 | } |
611 | |
612 | impl<'de> MapAccess<'de> for MapDeserializer { |
613 | type Error = Error; |
614 | |
615 | fn next_key_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Error> |
616 | where |
617 | T: DeserializeSeed<'de>, |
618 | { |
619 | match self.iter.next() { |
620 | Some((key, value)) => { |
621 | self.value = Some(value); |
622 | let key_de = MapKeyDeserializer { |
623 | key: Cow::Owned(key), |
624 | }; |
625 | seed.deserialize(key_de).map(Some) |
626 | } |
627 | None => Ok(None), |
628 | } |
629 | } |
630 | |
631 | fn next_value_seed<T>(&mut self, seed: T) -> Result<T::Value, Error> |
632 | where |
633 | T: DeserializeSeed<'de>, |
634 | { |
635 | match self.value.take() { |
636 | Some(value) => seed.deserialize(value), |
637 | None => Err(serde::de::Error::custom("value is missing" )), |
638 | } |
639 | } |
640 | |
641 | fn size_hint(&self) -> Option<usize> { |
642 | match self.iter.size_hint() { |
643 | (lower, Some(upper)) if lower == upper => Some(upper), |
644 | _ => None, |
645 | } |
646 | } |
647 | } |
648 | |
649 | macro_rules! deserialize_value_ref_number { |
650 | ($method:ident) => { |
651 | #[cfg(not(feature = "arbitrary_precision" ))] |
652 | fn $method<V>(self, visitor: V) -> Result<V::Value, Error> |
653 | where |
654 | V: Visitor<'de>, |
655 | { |
656 | match self { |
657 | Value::Number(n) => n.deserialize_any(visitor), |
658 | _ => Err(self.invalid_type(&visitor)), |
659 | } |
660 | } |
661 | |
662 | #[cfg(feature = "arbitrary_precision" )] |
663 | fn $method<V>(self, visitor: V) -> Result<V::Value, Error> |
664 | where |
665 | V: Visitor<'de>, |
666 | { |
667 | match self { |
668 | Value::Number(n) => n.$method(visitor), |
669 | _ => self.deserialize_any(visitor), |
670 | } |
671 | } |
672 | }; |
673 | } |
674 | |
675 | fn visit_array_ref<'de, V>(array: &'de [Value], visitor: V) -> Result<V::Value, Error> |
676 | where |
677 | V: Visitor<'de>, |
678 | { |
679 | let len = array.len(); |
680 | let mut deserializer = SeqRefDeserializer::new(array); |
681 | let seq = tri!(visitor.visit_seq(&mut deserializer)); |
682 | let remaining = deserializer.iter.len(); |
683 | if remaining == 0 { |
684 | Ok(seq) |
685 | } else { |
686 | Err(serde::de::Error::invalid_length( |
687 | len, |
688 | &"fewer elements in array" , |
689 | )) |
690 | } |
691 | } |
692 | |
693 | fn visit_object_ref<'de, V>(object: &'de Map<String, Value>, visitor: V) -> Result<V::Value, Error> |
694 | where |
695 | V: Visitor<'de>, |
696 | { |
697 | let len = object.len(); |
698 | let mut deserializer = MapRefDeserializer::new(object); |
699 | let map = tri!(visitor.visit_map(&mut deserializer)); |
700 | let remaining = deserializer.iter.len(); |
701 | if remaining == 0 { |
702 | Ok(map) |
703 | } else { |
704 | Err(serde::de::Error::invalid_length( |
705 | len, |
706 | &"fewer elements in map" , |
707 | )) |
708 | } |
709 | } |
710 | |
711 | impl<'de> serde::Deserializer<'de> for &'de Value { |
712 | type Error = Error; |
713 | |
714 | fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Error> |
715 | where |
716 | V: Visitor<'de>, |
717 | { |
718 | match self { |
719 | Value::Null => visitor.visit_unit(), |
720 | Value::Bool(v) => visitor.visit_bool(*v), |
721 | Value::Number(n) => n.deserialize_any(visitor), |
722 | Value::String(v) => visitor.visit_borrowed_str(v), |
723 | Value::Array(v) => visit_array_ref(v, visitor), |
724 | Value::Object(v) => visit_object_ref(v, visitor), |
725 | } |
726 | } |
727 | |
728 | deserialize_value_ref_number!(deserialize_i8); |
729 | deserialize_value_ref_number!(deserialize_i16); |
730 | deserialize_value_ref_number!(deserialize_i32); |
731 | deserialize_value_ref_number!(deserialize_i64); |
732 | deserialize_number!(deserialize_i128); |
733 | deserialize_value_ref_number!(deserialize_u8); |
734 | deserialize_value_ref_number!(deserialize_u16); |
735 | deserialize_value_ref_number!(deserialize_u32); |
736 | deserialize_value_ref_number!(deserialize_u64); |
737 | deserialize_number!(deserialize_u128); |
738 | deserialize_value_ref_number!(deserialize_f32); |
739 | deserialize_value_ref_number!(deserialize_f64); |
740 | |
741 | fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Error> |
742 | where |
743 | V: Visitor<'de>, |
744 | { |
745 | match *self { |
746 | Value::Null => visitor.visit_none(), |
747 | _ => visitor.visit_some(self), |
748 | } |
749 | } |
750 | |
751 | fn deserialize_enum<V>( |
752 | self, |
753 | _name: &str, |
754 | _variants: &'static [&'static str], |
755 | visitor: V, |
756 | ) -> Result<V::Value, Error> |
757 | where |
758 | V: Visitor<'de>, |
759 | { |
760 | let (variant, value) = match self { |
761 | Value::Object(value) => { |
762 | let mut iter = value.into_iter(); |
763 | let (variant, value) = match iter.next() { |
764 | Some(v) => v, |
765 | None => { |
766 | return Err(serde::de::Error::invalid_value( |
767 | Unexpected::Map, |
768 | &"map with a single key" , |
769 | )); |
770 | } |
771 | }; |
772 | // enums are encoded in json as maps with a single key:value pair |
773 | if iter.next().is_some() { |
774 | return Err(serde::de::Error::invalid_value( |
775 | Unexpected::Map, |
776 | &"map with a single key" , |
777 | )); |
778 | } |
779 | (variant, Some(value)) |
780 | } |
781 | Value::String(variant) => (variant, None), |
782 | other => { |
783 | return Err(serde::de::Error::invalid_type( |
784 | other.unexpected(), |
785 | &"string or map" , |
786 | )); |
787 | } |
788 | }; |
789 | |
790 | visitor.visit_enum(EnumRefDeserializer { variant, value }) |
791 | } |
792 | |
793 | #[inline ] |
794 | fn deserialize_newtype_struct<V>( |
795 | self, |
796 | name: &'static str, |
797 | visitor: V, |
798 | ) -> Result<V::Value, Error> |
799 | where |
800 | V: Visitor<'de>, |
801 | { |
802 | #[cfg (feature = "raw_value" )] |
803 | { |
804 | if name == crate::raw::TOKEN { |
805 | return visitor.visit_map(crate::raw::OwnedRawDeserializer { |
806 | raw_value: Some(self.to_string()), |
807 | }); |
808 | } |
809 | } |
810 | |
811 | let _ = name; |
812 | visitor.visit_newtype_struct(self) |
813 | } |
814 | |
815 | fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, Error> |
816 | where |
817 | V: Visitor<'de>, |
818 | { |
819 | match *self { |
820 | Value::Bool(v) => visitor.visit_bool(v), |
821 | _ => Err(self.invalid_type(&visitor)), |
822 | } |
823 | } |
824 | |
825 | fn deserialize_char<V>(self, visitor: V) -> Result<V::Value, Error> |
826 | where |
827 | V: Visitor<'de>, |
828 | { |
829 | self.deserialize_str(visitor) |
830 | } |
831 | |
832 | fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, Error> |
833 | where |
834 | V: Visitor<'de>, |
835 | { |
836 | match self { |
837 | Value::String(v) => visitor.visit_borrowed_str(v), |
838 | _ => Err(self.invalid_type(&visitor)), |
839 | } |
840 | } |
841 | |
842 | fn deserialize_string<V>(self, visitor: V) -> Result<V::Value, Error> |
843 | where |
844 | V: Visitor<'de>, |
845 | { |
846 | self.deserialize_str(visitor) |
847 | } |
848 | |
849 | fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value, Error> |
850 | where |
851 | V: Visitor<'de>, |
852 | { |
853 | match self { |
854 | Value::String(v) => visitor.visit_borrowed_str(v), |
855 | Value::Array(v) => visit_array_ref(v, visitor), |
856 | _ => Err(self.invalid_type(&visitor)), |
857 | } |
858 | } |
859 | |
860 | fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value, Error> |
861 | where |
862 | V: Visitor<'de>, |
863 | { |
864 | self.deserialize_bytes(visitor) |
865 | } |
866 | |
867 | fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Error> |
868 | where |
869 | V: Visitor<'de>, |
870 | { |
871 | match *self { |
872 | Value::Null => visitor.visit_unit(), |
873 | _ => Err(self.invalid_type(&visitor)), |
874 | } |
875 | } |
876 | |
877 | fn deserialize_unit_struct<V>(self, _name: &'static str, visitor: V) -> Result<V::Value, Error> |
878 | where |
879 | V: Visitor<'de>, |
880 | { |
881 | self.deserialize_unit(visitor) |
882 | } |
883 | |
884 | fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Error> |
885 | where |
886 | V: Visitor<'de>, |
887 | { |
888 | match self { |
889 | Value::Array(v) => visit_array_ref(v, visitor), |
890 | _ => Err(self.invalid_type(&visitor)), |
891 | } |
892 | } |
893 | |
894 | fn deserialize_tuple<V>(self, _len: usize, visitor: V) -> Result<V::Value, Error> |
895 | where |
896 | V: Visitor<'de>, |
897 | { |
898 | self.deserialize_seq(visitor) |
899 | } |
900 | |
901 | fn deserialize_tuple_struct<V>( |
902 | self, |
903 | _name: &'static str, |
904 | _len: usize, |
905 | visitor: V, |
906 | ) -> Result<V::Value, Error> |
907 | where |
908 | V: Visitor<'de>, |
909 | { |
910 | self.deserialize_seq(visitor) |
911 | } |
912 | |
913 | fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Error> |
914 | where |
915 | V: Visitor<'de>, |
916 | { |
917 | match self { |
918 | Value::Object(v) => visit_object_ref(v, visitor), |
919 | _ => Err(self.invalid_type(&visitor)), |
920 | } |
921 | } |
922 | |
923 | fn deserialize_struct<V>( |
924 | self, |
925 | _name: &'static str, |
926 | _fields: &'static [&'static str], |
927 | visitor: V, |
928 | ) -> Result<V::Value, Error> |
929 | where |
930 | V: Visitor<'de>, |
931 | { |
932 | match self { |
933 | Value::Array(v) => visit_array_ref(v, visitor), |
934 | Value::Object(v) => visit_object_ref(v, visitor), |
935 | _ => Err(self.invalid_type(&visitor)), |
936 | } |
937 | } |
938 | |
939 | fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value, Error> |
940 | where |
941 | V: Visitor<'de>, |
942 | { |
943 | self.deserialize_str(visitor) |
944 | } |
945 | |
946 | fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value, Error> |
947 | where |
948 | V: Visitor<'de>, |
949 | { |
950 | visitor.visit_unit() |
951 | } |
952 | } |
953 | |
954 | struct EnumRefDeserializer<'de> { |
955 | variant: &'de str, |
956 | value: Option<&'de Value>, |
957 | } |
958 | |
959 | impl<'de> EnumAccess<'de> for EnumRefDeserializer<'de> { |
960 | type Error = Error; |
961 | type Variant = VariantRefDeserializer<'de>; |
962 | |
963 | fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self::Variant), Error> |
964 | where |
965 | V: DeserializeSeed<'de>, |
966 | { |
967 | let variant = self.variant.into_deserializer(); |
968 | let visitor = VariantRefDeserializer { value: self.value }; |
969 | seed.deserialize(variant).map(|v| (v, visitor)) |
970 | } |
971 | } |
972 | |
973 | struct VariantRefDeserializer<'de> { |
974 | value: Option<&'de Value>, |
975 | } |
976 | |
977 | impl<'de> VariantAccess<'de> for VariantRefDeserializer<'de> { |
978 | type Error = Error; |
979 | |
980 | fn unit_variant(self) -> Result<(), Error> { |
981 | match self.value { |
982 | Some(value) => Deserialize::deserialize(value), |
983 | None => Ok(()), |
984 | } |
985 | } |
986 | |
987 | fn newtype_variant_seed<T>(self, seed: T) -> Result<T::Value, Error> |
988 | where |
989 | T: DeserializeSeed<'de>, |
990 | { |
991 | match self.value { |
992 | Some(value) => seed.deserialize(value), |
993 | None => Err(serde::de::Error::invalid_type( |
994 | Unexpected::UnitVariant, |
995 | &"newtype variant" , |
996 | )), |
997 | } |
998 | } |
999 | |
1000 | fn tuple_variant<V>(self, _len: usize, visitor: V) -> Result<V::Value, Error> |
1001 | where |
1002 | V: Visitor<'de>, |
1003 | { |
1004 | match self.value { |
1005 | Some(Value::Array(v)) => { |
1006 | if v.is_empty() { |
1007 | visitor.visit_unit() |
1008 | } else { |
1009 | visit_array_ref(v, visitor) |
1010 | } |
1011 | } |
1012 | Some(other) => Err(serde::de::Error::invalid_type( |
1013 | other.unexpected(), |
1014 | &"tuple variant" , |
1015 | )), |
1016 | None => Err(serde::de::Error::invalid_type( |
1017 | Unexpected::UnitVariant, |
1018 | &"tuple variant" , |
1019 | )), |
1020 | } |
1021 | } |
1022 | |
1023 | fn struct_variant<V>( |
1024 | self, |
1025 | _fields: &'static [&'static str], |
1026 | visitor: V, |
1027 | ) -> Result<V::Value, Error> |
1028 | where |
1029 | V: Visitor<'de>, |
1030 | { |
1031 | match self.value { |
1032 | Some(Value::Object(v)) => visit_object_ref(v, visitor), |
1033 | Some(other) => Err(serde::de::Error::invalid_type( |
1034 | other.unexpected(), |
1035 | &"struct variant" , |
1036 | )), |
1037 | None => Err(serde::de::Error::invalid_type( |
1038 | Unexpected::UnitVariant, |
1039 | &"struct variant" , |
1040 | )), |
1041 | } |
1042 | } |
1043 | } |
1044 | |
1045 | struct SeqRefDeserializer<'de> { |
1046 | iter: slice::Iter<'de, Value>, |
1047 | } |
1048 | |
1049 | impl<'de> SeqRefDeserializer<'de> { |
1050 | fn new(slice: &'de [Value]) -> Self { |
1051 | SeqRefDeserializer { iter: slice.iter() } |
1052 | } |
1053 | } |
1054 | |
1055 | impl<'de> SeqAccess<'de> for SeqRefDeserializer<'de> { |
1056 | type Error = Error; |
1057 | |
1058 | fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Error> |
1059 | where |
1060 | T: DeserializeSeed<'de>, |
1061 | { |
1062 | match self.iter.next() { |
1063 | Some(value) => seed.deserialize(value).map(Some), |
1064 | None => Ok(None), |
1065 | } |
1066 | } |
1067 | |
1068 | fn size_hint(&self) -> Option<usize> { |
1069 | match self.iter.size_hint() { |
1070 | (lower, Some(upper)) if lower == upper => Some(upper), |
1071 | _ => None, |
1072 | } |
1073 | } |
1074 | } |
1075 | |
1076 | struct MapRefDeserializer<'de> { |
1077 | iter: <&'de Map<String, Value> as IntoIterator>::IntoIter, |
1078 | value: Option<&'de Value>, |
1079 | } |
1080 | |
1081 | impl<'de> MapRefDeserializer<'de> { |
1082 | fn new(map: &'de Map<String, Value>) -> Self { |
1083 | MapRefDeserializer { |
1084 | iter: map.into_iter(), |
1085 | value: None, |
1086 | } |
1087 | } |
1088 | } |
1089 | |
1090 | impl<'de> MapAccess<'de> for MapRefDeserializer<'de> { |
1091 | type Error = Error; |
1092 | |
1093 | fn next_key_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Error> |
1094 | where |
1095 | T: DeserializeSeed<'de>, |
1096 | { |
1097 | match self.iter.next() { |
1098 | Some((key, value)) => { |
1099 | self.value = Some(value); |
1100 | let key_de = MapKeyDeserializer { |
1101 | key: Cow::Borrowed(&**key), |
1102 | }; |
1103 | seed.deserialize(key_de).map(Some) |
1104 | } |
1105 | None => Ok(None), |
1106 | } |
1107 | } |
1108 | |
1109 | fn next_value_seed<T>(&mut self, seed: T) -> Result<T::Value, Error> |
1110 | where |
1111 | T: DeserializeSeed<'de>, |
1112 | { |
1113 | match self.value.take() { |
1114 | Some(value) => seed.deserialize(value), |
1115 | None => Err(serde::de::Error::custom("value is missing" )), |
1116 | } |
1117 | } |
1118 | |
1119 | fn size_hint(&self) -> Option<usize> { |
1120 | match self.iter.size_hint() { |
1121 | (lower, Some(upper)) if lower == upper => Some(upper), |
1122 | _ => None, |
1123 | } |
1124 | } |
1125 | } |
1126 | |
1127 | struct MapKeyDeserializer<'de> { |
1128 | key: Cow<'de, str>, |
1129 | } |
1130 | |
1131 | macro_rules! deserialize_numeric_key { |
1132 | ($method:ident) => { |
1133 | deserialize_numeric_key!($method, deserialize_number); |
1134 | }; |
1135 | |
1136 | ($method:ident, $using:ident) => { |
1137 | fn $method<V>(self, visitor: V) -> Result<V::Value, Error> |
1138 | where |
1139 | V: Visitor<'de>, |
1140 | { |
1141 | let mut de = crate::Deserializer::from_str(&self.key); |
1142 | |
1143 | match tri!(de.peek()) { |
1144 | Some(b'0' ..=b'9' | b'-' ) => {} |
1145 | _ => return Err(Error::syntax(ErrorCode::ExpectedNumericKey, 0, 0)), |
1146 | } |
1147 | |
1148 | let number = tri!(de.$using(visitor)); |
1149 | |
1150 | if tri!(de.peek()).is_some() { |
1151 | return Err(Error::syntax(ErrorCode::ExpectedNumericKey, 0, 0)); |
1152 | } |
1153 | |
1154 | Ok(number) |
1155 | } |
1156 | }; |
1157 | } |
1158 | |
1159 | impl<'de> serde::Deserializer<'de> for MapKeyDeserializer<'de> { |
1160 | type Error = Error; |
1161 | |
1162 | fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Error> |
1163 | where |
1164 | V: Visitor<'de>, |
1165 | { |
1166 | BorrowedCowStrDeserializer::new(self.key).deserialize_any(visitor) |
1167 | } |
1168 | |
1169 | deserialize_numeric_key!(deserialize_i8); |
1170 | deserialize_numeric_key!(deserialize_i16); |
1171 | deserialize_numeric_key!(deserialize_i32); |
1172 | deserialize_numeric_key!(deserialize_i64); |
1173 | deserialize_numeric_key!(deserialize_u8); |
1174 | deserialize_numeric_key!(deserialize_u16); |
1175 | deserialize_numeric_key!(deserialize_u32); |
1176 | deserialize_numeric_key!(deserialize_u64); |
1177 | #[cfg (not(feature = "float_roundtrip" ))] |
1178 | deserialize_numeric_key!(deserialize_f32); |
1179 | deserialize_numeric_key!(deserialize_f64); |
1180 | |
1181 | #[cfg (feature = "float_roundtrip" )] |
1182 | deserialize_numeric_key!(deserialize_f32, do_deserialize_f32); |
1183 | deserialize_numeric_key!(deserialize_i128, do_deserialize_i128); |
1184 | deserialize_numeric_key!(deserialize_u128, do_deserialize_u128); |
1185 | |
1186 | fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, Error> |
1187 | where |
1188 | V: Visitor<'de>, |
1189 | { |
1190 | if self.key == "true" { |
1191 | visitor.visit_bool(true) |
1192 | } else if self.key == "false" { |
1193 | visitor.visit_bool(false) |
1194 | } else { |
1195 | Err(serde::de::Error::invalid_type( |
1196 | Unexpected::Str(&self.key), |
1197 | &visitor, |
1198 | )) |
1199 | } |
1200 | } |
1201 | |
1202 | #[inline ] |
1203 | fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Error> |
1204 | where |
1205 | V: Visitor<'de>, |
1206 | { |
1207 | // Map keys cannot be null. |
1208 | visitor.visit_some(self) |
1209 | } |
1210 | |
1211 | #[inline ] |
1212 | fn deserialize_newtype_struct<V>( |
1213 | self, |
1214 | _name: &'static str, |
1215 | visitor: V, |
1216 | ) -> Result<V::Value, Error> |
1217 | where |
1218 | V: Visitor<'de>, |
1219 | { |
1220 | visitor.visit_newtype_struct(self) |
1221 | } |
1222 | |
1223 | fn deserialize_enum<V>( |
1224 | self, |
1225 | name: &'static str, |
1226 | variants: &'static [&'static str], |
1227 | visitor: V, |
1228 | ) -> Result<V::Value, Error> |
1229 | where |
1230 | V: Visitor<'de>, |
1231 | { |
1232 | self.key |
1233 | .into_deserializer() |
1234 | .deserialize_enum(name, variants, visitor) |
1235 | } |
1236 | |
1237 | forward_to_deserialize_any! { |
1238 | char str string bytes byte_buf unit unit_struct seq tuple tuple_struct |
1239 | map struct identifier ignored_any |
1240 | } |
1241 | } |
1242 | |
1243 | struct KeyClassifier; |
1244 | |
1245 | enum KeyClass { |
1246 | Map(String), |
1247 | #[cfg (feature = "arbitrary_precision" )] |
1248 | Number, |
1249 | #[cfg (feature = "raw_value" )] |
1250 | RawValue, |
1251 | } |
1252 | |
1253 | impl<'de> DeserializeSeed<'de> for KeyClassifier { |
1254 | type Value = KeyClass; |
1255 | |
1256 | fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error> |
1257 | where |
1258 | D: serde::Deserializer<'de>, |
1259 | { |
1260 | deserializer.deserialize_str(self) |
1261 | } |
1262 | } |
1263 | |
1264 | impl<'de> Visitor<'de> for KeyClassifier { |
1265 | type Value = KeyClass; |
1266 | |
1267 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
1268 | formatter.write_str("a string key" ) |
1269 | } |
1270 | |
1271 | fn visit_str<E>(self, s: &str) -> Result<Self::Value, E> |
1272 | where |
1273 | E: de::Error, |
1274 | { |
1275 | match s { |
1276 | #[cfg (feature = "arbitrary_precision" )] |
1277 | crate::number::TOKEN => Ok(KeyClass::Number), |
1278 | #[cfg (feature = "raw_value" )] |
1279 | crate::raw::TOKEN => Ok(KeyClass::RawValue), |
1280 | _ => Ok(KeyClass::Map(s.to_owned())), |
1281 | } |
1282 | } |
1283 | |
1284 | #[cfg (any(feature = "std" , feature = "alloc" ))] |
1285 | fn visit_string<E>(self, s: String) -> Result<Self::Value, E> |
1286 | where |
1287 | E: de::Error, |
1288 | { |
1289 | match s.as_str() { |
1290 | #[cfg (feature = "arbitrary_precision" )] |
1291 | crate::number::TOKEN => Ok(KeyClass::Number), |
1292 | #[cfg (feature = "raw_value" )] |
1293 | crate::raw::TOKEN => Ok(KeyClass::RawValue), |
1294 | _ => Ok(KeyClass::Map(s)), |
1295 | } |
1296 | } |
1297 | } |
1298 | |
1299 | impl Value { |
1300 | #[cold ] |
1301 | fn invalid_type<E>(&self, exp: &dyn Expected) -> E |
1302 | where |
1303 | E: serde::de::Error, |
1304 | { |
1305 | serde::de::Error::invalid_type(self.unexpected(), exp) |
1306 | } |
1307 | |
1308 | #[cold ] |
1309 | fn unexpected(&self) -> Unexpected { |
1310 | match self { |
1311 | Value::Null => Unexpected::Unit, |
1312 | Value::Bool(b) => Unexpected::Bool(*b), |
1313 | Value::Number(n) => n.unexpected(), |
1314 | Value::String(s) => Unexpected::Str(s), |
1315 | Value::Array(_) => Unexpected::Seq, |
1316 | Value::Object(_) => Unexpected::Map, |
1317 | } |
1318 | } |
1319 | } |
1320 | |
1321 | struct BorrowedCowStrDeserializer<'de> { |
1322 | value: Cow<'de, str>, |
1323 | } |
1324 | |
1325 | impl<'de> BorrowedCowStrDeserializer<'de> { |
1326 | fn new(value: Cow<'de, str>) -> Self { |
1327 | BorrowedCowStrDeserializer { value } |
1328 | } |
1329 | } |
1330 | |
1331 | impl<'de> de::Deserializer<'de> for BorrowedCowStrDeserializer<'de> { |
1332 | type Error = Error; |
1333 | |
1334 | fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Error> |
1335 | where |
1336 | V: de::Visitor<'de>, |
1337 | { |
1338 | match self.value { |
1339 | Cow::Borrowed(string) => visitor.visit_borrowed_str(string), |
1340 | #[cfg (any(feature = "std" , feature = "alloc" ))] |
1341 | Cow::Owned(string) => visitor.visit_string(string), |
1342 | } |
1343 | } |
1344 | |
1345 | fn deserialize_enum<V>( |
1346 | self, |
1347 | _name: &str, |
1348 | _variants: &'static [&'static str], |
1349 | visitor: V, |
1350 | ) -> Result<V::Value, Error> |
1351 | where |
1352 | V: de::Visitor<'de>, |
1353 | { |
1354 | visitor.visit_enum(self) |
1355 | } |
1356 | |
1357 | forward_to_deserialize_any! { |
1358 | bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string |
1359 | bytes byte_buf option unit unit_struct newtype_struct seq tuple |
1360 | tuple_struct map struct identifier ignored_any |
1361 | } |
1362 | } |
1363 | |
1364 | impl<'de> de::EnumAccess<'de> for BorrowedCowStrDeserializer<'de> { |
1365 | type Error = Error; |
1366 | type Variant = UnitOnly; |
1367 | |
1368 | fn variant_seed<T>(self, seed: T) -> Result<(T::Value, Self::Variant), Error> |
1369 | where |
1370 | T: de::DeserializeSeed<'de>, |
1371 | { |
1372 | let value = tri!(seed.deserialize(self)); |
1373 | Ok((value, UnitOnly)) |
1374 | } |
1375 | } |
1376 | |
1377 | struct UnitOnly; |
1378 | |
1379 | impl<'de> de::VariantAccess<'de> for UnitOnly { |
1380 | type Error = Error; |
1381 | |
1382 | fn unit_variant(self) -> Result<(), Error> { |
1383 | Ok(()) |
1384 | } |
1385 | |
1386 | fn newtype_variant_seed<T>(self, _seed: T) -> Result<T::Value, Error> |
1387 | where |
1388 | T: de::DeserializeSeed<'de>, |
1389 | { |
1390 | Err(de::Error::invalid_type( |
1391 | Unexpected::UnitVariant, |
1392 | &"newtype variant" , |
1393 | )) |
1394 | } |
1395 | |
1396 | fn tuple_variant<V>(self, _len: usize, _visitor: V) -> Result<V::Value, Error> |
1397 | where |
1398 | V: de::Visitor<'de>, |
1399 | { |
1400 | Err(de::Error::invalid_type( |
1401 | Unexpected::UnitVariant, |
1402 | &"tuple variant" , |
1403 | )) |
1404 | } |
1405 | |
1406 | fn struct_variant<V>( |
1407 | self, |
1408 | _fields: &'static [&'static str], |
1409 | _visitor: V, |
1410 | ) -> Result<V::Value, Error> |
1411 | where |
1412 | V: de::Visitor<'de>, |
1413 | { |
1414 | Err(de::Error::invalid_type( |
1415 | Unexpected::UnitVariant, |
1416 | &"struct variant" , |
1417 | )) |
1418 | } |
1419 | } |
1420 | |