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 / raw.rs
| 1 | use crate::error::Error; |
| 2 | use alloc::borrow::ToOwned; |
| 3 | use alloc::boxed::Box; |
| 4 | use alloc::string::String; |
| 5 | use core::fmt::{self, Debug, Display}; |
| 6 | use core::mem; |
| 7 | use serde::de::value::BorrowedStrDeserializer; |
| 8 | use serde::de::{ |
| 9 | self, Deserialize, DeserializeSeed, Deserializer, IntoDeserializer, MapAccess, Unexpected, |
| 10 | Visitor, |
| 11 | }; |
| 12 | use serde::forward_to_deserialize_any; |
| 13 | use serde::ser::{Serialize, SerializeStruct, Serializer}; |
| 14 | |
| 15 | /// Reference to a range of bytes encompassing a single valid JSON value in the |
| 16 | /// input data. |
| 17 | /// |
| 18 | /// A `RawValue` can be used to defer parsing parts of a payload until later, |
| 19 | /// or to avoid parsing it at all in the case that part of the payload just |
| 20 | /// needs to be transferred verbatim into a different output object. |
| 21 | /// |
| 22 | /// When serializing, a value of this type will retain its original formatting |
| 23 | /// and will not be minified or pretty-printed. |
| 24 | /// |
| 25 | /// # Note |
| 26 | /// |
| 27 | /// `RawValue` is only available if serde\_json is built with the `"raw_value"` |
| 28 | /// feature. |
| 29 | /// |
| 30 | /// ```toml |
| 31 | /// [dependencies] |
| 32 | /// serde_json = { version = "1.0", features = ["raw_value"] } |
| 33 | /// ``` |
| 34 | /// |
| 35 | /// # Example |
| 36 | /// |
| 37 | /// ``` |
| 38 | /// use serde::{Deserialize, Serialize}; |
| 39 | /// use serde_json::{Result, value::RawValue}; |
| 40 | /// |
| 41 | /// #[derive(Deserialize)] |
| 42 | /// struct Input<'a> { |
| 43 | /// code: u32, |
| 44 | /// #[serde(borrow)] |
| 45 | /// payload: &'a RawValue, |
| 46 | /// } |
| 47 | /// |
| 48 | /// #[derive(Serialize)] |
| 49 | /// struct Output<'a> { |
| 50 | /// info: (u32, &'a RawValue), |
| 51 | /// } |
| 52 | /// |
| 53 | /// // Efficiently rearrange JSON input containing separate "code" and "payload" |
| 54 | /// // keys into a single "info" key holding an array of code and payload. |
| 55 | /// // |
| 56 | /// // This could be done equivalently using serde_json::Value as the type for |
| 57 | /// // payload, but &RawValue will perform better because it does not require |
| 58 | /// // memory allocation. The correct range of bytes is borrowed from the input |
| 59 | /// // data and pasted verbatim into the output. |
| 60 | /// fn rearrange(input: &str) -> Result<String> { |
| 61 | /// let input: Input = serde_json::from_str(input)?; |
| 62 | /// |
| 63 | /// let output = Output { |
| 64 | /// info: (input.code, input.payload), |
| 65 | /// }; |
| 66 | /// |
| 67 | /// serde_json::to_string(&output) |
| 68 | /// } |
| 69 | /// |
| 70 | /// fn main() -> Result<()> { |
| 71 | /// let out = rearrange(r#" {"code": 200, "payload": {}} "#)?; |
| 72 | /// |
| 73 | /// assert_eq!(out, r#"{"info":[200,{}]}"#); |
| 74 | /// |
| 75 | /// Ok(()) |
| 76 | /// } |
| 77 | /// ``` |
| 78 | /// |
| 79 | /// # Ownership |
| 80 | /// |
| 81 | /// The typical usage of `RawValue` will be in the borrowed form: |
| 82 | /// |
| 83 | /// ``` |
| 84 | /// # use serde::Deserialize; |
| 85 | /// # use serde_json::value::RawValue; |
| 86 | /// # |
| 87 | /// #[derive(Deserialize)] |
| 88 | /// struct SomeStruct<'a> { |
| 89 | /// #[serde(borrow)] |
| 90 | /// raw_value: &'a RawValue, |
| 91 | /// } |
| 92 | /// ``` |
| 93 | /// |
| 94 | /// The borrowed form is suitable when deserializing through |
| 95 | /// [`serde_json::from_str`] and [`serde_json::from_slice`] which support |
| 96 | /// borrowing from the input data without memory allocation. |
| 97 | /// |
| 98 | /// When deserializing through [`serde_json::from_reader`] you will need to use |
| 99 | /// the boxed form of `RawValue` instead. This is almost as efficient but |
| 100 | /// involves buffering the raw value from the I/O stream into memory. |
| 101 | /// |
| 102 | /// [`serde_json::from_str`]: crate::from_str |
| 103 | /// [`serde_json::from_slice`]: crate::from_slice |
| 104 | /// [`serde_json::from_reader`]: crate::from_reader |
| 105 | /// |
| 106 | /// ``` |
| 107 | /// # use serde::Deserialize; |
| 108 | /// # use serde_json::value::RawValue; |
| 109 | /// # |
| 110 | /// #[derive(Deserialize)] |
| 111 | /// struct SomeStruct { |
| 112 | /// raw_value: Box<RawValue>, |
| 113 | /// } |
| 114 | /// ``` |
| 115 | #[cfg_attr(docsrs, doc(cfg(feature = "raw_value")))] |
| 116 | #[repr(transparent)] |
| 117 | pub struct RawValue { |
| 118 | json: str, |
| 119 | } |
| 120 | |
| 121 | impl RawValue { |
| 122 | const fn from_borrowed(json: &str) -> &Self { |
| 123 | unsafe { mem::transmute::<&str, &RawValue>(json) } |
| 124 | } |
| 125 | |
| 126 | fn from_owned(json: Box<str>) -> Box<Self> { |
| 127 | unsafe { mem::transmute::<Box<str>, Box<RawValue>>(json) } |
| 128 | } |
| 129 | |
| 130 | fn into_owned(raw_value: Box<Self>) -> Box<str> { |
| 131 | unsafe { mem::transmute::<Box<RawValue>, Box<str>>(raw_value) } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | impl Clone for Box<RawValue> { |
| 136 | fn clone(&self) -> Self { |
| 137 | (**self).to_owned() |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | impl ToOwned for RawValue { |
| 142 | type Owned = Box<RawValue>; |
| 143 | |
| 144 | fn to_owned(&self) -> Self::Owned { |
| 145 | RawValue::from_owned(self.json.to_owned().into_boxed_str()) |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | impl Default for Box<RawValue> { |
| 150 | fn default() -> Self { |
| 151 | RawValue::NULL.to_owned() |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | impl Debug for RawValue { |
| 156 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 157 | formatter |
| 158 | .debug_tuple("RawValue") |
| 159 | .field(&format_args!("{}", &self.json)) |
| 160 | .finish() |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | impl Display for RawValue { |
| 165 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 166 | f.write_str(&self.json) |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | impl RawValue { |
| 171 | /// A constant RawValue with the JSON value `null`. |
| 172 | pub const NULL: &'static RawValue = RawValue::from_borrowed("null"); |
| 173 | /// A constant RawValue with the JSON value `true`. |
| 174 | pub const TRUE: &'static RawValue = RawValue::from_borrowed("true"); |
| 175 | /// A constant RawValue with the JSON value `false`. |
| 176 | pub const FALSE: &'static RawValue = RawValue::from_borrowed("false"); |
| 177 | |
| 178 | /// Convert an owned `String` of JSON data to an owned `RawValue`. |
| 179 | /// |
| 180 | /// This function is equivalent to `serde_json::from_str::<Box<RawValue>>` |
| 181 | /// except that we avoid an allocation and memcpy if both of the following |
| 182 | /// are true: |
| 183 | /// |
| 184 | /// - the input has no leading or trailing whitespace, and |
| 185 | /// - the input has capacity equal to its length. |
| 186 | pub fn from_string(json: String) -> Result<Box<Self>, Error> { |
| 187 | let borrowed = tri!(crate::from_str::<&Self>(&json)); |
| 188 | if borrowed.json.len() < json.len() { |
| 189 | return Ok(borrowed.to_owned()); |
| 190 | } |
| 191 | Ok(Self::from_owned(json.into_boxed_str())) |
| 192 | } |
| 193 | |
| 194 | /// Convert an owned `String` of JSON data to an owned `RawValue` without |
| 195 | /// checking that it contains valid JSON. |
| 196 | /// |
| 197 | /// This is the unchecked counterpart of [`RawValue::from_string`], for |
| 198 | /// strings that are already known to be valid JSON, such as the output of |
| 199 | /// another JSON serializer. Unlike `from_string`, it does not re-parse the |
| 200 | /// string; the only cost is `String::into_boxed_str`. |
| 201 | /// |
| 202 | /// # Safety |
| 203 | /// |
| 204 | /// The string passed in must contain a single well-formed JSON value with |
| 205 | /// no leading or trailing whitespace. `RawValue` is written verbatim into |
| 206 | /// JSON output wherever it is embedded, and other code (including unsafe |
| 207 | /// code) is allowed to rely on `RawValue` upholding this invariant, in the |
| 208 | /// same way that unsafe code may rely on `str` containing valid UTF-8. |
| 209 | /// |
| 210 | /// In debug builds this contract is checked with a `debug_assert!` that |
| 211 | /// re-parses the input; the check is compiled out in release builds, so |
| 212 | /// only release performance matches the "no re-parse" guarantee above. |
| 213 | /// |
| 214 | /// # Example |
| 215 | /// |
| 216 | /// ``` |
| 217 | /// use serde_json::value::RawValue; |
| 218 | /// |
| 219 | /// let json = serde_json::to_string(&[1, 2, 3])?; |
| 220 | /// |
| 221 | /// // SAFETY: `json` was produced by serde_json's own serializer, so it is |
| 222 | /// // a single well-formed JSON value without surrounding whitespace. |
| 223 | /// let raw = unsafe { RawValue::from_string_unchecked(json) }; |
| 224 | /// |
| 225 | /// assert_eq!(raw.get(), "[1,2,3]"); |
| 226 | /// # Ok::<(), serde_json::Error>(()) |
| 227 | /// ``` |
| 228 | pub unsafe fn from_string_unchecked(json: String) -> Box<Self> { |
| 229 | debug_assert!( |
| 230 | crate::from_str::<&Self>(&json).is_ok_and(|v| v.json.len() == json.len()), |
| 231 | "from_string_unchecked: input is not a single well-formed JSON value \ |
| 232 | with no leading or trailing whitespace", |
| 233 | ); |
| 234 | Self::from_owned(json.into_boxed_str()) |
| 235 | } |
| 236 | |
| 237 | /// Access the JSON text underlying a raw value. |
| 238 | /// |
| 239 | /// # Example |
| 240 | /// |
| 241 | /// ``` |
| 242 | /// use serde::Deserialize; |
| 243 | /// use serde_json::{Result, value::RawValue}; |
| 244 | /// |
| 245 | /// #[derive(Deserialize)] |
| 246 | /// struct Response<'a> { |
| 247 | /// code: u32, |
| 248 | /// #[serde(borrow)] |
| 249 | /// payload: &'a RawValue, |
| 250 | /// } |
| 251 | /// |
| 252 | /// fn process(input: &str) -> Result<()> { |
| 253 | /// let response: Response = serde_json::from_str(input)?; |
| 254 | /// |
| 255 | /// let payload = response.payload.get(); |
| 256 | /// if payload.starts_with('{') { |
| 257 | /// // handle a payload which is a JSON map |
| 258 | /// } else { |
| 259 | /// // handle any other type |
| 260 | /// } |
| 261 | /// |
| 262 | /// Ok(()) |
| 263 | /// } |
| 264 | /// |
| 265 | /// fn main() -> Result<()> { |
| 266 | /// process(r#" {"code": 200, "payload": {}} "#)?; |
| 267 | /// Ok(()) |
| 268 | /// } |
| 269 | /// ``` |
| 270 | pub fn get(&self) -> &str { |
| 271 | &self.json |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | impl From<Box<RawValue>> for Box<str> { |
| 276 | fn from(raw_value: Box<RawValue>) -> Self { |
| 277 | RawValue::into_owned(raw_value) |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | /// Convert a `T` into a boxed `RawValue`. |
| 282 | /// |
| 283 | /// # Example |
| 284 | /// |
| 285 | /// ``` |
| 286 | /// // Upstream crate |
| 287 | /// # #[derive(Serialize)] |
| 288 | /// pub struct Thing { |
| 289 | /// foo: String, |
| 290 | /// bar: Option<String>, |
| 291 | /// extra_data: Box<RawValue>, |
| 292 | /// } |
| 293 | /// |
| 294 | /// // Local crate |
| 295 | /// use serde::Serialize; |
| 296 | /// use serde_json::value::{to_raw_value, RawValue}; |
| 297 | /// |
| 298 | /// #[derive(Serialize)] |
| 299 | /// struct MyExtraData { |
| 300 | /// a: u32, |
| 301 | /// b: u32, |
| 302 | /// } |
| 303 | /// |
| 304 | /// let my_thing = Thing { |
| 305 | /// foo: "FooVal".into(), |
| 306 | /// bar: None, |
| 307 | /// extra_data: to_raw_value(&MyExtraData { a: 1, b: 2 }).unwrap(), |
| 308 | /// }; |
| 309 | /// # assert_eq!( |
| 310 | /// # serde_json::to_value(my_thing).unwrap(), |
| 311 | /// # serde_json::json!({ |
| 312 | /// # "foo": "FooVal", |
| 313 | /// # "bar": null, |
| 314 | /// # "extra_data": { "a": 1, "b": 2 } |
| 315 | /// # }) |
| 316 | /// # ); |
| 317 | /// ``` |
| 318 | /// |
| 319 | /// # Errors |
| 320 | /// |
| 321 | /// This conversion can fail if `T`'s implementation of `Serialize` decides to |
| 322 | /// fail, or if `T` contains a map with non-string keys. |
| 323 | /// |
| 324 | /// ``` |
| 325 | /// use std::collections::BTreeMap; |
| 326 | /// |
| 327 | /// // The keys in this map are vectors, not strings. |
| 328 | /// let mut map = BTreeMap::new(); |
| 329 | /// map.insert(vec![32, 64], "x86"); |
| 330 | /// |
| 331 | /// println!("{}", serde_json::value::to_raw_value(&map).unwrap_err()); |
| 332 | /// ``` |
| 333 | #[cfg_attr(docsrs, doc(cfg(feature = "raw_value")))] |
| 334 | pub fn to_raw_value<T>(value: &T) -> Result<Box<RawValue>, Error> |
| 335 | where |
| 336 | T: ?Sized + Serialize, |
| 337 | { |
| 338 | let json_string = tri!(crate::to_string(value)); |
| 339 | Ok(RawValue::from_owned(json_string.into_boxed_str())) |
| 340 | } |
| 341 | |
| 342 | pub const TOKEN: &str = "$serde_json::private::RawValue"; |
| 343 | |
| 344 | impl Serialize for RawValue { |
| 345 | fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
| 346 | where |
| 347 | S: Serializer, |
| 348 | { |
| 349 | let mut s = tri!(serializer.serialize_struct(TOKEN, 1)); |
| 350 | tri!(s.serialize_field(TOKEN, &self.json)); |
| 351 | s.end() |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | impl<'de: 'a, 'a> Deserialize<'de> for &'a RawValue { |
| 356 | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 357 | where |
| 358 | D: Deserializer<'de>, |
| 359 | { |
| 360 | struct ReferenceVisitor; |
| 361 | |
| 362 | impl<'de> Visitor<'de> for ReferenceVisitor { |
| 363 | type Value = &'de RawValue; |
| 364 | |
| 365 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 366 | write!(formatter, "any valid JSON value") |
| 367 | } |
| 368 | |
| 369 | fn visit_map<V>(self, mut visitor: V) -> Result<Self::Value, V::Error> |
| 370 | where |
| 371 | V: MapAccess<'de>, |
| 372 | { |
| 373 | let value = tri!(visitor.next_key::<RawKey>()); |
| 374 | if value.is_none() { |
| 375 | return Err(de::Error::invalid_type(Unexpected::Map, &self)); |
| 376 | } |
| 377 | visitor.next_value_seed(ReferenceFromString) |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | deserializer.deserialize_newtype_struct(TOKEN, ReferenceVisitor) |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | impl<'de> Deserialize<'de> for Box<RawValue> { |
| 386 | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 387 | where |
| 388 | D: Deserializer<'de>, |
| 389 | { |
| 390 | struct BoxedVisitor; |
| 391 | |
| 392 | impl<'de> Visitor<'de> for BoxedVisitor { |
| 393 | type Value = Box<RawValue>; |
| 394 | |
| 395 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 396 | write!(formatter, "any valid JSON value") |
| 397 | } |
| 398 | |
| 399 | fn visit_map<V>(self, mut visitor: V) -> Result<Self::Value, V::Error> |
| 400 | where |
| 401 | V: MapAccess<'de>, |
| 402 | { |
| 403 | let value = tri!(visitor.next_key::<RawKey>()); |
| 404 | if value.is_none() { |
| 405 | return Err(de::Error::invalid_type(Unexpected::Map, &self)); |
| 406 | } |
| 407 | visitor.next_value_seed(BoxedFromString) |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | deserializer.deserialize_newtype_struct(TOKEN, BoxedVisitor) |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | struct RawKey; |
| 416 | |
| 417 | impl<'de> Deserialize<'de> for RawKey { |
| 418 | fn deserialize<D>(deserializer: D) -> Result<RawKey, D::Error> |
| 419 | where |
| 420 | D: Deserializer<'de>, |
| 421 | { |
| 422 | struct FieldVisitor; |
| 423 | |
| 424 | impl<'de> Visitor<'de> for FieldVisitor { |
| 425 | type Value = (); |
| 426 | |
| 427 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 428 | formatter.write_str("raw value") |
| 429 | } |
| 430 | |
| 431 | fn visit_str<E>(self, s: &str) -> Result<(), E> |
| 432 | where |
| 433 | E: de::Error, |
| 434 | { |
| 435 | if s == TOKEN { |
| 436 | Ok(()) |
| 437 | } else { |
| 438 | Err(de::Error::custom("unexpected raw value")) |
| 439 | } |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | tri!(deserializer.deserialize_identifier(FieldVisitor)); |
| 444 | Ok(RawKey) |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | pub struct ReferenceFromString; |
| 449 | |
| 450 | impl<'de> DeserializeSeed<'de> for ReferenceFromString { |
| 451 | type Value = &'de RawValue; |
| 452 | |
| 453 | fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error> |
| 454 | where |
| 455 | D: Deserializer<'de>, |
| 456 | { |
| 457 | deserializer.deserialize_str(self) |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | impl<'de> Visitor<'de> for ReferenceFromString { |
| 462 | type Value = &'de RawValue; |
| 463 | |
| 464 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 465 | formatter.write_str("raw value") |
| 466 | } |
| 467 | |
| 468 | fn visit_borrowed_str<E>(self, s: &'de str) -> Result<Self::Value, E> |
| 469 | where |
| 470 | E: de::Error, |
| 471 | { |
| 472 | Ok(RawValue::from_borrowed(s)) |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | pub struct BoxedFromString; |
| 477 | |
| 478 | impl<'de> DeserializeSeed<'de> for BoxedFromString { |
| 479 | type Value = Box<RawValue>; |
| 480 | |
| 481 | fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error> |
| 482 | where |
| 483 | D: Deserializer<'de>, |
| 484 | { |
| 485 | deserializer.deserialize_str(self) |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | impl<'de> Visitor<'de> for BoxedFromString { |
| 490 | type Value = Box<RawValue>; |
| 491 | |
| 492 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 493 | formatter.write_str("raw value") |
| 494 | } |
| 495 | |
| 496 | fn visit_str<E>(self, s: &str) -> Result<Self::Value, E> |
| 497 | where |
| 498 | E: de::Error, |
| 499 | { |
| 500 | Ok(RawValue::from_owned(s.to_owned().into_boxed_str())) |
| 501 | } |
| 502 | |
| 503 | #[cfg(any(feature = "std", feature = "alloc"))] |
| 504 | fn visit_string<E>(self, s: String) -> Result<Self::Value, E> |
| 505 | where |
| 506 | E: de::Error, |
| 507 | { |
| 508 | Ok(RawValue::from_owned(s.into_boxed_str())) |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | struct RawKeyDeserializer; |
| 513 | |
| 514 | impl<'de> Deserializer<'de> for RawKeyDeserializer { |
| 515 | type Error = Error; |
| 516 | |
| 517 | fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Error> |
| 518 | where |
| 519 | V: de::Visitor<'de>, |
| 520 | { |
| 521 | visitor.visit_borrowed_str(TOKEN) |
| 522 | } |
| 523 | |
| 524 | forward_to_deserialize_any! { |
| 525 | bool u8 u16 u32 u64 u128 i8 i16 i32 i64 i128 f32 f64 char str string seq |
| 526 | bytes byte_buf map struct option unit newtype_struct ignored_any |
| 527 | unit_struct tuple_struct tuple enum identifier |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | pub struct OwnedRawDeserializer { |
| 532 | pub raw_value: Option<String>, |
| 533 | } |
| 534 | |
| 535 | impl<'de> MapAccess<'de> for OwnedRawDeserializer { |
| 536 | type Error = Error; |
| 537 | |
| 538 | fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Error> |
| 539 | where |
| 540 | K: de::DeserializeSeed<'de>, |
| 541 | { |
| 542 | if self.raw_value.is_none() { |
| 543 | return Ok(None); |
| 544 | } |
| 545 | seed.deserialize(RawKeyDeserializer).map(Some) |
| 546 | } |
| 547 | |
| 548 | fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Error> |
| 549 | where |
| 550 | V: de::DeserializeSeed<'de>, |
| 551 | { |
| 552 | seed.deserialize(self.raw_value.take().unwrap().into_deserializer()) |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | pub struct BorrowedRawDeserializer<'de> { |
| 557 | pub raw_value: Option<&'de str>, |
| 558 | } |
| 559 | |
| 560 | impl<'de> MapAccess<'de> for BorrowedRawDeserializer<'de> { |
| 561 | type Error = Error; |
| 562 | |
| 563 | fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Error> |
| 564 | where |
| 565 | K: de::DeserializeSeed<'de>, |
| 566 | { |
| 567 | if self.raw_value.is_none() { |
| 568 | return Ok(None); |
| 569 | } |
| 570 | seed.deserialize(RawKeyDeserializer).map(Some) |
| 571 | } |
| 572 | |
| 573 | fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Error> |
| 574 | where |
| 575 | V: de::DeserializeSeed<'de>, |
| 576 | { |
| 577 | seed.deserialize(BorrowedStrDeserializer::new(self.raw_value.take().unwrap())) |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | impl<'de> IntoDeserializer<'de, Error> for &'de RawValue { |
| 582 | type Deserializer = &'de RawValue; |
| 583 | |
| 584 | fn into_deserializer(self) -> Self::Deserializer { |
| 585 | self |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | impl<'de> Deserializer<'de> for &'de RawValue { |
| 590 | type Error = Error; |
| 591 | |
| 592 | fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Error> |
| 593 | where |
| 594 | V: Visitor<'de>, |
| 595 | { |
| 596 | crate::Deserializer::from_str(&self.json).deserialize_any(visitor) |
| 597 | } |
| 598 | |
| 599 | fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, Error> |
| 600 | where |
| 601 | V: Visitor<'de>, |
| 602 | { |
| 603 | crate::Deserializer::from_str(&self.json).deserialize_bool(visitor) |
| 604 | } |
| 605 | |
| 606 | fn deserialize_i8<V>(self, visitor: V) -> Result<V::Value, Error> |
| 607 | where |
| 608 | V: Visitor<'de>, |
| 609 | { |
| 610 | crate::Deserializer::from_str(&self.json).deserialize_i8(visitor) |
| 611 | } |
| 612 | |
| 613 | fn deserialize_i16<V>(self, visitor: V) -> Result<V::Value, Error> |
| 614 | where |
| 615 | V: Visitor<'de>, |
| 616 | { |
| 617 | crate::Deserializer::from_str(&self.json).deserialize_i16(visitor) |
| 618 | } |
| 619 | |
| 620 | fn deserialize_i32<V>(self, visitor: V) -> Result<V::Value, Error> |
| 621 | where |
| 622 | V: Visitor<'de>, |
| 623 | { |
| 624 | crate::Deserializer::from_str(&self.json).deserialize_i32(visitor) |
| 625 | } |
| 626 | |
| 627 | fn deserialize_i64<V>(self, visitor: V) -> Result<V::Value, Error> |
| 628 | where |
| 629 | V: Visitor<'de>, |
| 630 | { |
| 631 | crate::Deserializer::from_str(&self.json).deserialize_i64(visitor) |
| 632 | } |
| 633 | |
| 634 | fn deserialize_i128<V>(self, visitor: V) -> Result<V::Value, Error> |
| 635 | where |
| 636 | V: Visitor<'de>, |
| 637 | { |
| 638 | crate::Deserializer::from_str(&self.json).deserialize_i128(visitor) |
| 639 | } |
| 640 | |
| 641 | fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value, Error> |
| 642 | where |
| 643 | V: Visitor<'de>, |
| 644 | { |
| 645 | crate::Deserializer::from_str(&self.json).deserialize_u8(visitor) |
| 646 | } |
| 647 | |
| 648 | fn deserialize_u16<V>(self, visitor: V) -> Result<V::Value, Error> |
| 649 | where |
| 650 | V: Visitor<'de>, |
| 651 | { |
| 652 | crate::Deserializer::from_str(&self.json).deserialize_u16(visitor) |
| 653 | } |
| 654 | |
| 655 | fn deserialize_u32<V>(self, visitor: V) -> Result<V::Value, Error> |
| 656 | where |
| 657 | V: Visitor<'de>, |
| 658 | { |
| 659 | crate::Deserializer::from_str(&self.json).deserialize_u32(visitor) |
| 660 | } |
| 661 | |
| 662 | fn deserialize_u64<V>(self, visitor: V) -> Result<V::Value, Error> |
| 663 | where |
| 664 | V: Visitor<'de>, |
| 665 | { |
| 666 | crate::Deserializer::from_str(&self.json).deserialize_u64(visitor) |
| 667 | } |
| 668 | |
| 669 | fn deserialize_u128<V>(self, visitor: V) -> Result<V::Value, Error> |
| 670 | where |
| 671 | V: Visitor<'de>, |
| 672 | { |
| 673 | crate::Deserializer::from_str(&self.json).deserialize_u128(visitor) |
| 674 | } |
| 675 | |
| 676 | fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value, Error> |
| 677 | where |
| 678 | V: Visitor<'de>, |
| 679 | { |
| 680 | crate::Deserializer::from_str(&self.json).deserialize_f32(visitor) |
| 681 | } |
| 682 | |
| 683 | fn deserialize_f64<V>(self, visitor: V) -> Result<V::Value, Error> |
| 684 | where |
| 685 | V: Visitor<'de>, |
| 686 | { |
| 687 | crate::Deserializer::from_str(&self.json).deserialize_f64(visitor) |
| 688 | } |
| 689 | |
| 690 | fn deserialize_char<V>(self, visitor: V) -> Result<V::Value, Error> |
| 691 | where |
| 692 | V: Visitor<'de>, |
| 693 | { |
| 694 | crate::Deserializer::from_str(&self.json).deserialize_char(visitor) |
| 695 | } |
| 696 | |
| 697 | fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, Error> |
| 698 | where |
| 699 | V: Visitor<'de>, |
| 700 | { |
| 701 | crate::Deserializer::from_str(&self.json).deserialize_str(visitor) |
| 702 | } |
| 703 | |
| 704 | fn deserialize_string<V>(self, visitor: V) -> Result<V::Value, Error> |
| 705 | where |
| 706 | V: Visitor<'de>, |
| 707 | { |
| 708 | crate::Deserializer::from_str(&self.json).deserialize_string(visitor) |
| 709 | } |
| 710 | |
| 711 | fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value, Error> |
| 712 | where |
| 713 | V: Visitor<'de>, |
| 714 | { |
| 715 | crate::Deserializer::from_str(&self.json).deserialize_bytes(visitor) |
| 716 | } |
| 717 | |
| 718 | fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value, Error> |
| 719 | where |
| 720 | V: Visitor<'de>, |
| 721 | { |
| 722 | crate::Deserializer::from_str(&self.json).deserialize_byte_buf(visitor) |
| 723 | } |
| 724 | |
| 725 | fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Error> |
| 726 | where |
| 727 | V: Visitor<'de>, |
| 728 | { |
| 729 | crate::Deserializer::from_str(&self.json).deserialize_option(visitor) |
| 730 | } |
| 731 | |
| 732 | fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Error> |
| 733 | where |
| 734 | V: Visitor<'de>, |
| 735 | { |
| 736 | crate::Deserializer::from_str(&self.json).deserialize_unit(visitor) |
| 737 | } |
| 738 | |
| 739 | fn deserialize_unit_struct<V>(self, name: &'static str, visitor: V) -> Result<V::Value, Error> |
| 740 | where |
| 741 | V: Visitor<'de>, |
| 742 | { |
| 743 | crate::Deserializer::from_str(&self.json).deserialize_unit_struct(name, visitor) |
| 744 | } |
| 745 | |
| 746 | fn deserialize_newtype_struct<V>( |
| 747 | self, |
| 748 | name: &'static str, |
| 749 | visitor: V, |
| 750 | ) -> Result<V::Value, Error> |
| 751 | where |
| 752 | V: Visitor<'de>, |
| 753 | { |
| 754 | crate::Deserializer::from_str(&self.json).deserialize_newtype_struct(name, visitor) |
| 755 | } |
| 756 | |
| 757 | fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Error> |
| 758 | where |
| 759 | V: Visitor<'de>, |
| 760 | { |
| 761 | crate::Deserializer::from_str(&self.json).deserialize_seq(visitor) |
| 762 | } |
| 763 | |
| 764 | fn deserialize_tuple<V>(self, len: usize, visitor: V) -> Result<V::Value, Error> |
| 765 | where |
| 766 | V: Visitor<'de>, |
| 767 | { |
| 768 | crate::Deserializer::from_str(&self.json).deserialize_tuple(len, visitor) |
| 769 | } |
| 770 | |
| 771 | fn deserialize_tuple_struct<V>( |
| 772 | self, |
| 773 | name: &'static str, |
| 774 | len: usize, |
| 775 | visitor: V, |
| 776 | ) -> Result<V::Value, Error> |
| 777 | where |
| 778 | V: Visitor<'de>, |
| 779 | { |
| 780 | crate::Deserializer::from_str(&self.json).deserialize_tuple_struct(name, len, visitor) |
| 781 | } |
| 782 | |
| 783 | fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Error> |
| 784 | where |
| 785 | V: Visitor<'de>, |
| 786 | { |
| 787 | crate::Deserializer::from_str(&self.json).deserialize_map(visitor) |
| 788 | } |
| 789 | |
| 790 | fn deserialize_struct<V>( |
| 791 | self, |
| 792 | name: &'static str, |
| 793 | fields: &'static [&'static str], |
| 794 | visitor: V, |
| 795 | ) -> Result<V::Value, Error> |
| 796 | where |
| 797 | V: Visitor<'de>, |
| 798 | { |
| 799 | crate::Deserializer::from_str(&self.json).deserialize_struct(name, fields, visitor) |
| 800 | } |
| 801 | |
| 802 | fn deserialize_enum<V>( |
| 803 | self, |
| 804 | name: &'static str, |
| 805 | variants: &'static [&'static str], |
| 806 | visitor: V, |
| 807 | ) -> Result<V::Value, Error> |
| 808 | where |
| 809 | V: Visitor<'de>, |
| 810 | { |
| 811 | crate::Deserializer::from_str(&self.json).deserialize_enum(name, variants, visitor) |
| 812 | } |
| 813 | |
| 814 | fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value, Error> |
| 815 | where |
| 816 | V: Visitor<'de>, |
| 817 | { |
| 818 | crate::Deserializer::from_str(&self.json).deserialize_identifier(visitor) |
| 819 | } |
| 820 | |
| 821 | fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value, Error> |
| 822 | where |
| 823 | V: Visitor<'de>, |
| 824 | { |
| 825 | crate::Deserializer::from_str(&self.json).deserialize_ignored_any(visitor) |
| 826 | } |
| 827 | } |