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