1 | //! Deserialize JSON data to a Rust data structure. |
2 | |
3 | use crate::error::{Error, ErrorCode, Result}; |
4 | #[cfg (feature = "float_roundtrip" )] |
5 | use crate::lexical; |
6 | use crate::number::Number; |
7 | use crate::read::{self, Fused, Reference}; |
8 | use alloc::string::String; |
9 | use alloc::vec::Vec; |
10 | #[cfg (feature = "float_roundtrip" )] |
11 | use core::iter; |
12 | use core::iter::FusedIterator; |
13 | use core::marker::PhantomData; |
14 | use core::result; |
15 | use core::str::FromStr; |
16 | use serde::de::{self, Expected, Unexpected}; |
17 | use serde::forward_to_deserialize_any; |
18 | |
19 | #[cfg (feature = "arbitrary_precision" )] |
20 | use crate::number::NumberDeserializer; |
21 | |
22 | pub use crate::read::{Read, SliceRead, StrRead}; |
23 | |
24 | #[cfg (feature = "std" )] |
25 | #[cfg_attr (docsrs, doc(cfg(feature = "std" )))] |
26 | pub use crate::read::IoRead; |
27 | |
28 | ////////////////////////////////////////////////////////////////////////////// |
29 | |
30 | /// A structure that deserializes JSON into Rust values. |
31 | pub struct Deserializer<R> { |
32 | read: R, |
33 | scratch: Vec<u8>, |
34 | remaining_depth: u8, |
35 | #[cfg (feature = "float_roundtrip" )] |
36 | single_precision: bool, |
37 | #[cfg (feature = "unbounded_depth" )] |
38 | disable_recursion_limit: bool, |
39 | } |
40 | |
41 | impl<'de, R> Deserializer<R> |
42 | where |
43 | R: read::Read<'de>, |
44 | { |
45 | /// Create a JSON deserializer from one of the possible serde_json input |
46 | /// sources. |
47 | /// |
48 | /// Typically it is more convenient to use one of these methods instead: |
49 | /// |
50 | /// - Deserializer::from_str |
51 | /// - Deserializer::from_slice |
52 | /// - Deserializer::from_reader |
53 | pub fn new(read: R) -> Self { |
54 | Deserializer { |
55 | read, |
56 | scratch: Vec::new(), |
57 | remaining_depth: 128, |
58 | #[cfg (feature = "float_roundtrip" )] |
59 | single_precision: false, |
60 | #[cfg (feature = "unbounded_depth" )] |
61 | disable_recursion_limit: false, |
62 | } |
63 | } |
64 | } |
65 | |
66 | #[cfg (feature = "std" )] |
67 | impl<R> Deserializer<read::IoRead<R>> |
68 | where |
69 | R: crate::io::Read, |
70 | { |
71 | /// Creates a JSON deserializer from an `io::Read`. |
72 | /// |
73 | /// Reader-based deserializers do not support deserializing borrowed types |
74 | /// like `&str`, since the `std::io::Read` trait has no non-copying methods |
75 | /// -- everything it does involves copying bytes out of the data source. |
76 | pub fn from_reader(reader: R) -> Self { |
77 | Deserializer::new(read:read::IoRead::new(reader)) |
78 | } |
79 | } |
80 | |
81 | impl<'a> Deserializer<read::SliceRead<'a>> { |
82 | /// Creates a JSON deserializer from a `&[u8]`. |
83 | pub fn from_slice(bytes: &'a [u8]) -> Self { |
84 | Deserializer::new(read:read::SliceRead::new(slice:bytes)) |
85 | } |
86 | } |
87 | |
88 | impl<'a> Deserializer<read::StrRead<'a>> { |
89 | /// Creates a JSON deserializer from a `&str`. |
90 | pub fn from_str(s: &'a str) -> Self { |
91 | Deserializer::new(read:read::StrRead::new(s)) |
92 | } |
93 | } |
94 | |
95 | macro_rules! overflow { |
96 | ($a:ident * 10 + $b:ident, $c:expr) => { |
97 | match $c { |
98 | c => $a >= c / 10 && ($a > c / 10 || $b > c % 10), |
99 | } |
100 | }; |
101 | } |
102 | |
103 | pub(crate) enum ParserNumber { |
104 | F64(f64), |
105 | U64(u64), |
106 | I64(i64), |
107 | #[cfg (feature = "arbitrary_precision" )] |
108 | String(String), |
109 | } |
110 | |
111 | impl ParserNumber { |
112 | fn visit<'de, V>(self, visitor: V) -> Result<V::Value> |
113 | where |
114 | V: de::Visitor<'de>, |
115 | { |
116 | match self { |
117 | ParserNumber::F64(x: f64) => visitor.visit_f64(x), |
118 | ParserNumber::U64(x: u64) => visitor.visit_u64(x), |
119 | ParserNumber::I64(x: i64) => visitor.visit_i64(x), |
120 | #[cfg (feature = "arbitrary_precision" )] |
121 | ParserNumber::String(x) => visitor.visit_map(NumberDeserializer { number: x.into() }), |
122 | } |
123 | } |
124 | |
125 | fn invalid_type(self, exp: &dyn Expected) -> Error { |
126 | match self { |
127 | ParserNumber::F64(x: f64) => de::Error::invalid_type(unexp:Unexpected::Float(x), exp), |
128 | ParserNumber::U64(x: u64) => de::Error::invalid_type(unexp:Unexpected::Unsigned(x), exp), |
129 | ParserNumber::I64(x: i64) => de::Error::invalid_type(unexp:Unexpected::Signed(x), exp), |
130 | #[cfg (feature = "arbitrary_precision" )] |
131 | ParserNumber::String(_) => de::Error::invalid_type(Unexpected::Other("number" ), exp), |
132 | } |
133 | } |
134 | } |
135 | |
136 | impl<'de, R: Read<'de>> Deserializer<R> { |
137 | /// The `Deserializer::end` method should be called after a value has been fully deserialized. |
138 | /// This allows the `Deserializer` to validate that the input stream is at the end or that it |
139 | /// only has trailing whitespace. |
140 | pub fn end(&mut self) -> Result<()> { |
141 | match tri!(self.parse_whitespace()) { |
142 | Some(_) => Err(self.peek_error(ErrorCode::TrailingCharacters)), |
143 | None => Ok(()), |
144 | } |
145 | } |
146 | |
147 | /// Turn a JSON deserializer into an iterator over values of type T. |
148 | pub fn into_iter<T>(self) -> StreamDeserializer<'de, R, T> |
149 | where |
150 | T: de::Deserialize<'de>, |
151 | { |
152 | // This cannot be an implementation of std::iter::IntoIterator because |
153 | // we need the caller to choose what T is. |
154 | let offset = self.read.byte_offset(); |
155 | StreamDeserializer { |
156 | de: self, |
157 | offset, |
158 | failed: false, |
159 | output: PhantomData, |
160 | lifetime: PhantomData, |
161 | } |
162 | } |
163 | |
164 | /// Parse arbitrarily deep JSON structures without any consideration for |
165 | /// overflowing the stack. |
166 | /// |
167 | /// You will want to provide some other way to protect against stack |
168 | /// overflows, such as by wrapping your Deserializer in the dynamically |
169 | /// growing stack adapter provided by the serde_stacker crate. Additionally |
170 | /// you will need to be careful around other recursive operations on the |
171 | /// parsed result which may overflow the stack after deserialization has |
172 | /// completed, including, but not limited to, Display and Debug and Drop |
173 | /// impls. |
174 | /// |
175 | /// *This method is only available if serde_json is built with the |
176 | /// `"unbounded_depth"` feature.* |
177 | /// |
178 | /// # Examples |
179 | /// |
180 | /// ``` |
181 | /// use serde::Deserialize; |
182 | /// use serde_json::Value; |
183 | /// |
184 | /// fn main() { |
185 | /// let mut json = String::new(); |
186 | /// for _ in 0..10000 { |
187 | /// json = format!("[{}]", json); |
188 | /// } |
189 | /// |
190 | /// let mut deserializer = serde_json::Deserializer::from_str(&json); |
191 | /// deserializer.disable_recursion_limit(); |
192 | /// let deserializer = serde_stacker::Deserializer::new(&mut deserializer); |
193 | /// let value = Value::deserialize(deserializer).unwrap(); |
194 | /// |
195 | /// carefully_drop_nested_arrays(value); |
196 | /// } |
197 | /// |
198 | /// fn carefully_drop_nested_arrays(value: Value) { |
199 | /// let mut stack = vec![value]; |
200 | /// while let Some(value) = stack.pop() { |
201 | /// if let Value::Array(array) = value { |
202 | /// stack.extend(array); |
203 | /// } |
204 | /// } |
205 | /// } |
206 | /// ``` |
207 | #[cfg (feature = "unbounded_depth" )] |
208 | #[cfg_attr (docsrs, doc(cfg(feature = "unbounded_depth" )))] |
209 | pub fn disable_recursion_limit(&mut self) { |
210 | self.disable_recursion_limit = true; |
211 | } |
212 | |
213 | pub(crate) fn peek(&mut self) -> Result<Option<u8>> { |
214 | self.read.peek() |
215 | } |
216 | |
217 | fn peek_or_null(&mut self) -> Result<u8> { |
218 | Ok(tri!(self.peek()).unwrap_or(b' \x00' )) |
219 | } |
220 | |
221 | fn eat_char(&mut self) { |
222 | self.read.discard(); |
223 | } |
224 | |
225 | fn next_char(&mut self) -> Result<Option<u8>> { |
226 | self.read.next() |
227 | } |
228 | |
229 | fn next_char_or_null(&mut self) -> Result<u8> { |
230 | Ok(tri!(self.next_char()).unwrap_or(b' \x00' )) |
231 | } |
232 | |
233 | /// Error caused by a byte from next_char(). |
234 | #[cold ] |
235 | fn error(&self, reason: ErrorCode) -> Error { |
236 | let position = self.read.position(); |
237 | Error::syntax(reason, position.line, position.column) |
238 | } |
239 | |
240 | /// Error caused by a byte from peek(). |
241 | #[cold ] |
242 | fn peek_error(&self, reason: ErrorCode) -> Error { |
243 | let position = self.read.peek_position(); |
244 | Error::syntax(reason, position.line, position.column) |
245 | } |
246 | |
247 | /// Returns the first non-whitespace byte without consuming it, or `None` if |
248 | /// EOF is encountered. |
249 | fn parse_whitespace(&mut self) -> Result<Option<u8>> { |
250 | loop { |
251 | match tri!(self.peek()) { |
252 | Some(b' ' | b' \n' | b' \t' | b' \r' ) => { |
253 | self.eat_char(); |
254 | } |
255 | other => { |
256 | return Ok(other); |
257 | } |
258 | } |
259 | } |
260 | } |
261 | |
262 | #[cold ] |
263 | fn peek_invalid_type(&mut self, exp: &dyn Expected) -> Error { |
264 | let err = match self.peek_or_null().unwrap_or(b' \x00' ) { |
265 | b'n' => { |
266 | self.eat_char(); |
267 | if let Err(err) = self.parse_ident(b"ull" ) { |
268 | return err; |
269 | } |
270 | de::Error::invalid_type(Unexpected::Unit, exp) |
271 | } |
272 | b't' => { |
273 | self.eat_char(); |
274 | if let Err(err) = self.parse_ident(b"rue" ) { |
275 | return err; |
276 | } |
277 | de::Error::invalid_type(Unexpected::Bool(true), exp) |
278 | } |
279 | b'f' => { |
280 | self.eat_char(); |
281 | if let Err(err) = self.parse_ident(b"alse" ) { |
282 | return err; |
283 | } |
284 | de::Error::invalid_type(Unexpected::Bool(false), exp) |
285 | } |
286 | b'-' => { |
287 | self.eat_char(); |
288 | match self.parse_any_number(false) { |
289 | Ok(n) => n.invalid_type(exp), |
290 | Err(err) => return err, |
291 | } |
292 | } |
293 | b'0' ..=b'9' => match self.parse_any_number(true) { |
294 | Ok(n) => n.invalid_type(exp), |
295 | Err(err) => return err, |
296 | }, |
297 | b'"' => { |
298 | self.eat_char(); |
299 | self.scratch.clear(); |
300 | match self.read.parse_str(&mut self.scratch) { |
301 | Ok(s) => de::Error::invalid_type(Unexpected::Str(&s), exp), |
302 | Err(err) => return err, |
303 | } |
304 | } |
305 | b'[' => de::Error::invalid_type(Unexpected::Seq, exp), |
306 | b'{' => de::Error::invalid_type(Unexpected::Map, exp), |
307 | _ => self.peek_error(ErrorCode::ExpectedSomeValue), |
308 | }; |
309 | |
310 | self.fix_position(err) |
311 | } |
312 | |
313 | pub(crate) fn deserialize_number<'any, V>(&mut self, visitor: V) -> Result<V::Value> |
314 | where |
315 | V: de::Visitor<'any>, |
316 | { |
317 | let peek = match tri!(self.parse_whitespace()) { |
318 | Some(b) => b, |
319 | None => { |
320 | return Err(self.peek_error(ErrorCode::EofWhileParsingValue)); |
321 | } |
322 | }; |
323 | |
324 | let value = match peek { |
325 | b'-' => { |
326 | self.eat_char(); |
327 | tri!(self.parse_integer(false)).visit(visitor) |
328 | } |
329 | b'0' ..=b'9' => tri!(self.parse_integer(true)).visit(visitor), |
330 | _ => Err(self.peek_invalid_type(&visitor)), |
331 | }; |
332 | |
333 | match value { |
334 | Ok(value) => Ok(value), |
335 | Err(err) => Err(self.fix_position(err)), |
336 | } |
337 | } |
338 | |
339 | #[cfg (feature = "float_roundtrip" )] |
340 | pub(crate) fn do_deserialize_f32<'any, V>(&mut self, visitor: V) -> Result<V::Value> |
341 | where |
342 | V: de::Visitor<'any>, |
343 | { |
344 | self.single_precision = true; |
345 | let val = self.deserialize_number(visitor); |
346 | self.single_precision = false; |
347 | val |
348 | } |
349 | |
350 | pub(crate) fn do_deserialize_i128<'any, V>(&mut self, visitor: V) -> Result<V::Value> |
351 | where |
352 | V: de::Visitor<'any>, |
353 | { |
354 | let mut buf = String::new(); |
355 | |
356 | match tri!(self.parse_whitespace()) { |
357 | Some(b'-' ) => { |
358 | self.eat_char(); |
359 | buf.push('-' ); |
360 | } |
361 | Some(_) => {} |
362 | None => { |
363 | return Err(self.peek_error(ErrorCode::EofWhileParsingValue)); |
364 | } |
365 | }; |
366 | |
367 | tri!(self.scan_integer128(&mut buf)); |
368 | |
369 | let value = match buf.parse() { |
370 | Ok(int) => visitor.visit_i128(int), |
371 | Err(_) => { |
372 | return Err(self.error(ErrorCode::NumberOutOfRange)); |
373 | } |
374 | }; |
375 | |
376 | match value { |
377 | Ok(value) => Ok(value), |
378 | Err(err) => Err(self.fix_position(err)), |
379 | } |
380 | } |
381 | |
382 | pub(crate) fn do_deserialize_u128<'any, V>(&mut self, visitor: V) -> Result<V::Value> |
383 | where |
384 | V: de::Visitor<'any>, |
385 | { |
386 | match tri!(self.parse_whitespace()) { |
387 | Some(b'-' ) => { |
388 | return Err(self.peek_error(ErrorCode::NumberOutOfRange)); |
389 | } |
390 | Some(_) => {} |
391 | None => { |
392 | return Err(self.peek_error(ErrorCode::EofWhileParsingValue)); |
393 | } |
394 | } |
395 | |
396 | let mut buf = String::new(); |
397 | tri!(self.scan_integer128(&mut buf)); |
398 | |
399 | let value = match buf.parse() { |
400 | Ok(int) => visitor.visit_u128(int), |
401 | Err(_) => { |
402 | return Err(self.error(ErrorCode::NumberOutOfRange)); |
403 | } |
404 | }; |
405 | |
406 | match value { |
407 | Ok(value) => Ok(value), |
408 | Err(err) => Err(self.fix_position(err)), |
409 | } |
410 | } |
411 | |
412 | fn scan_integer128(&mut self, buf: &mut String) -> Result<()> { |
413 | match tri!(self.next_char_or_null()) { |
414 | b'0' => { |
415 | buf.push('0' ); |
416 | // There can be only one leading '0'. |
417 | match tri!(self.peek_or_null()) { |
418 | b'0' ..=b'9' => Err(self.peek_error(ErrorCode::InvalidNumber)), |
419 | _ => Ok(()), |
420 | } |
421 | } |
422 | c @ b'1' ..=b'9' => { |
423 | buf.push(c as char); |
424 | while let c @ b'0' ..=b'9' = tri!(self.peek_or_null()) { |
425 | self.eat_char(); |
426 | buf.push(c as char); |
427 | } |
428 | Ok(()) |
429 | } |
430 | _ => Err(self.error(ErrorCode::InvalidNumber)), |
431 | } |
432 | } |
433 | |
434 | #[cold ] |
435 | fn fix_position(&self, err: Error) -> Error { |
436 | err.fix_position(move |code| self.error(code)) |
437 | } |
438 | |
439 | fn parse_ident(&mut self, ident: &[u8]) -> Result<()> { |
440 | for expected in ident { |
441 | match tri!(self.next_char()) { |
442 | None => { |
443 | return Err(self.error(ErrorCode::EofWhileParsingValue)); |
444 | } |
445 | Some(next) => { |
446 | if next != *expected { |
447 | return Err(self.error(ErrorCode::ExpectedSomeIdent)); |
448 | } |
449 | } |
450 | } |
451 | } |
452 | |
453 | Ok(()) |
454 | } |
455 | |
456 | fn parse_integer(&mut self, positive: bool) -> Result<ParserNumber> { |
457 | let next = match tri!(self.next_char()) { |
458 | Some(b) => b, |
459 | None => { |
460 | return Err(self.error(ErrorCode::EofWhileParsingValue)); |
461 | } |
462 | }; |
463 | |
464 | match next { |
465 | b'0' => { |
466 | // There can be only one leading '0'. |
467 | match tri!(self.peek_or_null()) { |
468 | b'0' ..=b'9' => Err(self.peek_error(ErrorCode::InvalidNumber)), |
469 | _ => self.parse_number(positive, 0), |
470 | } |
471 | } |
472 | c @ b'1' ..=b'9' => { |
473 | let mut significand = (c - b'0' ) as u64; |
474 | |
475 | loop { |
476 | match tri!(self.peek_or_null()) { |
477 | c @ b'0' ..=b'9' => { |
478 | let digit = (c - b'0' ) as u64; |
479 | |
480 | // We need to be careful with overflow. If we can, |
481 | // try to keep the number as a `u64` until we grow |
482 | // too large. At that point, switch to parsing the |
483 | // value as a `f64`. |
484 | if overflow!(significand * 10 + digit, u64::max_value()) { |
485 | return Ok(ParserNumber::F64(tri!( |
486 | self.parse_long_integer(positive, significand), |
487 | ))); |
488 | } |
489 | |
490 | self.eat_char(); |
491 | significand = significand * 10 + digit; |
492 | } |
493 | _ => { |
494 | return self.parse_number(positive, significand); |
495 | } |
496 | } |
497 | } |
498 | } |
499 | _ => Err(self.error(ErrorCode::InvalidNumber)), |
500 | } |
501 | } |
502 | |
503 | fn parse_number(&mut self, positive: bool, significand: u64) -> Result<ParserNumber> { |
504 | Ok(match tri!(self.peek_or_null()) { |
505 | b'.' => ParserNumber::F64(tri!(self.parse_decimal(positive, significand, 0))), |
506 | b'e' | b'E' => ParserNumber::F64(tri!(self.parse_exponent(positive, significand, 0))), |
507 | _ => { |
508 | if positive { |
509 | ParserNumber::U64(significand) |
510 | } else { |
511 | let neg = (significand as i64).wrapping_neg(); |
512 | |
513 | // Convert into a float if we underflow, or on `-0`. |
514 | if neg >= 0 { |
515 | ParserNumber::F64(-(significand as f64)) |
516 | } else { |
517 | ParserNumber::I64(neg) |
518 | } |
519 | } |
520 | } |
521 | }) |
522 | } |
523 | |
524 | fn parse_decimal( |
525 | &mut self, |
526 | positive: bool, |
527 | mut significand: u64, |
528 | exponent_before_decimal_point: i32, |
529 | ) -> Result<f64> { |
530 | self.eat_char(); |
531 | |
532 | let mut exponent_after_decimal_point = 0; |
533 | while let c @ b'0' ..=b'9' = tri!(self.peek_or_null()) { |
534 | let digit = (c - b'0' ) as u64; |
535 | |
536 | if overflow!(significand * 10 + digit, u64::max_value()) { |
537 | let exponent = exponent_before_decimal_point + exponent_after_decimal_point; |
538 | return self.parse_decimal_overflow(positive, significand, exponent); |
539 | } |
540 | |
541 | self.eat_char(); |
542 | significand = significand * 10 + digit; |
543 | exponent_after_decimal_point -= 1; |
544 | } |
545 | |
546 | // Error if there is not at least one digit after the decimal point. |
547 | if exponent_after_decimal_point == 0 { |
548 | match tri!(self.peek()) { |
549 | Some(_) => return Err(self.peek_error(ErrorCode::InvalidNumber)), |
550 | None => return Err(self.peek_error(ErrorCode::EofWhileParsingValue)), |
551 | } |
552 | } |
553 | |
554 | let exponent = exponent_before_decimal_point + exponent_after_decimal_point; |
555 | match tri!(self.peek_or_null()) { |
556 | b'e' | b'E' => self.parse_exponent(positive, significand, exponent), |
557 | _ => self.f64_from_parts(positive, significand, exponent), |
558 | } |
559 | } |
560 | |
561 | fn parse_exponent( |
562 | &mut self, |
563 | positive: bool, |
564 | significand: u64, |
565 | starting_exp: i32, |
566 | ) -> Result<f64> { |
567 | self.eat_char(); |
568 | |
569 | let positive_exp = match tri!(self.peek_or_null()) { |
570 | b'+' => { |
571 | self.eat_char(); |
572 | true |
573 | } |
574 | b'-' => { |
575 | self.eat_char(); |
576 | false |
577 | } |
578 | _ => true, |
579 | }; |
580 | |
581 | let next = match tri!(self.next_char()) { |
582 | Some(b) => b, |
583 | None => { |
584 | return Err(self.error(ErrorCode::EofWhileParsingValue)); |
585 | } |
586 | }; |
587 | |
588 | // Make sure a digit follows the exponent place. |
589 | let mut exp = match next { |
590 | c @ b'0' ..=b'9' => (c - b'0' ) as i32, |
591 | _ => { |
592 | return Err(self.error(ErrorCode::InvalidNumber)); |
593 | } |
594 | }; |
595 | |
596 | while let c @ b'0' ..=b'9' = tri!(self.peek_or_null()) { |
597 | self.eat_char(); |
598 | let digit = (c - b'0' ) as i32; |
599 | |
600 | if overflow!(exp * 10 + digit, i32::max_value()) { |
601 | let zero_significand = significand == 0; |
602 | return self.parse_exponent_overflow(positive, zero_significand, positive_exp); |
603 | } |
604 | |
605 | exp = exp * 10 + digit; |
606 | } |
607 | |
608 | let final_exp = if positive_exp { |
609 | starting_exp.saturating_add(exp) |
610 | } else { |
611 | starting_exp.saturating_sub(exp) |
612 | }; |
613 | |
614 | self.f64_from_parts(positive, significand, final_exp) |
615 | } |
616 | |
617 | #[cfg (feature = "float_roundtrip" )] |
618 | fn f64_from_parts(&mut self, positive: bool, significand: u64, exponent: i32) -> Result<f64> { |
619 | let f = if self.single_precision { |
620 | lexical::parse_concise_float::<f32>(significand, exponent) as f64 |
621 | } else { |
622 | lexical::parse_concise_float::<f64>(significand, exponent) |
623 | }; |
624 | |
625 | if f.is_infinite() { |
626 | Err(self.error(ErrorCode::NumberOutOfRange)) |
627 | } else { |
628 | Ok(if positive { f } else { -f }) |
629 | } |
630 | } |
631 | |
632 | #[cfg (not(feature = "float_roundtrip" ))] |
633 | fn f64_from_parts( |
634 | &mut self, |
635 | positive: bool, |
636 | significand: u64, |
637 | mut exponent: i32, |
638 | ) -> Result<f64> { |
639 | let mut f = significand as f64; |
640 | loop { |
641 | match POW10.get(exponent.wrapping_abs() as usize) { |
642 | Some(&pow) => { |
643 | if exponent >= 0 { |
644 | f *= pow; |
645 | if f.is_infinite() { |
646 | return Err(self.error(ErrorCode::NumberOutOfRange)); |
647 | } |
648 | } else { |
649 | f /= pow; |
650 | } |
651 | break; |
652 | } |
653 | None => { |
654 | if f == 0.0 { |
655 | break; |
656 | } |
657 | if exponent >= 0 { |
658 | return Err(self.error(ErrorCode::NumberOutOfRange)); |
659 | } |
660 | f /= 1e308; |
661 | exponent += 308; |
662 | } |
663 | } |
664 | } |
665 | Ok(if positive { f } else { -f }) |
666 | } |
667 | |
668 | #[cfg (feature = "float_roundtrip" )] |
669 | #[cold ] |
670 | #[inline (never)] |
671 | fn parse_long_integer(&mut self, positive: bool, partial_significand: u64) -> Result<f64> { |
672 | // To deserialize floats we'll first push the integer and fraction |
673 | // parts, both as byte strings, into the scratch buffer and then feed |
674 | // both slices to lexical's parser. For example if the input is |
675 | // `12.34e5` we'll push b"1234" into scratch and then pass b"12" and |
676 | // b"34" to lexical. `integer_end` will be used to track where to split |
677 | // the scratch buffer. |
678 | // |
679 | // Note that lexical expects the integer part to contain *no* leading |
680 | // zeroes and the fraction part to contain *no* trailing zeroes. The |
681 | // first requirement is already handled by the integer parsing logic. |
682 | // The second requirement will be enforced just before passing the |
683 | // slices to lexical in f64_long_from_parts. |
684 | self.scratch.clear(); |
685 | self.scratch |
686 | .extend_from_slice(itoa::Buffer::new().format(partial_significand).as_bytes()); |
687 | |
688 | loop { |
689 | match tri!(self.peek_or_null()) { |
690 | c @ b'0' ..=b'9' => { |
691 | self.scratch.push(c); |
692 | self.eat_char(); |
693 | } |
694 | b'.' => { |
695 | self.eat_char(); |
696 | return self.parse_long_decimal(positive, self.scratch.len()); |
697 | } |
698 | b'e' | b'E' => { |
699 | return self.parse_long_exponent(positive, self.scratch.len()); |
700 | } |
701 | _ => { |
702 | return self.f64_long_from_parts(positive, self.scratch.len(), 0); |
703 | } |
704 | } |
705 | } |
706 | } |
707 | |
708 | #[cfg (not(feature = "float_roundtrip" ))] |
709 | #[cold ] |
710 | #[inline (never)] |
711 | fn parse_long_integer(&mut self, positive: bool, significand: u64) -> Result<f64> { |
712 | let mut exponent = 0; |
713 | loop { |
714 | match tri!(self.peek_or_null()) { |
715 | b'0' ..=b'9' => { |
716 | self.eat_char(); |
717 | // This could overflow... if your integer is gigabytes long. |
718 | // Ignore that possibility. |
719 | exponent += 1; |
720 | } |
721 | b'.' => { |
722 | return self.parse_decimal(positive, significand, exponent); |
723 | } |
724 | b'e' | b'E' => { |
725 | return self.parse_exponent(positive, significand, exponent); |
726 | } |
727 | _ => { |
728 | return self.f64_from_parts(positive, significand, exponent); |
729 | } |
730 | } |
731 | } |
732 | } |
733 | |
734 | #[cfg (feature = "float_roundtrip" )] |
735 | #[cold ] |
736 | fn parse_long_decimal(&mut self, positive: bool, integer_end: usize) -> Result<f64> { |
737 | let mut at_least_one_digit = integer_end < self.scratch.len(); |
738 | while let c @ b'0' ..=b'9' = tri!(self.peek_or_null()) { |
739 | self.scratch.push(c); |
740 | self.eat_char(); |
741 | at_least_one_digit = true; |
742 | } |
743 | |
744 | if !at_least_one_digit { |
745 | match tri!(self.peek()) { |
746 | Some(_) => return Err(self.peek_error(ErrorCode::InvalidNumber)), |
747 | None => return Err(self.peek_error(ErrorCode::EofWhileParsingValue)), |
748 | } |
749 | } |
750 | |
751 | match tri!(self.peek_or_null()) { |
752 | b'e' | b'E' => self.parse_long_exponent(positive, integer_end), |
753 | _ => self.f64_long_from_parts(positive, integer_end, 0), |
754 | } |
755 | } |
756 | |
757 | #[cfg (feature = "float_roundtrip" )] |
758 | fn parse_long_exponent(&mut self, positive: bool, integer_end: usize) -> Result<f64> { |
759 | self.eat_char(); |
760 | |
761 | let positive_exp = match tri!(self.peek_or_null()) { |
762 | b'+' => { |
763 | self.eat_char(); |
764 | true |
765 | } |
766 | b'-' => { |
767 | self.eat_char(); |
768 | false |
769 | } |
770 | _ => true, |
771 | }; |
772 | |
773 | let next = match tri!(self.next_char()) { |
774 | Some(b) => b, |
775 | None => { |
776 | return Err(self.error(ErrorCode::EofWhileParsingValue)); |
777 | } |
778 | }; |
779 | |
780 | // Make sure a digit follows the exponent place. |
781 | let mut exp = match next { |
782 | c @ b'0' ..=b'9' => (c - b'0' ) as i32, |
783 | _ => { |
784 | return Err(self.error(ErrorCode::InvalidNumber)); |
785 | } |
786 | }; |
787 | |
788 | while let c @ b'0' ..=b'9' = tri!(self.peek_or_null()) { |
789 | self.eat_char(); |
790 | let digit = (c - b'0' ) as i32; |
791 | |
792 | if overflow!(exp * 10 + digit, i32::max_value()) { |
793 | let zero_significand = self.scratch.iter().all(|&digit| digit == b'0' ); |
794 | return self.parse_exponent_overflow(positive, zero_significand, positive_exp); |
795 | } |
796 | |
797 | exp = exp * 10 + digit; |
798 | } |
799 | |
800 | let final_exp = if positive_exp { exp } else { -exp }; |
801 | |
802 | self.f64_long_from_parts(positive, integer_end, final_exp) |
803 | } |
804 | |
805 | // This cold code should not be inlined into the middle of the hot |
806 | // decimal-parsing loop above. |
807 | #[cfg (feature = "float_roundtrip" )] |
808 | #[cold ] |
809 | #[inline (never)] |
810 | fn parse_decimal_overflow( |
811 | &mut self, |
812 | positive: bool, |
813 | significand: u64, |
814 | exponent: i32, |
815 | ) -> Result<f64> { |
816 | let mut buffer = itoa::Buffer::new(); |
817 | let significand = buffer.format(significand); |
818 | let fraction_digits = -exponent as usize; |
819 | self.scratch.clear(); |
820 | if let Some(zeros) = fraction_digits.checked_sub(significand.len() + 1) { |
821 | self.scratch.extend(iter::repeat(b'0' ).take(zeros + 1)); |
822 | } |
823 | self.scratch.extend_from_slice(significand.as_bytes()); |
824 | let integer_end = self.scratch.len() - fraction_digits; |
825 | self.parse_long_decimal(positive, integer_end) |
826 | } |
827 | |
828 | #[cfg (not(feature = "float_roundtrip" ))] |
829 | #[cold ] |
830 | #[inline (never)] |
831 | fn parse_decimal_overflow( |
832 | &mut self, |
833 | positive: bool, |
834 | significand: u64, |
835 | exponent: i32, |
836 | ) -> Result<f64> { |
837 | // The next multiply/add would overflow, so just ignore all further |
838 | // digits. |
839 | while let b'0' ..=b'9' = tri!(self.peek_or_null()) { |
840 | self.eat_char(); |
841 | } |
842 | |
843 | match tri!(self.peek_or_null()) { |
844 | b'e' | b'E' => self.parse_exponent(positive, significand, exponent), |
845 | _ => self.f64_from_parts(positive, significand, exponent), |
846 | } |
847 | } |
848 | |
849 | // This cold code should not be inlined into the middle of the hot |
850 | // exponent-parsing loop above. |
851 | #[cold ] |
852 | #[inline (never)] |
853 | fn parse_exponent_overflow( |
854 | &mut self, |
855 | positive: bool, |
856 | zero_significand: bool, |
857 | positive_exp: bool, |
858 | ) -> Result<f64> { |
859 | // Error instead of +/- infinity. |
860 | if !zero_significand && positive_exp { |
861 | return Err(self.error(ErrorCode::NumberOutOfRange)); |
862 | } |
863 | |
864 | while let b'0' ..=b'9' = tri!(self.peek_or_null()) { |
865 | self.eat_char(); |
866 | } |
867 | Ok(if positive { 0.0 } else { -0.0 }) |
868 | } |
869 | |
870 | #[cfg (feature = "float_roundtrip" )] |
871 | fn f64_long_from_parts( |
872 | &mut self, |
873 | positive: bool, |
874 | integer_end: usize, |
875 | exponent: i32, |
876 | ) -> Result<f64> { |
877 | let integer = &self.scratch[..integer_end]; |
878 | let fraction = &self.scratch[integer_end..]; |
879 | |
880 | let f = if self.single_precision { |
881 | lexical::parse_truncated_float::<f32>(integer, fraction, exponent) as f64 |
882 | } else { |
883 | lexical::parse_truncated_float::<f64>(integer, fraction, exponent) |
884 | }; |
885 | |
886 | if f.is_infinite() { |
887 | Err(self.error(ErrorCode::NumberOutOfRange)) |
888 | } else { |
889 | Ok(if positive { f } else { -f }) |
890 | } |
891 | } |
892 | |
893 | fn parse_any_signed_number(&mut self) -> Result<ParserNumber> { |
894 | let peek = match tri!(self.peek()) { |
895 | Some(b) => b, |
896 | None => { |
897 | return Err(self.peek_error(ErrorCode::EofWhileParsingValue)); |
898 | } |
899 | }; |
900 | |
901 | let value = match peek { |
902 | b'-' => { |
903 | self.eat_char(); |
904 | self.parse_any_number(false) |
905 | } |
906 | b'0' ..=b'9' => self.parse_any_number(true), |
907 | _ => Err(self.peek_error(ErrorCode::InvalidNumber)), |
908 | }; |
909 | |
910 | let value = match tri!(self.peek()) { |
911 | Some(_) => Err(self.peek_error(ErrorCode::InvalidNumber)), |
912 | None => value, |
913 | }; |
914 | |
915 | match value { |
916 | Ok(value) => Ok(value), |
917 | // The de::Error impl creates errors with unknown line and column. |
918 | // Fill in the position here by looking at the current index in the |
919 | // input. There is no way to tell whether this should call `error` |
920 | // or `peek_error` so pick the one that seems correct more often. |
921 | // Worst case, the position is off by one character. |
922 | Err(err) => Err(self.fix_position(err)), |
923 | } |
924 | } |
925 | |
926 | #[cfg (not(feature = "arbitrary_precision" ))] |
927 | fn parse_any_number(&mut self, positive: bool) -> Result<ParserNumber> { |
928 | self.parse_integer(positive) |
929 | } |
930 | |
931 | #[cfg (feature = "arbitrary_precision" )] |
932 | fn parse_any_number(&mut self, positive: bool) -> Result<ParserNumber> { |
933 | let mut buf = String::with_capacity(16); |
934 | if !positive { |
935 | buf.push('-' ); |
936 | } |
937 | tri!(self.scan_integer(&mut buf)); |
938 | if positive { |
939 | if let Ok(unsigned) = buf.parse() { |
940 | return Ok(ParserNumber::U64(unsigned)); |
941 | } |
942 | } else { |
943 | if let Ok(signed) = buf.parse() { |
944 | return Ok(ParserNumber::I64(signed)); |
945 | } |
946 | } |
947 | Ok(ParserNumber::String(buf)) |
948 | } |
949 | |
950 | #[cfg (feature = "arbitrary_precision" )] |
951 | fn scan_or_eof(&mut self, buf: &mut String) -> Result<u8> { |
952 | match tri!(self.next_char()) { |
953 | Some(b) => { |
954 | buf.push(b as char); |
955 | Ok(b) |
956 | } |
957 | None => Err(self.error(ErrorCode::EofWhileParsingValue)), |
958 | } |
959 | } |
960 | |
961 | #[cfg (feature = "arbitrary_precision" )] |
962 | fn scan_integer(&mut self, buf: &mut String) -> Result<()> { |
963 | match tri!(self.scan_or_eof(buf)) { |
964 | b'0' => { |
965 | // There can be only one leading '0'. |
966 | match tri!(self.peek_or_null()) { |
967 | b'0' ..=b'9' => Err(self.peek_error(ErrorCode::InvalidNumber)), |
968 | _ => self.scan_number(buf), |
969 | } |
970 | } |
971 | b'1' ..=b'9' => loop { |
972 | match tri!(self.peek_or_null()) { |
973 | c @ b'0' ..=b'9' => { |
974 | self.eat_char(); |
975 | buf.push(c as char); |
976 | } |
977 | _ => { |
978 | return self.scan_number(buf); |
979 | } |
980 | } |
981 | }, |
982 | _ => Err(self.error(ErrorCode::InvalidNumber)), |
983 | } |
984 | } |
985 | |
986 | #[cfg (feature = "arbitrary_precision" )] |
987 | fn scan_number(&mut self, buf: &mut String) -> Result<()> { |
988 | match tri!(self.peek_or_null()) { |
989 | b'.' => self.scan_decimal(buf), |
990 | e @ (b'e' | b'E' ) => self.scan_exponent(e as char, buf), |
991 | _ => Ok(()), |
992 | } |
993 | } |
994 | |
995 | #[cfg (feature = "arbitrary_precision" )] |
996 | fn scan_decimal(&mut self, buf: &mut String) -> Result<()> { |
997 | self.eat_char(); |
998 | buf.push('.' ); |
999 | |
1000 | let mut at_least_one_digit = false; |
1001 | while let c @ b'0' ..=b'9' = tri!(self.peek_or_null()) { |
1002 | self.eat_char(); |
1003 | buf.push(c as char); |
1004 | at_least_one_digit = true; |
1005 | } |
1006 | |
1007 | if !at_least_one_digit { |
1008 | match tri!(self.peek()) { |
1009 | Some(_) => return Err(self.peek_error(ErrorCode::InvalidNumber)), |
1010 | None => return Err(self.peek_error(ErrorCode::EofWhileParsingValue)), |
1011 | } |
1012 | } |
1013 | |
1014 | match tri!(self.peek_or_null()) { |
1015 | e @ (b'e' | b'E' ) => self.scan_exponent(e as char, buf), |
1016 | _ => Ok(()), |
1017 | } |
1018 | } |
1019 | |
1020 | #[cfg (feature = "arbitrary_precision" )] |
1021 | fn scan_exponent(&mut self, e: char, buf: &mut String) -> Result<()> { |
1022 | self.eat_char(); |
1023 | buf.push(e); |
1024 | |
1025 | match tri!(self.peek_or_null()) { |
1026 | b'+' => { |
1027 | self.eat_char(); |
1028 | buf.push('+' ); |
1029 | } |
1030 | b'-' => { |
1031 | self.eat_char(); |
1032 | buf.push('-' ); |
1033 | } |
1034 | _ => {} |
1035 | } |
1036 | |
1037 | // Make sure a digit follows the exponent place. |
1038 | match tri!(self.scan_or_eof(buf)) { |
1039 | b'0' ..=b'9' => {} |
1040 | _ => { |
1041 | return Err(self.error(ErrorCode::InvalidNumber)); |
1042 | } |
1043 | } |
1044 | |
1045 | while let c @ b'0' ..=b'9' = tri!(self.peek_or_null()) { |
1046 | self.eat_char(); |
1047 | buf.push(c as char); |
1048 | } |
1049 | |
1050 | Ok(()) |
1051 | } |
1052 | |
1053 | fn parse_object_colon(&mut self) -> Result<()> { |
1054 | match tri!(self.parse_whitespace()) { |
1055 | Some(b':' ) => { |
1056 | self.eat_char(); |
1057 | Ok(()) |
1058 | } |
1059 | Some(_) => Err(self.peek_error(ErrorCode::ExpectedColon)), |
1060 | None => Err(self.peek_error(ErrorCode::EofWhileParsingObject)), |
1061 | } |
1062 | } |
1063 | |
1064 | fn end_seq(&mut self) -> Result<()> { |
1065 | match tri!(self.parse_whitespace()) { |
1066 | Some(b']' ) => { |
1067 | self.eat_char(); |
1068 | Ok(()) |
1069 | } |
1070 | Some(b',' ) => { |
1071 | self.eat_char(); |
1072 | match self.parse_whitespace() { |
1073 | Ok(Some(b']' )) => Err(self.peek_error(ErrorCode::TrailingComma)), |
1074 | _ => Err(self.peek_error(ErrorCode::TrailingCharacters)), |
1075 | } |
1076 | } |
1077 | Some(_) => Err(self.peek_error(ErrorCode::TrailingCharacters)), |
1078 | None => Err(self.peek_error(ErrorCode::EofWhileParsingList)), |
1079 | } |
1080 | } |
1081 | |
1082 | fn end_map(&mut self) -> Result<()> { |
1083 | match tri!(self.parse_whitespace()) { |
1084 | Some(b'}' ) => { |
1085 | self.eat_char(); |
1086 | Ok(()) |
1087 | } |
1088 | Some(b',' ) => Err(self.peek_error(ErrorCode::TrailingComma)), |
1089 | Some(_) => Err(self.peek_error(ErrorCode::TrailingCharacters)), |
1090 | None => Err(self.peek_error(ErrorCode::EofWhileParsingObject)), |
1091 | } |
1092 | } |
1093 | |
1094 | fn ignore_value(&mut self) -> Result<()> { |
1095 | self.scratch.clear(); |
1096 | let mut enclosing = None; |
1097 | |
1098 | loop { |
1099 | let peek = match tri!(self.parse_whitespace()) { |
1100 | Some(b) => b, |
1101 | None => { |
1102 | return Err(self.peek_error(ErrorCode::EofWhileParsingValue)); |
1103 | } |
1104 | }; |
1105 | |
1106 | let frame = match peek { |
1107 | b'n' => { |
1108 | self.eat_char(); |
1109 | tri!(self.parse_ident(b"ull" )); |
1110 | None |
1111 | } |
1112 | b't' => { |
1113 | self.eat_char(); |
1114 | tri!(self.parse_ident(b"rue" )); |
1115 | None |
1116 | } |
1117 | b'f' => { |
1118 | self.eat_char(); |
1119 | tri!(self.parse_ident(b"alse" )); |
1120 | None |
1121 | } |
1122 | b'-' => { |
1123 | self.eat_char(); |
1124 | tri!(self.ignore_integer()); |
1125 | None |
1126 | } |
1127 | b'0' ..=b'9' => { |
1128 | tri!(self.ignore_integer()); |
1129 | None |
1130 | } |
1131 | b'"' => { |
1132 | self.eat_char(); |
1133 | tri!(self.read.ignore_str()); |
1134 | None |
1135 | } |
1136 | frame @ (b'[' | b'{' ) => { |
1137 | self.scratch.extend(enclosing.take()); |
1138 | self.eat_char(); |
1139 | Some(frame) |
1140 | } |
1141 | _ => return Err(self.peek_error(ErrorCode::ExpectedSomeValue)), |
1142 | }; |
1143 | |
1144 | let (mut accept_comma, mut frame) = match frame { |
1145 | Some(frame) => (false, frame), |
1146 | None => match enclosing.take() { |
1147 | Some(frame) => (true, frame), |
1148 | None => match self.scratch.pop() { |
1149 | Some(frame) => (true, frame), |
1150 | None => return Ok(()), |
1151 | }, |
1152 | }, |
1153 | }; |
1154 | |
1155 | loop { |
1156 | match tri!(self.parse_whitespace()) { |
1157 | Some(b',' ) if accept_comma => { |
1158 | self.eat_char(); |
1159 | break; |
1160 | } |
1161 | Some(b']' ) if frame == b'[' => {} |
1162 | Some(b'}' ) if frame == b'{' => {} |
1163 | Some(_) => { |
1164 | if accept_comma { |
1165 | return Err(self.peek_error(match frame { |
1166 | b'[' => ErrorCode::ExpectedListCommaOrEnd, |
1167 | b'{' => ErrorCode::ExpectedObjectCommaOrEnd, |
1168 | _ => unreachable!(), |
1169 | })); |
1170 | } else { |
1171 | break; |
1172 | } |
1173 | } |
1174 | None => { |
1175 | return Err(self.peek_error(match frame { |
1176 | b'[' => ErrorCode::EofWhileParsingList, |
1177 | b'{' => ErrorCode::EofWhileParsingObject, |
1178 | _ => unreachable!(), |
1179 | })); |
1180 | } |
1181 | } |
1182 | |
1183 | self.eat_char(); |
1184 | frame = match self.scratch.pop() { |
1185 | Some(frame) => frame, |
1186 | None => return Ok(()), |
1187 | }; |
1188 | accept_comma = true; |
1189 | } |
1190 | |
1191 | if frame == b'{' { |
1192 | match tri!(self.parse_whitespace()) { |
1193 | Some(b'"' ) => self.eat_char(), |
1194 | Some(_) => return Err(self.peek_error(ErrorCode::KeyMustBeAString)), |
1195 | None => return Err(self.peek_error(ErrorCode::EofWhileParsingObject)), |
1196 | } |
1197 | tri!(self.read.ignore_str()); |
1198 | match tri!(self.parse_whitespace()) { |
1199 | Some(b':' ) => self.eat_char(), |
1200 | Some(_) => return Err(self.peek_error(ErrorCode::ExpectedColon)), |
1201 | None => return Err(self.peek_error(ErrorCode::EofWhileParsingObject)), |
1202 | } |
1203 | } |
1204 | |
1205 | enclosing = Some(frame); |
1206 | } |
1207 | } |
1208 | |
1209 | fn ignore_integer(&mut self) -> Result<()> { |
1210 | match tri!(self.next_char_or_null()) { |
1211 | b'0' => { |
1212 | // There can be only one leading '0'. |
1213 | if let b'0' ..=b'9' = tri!(self.peek_or_null()) { |
1214 | return Err(self.peek_error(ErrorCode::InvalidNumber)); |
1215 | } |
1216 | } |
1217 | b'1' ..=b'9' => { |
1218 | while let b'0' ..=b'9' = tri!(self.peek_or_null()) { |
1219 | self.eat_char(); |
1220 | } |
1221 | } |
1222 | _ => { |
1223 | return Err(self.error(ErrorCode::InvalidNumber)); |
1224 | } |
1225 | } |
1226 | |
1227 | match tri!(self.peek_or_null()) { |
1228 | b'.' => self.ignore_decimal(), |
1229 | b'e' | b'E' => self.ignore_exponent(), |
1230 | _ => Ok(()), |
1231 | } |
1232 | } |
1233 | |
1234 | fn ignore_decimal(&mut self) -> Result<()> { |
1235 | self.eat_char(); |
1236 | |
1237 | let mut at_least_one_digit = false; |
1238 | while let b'0' ..=b'9' = tri!(self.peek_or_null()) { |
1239 | self.eat_char(); |
1240 | at_least_one_digit = true; |
1241 | } |
1242 | |
1243 | if !at_least_one_digit { |
1244 | return Err(self.peek_error(ErrorCode::InvalidNumber)); |
1245 | } |
1246 | |
1247 | match tri!(self.peek_or_null()) { |
1248 | b'e' | b'E' => self.ignore_exponent(), |
1249 | _ => Ok(()), |
1250 | } |
1251 | } |
1252 | |
1253 | fn ignore_exponent(&mut self) -> Result<()> { |
1254 | self.eat_char(); |
1255 | |
1256 | match tri!(self.peek_or_null()) { |
1257 | b'+' | b'-' => self.eat_char(), |
1258 | _ => {} |
1259 | } |
1260 | |
1261 | // Make sure a digit follows the exponent place. |
1262 | match tri!(self.next_char_or_null()) { |
1263 | b'0' ..=b'9' => {} |
1264 | _ => { |
1265 | return Err(self.error(ErrorCode::InvalidNumber)); |
1266 | } |
1267 | } |
1268 | |
1269 | while let b'0' ..=b'9' = tri!(self.peek_or_null()) { |
1270 | self.eat_char(); |
1271 | } |
1272 | |
1273 | Ok(()) |
1274 | } |
1275 | |
1276 | #[cfg (feature = "raw_value" )] |
1277 | fn deserialize_raw_value<V>(&mut self, visitor: V) -> Result<V::Value> |
1278 | where |
1279 | V: de::Visitor<'de>, |
1280 | { |
1281 | tri!(self.parse_whitespace()); |
1282 | self.read.begin_raw_buffering(); |
1283 | tri!(self.ignore_value()); |
1284 | self.read.end_raw_buffering(visitor) |
1285 | } |
1286 | } |
1287 | |
1288 | impl FromStr for Number { |
1289 | type Err = Error; |
1290 | |
1291 | fn from_str(s: &str) -> result::Result<Self, Self::Err> { |
1292 | Deserializer::from_str(s) |
1293 | .parse_any_signed_number() |
1294 | .map(op:Into::into) |
1295 | } |
1296 | } |
1297 | |
1298 | #[cfg (not(feature = "float_roundtrip" ))] |
1299 | static POW10: [f64; 309] = [ |
1300 | 1e000, 1e001, 1e002, 1e003, 1e004, 1e005, 1e006, 1e007, 1e008, 1e009, // |
1301 | 1e010, 1e011, 1e012, 1e013, 1e014, 1e015, 1e016, 1e017, 1e018, 1e019, // |
1302 | 1e020, 1e021, 1e022, 1e023, 1e024, 1e025, 1e026, 1e027, 1e028, 1e029, // |
1303 | 1e030, 1e031, 1e032, 1e033, 1e034, 1e035, 1e036, 1e037, 1e038, 1e039, // |
1304 | 1e040, 1e041, 1e042, 1e043, 1e044, 1e045, 1e046, 1e047, 1e048, 1e049, // |
1305 | 1e050, 1e051, 1e052, 1e053, 1e054, 1e055, 1e056, 1e057, 1e058, 1e059, // |
1306 | 1e060, 1e061, 1e062, 1e063, 1e064, 1e065, 1e066, 1e067, 1e068, 1e069, // |
1307 | 1e070, 1e071, 1e072, 1e073, 1e074, 1e075, 1e076, 1e077, 1e078, 1e079, // |
1308 | 1e080, 1e081, 1e082, 1e083, 1e084, 1e085, 1e086, 1e087, 1e088, 1e089, // |
1309 | 1e090, 1e091, 1e092, 1e093, 1e094, 1e095, 1e096, 1e097, 1e098, 1e099, // |
1310 | 1e100, 1e101, 1e102, 1e103, 1e104, 1e105, 1e106, 1e107, 1e108, 1e109, // |
1311 | 1e110, 1e111, 1e112, 1e113, 1e114, 1e115, 1e116, 1e117, 1e118, 1e119, // |
1312 | 1e120, 1e121, 1e122, 1e123, 1e124, 1e125, 1e126, 1e127, 1e128, 1e129, // |
1313 | 1e130, 1e131, 1e132, 1e133, 1e134, 1e135, 1e136, 1e137, 1e138, 1e139, // |
1314 | 1e140, 1e141, 1e142, 1e143, 1e144, 1e145, 1e146, 1e147, 1e148, 1e149, // |
1315 | 1e150, 1e151, 1e152, 1e153, 1e154, 1e155, 1e156, 1e157, 1e158, 1e159, // |
1316 | 1e160, 1e161, 1e162, 1e163, 1e164, 1e165, 1e166, 1e167, 1e168, 1e169, // |
1317 | 1e170, 1e171, 1e172, 1e173, 1e174, 1e175, 1e176, 1e177, 1e178, 1e179, // |
1318 | 1e180, 1e181, 1e182, 1e183, 1e184, 1e185, 1e186, 1e187, 1e188, 1e189, // |
1319 | 1e190, 1e191, 1e192, 1e193, 1e194, 1e195, 1e196, 1e197, 1e198, 1e199, // |
1320 | 1e200, 1e201, 1e202, 1e203, 1e204, 1e205, 1e206, 1e207, 1e208, 1e209, // |
1321 | 1e210, 1e211, 1e212, 1e213, 1e214, 1e215, 1e216, 1e217, 1e218, 1e219, // |
1322 | 1e220, 1e221, 1e222, 1e223, 1e224, 1e225, 1e226, 1e227, 1e228, 1e229, // |
1323 | 1e230, 1e231, 1e232, 1e233, 1e234, 1e235, 1e236, 1e237, 1e238, 1e239, // |
1324 | 1e240, 1e241, 1e242, 1e243, 1e244, 1e245, 1e246, 1e247, 1e248, 1e249, // |
1325 | 1e250, 1e251, 1e252, 1e253, 1e254, 1e255, 1e256, 1e257, 1e258, 1e259, // |
1326 | 1e260, 1e261, 1e262, 1e263, 1e264, 1e265, 1e266, 1e267, 1e268, 1e269, // |
1327 | 1e270, 1e271, 1e272, 1e273, 1e274, 1e275, 1e276, 1e277, 1e278, 1e279, // |
1328 | 1e280, 1e281, 1e282, 1e283, 1e284, 1e285, 1e286, 1e287, 1e288, 1e289, // |
1329 | 1e290, 1e291, 1e292, 1e293, 1e294, 1e295, 1e296, 1e297, 1e298, 1e299, // |
1330 | 1e300, 1e301, 1e302, 1e303, 1e304, 1e305, 1e306, 1e307, 1e308, |
1331 | ]; |
1332 | |
1333 | macro_rules! deserialize_number { |
1334 | ($method:ident) => { |
1335 | deserialize_number!($method, deserialize_number); |
1336 | }; |
1337 | |
1338 | ($method:ident, $using:ident) => { |
1339 | fn $method<V>(self, visitor: V) -> Result<V::Value> |
1340 | where |
1341 | V: de::Visitor<'de>, |
1342 | { |
1343 | self.$using(visitor) |
1344 | } |
1345 | }; |
1346 | } |
1347 | |
1348 | #[cfg (not(feature = "unbounded_depth" ))] |
1349 | macro_rules! if_checking_recursion_limit { |
1350 | ($($body:tt)*) => { |
1351 | $($body)* |
1352 | }; |
1353 | } |
1354 | |
1355 | #[cfg (feature = "unbounded_depth" )] |
1356 | macro_rules! if_checking_recursion_limit { |
1357 | ($this:ident $($body:tt)*) => { |
1358 | if !$this.disable_recursion_limit { |
1359 | $this $($body)* |
1360 | } |
1361 | }; |
1362 | } |
1363 | |
1364 | macro_rules! check_recursion { |
1365 | ($this:ident $($body:tt)*) => { |
1366 | if_checking_recursion_limit! { |
1367 | $this.remaining_depth -= 1; |
1368 | if $this.remaining_depth == 0 { |
1369 | return Err($this.peek_error(ErrorCode::RecursionLimitExceeded)); |
1370 | } |
1371 | } |
1372 | |
1373 | $this $($body)* |
1374 | |
1375 | if_checking_recursion_limit! { |
1376 | $this.remaining_depth += 1; |
1377 | } |
1378 | }; |
1379 | } |
1380 | |
1381 | impl<'de, 'a, R: Read<'de>> de::Deserializer<'de> for &'a mut Deserializer<R> { |
1382 | type Error = Error; |
1383 | |
1384 | #[inline ] |
1385 | fn deserialize_any<V>(self, visitor: V) -> Result<V::Value> |
1386 | where |
1387 | V: de::Visitor<'de>, |
1388 | { |
1389 | let peek = match tri!(self.parse_whitespace()) { |
1390 | Some(b) => b, |
1391 | None => { |
1392 | return Err(self.peek_error(ErrorCode::EofWhileParsingValue)); |
1393 | } |
1394 | }; |
1395 | |
1396 | let value = match peek { |
1397 | b'n' => { |
1398 | self.eat_char(); |
1399 | tri!(self.parse_ident(b"ull" )); |
1400 | visitor.visit_unit() |
1401 | } |
1402 | b't' => { |
1403 | self.eat_char(); |
1404 | tri!(self.parse_ident(b"rue" )); |
1405 | visitor.visit_bool(true) |
1406 | } |
1407 | b'f' => { |
1408 | self.eat_char(); |
1409 | tri!(self.parse_ident(b"alse" )); |
1410 | visitor.visit_bool(false) |
1411 | } |
1412 | b'-' => { |
1413 | self.eat_char(); |
1414 | tri!(self.parse_any_number(false)).visit(visitor) |
1415 | } |
1416 | b'0' ..=b'9' => tri!(self.parse_any_number(true)).visit(visitor), |
1417 | b'"' => { |
1418 | self.eat_char(); |
1419 | self.scratch.clear(); |
1420 | match tri!(self.read.parse_str(&mut self.scratch)) { |
1421 | Reference::Borrowed(s) => visitor.visit_borrowed_str(s), |
1422 | Reference::Copied(s) => visitor.visit_str(s), |
1423 | } |
1424 | } |
1425 | b'[' => { |
1426 | check_recursion! { |
1427 | self.eat_char(); |
1428 | let ret = visitor.visit_seq(SeqAccess::new(self)); |
1429 | } |
1430 | |
1431 | match (ret, self.end_seq()) { |
1432 | (Ok(ret), Ok(())) => Ok(ret), |
1433 | (Err(err), _) | (_, Err(err)) => Err(err), |
1434 | } |
1435 | } |
1436 | b'{' => { |
1437 | check_recursion! { |
1438 | self.eat_char(); |
1439 | let ret = visitor.visit_map(MapAccess::new(self)); |
1440 | } |
1441 | |
1442 | match (ret, self.end_map()) { |
1443 | (Ok(ret), Ok(())) => Ok(ret), |
1444 | (Err(err), _) | (_, Err(err)) => Err(err), |
1445 | } |
1446 | } |
1447 | _ => Err(self.peek_error(ErrorCode::ExpectedSomeValue)), |
1448 | }; |
1449 | |
1450 | match value { |
1451 | Ok(value) => Ok(value), |
1452 | // The de::Error impl creates errors with unknown line and column. |
1453 | // Fill in the position here by looking at the current index in the |
1454 | // input. There is no way to tell whether this should call `error` |
1455 | // or `peek_error` so pick the one that seems correct more often. |
1456 | // Worst case, the position is off by one character. |
1457 | Err(err) => Err(self.fix_position(err)), |
1458 | } |
1459 | } |
1460 | |
1461 | fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value> |
1462 | where |
1463 | V: de::Visitor<'de>, |
1464 | { |
1465 | let peek = match tri!(self.parse_whitespace()) { |
1466 | Some(b) => b, |
1467 | None => { |
1468 | return Err(self.peek_error(ErrorCode::EofWhileParsingValue)); |
1469 | } |
1470 | }; |
1471 | |
1472 | let value = match peek { |
1473 | b't' => { |
1474 | self.eat_char(); |
1475 | tri!(self.parse_ident(b"rue" )); |
1476 | visitor.visit_bool(true) |
1477 | } |
1478 | b'f' => { |
1479 | self.eat_char(); |
1480 | tri!(self.parse_ident(b"alse" )); |
1481 | visitor.visit_bool(false) |
1482 | } |
1483 | _ => Err(self.peek_invalid_type(&visitor)), |
1484 | }; |
1485 | |
1486 | match value { |
1487 | Ok(value) => Ok(value), |
1488 | Err(err) => Err(self.fix_position(err)), |
1489 | } |
1490 | } |
1491 | |
1492 | deserialize_number!(deserialize_i8); |
1493 | deserialize_number!(deserialize_i16); |
1494 | deserialize_number!(deserialize_i32); |
1495 | deserialize_number!(deserialize_i64); |
1496 | deserialize_number!(deserialize_u8); |
1497 | deserialize_number!(deserialize_u16); |
1498 | deserialize_number!(deserialize_u32); |
1499 | deserialize_number!(deserialize_u64); |
1500 | #[cfg (not(feature = "float_roundtrip" ))] |
1501 | deserialize_number!(deserialize_f32); |
1502 | deserialize_number!(deserialize_f64); |
1503 | |
1504 | #[cfg (feature = "float_roundtrip" )] |
1505 | deserialize_number!(deserialize_f32, do_deserialize_f32); |
1506 | deserialize_number!(deserialize_i128, do_deserialize_i128); |
1507 | deserialize_number!(deserialize_u128, do_deserialize_u128); |
1508 | |
1509 | fn deserialize_char<V>(self, visitor: V) -> Result<V::Value> |
1510 | where |
1511 | V: de::Visitor<'de>, |
1512 | { |
1513 | self.deserialize_str(visitor) |
1514 | } |
1515 | |
1516 | fn deserialize_str<V>(self, visitor: V) -> Result<V::Value> |
1517 | where |
1518 | V: de::Visitor<'de>, |
1519 | { |
1520 | let peek = match tri!(self.parse_whitespace()) { |
1521 | Some(b) => b, |
1522 | None => { |
1523 | return Err(self.peek_error(ErrorCode::EofWhileParsingValue)); |
1524 | } |
1525 | }; |
1526 | |
1527 | let value = match peek { |
1528 | b'"' => { |
1529 | self.eat_char(); |
1530 | self.scratch.clear(); |
1531 | match tri!(self.read.parse_str(&mut self.scratch)) { |
1532 | Reference::Borrowed(s) => visitor.visit_borrowed_str(s), |
1533 | Reference::Copied(s) => visitor.visit_str(s), |
1534 | } |
1535 | } |
1536 | _ => Err(self.peek_invalid_type(&visitor)), |
1537 | }; |
1538 | |
1539 | match value { |
1540 | Ok(value) => Ok(value), |
1541 | Err(err) => Err(self.fix_position(err)), |
1542 | } |
1543 | } |
1544 | |
1545 | fn deserialize_string<V>(self, visitor: V) -> Result<V::Value> |
1546 | where |
1547 | V: de::Visitor<'de>, |
1548 | { |
1549 | self.deserialize_str(visitor) |
1550 | } |
1551 | |
1552 | /// Parses a JSON string as bytes. Note that this function does not check |
1553 | /// whether the bytes represent a valid UTF-8 string. |
1554 | /// |
1555 | /// The relevant part of the JSON specification is Section 8.2 of [RFC |
1556 | /// 7159]: |
1557 | /// |
1558 | /// > When all the strings represented in a JSON text are composed entirely |
1559 | /// > of Unicode characters (however escaped), then that JSON text is |
1560 | /// > interoperable in the sense that all software implementations that |
1561 | /// > parse it will agree on the contents of names and of string values in |
1562 | /// > objects and arrays. |
1563 | /// > |
1564 | /// > However, the ABNF in this specification allows member names and string |
1565 | /// > values to contain bit sequences that cannot encode Unicode characters; |
1566 | /// > for example, "\uDEAD" (a single unpaired UTF-16 surrogate). Instances |
1567 | /// > of this have been observed, for example, when a library truncates a |
1568 | /// > UTF-16 string without checking whether the truncation split a |
1569 | /// > surrogate pair. The behavior of software that receives JSON texts |
1570 | /// > containing such values is unpredictable; for example, implementations |
1571 | /// > might return different values for the length of a string value or even |
1572 | /// > suffer fatal runtime exceptions. |
1573 | /// |
1574 | /// [RFC 7159]: https://tools.ietf.org/html/rfc7159 |
1575 | /// |
1576 | /// The behavior of serde_json is specified to fail on non-UTF-8 strings |
1577 | /// when deserializing into Rust UTF-8 string types such as String, and |
1578 | /// succeed with non-UTF-8 bytes when deserializing using this method. |
1579 | /// |
1580 | /// Escape sequences are processed as usual, and for `\uXXXX` escapes it is |
1581 | /// still checked if the hex number represents a valid Unicode code point. |
1582 | /// |
1583 | /// # Examples |
1584 | /// |
1585 | /// You can use this to parse JSON strings containing invalid UTF-8 bytes, |
1586 | /// or unpaired surrogates. |
1587 | /// |
1588 | /// ``` |
1589 | /// use serde_bytes::ByteBuf; |
1590 | /// |
1591 | /// fn look_at_bytes() -> Result<(), serde_json::Error> { |
1592 | /// let json_data = b" \"some bytes: \xe5\x00\xe5\"" ; |
1593 | /// let bytes: ByteBuf = serde_json::from_slice(json_data)?; |
1594 | /// |
1595 | /// assert_eq!(b' \xe5' , bytes[12]); |
1596 | /// assert_eq!(b' \0' , bytes[13]); |
1597 | /// assert_eq!(b' \xe5' , bytes[14]); |
1598 | /// |
1599 | /// Ok(()) |
1600 | /// } |
1601 | /// # |
1602 | /// # look_at_bytes().unwrap(); |
1603 | /// ``` |
1604 | /// |
1605 | /// Backslash escape sequences like `\n` are still interpreted and required |
1606 | /// to be valid. `\u` escape sequences are required to represent a valid |
1607 | /// Unicode code point or lone surrogate. |
1608 | /// |
1609 | /// ``` |
1610 | /// use serde_bytes::ByteBuf; |
1611 | /// |
1612 | /// fn look_at_bytes() -> Result<(), serde_json::Error> { |
1613 | /// let json_data = b" \"lone surrogate: \\uD801 \"" ; |
1614 | /// let bytes: ByteBuf = serde_json::from_slice(json_data)?; |
1615 | /// let expected = b"lone surrogate: \xED\xA0\x81" ; |
1616 | /// assert_eq!(expected, bytes.as_slice()); |
1617 | /// Ok(()) |
1618 | /// } |
1619 | /// # |
1620 | /// # look_at_bytes(); |
1621 | /// ``` |
1622 | fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value> |
1623 | where |
1624 | V: de::Visitor<'de>, |
1625 | { |
1626 | let peek = match tri!(self.parse_whitespace()) { |
1627 | Some(b) => b, |
1628 | None => { |
1629 | return Err(self.peek_error(ErrorCode::EofWhileParsingValue)); |
1630 | } |
1631 | }; |
1632 | |
1633 | let value = match peek { |
1634 | b'"' => { |
1635 | self.eat_char(); |
1636 | self.scratch.clear(); |
1637 | match tri!(self.read.parse_str_raw(&mut self.scratch)) { |
1638 | Reference::Borrowed(b) => visitor.visit_borrowed_bytes(b), |
1639 | Reference::Copied(b) => visitor.visit_bytes(b), |
1640 | } |
1641 | } |
1642 | b'[' => self.deserialize_seq(visitor), |
1643 | _ => Err(self.peek_invalid_type(&visitor)), |
1644 | }; |
1645 | |
1646 | match value { |
1647 | Ok(value) => Ok(value), |
1648 | Err(err) => Err(self.fix_position(err)), |
1649 | } |
1650 | } |
1651 | |
1652 | #[inline ] |
1653 | fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value> |
1654 | where |
1655 | V: de::Visitor<'de>, |
1656 | { |
1657 | self.deserialize_bytes(visitor) |
1658 | } |
1659 | |
1660 | /// Parses a `null` as a None, and any other values as a `Some(...)`. |
1661 | #[inline ] |
1662 | fn deserialize_option<V>(self, visitor: V) -> Result<V::Value> |
1663 | where |
1664 | V: de::Visitor<'de>, |
1665 | { |
1666 | match tri!(self.parse_whitespace()) { |
1667 | Some(b'n' ) => { |
1668 | self.eat_char(); |
1669 | tri!(self.parse_ident(b"ull" )); |
1670 | visitor.visit_none() |
1671 | } |
1672 | _ => visitor.visit_some(self), |
1673 | } |
1674 | } |
1675 | |
1676 | fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value> |
1677 | where |
1678 | V: de::Visitor<'de>, |
1679 | { |
1680 | let peek = match tri!(self.parse_whitespace()) { |
1681 | Some(b) => b, |
1682 | None => { |
1683 | return Err(self.peek_error(ErrorCode::EofWhileParsingValue)); |
1684 | } |
1685 | }; |
1686 | |
1687 | let value = match peek { |
1688 | b'n' => { |
1689 | self.eat_char(); |
1690 | tri!(self.parse_ident(b"ull" )); |
1691 | visitor.visit_unit() |
1692 | } |
1693 | _ => Err(self.peek_invalid_type(&visitor)), |
1694 | }; |
1695 | |
1696 | match value { |
1697 | Ok(value) => Ok(value), |
1698 | Err(err) => Err(self.fix_position(err)), |
1699 | } |
1700 | } |
1701 | |
1702 | fn deserialize_unit_struct<V>(self, _name: &'static str, visitor: V) -> Result<V::Value> |
1703 | where |
1704 | V: de::Visitor<'de>, |
1705 | { |
1706 | self.deserialize_unit(visitor) |
1707 | } |
1708 | |
1709 | /// Parses a newtype struct as the underlying value. |
1710 | #[inline ] |
1711 | fn deserialize_newtype_struct<V>(self, name: &str, visitor: V) -> Result<V::Value> |
1712 | where |
1713 | V: de::Visitor<'de>, |
1714 | { |
1715 | #[cfg (feature = "raw_value" )] |
1716 | { |
1717 | if name == crate::raw::TOKEN { |
1718 | return self.deserialize_raw_value(visitor); |
1719 | } |
1720 | } |
1721 | |
1722 | let _ = name; |
1723 | visitor.visit_newtype_struct(self) |
1724 | } |
1725 | |
1726 | fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value> |
1727 | where |
1728 | V: de::Visitor<'de>, |
1729 | { |
1730 | let peek = match tri!(self.parse_whitespace()) { |
1731 | Some(b) => b, |
1732 | None => { |
1733 | return Err(self.peek_error(ErrorCode::EofWhileParsingValue)); |
1734 | } |
1735 | }; |
1736 | |
1737 | let value = match peek { |
1738 | b'[' => { |
1739 | check_recursion! { |
1740 | self.eat_char(); |
1741 | let ret = visitor.visit_seq(SeqAccess::new(self)); |
1742 | } |
1743 | |
1744 | match (ret, self.end_seq()) { |
1745 | (Ok(ret), Ok(())) => Ok(ret), |
1746 | (Err(err), _) | (_, Err(err)) => Err(err), |
1747 | } |
1748 | } |
1749 | _ => Err(self.peek_invalid_type(&visitor)), |
1750 | }; |
1751 | |
1752 | match value { |
1753 | Ok(value) => Ok(value), |
1754 | Err(err) => Err(self.fix_position(err)), |
1755 | } |
1756 | } |
1757 | |
1758 | fn deserialize_tuple<V>(self, _len: usize, visitor: V) -> Result<V::Value> |
1759 | where |
1760 | V: de::Visitor<'de>, |
1761 | { |
1762 | self.deserialize_seq(visitor) |
1763 | } |
1764 | |
1765 | fn deserialize_tuple_struct<V>( |
1766 | self, |
1767 | _name: &'static str, |
1768 | _len: usize, |
1769 | visitor: V, |
1770 | ) -> Result<V::Value> |
1771 | where |
1772 | V: de::Visitor<'de>, |
1773 | { |
1774 | self.deserialize_seq(visitor) |
1775 | } |
1776 | |
1777 | fn deserialize_map<V>(self, visitor: V) -> Result<V::Value> |
1778 | where |
1779 | V: de::Visitor<'de>, |
1780 | { |
1781 | let peek = match tri!(self.parse_whitespace()) { |
1782 | Some(b) => b, |
1783 | None => { |
1784 | return Err(self.peek_error(ErrorCode::EofWhileParsingValue)); |
1785 | } |
1786 | }; |
1787 | |
1788 | let value = match peek { |
1789 | b'{' => { |
1790 | check_recursion! { |
1791 | self.eat_char(); |
1792 | let ret = visitor.visit_map(MapAccess::new(self)); |
1793 | } |
1794 | |
1795 | match (ret, self.end_map()) { |
1796 | (Ok(ret), Ok(())) => Ok(ret), |
1797 | (Err(err), _) | (_, Err(err)) => Err(err), |
1798 | } |
1799 | } |
1800 | _ => Err(self.peek_invalid_type(&visitor)), |
1801 | }; |
1802 | |
1803 | match value { |
1804 | Ok(value) => Ok(value), |
1805 | Err(err) => Err(self.fix_position(err)), |
1806 | } |
1807 | } |
1808 | |
1809 | fn deserialize_struct<V>( |
1810 | self, |
1811 | _name: &'static str, |
1812 | _fields: &'static [&'static str], |
1813 | visitor: V, |
1814 | ) -> Result<V::Value> |
1815 | where |
1816 | V: de::Visitor<'de>, |
1817 | { |
1818 | let peek = match tri!(self.parse_whitespace()) { |
1819 | Some(b) => b, |
1820 | None => { |
1821 | return Err(self.peek_error(ErrorCode::EofWhileParsingValue)); |
1822 | } |
1823 | }; |
1824 | |
1825 | let value = match peek { |
1826 | b'[' => { |
1827 | check_recursion! { |
1828 | self.eat_char(); |
1829 | let ret = visitor.visit_seq(SeqAccess::new(self)); |
1830 | } |
1831 | |
1832 | match (ret, self.end_seq()) { |
1833 | (Ok(ret), Ok(())) => Ok(ret), |
1834 | (Err(err), _) | (_, Err(err)) => Err(err), |
1835 | } |
1836 | } |
1837 | b'{' => { |
1838 | check_recursion! { |
1839 | self.eat_char(); |
1840 | let ret = visitor.visit_map(MapAccess::new(self)); |
1841 | } |
1842 | |
1843 | match (ret, self.end_map()) { |
1844 | (Ok(ret), Ok(())) => Ok(ret), |
1845 | (Err(err), _) | (_, Err(err)) => Err(err), |
1846 | } |
1847 | } |
1848 | _ => Err(self.peek_invalid_type(&visitor)), |
1849 | }; |
1850 | |
1851 | match value { |
1852 | Ok(value) => Ok(value), |
1853 | Err(err) => Err(self.fix_position(err)), |
1854 | } |
1855 | } |
1856 | |
1857 | /// Parses an enum as an object like `{"$KEY":$VALUE}`, where $VALUE is either a straight |
1858 | /// value, a `[..]`, or a `{..}`. |
1859 | #[inline ] |
1860 | fn deserialize_enum<V>( |
1861 | self, |
1862 | _name: &str, |
1863 | _variants: &'static [&'static str], |
1864 | visitor: V, |
1865 | ) -> Result<V::Value> |
1866 | where |
1867 | V: de::Visitor<'de>, |
1868 | { |
1869 | match tri!(self.parse_whitespace()) { |
1870 | Some(b'{' ) => { |
1871 | check_recursion! { |
1872 | self.eat_char(); |
1873 | let value = tri!(visitor.visit_enum(VariantAccess::new(self))); |
1874 | } |
1875 | |
1876 | match tri!(self.parse_whitespace()) { |
1877 | Some(b'}' ) => { |
1878 | self.eat_char(); |
1879 | Ok(value) |
1880 | } |
1881 | Some(_) => Err(self.error(ErrorCode::ExpectedSomeValue)), |
1882 | None => Err(self.error(ErrorCode::EofWhileParsingObject)), |
1883 | } |
1884 | } |
1885 | Some(b'"' ) => visitor.visit_enum(UnitVariantAccess::new(self)), |
1886 | Some(_) => Err(self.peek_error(ErrorCode::ExpectedSomeValue)), |
1887 | None => Err(self.peek_error(ErrorCode::EofWhileParsingValue)), |
1888 | } |
1889 | } |
1890 | |
1891 | fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value> |
1892 | where |
1893 | V: de::Visitor<'de>, |
1894 | { |
1895 | self.deserialize_str(visitor) |
1896 | } |
1897 | |
1898 | fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value> |
1899 | where |
1900 | V: de::Visitor<'de>, |
1901 | { |
1902 | tri!(self.ignore_value()); |
1903 | visitor.visit_unit() |
1904 | } |
1905 | } |
1906 | |
1907 | struct SeqAccess<'a, R: 'a> { |
1908 | de: &'a mut Deserializer<R>, |
1909 | first: bool, |
1910 | } |
1911 | |
1912 | impl<'a, R: 'a> SeqAccess<'a, R> { |
1913 | fn new(de: &'a mut Deserializer<R>) -> Self { |
1914 | SeqAccess { de, first: true } |
1915 | } |
1916 | } |
1917 | |
1918 | impl<'de, 'a, R: Read<'de> + 'a> de::SeqAccess<'de> for SeqAccess<'a, R> { |
1919 | type Error = Error; |
1920 | |
1921 | fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>> |
1922 | where |
1923 | T: de::DeserializeSeed<'de>, |
1924 | { |
1925 | let peek = match tri!(self.de.parse_whitespace()) { |
1926 | Some(b']' ) => { |
1927 | return Ok(None); |
1928 | } |
1929 | Some(b',' ) if !self.first => { |
1930 | self.de.eat_char(); |
1931 | tri!(self.de.parse_whitespace()) |
1932 | } |
1933 | Some(b) => { |
1934 | if self.first { |
1935 | self.first = false; |
1936 | Some(b) |
1937 | } else { |
1938 | return Err(self.de.peek_error(ErrorCode::ExpectedListCommaOrEnd)); |
1939 | } |
1940 | } |
1941 | None => { |
1942 | return Err(self.de.peek_error(ErrorCode::EofWhileParsingList)); |
1943 | } |
1944 | }; |
1945 | |
1946 | match peek { |
1947 | Some(b']' ) => Err(self.de.peek_error(ErrorCode::TrailingComma)), |
1948 | Some(_) => Ok(Some(tri!(seed.deserialize(&mut *self.de)))), |
1949 | None => Err(self.de.peek_error(ErrorCode::EofWhileParsingValue)), |
1950 | } |
1951 | } |
1952 | } |
1953 | |
1954 | struct MapAccess<'a, R: 'a> { |
1955 | de: &'a mut Deserializer<R>, |
1956 | first: bool, |
1957 | } |
1958 | |
1959 | impl<'a, R: 'a> MapAccess<'a, R> { |
1960 | fn new(de: &'a mut Deserializer<R>) -> Self { |
1961 | MapAccess { de, first: true } |
1962 | } |
1963 | } |
1964 | |
1965 | impl<'de, 'a, R: Read<'de> + 'a> de::MapAccess<'de> for MapAccess<'a, R> { |
1966 | type Error = Error; |
1967 | |
1968 | fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>> |
1969 | where |
1970 | K: de::DeserializeSeed<'de>, |
1971 | { |
1972 | let peek = match tri!(self.de.parse_whitespace()) { |
1973 | Some(b'}' ) => { |
1974 | return Ok(None); |
1975 | } |
1976 | Some(b',' ) if !self.first => { |
1977 | self.de.eat_char(); |
1978 | tri!(self.de.parse_whitespace()) |
1979 | } |
1980 | Some(b) => { |
1981 | if self.first { |
1982 | self.first = false; |
1983 | Some(b) |
1984 | } else { |
1985 | return Err(self.de.peek_error(ErrorCode::ExpectedObjectCommaOrEnd)); |
1986 | } |
1987 | } |
1988 | None => { |
1989 | return Err(self.de.peek_error(ErrorCode::EofWhileParsingObject)); |
1990 | } |
1991 | }; |
1992 | |
1993 | match peek { |
1994 | Some(b'"' ) => seed.deserialize(MapKey { de: &mut *self.de }).map(Some), |
1995 | Some(b'}' ) => Err(self.de.peek_error(ErrorCode::TrailingComma)), |
1996 | Some(_) => Err(self.de.peek_error(ErrorCode::KeyMustBeAString)), |
1997 | None => Err(self.de.peek_error(ErrorCode::EofWhileParsingValue)), |
1998 | } |
1999 | } |
2000 | |
2001 | fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value> |
2002 | where |
2003 | V: de::DeserializeSeed<'de>, |
2004 | { |
2005 | tri!(self.de.parse_object_colon()); |
2006 | |
2007 | seed.deserialize(&mut *self.de) |
2008 | } |
2009 | } |
2010 | |
2011 | struct VariantAccess<'a, R: 'a> { |
2012 | de: &'a mut Deserializer<R>, |
2013 | } |
2014 | |
2015 | impl<'a, R: 'a> VariantAccess<'a, R> { |
2016 | fn new(de: &'a mut Deserializer<R>) -> Self { |
2017 | VariantAccess { de } |
2018 | } |
2019 | } |
2020 | |
2021 | impl<'de, 'a, R: Read<'de> + 'a> de::EnumAccess<'de> for VariantAccess<'a, R> { |
2022 | type Error = Error; |
2023 | type Variant = Self; |
2024 | |
2025 | fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self)> |
2026 | where |
2027 | V: de::DeserializeSeed<'de>, |
2028 | { |
2029 | let val: ::Value = tri!(seed.deserialize(&mut *self.de)); |
2030 | tri!(self.de.parse_object_colon()); |
2031 | Ok((val, self)) |
2032 | } |
2033 | } |
2034 | |
2035 | impl<'de, 'a, R: Read<'de> + 'a> de::VariantAccess<'de> for VariantAccess<'a, R> { |
2036 | type Error = Error; |
2037 | |
2038 | fn unit_variant(self) -> Result<()> { |
2039 | de::Deserialize::deserialize(self.de) |
2040 | } |
2041 | |
2042 | fn newtype_variant_seed<T>(self, seed: T) -> Result<T::Value> |
2043 | where |
2044 | T: de::DeserializeSeed<'de>, |
2045 | { |
2046 | seed.deserialize(self.de) |
2047 | } |
2048 | |
2049 | fn tuple_variant<V>(self, _len: usize, visitor: V) -> Result<V::Value> |
2050 | where |
2051 | V: de::Visitor<'de>, |
2052 | { |
2053 | de::Deserializer::deserialize_seq(self.de, visitor) |
2054 | } |
2055 | |
2056 | fn struct_variant<V>(self, fields: &'static [&'static str], visitor: V) -> Result<V::Value> |
2057 | where |
2058 | V: de::Visitor<'de>, |
2059 | { |
2060 | de::Deserializer::deserialize_struct(self.de, "" , fields, visitor) |
2061 | } |
2062 | } |
2063 | |
2064 | struct UnitVariantAccess<'a, R: 'a> { |
2065 | de: &'a mut Deserializer<R>, |
2066 | } |
2067 | |
2068 | impl<'a, R: 'a> UnitVariantAccess<'a, R> { |
2069 | fn new(de: &'a mut Deserializer<R>) -> Self { |
2070 | UnitVariantAccess { de } |
2071 | } |
2072 | } |
2073 | |
2074 | impl<'de, 'a, R: Read<'de> + 'a> de::EnumAccess<'de> for UnitVariantAccess<'a, R> { |
2075 | type Error = Error; |
2076 | type Variant = Self; |
2077 | |
2078 | fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self)> |
2079 | where |
2080 | V: de::DeserializeSeed<'de>, |
2081 | { |
2082 | let variant: ::Value = tri!(seed.deserialize(&mut *self.de)); |
2083 | Ok((variant, self)) |
2084 | } |
2085 | } |
2086 | |
2087 | impl<'de, 'a, R: Read<'de> + 'a> de::VariantAccess<'de> for UnitVariantAccess<'a, R> { |
2088 | type Error = Error; |
2089 | |
2090 | fn unit_variant(self) -> Result<()> { |
2091 | Ok(()) |
2092 | } |
2093 | |
2094 | fn newtype_variant_seed<T>(self, _seed: T) -> Result<T::Value> |
2095 | where |
2096 | T: de::DeserializeSeed<'de>, |
2097 | { |
2098 | Err(de::Error::invalid_type( |
2099 | Unexpected::UnitVariant, |
2100 | &"newtype variant" , |
2101 | )) |
2102 | } |
2103 | |
2104 | fn tuple_variant<V>(self, _len: usize, _visitor: V) -> Result<V::Value> |
2105 | where |
2106 | V: de::Visitor<'de>, |
2107 | { |
2108 | Err(de::Error::invalid_type( |
2109 | Unexpected::UnitVariant, |
2110 | &"tuple variant" , |
2111 | )) |
2112 | } |
2113 | |
2114 | fn struct_variant<V>(self, _fields: &'static [&'static str], _visitor: V) -> Result<V::Value> |
2115 | where |
2116 | V: de::Visitor<'de>, |
2117 | { |
2118 | Err(de::Error::invalid_type( |
2119 | Unexpected::UnitVariant, |
2120 | &"struct variant" , |
2121 | )) |
2122 | } |
2123 | } |
2124 | |
2125 | /// Only deserialize from this after peeking a '"' byte! Otherwise it may |
2126 | /// deserialize invalid JSON successfully. |
2127 | struct MapKey<'a, R: 'a> { |
2128 | de: &'a mut Deserializer<R>, |
2129 | } |
2130 | |
2131 | macro_rules! deserialize_numeric_key { |
2132 | ($method:ident) => { |
2133 | fn $method<V>(self, visitor: V) -> Result<V::Value> |
2134 | where |
2135 | V: de::Visitor<'de>, |
2136 | { |
2137 | self.deserialize_number(visitor) |
2138 | } |
2139 | }; |
2140 | |
2141 | ($method:ident, $delegate:ident) => { |
2142 | fn $method<V>(self, visitor: V) -> Result<V::Value> |
2143 | where |
2144 | V: de::Visitor<'de>, |
2145 | { |
2146 | self.de.eat_char(); |
2147 | |
2148 | match tri!(self.de.peek()) { |
2149 | Some(b'0' ..=b'9' | b'-' ) => {} |
2150 | _ => return Err(self.de.error(ErrorCode::ExpectedNumericKey)), |
2151 | } |
2152 | |
2153 | let value = tri!(self.de.$delegate(visitor)); |
2154 | |
2155 | match tri!(self.de.peek()) { |
2156 | Some(b'"' ) => self.de.eat_char(), |
2157 | _ => return Err(self.de.peek_error(ErrorCode::ExpectedDoubleQuote)), |
2158 | } |
2159 | |
2160 | Ok(value) |
2161 | } |
2162 | }; |
2163 | } |
2164 | |
2165 | impl<'de, 'a, R> MapKey<'a, R> |
2166 | where |
2167 | R: Read<'de>, |
2168 | { |
2169 | deserialize_numeric_key!(deserialize_number, deserialize_number); |
2170 | } |
2171 | |
2172 | impl<'de, 'a, R> de::Deserializer<'de> for MapKey<'a, R> |
2173 | where |
2174 | R: Read<'de>, |
2175 | { |
2176 | type Error = Error; |
2177 | |
2178 | #[inline ] |
2179 | fn deserialize_any<V>(self, visitor: V) -> Result<V::Value> |
2180 | where |
2181 | V: de::Visitor<'de>, |
2182 | { |
2183 | self.de.eat_char(); |
2184 | self.de.scratch.clear(); |
2185 | match tri!(self.de.read.parse_str(&mut self.de.scratch)) { |
2186 | Reference::Borrowed(s) => visitor.visit_borrowed_str(s), |
2187 | Reference::Copied(s) => visitor.visit_str(s), |
2188 | } |
2189 | } |
2190 | |
2191 | deserialize_numeric_key!(deserialize_i8); |
2192 | deserialize_numeric_key!(deserialize_i16); |
2193 | deserialize_numeric_key!(deserialize_i32); |
2194 | deserialize_numeric_key!(deserialize_i64); |
2195 | deserialize_numeric_key!(deserialize_i128, deserialize_i128); |
2196 | deserialize_numeric_key!(deserialize_u8); |
2197 | deserialize_numeric_key!(deserialize_u16); |
2198 | deserialize_numeric_key!(deserialize_u32); |
2199 | deserialize_numeric_key!(deserialize_u64); |
2200 | deserialize_numeric_key!(deserialize_u128, deserialize_u128); |
2201 | #[cfg (not(feature = "float_roundtrip" ))] |
2202 | deserialize_numeric_key!(deserialize_f32); |
2203 | #[cfg (feature = "float_roundtrip" )] |
2204 | deserialize_numeric_key!(deserialize_f32, deserialize_f32); |
2205 | deserialize_numeric_key!(deserialize_f64); |
2206 | |
2207 | fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value> |
2208 | where |
2209 | V: de::Visitor<'de>, |
2210 | { |
2211 | self.de.eat_char(); |
2212 | |
2213 | let peek = match tri!(self.de.next_char()) { |
2214 | Some(b) => b, |
2215 | None => { |
2216 | return Err(self.de.peek_error(ErrorCode::EofWhileParsingValue)); |
2217 | } |
2218 | }; |
2219 | |
2220 | let value = match peek { |
2221 | b't' => { |
2222 | tri!(self.de.parse_ident(b"rue \"" )); |
2223 | visitor.visit_bool(true) |
2224 | } |
2225 | b'f' => { |
2226 | tri!(self.de.parse_ident(b"alse \"" )); |
2227 | visitor.visit_bool(false) |
2228 | } |
2229 | _ => { |
2230 | self.de.scratch.clear(); |
2231 | let s = tri!(self.de.read.parse_str(&mut self.de.scratch)); |
2232 | Err(de::Error::invalid_type(Unexpected::Str(&s), &visitor)) |
2233 | } |
2234 | }; |
2235 | |
2236 | match value { |
2237 | Ok(value) => Ok(value), |
2238 | Err(err) => Err(self.de.fix_position(err)), |
2239 | } |
2240 | } |
2241 | |
2242 | #[inline ] |
2243 | fn deserialize_option<V>(self, visitor: V) -> Result<V::Value> |
2244 | where |
2245 | V: de::Visitor<'de>, |
2246 | { |
2247 | // Map keys cannot be null. |
2248 | visitor.visit_some(self) |
2249 | } |
2250 | |
2251 | #[inline ] |
2252 | fn deserialize_newtype_struct<V>(self, name: &'static str, visitor: V) -> Result<V::Value> |
2253 | where |
2254 | V: de::Visitor<'de>, |
2255 | { |
2256 | #[cfg (feature = "raw_value" )] |
2257 | { |
2258 | if name == crate::raw::TOKEN { |
2259 | return self.de.deserialize_raw_value(visitor); |
2260 | } |
2261 | } |
2262 | |
2263 | let _ = name; |
2264 | visitor.visit_newtype_struct(self) |
2265 | } |
2266 | |
2267 | #[inline ] |
2268 | fn deserialize_enum<V>( |
2269 | self, |
2270 | name: &'static str, |
2271 | variants: &'static [&'static str], |
2272 | visitor: V, |
2273 | ) -> Result<V::Value> |
2274 | where |
2275 | V: de::Visitor<'de>, |
2276 | { |
2277 | self.de.deserialize_enum(name, variants, visitor) |
2278 | } |
2279 | |
2280 | #[inline ] |
2281 | fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value> |
2282 | where |
2283 | V: de::Visitor<'de>, |
2284 | { |
2285 | self.de.deserialize_bytes(visitor) |
2286 | } |
2287 | |
2288 | #[inline ] |
2289 | fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value> |
2290 | where |
2291 | V: de::Visitor<'de>, |
2292 | { |
2293 | self.de.deserialize_bytes(visitor) |
2294 | } |
2295 | |
2296 | forward_to_deserialize_any! { |
2297 | char str string unit unit_struct seq tuple tuple_struct map struct |
2298 | identifier ignored_any |
2299 | } |
2300 | } |
2301 | |
2302 | ////////////////////////////////////////////////////////////////////////////// |
2303 | |
2304 | /// Iterator that deserializes a stream into multiple JSON values. |
2305 | /// |
2306 | /// A stream deserializer can be created from any JSON deserializer using the |
2307 | /// `Deserializer::into_iter` method. |
2308 | /// |
2309 | /// The data can consist of any JSON value. Values need to be a self-delineating value e.g. |
2310 | /// arrays, objects, or strings, or be followed by whitespace or a self-delineating value. |
2311 | /// |
2312 | /// ``` |
2313 | /// use serde_json::{Deserializer, Value}; |
2314 | /// |
2315 | /// fn main() { |
2316 | /// let data = "{ \"k \": 3}1 \"cool \"\"stuff \" 3{} [0, 1, 2]" ; |
2317 | /// |
2318 | /// let stream = Deserializer::from_str(data).into_iter::<Value>(); |
2319 | /// |
2320 | /// for value in stream { |
2321 | /// println!("{}" , value.unwrap()); |
2322 | /// } |
2323 | /// } |
2324 | /// ``` |
2325 | pub struct StreamDeserializer<'de, R, T> { |
2326 | de: Deserializer<R>, |
2327 | offset: usize, |
2328 | failed: bool, |
2329 | output: PhantomData<T>, |
2330 | lifetime: PhantomData<&'de ()>, |
2331 | } |
2332 | |
2333 | impl<'de, R, T> StreamDeserializer<'de, R, T> |
2334 | where |
2335 | R: read::Read<'de>, |
2336 | T: de::Deserialize<'de>, |
2337 | { |
2338 | /// Create a JSON stream deserializer from one of the possible serde_json |
2339 | /// input sources. |
2340 | /// |
2341 | /// Typically it is more convenient to use one of these methods instead: |
2342 | /// |
2343 | /// - Deserializer::from_str(...).into_iter() |
2344 | /// - Deserializer::from_slice(...).into_iter() |
2345 | /// - Deserializer::from_reader(...).into_iter() |
2346 | pub fn new(read: R) -> Self { |
2347 | let offset = read.byte_offset(); |
2348 | StreamDeserializer { |
2349 | de: Deserializer::new(read), |
2350 | offset, |
2351 | failed: false, |
2352 | output: PhantomData, |
2353 | lifetime: PhantomData, |
2354 | } |
2355 | } |
2356 | |
2357 | /// Returns the number of bytes so far deserialized into a successful `T`. |
2358 | /// |
2359 | /// If a stream deserializer returns an EOF error, new data can be joined to |
2360 | /// `old_data[stream.byte_offset()..]` to try again. |
2361 | /// |
2362 | /// ``` |
2363 | /// let data = b"[0] [1] [" ; |
2364 | /// |
2365 | /// let de = serde_json::Deserializer::from_slice(data); |
2366 | /// let mut stream = de.into_iter::<Vec<i32>>(); |
2367 | /// assert_eq!(0, stream.byte_offset()); |
2368 | /// |
2369 | /// println!("{:?}" , stream.next()); // [0] |
2370 | /// assert_eq!(3, stream.byte_offset()); |
2371 | /// |
2372 | /// println!("{:?}" , stream.next()); // [1] |
2373 | /// assert_eq!(7, stream.byte_offset()); |
2374 | /// |
2375 | /// println!("{:?}" , stream.next()); // error |
2376 | /// assert_eq!(8, stream.byte_offset()); |
2377 | /// |
2378 | /// // If err.is_eof(), can join the remaining data to new data and continue. |
2379 | /// let remaining = &data[stream.byte_offset()..]; |
2380 | /// ``` |
2381 | /// |
2382 | /// *Note:* In the future this method may be changed to return the number of |
2383 | /// bytes so far deserialized into a successful T *or* syntactically valid |
2384 | /// JSON skipped over due to a type error. See [serde-rs/json#70] for an |
2385 | /// example illustrating this. |
2386 | /// |
2387 | /// [serde-rs/json#70]: https://github.com/serde-rs/json/issues/70 |
2388 | pub fn byte_offset(&self) -> usize { |
2389 | self.offset |
2390 | } |
2391 | |
2392 | fn peek_end_of_value(&mut self) -> Result<()> { |
2393 | match tri!(self.de.peek()) { |
2394 | Some(b' ' | b' \n' | b' \t' | b' \r' | b'"' | b'[' | b']' | b'{' | b'}' | b',' | b':' ) |
2395 | | None => Ok(()), |
2396 | Some(_) => { |
2397 | let position = self.de.read.peek_position(); |
2398 | Err(Error::syntax( |
2399 | ErrorCode::TrailingCharacters, |
2400 | position.line, |
2401 | position.column, |
2402 | )) |
2403 | } |
2404 | } |
2405 | } |
2406 | } |
2407 | |
2408 | impl<'de, R, T> Iterator for StreamDeserializer<'de, R, T> |
2409 | where |
2410 | R: Read<'de>, |
2411 | T: de::Deserialize<'de>, |
2412 | { |
2413 | type Item = Result<T>; |
2414 | |
2415 | fn next(&mut self) -> Option<Result<T>> { |
2416 | if R::should_early_return_if_failed && self.failed { |
2417 | return None; |
2418 | } |
2419 | |
2420 | // skip whitespaces, if any |
2421 | // this helps with trailing whitespaces, since whitespaces between |
2422 | // values are handled for us. |
2423 | match self.de.parse_whitespace() { |
2424 | Ok(None) => { |
2425 | self.offset = self.de.read.byte_offset(); |
2426 | None |
2427 | } |
2428 | Ok(Some(b)) => { |
2429 | // If the value does not have a clear way to show the end of the value |
2430 | // (like numbers, null, true etc.) we have to look for whitespace or |
2431 | // the beginning of a self-delineated value. |
2432 | let self_delineated_value = match b { |
2433 | b'[' | b'"' | b'{' => true, |
2434 | _ => false, |
2435 | }; |
2436 | self.offset = self.de.read.byte_offset(); |
2437 | let result = de::Deserialize::deserialize(&mut self.de); |
2438 | |
2439 | Some(match result { |
2440 | Ok(value) => { |
2441 | self.offset = self.de.read.byte_offset(); |
2442 | if self_delineated_value { |
2443 | Ok(value) |
2444 | } else { |
2445 | self.peek_end_of_value().map(|()| value) |
2446 | } |
2447 | } |
2448 | Err(e) => { |
2449 | self.de.read.set_failed(&mut self.failed); |
2450 | Err(e) |
2451 | } |
2452 | }) |
2453 | } |
2454 | Err(e) => { |
2455 | self.de.read.set_failed(&mut self.failed); |
2456 | Some(Err(e)) |
2457 | } |
2458 | } |
2459 | } |
2460 | } |
2461 | |
2462 | impl<'de, R, T> FusedIterator for StreamDeserializer<'de, R, T> |
2463 | where |
2464 | R: Read<'de> + Fused, |
2465 | T: de::Deserialize<'de>, |
2466 | { |
2467 | } |
2468 | |
2469 | ////////////////////////////////////////////////////////////////////////////// |
2470 | |
2471 | fn from_trait<'de, R, T>(read: R) -> Result<T> |
2472 | where |
2473 | R: Read<'de>, |
2474 | T: de::Deserialize<'de>, |
2475 | { |
2476 | let mut de: Deserializer = Deserializer::new(read); |
2477 | let value: T = tri!(de::Deserialize::deserialize(&mut de)); |
2478 | |
2479 | // Make sure the whole stream has been consumed. |
2480 | tri!(de.end()); |
2481 | Ok(value) |
2482 | } |
2483 | |
2484 | /// Deserialize an instance of type `T` from an I/O stream of JSON. |
2485 | /// |
2486 | /// The content of the I/O stream is deserialized directly from the stream |
2487 | /// without being buffered in memory by serde_json. |
2488 | /// |
2489 | /// When reading from a source against which short reads are not efficient, such |
2490 | /// as a [`File`], you will want to apply your own buffering because serde_json |
2491 | /// will not buffer the input. See [`std::io::BufReader`]. |
2492 | /// |
2493 | /// It is expected that the input stream ends after the deserialized object. |
2494 | /// If the stream does not end, such as in the case of a persistent socket connection, |
2495 | /// this function will not return. It is possible instead to deserialize from a prefix of an input |
2496 | /// stream without looking for EOF by managing your own [`Deserializer`]. |
2497 | /// |
2498 | /// Note that counter to intuition, this function is usually slower than |
2499 | /// reading a file completely into memory and then applying [`from_str`] |
2500 | /// or [`from_slice`] on it. See [issue #160]. |
2501 | /// |
2502 | /// [`File`]: https://doc.rust-lang.org/std/fs/struct.File.html |
2503 | /// [`std::io::BufReader`]: https://doc.rust-lang.org/std/io/struct.BufReader.html |
2504 | /// [`from_str`]: ./fn.from_str.html |
2505 | /// [`from_slice`]: ./fn.from_slice.html |
2506 | /// [issue #160]: https://github.com/serde-rs/json/issues/160 |
2507 | /// |
2508 | /// # Example |
2509 | /// |
2510 | /// Reading the contents of a file. |
2511 | /// |
2512 | /// ``` |
2513 | /// use serde::Deserialize; |
2514 | /// |
2515 | /// use std::error::Error; |
2516 | /// use std::fs::File; |
2517 | /// use std::io::BufReader; |
2518 | /// use std::path::Path; |
2519 | /// |
2520 | /// #[derive(Deserialize, Debug)] |
2521 | /// struct User { |
2522 | /// fingerprint: String, |
2523 | /// location: String, |
2524 | /// } |
2525 | /// |
2526 | /// fn read_user_from_file<P: AsRef<Path>>(path: P) -> Result<User, Box<dyn Error>> { |
2527 | /// // Open the file in read-only mode with buffer. |
2528 | /// let file = File::open(path)?; |
2529 | /// let reader = BufReader::new(file); |
2530 | /// |
2531 | /// // Read the JSON contents of the file as an instance of `User`. |
2532 | /// let u = serde_json::from_reader(reader)?; |
2533 | /// |
2534 | /// // Return the `User`. |
2535 | /// Ok(u) |
2536 | /// } |
2537 | /// |
2538 | /// fn main() { |
2539 | /// # } |
2540 | /// # fn fake_main() { |
2541 | /// let u = read_user_from_file("test.json" ).unwrap(); |
2542 | /// println!("{:#?}" , u); |
2543 | /// } |
2544 | /// ``` |
2545 | /// |
2546 | /// Reading from a persistent socket connection. |
2547 | /// |
2548 | /// ``` |
2549 | /// use serde::Deserialize; |
2550 | /// |
2551 | /// use std::error::Error; |
2552 | /// use std::net::{TcpListener, TcpStream}; |
2553 | /// |
2554 | /// #[derive(Deserialize, Debug)] |
2555 | /// struct User { |
2556 | /// fingerprint: String, |
2557 | /// location: String, |
2558 | /// } |
2559 | /// |
2560 | /// fn read_user_from_stream(tcp_stream: TcpStream) -> Result<User, Box<dyn Error>> { |
2561 | /// let mut de = serde_json::Deserializer::from_reader(tcp_stream); |
2562 | /// let u = User::deserialize(&mut de)?; |
2563 | /// |
2564 | /// Ok(u) |
2565 | /// } |
2566 | /// |
2567 | /// fn main() { |
2568 | /// # } |
2569 | /// # fn fake_main() { |
2570 | /// let listener = TcpListener::bind("127.0.0.1:4000" ).unwrap(); |
2571 | /// |
2572 | /// for stream in listener.incoming() { |
2573 | /// println!("{:#?}" , read_user_from_stream(stream.unwrap())); |
2574 | /// } |
2575 | /// } |
2576 | /// ``` |
2577 | /// |
2578 | /// # Errors |
2579 | /// |
2580 | /// This conversion can fail if the structure of the input does not match the |
2581 | /// structure expected by `T`, for example if `T` is a struct type but the input |
2582 | /// contains something other than a JSON map. It can also fail if the structure |
2583 | /// is correct but `T`'s implementation of `Deserialize` decides that something |
2584 | /// is wrong with the data, for example required struct fields are missing from |
2585 | /// the JSON map or some number is too big to fit in the expected primitive |
2586 | /// type. |
2587 | #[cfg (feature = "std" )] |
2588 | #[cfg_attr (docsrs, doc(cfg(feature = "std" )))] |
2589 | pub fn from_reader<R, T>(rdr: R) -> Result<T> |
2590 | where |
2591 | R: crate::io::Read, |
2592 | T: de::DeserializeOwned, |
2593 | { |
2594 | from_trait(read:read::IoRead::new(reader:rdr)) |
2595 | } |
2596 | |
2597 | /// Deserialize an instance of type `T` from bytes of JSON text. |
2598 | /// |
2599 | /// # Example |
2600 | /// |
2601 | /// ``` |
2602 | /// use serde::Deserialize; |
2603 | /// |
2604 | /// #[derive(Deserialize, Debug)] |
2605 | /// struct User { |
2606 | /// fingerprint: String, |
2607 | /// location: String, |
2608 | /// } |
2609 | /// |
2610 | /// fn main() { |
2611 | /// // The type of `j` is `&[u8]` |
2612 | /// let j = b" |
2613 | /// { |
2614 | /// \"fingerprint \": \"0xF9BA143B95FF6D82 \", |
2615 | /// \"location \": \"Menlo Park, CA \" |
2616 | /// }" ; |
2617 | /// |
2618 | /// let u: User = serde_json::from_slice(j).unwrap(); |
2619 | /// println!("{:#?}" , u); |
2620 | /// } |
2621 | /// ``` |
2622 | /// |
2623 | /// # Errors |
2624 | /// |
2625 | /// This conversion can fail if the structure of the input does not match the |
2626 | /// structure expected by `T`, for example if `T` is a struct type but the input |
2627 | /// contains something other than a JSON map. It can also fail if the structure |
2628 | /// is correct but `T`'s implementation of `Deserialize` decides that something |
2629 | /// is wrong with the data, for example required struct fields are missing from |
2630 | /// the JSON map or some number is too big to fit in the expected primitive |
2631 | /// type. |
2632 | pub fn from_slice<'a, T>(v: &'a [u8]) -> Result<T> |
2633 | where |
2634 | T: de::Deserialize<'a>, |
2635 | { |
2636 | from_trait(read:read::SliceRead::new(slice:v)) |
2637 | } |
2638 | |
2639 | /// Deserialize an instance of type `T` from a string of JSON text. |
2640 | /// |
2641 | /// # Example |
2642 | /// |
2643 | /// ``` |
2644 | /// use serde::Deserialize; |
2645 | /// |
2646 | /// #[derive(Deserialize, Debug)] |
2647 | /// struct User { |
2648 | /// fingerprint: String, |
2649 | /// location: String, |
2650 | /// } |
2651 | /// |
2652 | /// fn main() { |
2653 | /// // The type of `j` is `&str` |
2654 | /// let j = " |
2655 | /// { |
2656 | /// \"fingerprint \": \"0xF9BA143B95FF6D82 \", |
2657 | /// \"location \": \"Menlo Park, CA \" |
2658 | /// }" ; |
2659 | /// |
2660 | /// let u: User = serde_json::from_str(j).unwrap(); |
2661 | /// println!("{:#?}" , u); |
2662 | /// } |
2663 | /// ``` |
2664 | /// |
2665 | /// # Errors |
2666 | /// |
2667 | /// This conversion can fail if the structure of the input does not match the |
2668 | /// structure expected by `T`, for example if `T` is a struct type but the input |
2669 | /// contains something other than a JSON map. It can also fail if the structure |
2670 | /// is correct but `T`'s implementation of `Deserialize` decides that something |
2671 | /// is wrong with the data, for example required struct fields are missing from |
2672 | /// the JSON map or some number is too big to fit in the expected primitive |
2673 | /// type. |
2674 | pub fn from_str<'a, T>(s: &'a str) -> Result<T> |
2675 | where |
2676 | T: de::Deserialize<'a>, |
2677 | { |
2678 | from_trait(read:read::StrRead::new(s)) |
2679 | } |
2680 | |