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