| 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`]: ../fn.from_str.html |
| 103 |
/// [`serde_json::from_slice`]: ../fn.from_slice.html |
| 104 |
/// [`serde_json::from_reader`]: ../fn.from_reader.html |
| 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 |
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::from_borrowed("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 |
/// Convert an owned `String` of JSON data to an owned `RawValue`. |
| 172 |
/// |
| 173 |
/// This function is equivalent to `serde_json::from_str::<Box<RawValue>>` |
| 174 |
/// except that we avoid an allocation and memcpy if both of the following |
| 175 |
/// are true: |
| 176 |
/// |
| 177 |
/// - the input has no leading or trailing whitespace, and |
| 178 |
/// - the input has capacity equal to its length. |
| 179 |
pub fn from_string(json: String) -> Result<Box<Self>, Error> { |
| 180 |
let borrowed = tri!(crate::from_str::<&Self>(&json)); |
| 181 |
if borrowed.json.len() < json.len() { |
| 182 |
return Ok(borrowed.to_owned()); |
| 183 |
} |
| 184 |
Ok(Self::from_owned(json.into_boxed_str())) |
| 185 |
} |
| 186 |
|
| 187 |
/// Access the JSON text underlying a raw value. |
| 188 |
/// |
| 189 |
/// # Example |
| 190 |
/// |
| 191 |
/// ``` |
| 192 |
/// use serde::Deserialize; |
| 193 |
/// use serde_json::{Result, value::RawValue}; |
| 194 |
/// |
| 195 |
/// #[derive(Deserialize)] |
| 196 |
/// struct Response<'a> { |
| 197 |
/// code: u32, |
| 198 |
/// #[serde(borrow)] |
| 199 |
/// payload: &'a RawValue, |
| 200 |
/// } |
| 201 |
/// |
| 202 |
/// fn process(input: &str) -> Result<()> { |
| 203 |
/// let response: Response = serde_json::from_str(input)?; |
| 204 |
/// |
| 205 |
/// let payload = response.payload.get(); |
| 206 |
/// if payload.starts_with('{') { |
| 207 |
/// // handle a payload which is a JSON map |
| 208 |
/// } else { |
| 209 |
/// // handle any other type |
| 210 |
/// } |
| 211 |
/// |
| 212 |
/// Ok(()) |
| 213 |
/// } |
| 214 |
/// |
| 215 |
/// fn main() -> Result<()> { |
| 216 |
/// process(r#" {"code": 200, "payload": {}} "#)?; |
| 217 |
/// Ok(()) |
| 218 |
/// } |
| 219 |
/// ``` |
| 220 |
pub fn get(&self) -> &str { |
| 221 |
&self.json |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
impl From<Box<RawValue>> for Box<str> { |
| 226 |
fn from(raw_value: Box<RawValue>) -> Self { |
| 227 |
RawValue::into_owned(raw_value) |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
/// Convert a `T` into a boxed `RawValue`. |
| 232 |
/// |
| 233 |
/// # Example |
| 234 |
/// |
| 235 |
/// ``` |
| 236 |
/// // Upstream crate |
| 237 |
/// # #[derive(Serialize)] |
| 238 |
/// pub struct Thing { |
| 239 |
/// foo: String, |
| 240 |
/// bar: Option<String>, |
| 241 |
/// extra_data: Box<RawValue>, |
| 242 |
/// } |
| 243 |
/// |
| 244 |
/// // Local crate |
| 245 |
/// use serde::Serialize; |
| 246 |
/// use serde_json::value::{to_raw_value, RawValue}; |
| 247 |
/// |
| 248 |
/// #[derive(Serialize)] |
| 249 |
/// struct MyExtraData { |
| 250 |
/// a: u32, |
| 251 |
/// b: u32, |
| 252 |
/// } |
| 253 |
/// |
| 254 |
/// let my_thing = Thing { |
| 255 |
/// foo: "FooVal".into(), |
| 256 |
/// bar: None, |
| 257 |
/// extra_data: to_raw_value(&MyExtraData { a: 1, b: 2 }).unwrap(), |
| 258 |
/// }; |
| 259 |
/// # assert_eq!( |
| 260 |
/// # serde_json::to_value(my_thing).unwrap(), |
| 261 |
/// # serde_json::json!({ |
| 262 |
/// # "foo": "FooVal", |
| 263 |
/// # "bar": null, |
| 264 |
/// # "extra_data": { "a": 1, "b": 2 } |
| 265 |
/// # }) |
| 266 |
/// # ); |
| 267 |
/// ``` |
| 268 |
/// |
| 269 |
/// # Errors |
| 270 |
/// |
| 271 |
/// This conversion can fail if `T`'s implementation of `Serialize` decides to |
| 272 |
/// fail, or if `T` contains a map with non-string keys. |
| 273 |
/// |
| 274 |
/// ``` |
| 275 |
/// use std::collections::BTreeMap; |
| 276 |
/// |
| 277 |
/// // The keys in this map are vectors, not strings. |
| 278 |
/// let mut map = BTreeMap::new(); |
| 279 |
/// map.insert(vec![32, 64], "x86"); |
| 280 |
/// |
| 281 |
/// println!("{}", serde_json::value::to_raw_value(&map).unwrap_err()); |
| 282 |
/// ``` |
| 283 |
#[cfg_attr(docsrs, doc(cfg(feature = "raw_value")))] |
| 284 |
pub fn to_raw_value<T>(value: &T) -> Result<Box<RawValue>, Error> |
| 285 |
where |
| 286 |
T: ?Sized + Serialize, |
| 287 |
{ |
| 288 |
let json_string = tri!(crate::to_string(value)); |
| 289 |
Ok(RawValue::from_owned(json_string.into_boxed_str())) |
| 290 |
} |
| 291 |
|
| 292 |
pub const TOKEN: &str = "$serde_json::private::RawValue"; |
| 293 |
|
| 294 |
impl Serialize for RawValue { |
| 295 |
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
| 296 |
where |
| 297 |
S: Serializer, |
| 298 |
{ |
| 299 |
let mut s = tri!(serializer.serialize_struct(TOKEN, 1)); |
| 300 |
tri!(s.serialize_field(TOKEN, &self.json)); |
| 301 |
s.end() |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
impl<'de: 'a, 'a> Deserialize<'de> for &'a RawValue { |
| 306 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 307 |
where |
| 308 |
D: Deserializer<'de>, |
| 309 |
{ |
| 310 |
struct ReferenceVisitor; |
| 311 |
|
| 312 |
impl<'de> Visitor<'de> for ReferenceVisitor { |
| 313 |
type Value = &'de RawValue; |
| 314 |
|
| 315 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 316 |
write!(formatter, "any valid JSON value") |
| 317 |
} |
| 318 |
|
| 319 |
fn visit_map<V>(self, mut visitor: V) -> Result<Self::Value, V::Error> |
| 320 |
where |
| 321 |
V: MapAccess<'de>, |
| 322 |
{ |
| 323 |
let value = tri!(visitor.next_key::<RawKey>()); |
| 324 |
if value.is_none() { |
| 325 |
return Err(de::Error::invalid_type(Unexpected::Map, &self)); |
| 326 |
} |
| 327 |
visitor.next_value_seed(ReferenceFromString) |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
deserializer.deserialize_newtype_struct(TOKEN, ReferenceVisitor) |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
impl<'de> Deserialize<'de> for Box<RawValue> { |
| 336 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 337 |
where |
| 338 |
D: Deserializer<'de>, |
| 339 |
{ |
| 340 |
struct BoxedVisitor; |
| 341 |
|
| 342 |
impl<'de> Visitor<'de> for BoxedVisitor { |
| 343 |
type Value = Box<RawValue>; |
| 344 |
|
| 345 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 346 |
write!(formatter, "any valid JSON value") |
| 347 |
} |
| 348 |
|
| 349 |
fn visit_map<V>(self, mut visitor: V) -> Result<Self::Value, V::Error> |
| 350 |
where |
| 351 |
V: MapAccess<'de>, |
| 352 |
{ |
| 353 |
let value = tri!(visitor.next_key::<RawKey>()); |
| 354 |
if value.is_none() { |
| 355 |
return Err(de::Error::invalid_type(Unexpected::Map, &self)); |
| 356 |
} |
| 357 |
visitor.next_value_seed(BoxedFromString) |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
deserializer.deserialize_newtype_struct(TOKEN, BoxedVisitor) |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
struct RawKey; |
| 366 |
|
| 367 |
impl<'de> Deserialize<'de> for RawKey { |
| 368 |
fn deserialize<D>(deserializer: D) -> Result<RawKey, D::Error> |
| 369 |
where |
| 370 |
D: Deserializer<'de>, |
| 371 |
{ |
| 372 |
struct FieldVisitor; |
| 373 |
|
| 374 |
impl<'de> Visitor<'de> for FieldVisitor { |
| 375 |
type Value = (); |
| 376 |
|
| 377 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 378 |
formatter.write_str("raw value") |
| 379 |
} |
| 380 |
|
| 381 |
fn visit_str<E>(self, s: &str) -> Result<(), E> |
| 382 |
where |
| 383 |
E: de::Error, |
| 384 |
{ |
| 385 |
if s == TOKEN { |
| 386 |
Ok(()) |
| 387 |
} else { |
| 388 |
Err(de::Error::custom("unexpected raw value")) |
| 389 |
} |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
tri!(deserializer.deserialize_identifier(FieldVisitor)); |
| 394 |
Ok(RawKey) |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
pub struct ReferenceFromString; |
| 399 |
|
| 400 |
impl<'de> DeserializeSeed<'de> for ReferenceFromString { |
| 401 |
type Value = &'de RawValue; |
| 402 |
|
| 403 |
fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error> |
| 404 |
where |
| 405 |
D: Deserializer<'de>, |
| 406 |
{ |
| 407 |
deserializer.deserialize_str(self) |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
impl<'de> Visitor<'de> for ReferenceFromString { |
| 412 |
type Value = &'de RawValue; |
| 413 |
|
| 414 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 415 |
formatter.write_str("raw value") |
| 416 |
} |
| 417 |
|
| 418 |
fn visit_borrowed_str<E>(self, s: &'de str) -> Result<Self::Value, E> |
| 419 |
where |
| 420 |
E: de::Error, |
| 421 |
{ |
| 422 |
Ok(RawValue::from_borrowed(s)) |
| 423 |
} |
| 424 |
} |
| 425 |
|
| 426 |
pub struct BoxedFromString; |
| 427 |
|
| 428 |
impl<'de> DeserializeSeed<'de> for BoxedFromString { |
| 429 |
type Value = Box<RawValue>; |
| 430 |
|
| 431 |
fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error> |
| 432 |
where |
| 433 |
D: Deserializer<'de>, |
| 434 |
{ |
| 435 |
deserializer.deserialize_str(self) |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
impl<'de> Visitor<'de> for BoxedFromString { |
| 440 |
type Value = Box<RawValue>; |
| 441 |
|
| 442 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 443 |
formatter.write_str("raw value") |
| 444 |
} |
| 445 |
|
| 446 |
fn visit_str<E>(self, s: &str) -> Result<Self::Value, E> |
| 447 |
where |
| 448 |
E: de::Error, |
| 449 |
{ |
| 450 |
Ok(RawValue::from_owned(s.to_owned().into_boxed_str())) |
| 451 |
} |
| 452 |
|
| 453 |
#[cfg(any(feature = "std", feature = "alloc"))] |
| 454 |
fn visit_string<E>(self, s: String) -> Result<Self::Value, E> |
| 455 |
where |
| 456 |
E: de::Error, |
| 457 |
{ |
| 458 |
Ok(RawValue::from_owned(s.into_boxed_str())) |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
struct RawKeyDeserializer; |
| 463 |
|
| 464 |
impl<'de> Deserializer<'de> for RawKeyDeserializer { |
| 465 |
type Error = Error; |
| 466 |
|
| 467 |
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Error> |
| 468 |
where |
| 469 |
V: de::Visitor<'de>, |
| 470 |
{ |
| 471 |
visitor.visit_borrowed_str(TOKEN) |
| 472 |
} |
| 473 |
|
| 474 |
forward_to_deserialize_any! { |
| 475 |
bool u8 u16 u32 u64 u128 i8 i16 i32 i64 i128 f32 f64 char str string seq |
| 476 |
bytes byte_buf map struct option unit newtype_struct ignored_any |
| 477 |
unit_struct tuple_struct tuple enum identifier |
| 478 |
} |
| 479 |
} |
| 480 |
|
| 481 |
pub struct OwnedRawDeserializer { |
| 482 |
pub raw_value: Option<String>, |
| 483 |
} |
| 484 |
|
| 485 |
impl<'de> MapAccess<'de> for OwnedRawDeserializer { |
| 486 |
type Error = Error; |
| 487 |
|
| 488 |
fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Error> |
| 489 |
where |
| 490 |
K: de::DeserializeSeed<'de>, |
| 491 |
{ |
| 492 |
if self.raw_value.is_none() { |
| 493 |
return Ok(None); |
| 494 |
} |
| 495 |
seed.deserialize(RawKeyDeserializer).map(Some) |
| 496 |
} |
| 497 |
|
| 498 |
fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Error> |
| 499 |
where |
| 500 |
V: de::DeserializeSeed<'de>, |
| 501 |
{ |
| 502 |
seed.deserialize(self.raw_value.take().unwrap().into_deserializer()) |
| 503 |
} |
| 504 |
} |
| 505 |
|
| 506 |
pub struct BorrowedRawDeserializer<'de> { |
| 507 |
pub raw_value: Option<&'de str>, |
| 508 |
} |
| 509 |
|
| 510 |
impl<'de> MapAccess<'de> for BorrowedRawDeserializer<'de> { |
| 511 |
type Error = Error; |
| 512 |
|
| 513 |
fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Error> |
| 514 |
where |
| 515 |
K: de::DeserializeSeed<'de>, |
| 516 |
{ |
| 517 |
if self.raw_value.is_none() { |
| 518 |
return Ok(None); |
| 519 |
} |
| 520 |
seed.deserialize(RawKeyDeserializer).map(Some) |
| 521 |
} |
| 522 |
|
| 523 |
fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Error> |
| 524 |
where |
| 525 |
V: de::DeserializeSeed<'de>, |
| 526 |
{ |
| 527 |
seed.deserialize(BorrowedStrDeserializer::new(self.raw_value.take().unwrap())) |
| 528 |
} |
| 529 |
} |
| 530 |
|
| 531 |
impl<'de> IntoDeserializer<'de, Error> for &'de RawValue { |
| 532 |
type Deserializer = &'de RawValue; |
| 533 |
|
| 534 |
fn into_deserializer(self) -> Self::Deserializer { |
| 535 |
self |
| 536 |
} |
| 537 |
} |
| 538 |
|
| 539 |
impl<'de> Deserializer<'de> for &'de RawValue { |
| 540 |
type Error = Error; |
| 541 |
|
| 542 |
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Error> |
| 543 |
where |
| 544 |
V: Visitor<'de>, |
| 545 |
{ |
| 546 |
crate::Deserializer::from_str(&self.json).deserialize_any(visitor) |
| 547 |
} |
| 548 |
|
| 549 |
fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, Error> |
| 550 |
where |
| 551 |
V: Visitor<'de>, |
| 552 |
{ |
| 553 |
crate::Deserializer::from_str(&self.json).deserialize_bool(visitor) |
| 554 |
} |
| 555 |
|
| 556 |
fn deserialize_i8<V>(self, visitor: V) -> Result<V::Value, Error> |
| 557 |
where |
| 558 |
V: Visitor<'de>, |
| 559 |
{ |
| 560 |
crate::Deserializer::from_str(&self.json).deserialize_i8(visitor) |
| 561 |
} |
| 562 |
|
| 563 |
fn deserialize_i16<V>(self, visitor: V) -> Result<V::Value, Error> |
| 564 |
where |
| 565 |
V: Visitor<'de>, |
| 566 |
{ |
| 567 |
crate::Deserializer::from_str(&self.json).deserialize_i16(visitor) |
| 568 |
} |
| 569 |
|
| 570 |
fn deserialize_i32<V>(self, visitor: V) -> Result<V::Value, Error> |
| 571 |
where |
| 572 |
V: Visitor<'de>, |
| 573 |
{ |
| 574 |
crate::Deserializer::from_str(&self.json).deserialize_i32(visitor) |
| 575 |
} |
| 576 |
|
| 577 |
fn deserialize_i64<V>(self, visitor: V) -> Result<V::Value, Error> |
| 578 |
where |
| 579 |
V: Visitor<'de>, |
| 580 |
{ |
| 581 |
crate::Deserializer::from_str(&self.json).deserialize_i64(visitor) |
| 582 |
} |
| 583 |
|
| 584 |
fn deserialize_i128<V>(self, visitor: V) -> Result<V::Value, Error> |
| 585 |
where |
| 586 |
V: Visitor<'de>, |
| 587 |
{ |
| 588 |
crate::Deserializer::from_str(&self.json).deserialize_i128(visitor) |
| 589 |
} |
| 590 |
|
| 591 |
fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value, Error> |
| 592 |
where |
| 593 |
V: Visitor<'de>, |
| 594 |
{ |
| 595 |
crate::Deserializer::from_str(&self.json).deserialize_u8(visitor) |
| 596 |
} |
| 597 |
|
| 598 |
fn deserialize_u16<V>(self, visitor: V) -> Result<V::Value, Error> |
| 599 |
where |
| 600 |
V: Visitor<'de>, |
| 601 |
{ |
| 602 |
crate::Deserializer::from_str(&self.json).deserialize_u16(visitor) |
| 603 |
} |
| 604 |
|
| 605 |
fn deserialize_u32<V>(self, visitor: V) -> Result<V::Value, Error> |
| 606 |
where |
| 607 |
V: Visitor<'de>, |
| 608 |
{ |
| 609 |
crate::Deserializer::from_str(&self.json).deserialize_u32(visitor) |
| 610 |
} |
| 611 |
|
| 612 |
fn deserialize_u64<V>(self, visitor: V) -> Result<V::Value, Error> |
| 613 |
where |
| 614 |
V: Visitor<'de>, |
| 615 |
{ |
| 616 |
crate::Deserializer::from_str(&self.json).deserialize_u64(visitor) |
| 617 |
} |
| 618 |
|
| 619 |
fn deserialize_u128<V>(self, visitor: V) -> Result<V::Value, Error> |
| 620 |
where |
| 621 |
V: Visitor<'de>, |
| 622 |
{ |
| 623 |
crate::Deserializer::from_str(&self.json).deserialize_u128(visitor) |
| 624 |
} |
| 625 |
|
| 626 |
fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value, Error> |
| 627 |
where |
| 628 |
V: Visitor<'de>, |
| 629 |
{ |
| 630 |
crate::Deserializer::from_str(&self.json).deserialize_f32(visitor) |
| 631 |
} |
| 632 |
|
| 633 |
fn deserialize_f64<V>(self, visitor: V) -> Result<V::Value, Error> |
| 634 |
where |
| 635 |
V: Visitor<'de>, |
| 636 |
{ |
| 637 |
crate::Deserializer::from_str(&self.json).deserialize_f64(visitor) |
| 638 |
} |
| 639 |
|
| 640 |
fn deserialize_char<V>(self, visitor: V) -> Result<V::Value, Error> |
| 641 |
where |
| 642 |
V: Visitor<'de>, |
| 643 |
{ |
| 644 |
crate::Deserializer::from_str(&self.json).deserialize_char(visitor) |
| 645 |
} |
| 646 |
|
| 647 |
fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, Error> |
| 648 |
where |
| 649 |
V: Visitor<'de>, |
| 650 |
{ |
| 651 |
crate::Deserializer::from_str(&self.json).deserialize_str(visitor) |
| 652 |
} |
| 653 |
|
| 654 |
fn deserialize_string<V>(self, visitor: V) -> Result<V::Value, Error> |
| 655 |
where |
| 656 |
V: Visitor<'de>, |
| 657 |
{ |
| 658 |
crate::Deserializer::from_str(&self.json).deserialize_string(visitor) |
| 659 |
} |
| 660 |
|
| 661 |
fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value, Error> |
| 662 |
where |
| 663 |
V: Visitor<'de>, |
| 664 |
{ |
| 665 |
crate::Deserializer::from_str(&self.json).deserialize_bytes(visitor) |
| 666 |
} |
| 667 |
|
| 668 |
fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value, Error> |
| 669 |
where |
| 670 |
V: Visitor<'de>, |
| 671 |
{ |
| 672 |
crate::Deserializer::from_str(&self.json).deserialize_byte_buf(visitor) |
| 673 |
} |
| 674 |
|
| 675 |
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Error> |
| 676 |
where |
| 677 |
V: Visitor<'de>, |
| 678 |
{ |
| 679 |
crate::Deserializer::from_str(&self.json).deserialize_option(visitor) |
| 680 |
} |
| 681 |
|
| 682 |
fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Error> |
| 683 |
where |
| 684 |
V: Visitor<'de>, |
| 685 |
{ |
| 686 |
crate::Deserializer::from_str(&self.json).deserialize_unit(visitor) |
| 687 |
} |
| 688 |
|
| 689 |
fn deserialize_unit_struct<V>(self, name: &'static str, visitor: V) -> Result<V::Value, Error> |
| 690 |
where |
| 691 |
V: Visitor<'de>, |
| 692 |
{ |
| 693 |
crate::Deserializer::from_str(&self.json).deserialize_unit_struct(name, visitor) |
| 694 |
} |
| 695 |
|
| 696 |
fn deserialize_newtype_struct<V>( |
| 697 |
self, |
| 698 |
name: &'static str, |
| 699 |
visitor: V, |
| 700 |
) -> Result<V::Value, Error> |
| 701 |
where |
| 702 |
V: Visitor<'de>, |
| 703 |
{ |
| 704 |
crate::Deserializer::from_str(&self.json).deserialize_newtype_struct(name, visitor) |
| 705 |
} |
| 706 |
|
| 707 |
fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Error> |
| 708 |
where |
| 709 |
V: Visitor<'de>, |
| 710 |
{ |
| 711 |
crate::Deserializer::from_str(&self.json).deserialize_seq(visitor) |
| 712 |
} |
| 713 |
|
| 714 |
fn deserialize_tuple<V>(self, len: usize, visitor: V) -> Result<V::Value, Error> |
| 715 |
where |
| 716 |
V: Visitor<'de>, |
| 717 |
{ |
| 718 |
crate::Deserializer::from_str(&self.json).deserialize_tuple(len, visitor) |
| 719 |
} |
| 720 |
|
| 721 |
fn deserialize_tuple_struct<V>( |
| 722 |
self, |
| 723 |
name: &'static str, |
| 724 |
len: usize, |
| 725 |
visitor: V, |
| 726 |
) -> Result<V::Value, Error> |
| 727 |
where |
| 728 |
V: Visitor<'de>, |
| 729 |
{ |
| 730 |
crate::Deserializer::from_str(&self.json).deserialize_tuple_struct(name, len, visitor) |
| 731 |
} |
| 732 |
|
| 733 |
fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Error> |
| 734 |
where |
| 735 |
V: Visitor<'de>, |
| 736 |
{ |
| 737 |
crate::Deserializer::from_str(&self.json).deserialize_map(visitor) |
| 738 |
} |
| 739 |
|
| 740 |
fn deserialize_struct<V>( |
| 741 |
self, |
| 742 |
name: &'static str, |
| 743 |
fields: &'static [&'static str], |
| 744 |
visitor: V, |
| 745 |
) -> Result<V::Value, Error> |
| 746 |
where |
| 747 |
V: Visitor<'de>, |
| 748 |
{ |
| 749 |
crate::Deserializer::from_str(&self.json).deserialize_struct(name, fields, visitor) |
| 750 |
} |
| 751 |
|
| 752 |
fn deserialize_enum<V>( |
| 753 |
self, |
| 754 |
name: &'static str, |
| 755 |
variants: &'static [&'static str], |
| 756 |
visitor: V, |
| 757 |
) -> Result<V::Value, Error> |
| 758 |
where |
| 759 |
V: Visitor<'de>, |
| 760 |
{ |
| 761 |
crate::Deserializer::from_str(&self.json).deserialize_enum(name, variants, visitor) |
| 762 |
} |
| 763 |
|
| 764 |
fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value, Error> |
| 765 |
where |
| 766 |
V: Visitor<'de>, |
| 767 |
{ |
| 768 |
crate::Deserializer::from_str(&self.json).deserialize_identifier(visitor) |
| 769 |
} |
| 770 |
|
| 771 |
fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value, Error> |
| 772 |
where |
| 773 |
V: Visitor<'de>, |
| 774 |
{ |
| 775 |
crate::Deserializer::from_str(&self.json).deserialize_ignored_any(visitor) |
| 776 |
} |
| 777 |
} |