json_codec_wasm/
decoder.rs

1//! JSON ([RFC 7159](http://tools.ietf.org/html/rfc7159))
2//! decoder implementation.
3//!
4//! The decoder supports direct decoding into Rust types (providing
5//! helper macros such as `object!`) as well as generic decoding
6//! using an intermediate representation.
7//!
8//! # Example 1: Generic decoding
9//!
10//! ```
11//! use json_codec_wasm::{Decoder, Json};
12//! use json_codec_wasm::ast::Ref;
13//!
14//! let json = r#"
15//! { "field1": true,
16//!   "field2": { "nested": [42, 73] }
17//! }"#;
18//!
19//! let mut d = Decoder::default(json.chars());
20//! let     v = d.decode().unwrap();
21//! let     r = Ref::new(&v);
22//! assert_eq!(true, r.get("field1").bool().unwrap());
23//! assert_eq!(73i128, r.get("field2").get("nested").at(1).i128().unwrap());
24//!
25//! ```
26//! # Example 2: Direct decoding using macros
27//!
28//! ```
29//! #
30//! #[macro_use] extern crate json_codec_wasm;
31//! # fn main() {
32//!
33//! use json_codec_wasm::Decoder;
34//!
35//! const JSON: &'static str =
36//! r#"
37//! {
38//!     "Width":  800,
39//!     "Height": 600,
40//!     "Title":  "View from 15th Floor",
41//!     "Thumbnail": {
42//!         "Url":    "http://www.example.com/image/481989943",
43//!         "Height": 125,
44//!         "Width":  100
45//!     },
46//!     "Animated" : false,
47//!     "IDs": [116, 943, 234, 38793]
48//! }
49//! "#;
50//!
51//! struct Image {
52//!     width:     usize,
53//!     height:    usize,
54//!     title:     String,
55//!     thumbnail: Thumbnail,
56//!     animated:  bool,
57//!     ids:       Vec<u32>,
58//!     geo:       Option<(i128, i128)>
59//! }
60//!
61//! struct Thumbnail {
62//!     url:    String,
63//!     height: u16,
64//!     width:  u16
65//! }
66//!
67//! let mut d = Decoder::default(JSON.chars());
68//! let image = object! {
69//!     let decoder = d;
70//!     Image {
71//!         width:     req. "Width"     => d.usize(),
72//!         height:    req. "Height"    => d.usize(),
73//!         title:     req. "Title"     => d.string(),
74//!         thumbnail: req. "Thumbnail" => object! {
75//!             let decoder = d;
76//!             Thumbnail {
77//!                 url:    req. "Url"    => d.string(),
78//!                 height: req. "Height" => d.u16(),
79//!                 width:  req. "Width"  => d.u16()
80//!             }
81//!         },
82//!         animated:  req. "Animated" => d.bool(),
83//!         ids:       req. "IDs"      => array!(d, d.u32()),
84//!         geo:       opt. "Geo"      => d.optional(|d| {
85//!             let lat = d.i128()?;
86//!             let lon = d.i128()?;
87//!             Ok((lat, lon))
88//!         })
89//!     }
90//! };
91//! assert!(image.is_ok());
92//! # }
93//! ```
94
95use std::borrow::Cow;
96use std::char;
97use std::collections::HashMap;
98use std::error::Error;
99use std::fmt;
100use std::io::Read;
101use std::marker::PhantomData;
102use std::str;
103use std::{i16, i32, i64, i8, isize};
104use std::{u16, u32, u64, u8, usize};
105
106use ast::Json;
107use buffer::{Buffer, Utf8Buffer};
108use stack::{Scope, Stack};
109use utf8;
110pub use utf8::ReadError;
111
112// FromJson trait ///////////////////////////////////////////////////////////
113
114pub trait FromJson {
115    fn decode<I: Iterator<Item = char>>(d: &mut Decoder<I>) -> DecodeResult<Self>
116    where
117        Self: Sized;
118}
119
120macro_rules! instance {
121    ($name: ident) => {
122        impl FromJson for $name {
123            fn decode<I: Iterator<Item = char>>(d: &mut Decoder<I>) -> DecodeResult<Self> {
124                d.$name()
125            }
126        }
127    };
128}
129
130instance!(bool);
131instance!(u8);
132instance!(u16);
133instance!(u32);
134instance!(u64);
135instance!(usize);
136instance!(i8);
137instance!(i16);
138instance!(i32);
139instance!(i64);
140instance!(isize);
141instance!(i128);
142
143impl FromJson for String {
144    fn decode<I: Iterator<Item = char>>(d: &mut Decoder<I>) -> DecodeResult<Self> {
145        d.string()
146    }
147}
148
149impl FromJson for Json {
150    fn decode<I: Iterator<Item = char>>(d: &mut Decoder<I>) -> DecodeResult<Self> {
151        d.decode()
152    }
153}
154
155impl<T: FromJson> FromJson for Option<T> {
156    fn decode<I: Iterator<Item = char>>(d: &mut Decoder<I>) -> DecodeResult<Self> {
157        d.optional(FromJson::decode)
158    }
159}
160
161impl<'a, T: FromJson + Clone> FromJson for Cow<'a, T> {
162    fn decode<I: Iterator<Item = char>>(d: &mut Decoder<I>) -> DecodeResult<Self> {
163        Ok(Cow::Owned(FromJson::decode(d)?))
164    }
165}
166
167impl<T: FromJson> FromJson for Vec<T> {
168    fn decode<I: Iterator<Item = char>>(d: &mut Decoder<I>) -> DecodeResult<Self> {
169        d.array()?;
170        let mut v = Vec::new();
171        while d.has_more()? {
172            let x = d.from_json()?;
173            v.push(x)
174        }
175        Ok(v)
176    }
177}
178
179// Decoder //////////////////////////////////////////////////////////////////
180
181/// JSON decoder over `char` iterators.
182pub struct Decoder<I: Iterator> {
183    chars: Source<I>,
184    config: Config,
185    buffer: [u8; 512],
186    stack: Stack,
187}
188
189#[derive(Clone, Debug, PartialEq, Eq)]
190/// Decoder configuration.
191pub struct Config {
192    /// Maximum recursion steps when decoding generic `Json`
193    pub max_nesting: usize,
194}
195
196const DEFAULT_CONFIG: Config = Config { max_nesting: 16 };
197
198impl Config {
199    /// Create default configuration with
200    /// - `max_nesting` = 16
201    pub fn default() -> Config {
202        DEFAULT_CONFIG
203    }
204}
205
206// Macros ///////////////////////////////////////////////////////////////////
207
208macro_rules! require {
209    ($e: expr) => {
210        match $e {
211            Some(c) => c,
212            None => return Err(DecodeError::EndOfInput),
213        }
214    };
215}
216
217macro_rules! integral {
218    ($name: ident, $ty: ty, false) => {
219        pub fn $name(&mut self) -> DecodeResult<$ty> {
220            self.skip_whitespace();
221            let digit = require!(self.chars.next())
222                .to_digit(10)
223                .ok_or(DecodeError::Expected("digit"))?;
224            let mut dec = digit as $ty;
225            while let Some(c) = self.chars.next() {
226                match c.to_digit(10) {
227                    Some(n) => {
228                        if dec > ($name::MAX - n as $ty) / 10 {
229                            return Err(DecodeError::IntOverflow);
230                        }
231                        dec = dec * 10 + n as $ty;
232                    }
233                    None => {
234                        self.chars.put_back(c);
235                        break;
236                    }
237                }
238            }
239            Ok(dec)
240        }
241    };
242    ($name: ident, $ty: ty, true) => {
243        pub fn $name(&mut self) -> DecodeResult<$ty> {
244            self.skip_whitespace();
245            let is_neg = match self.chars.peek() {
246                Some('-') => {
247                    self.chars.next();
248                    true
249                }
250                _ => false,
251            };
252            let digit = require!(self.chars.next())
253                .to_digit(10)
254                .ok_or(DecodeError::Expected("digit"))?;
255            let mut dec = -(digit as $ty);
256            while let Some(c) = self.chars.next() {
257                match c.to_digit(10) {
258                    Some(n) => {
259                        if dec < ($name::MIN + n as $ty) / 10 {
260                            return Err(DecodeError::IntOverflow);
261                        }
262                        dec = dec * 10 - n as $ty;
263                    }
264                    None => {
265                        self.chars.put_back(c);
266                        break;
267                    }
268                }
269            }
270            Ok(if is_neg { dec } else { -dec })
271        }
272    };
273}
274
275impl<I: Iterator<Item = char>> Decoder<I> {
276    pub fn new(c: Config, i: I) -> Decoder<I> {
277        Decoder {
278            chars: Source::new(i),
279            config: c,
280            buffer: [0; 512],
281            stack: Stack::new(),
282        }
283    }
284
285    /// Create decoder with default `Config`.
286    pub fn default(i: I) -> Decoder<I> {
287        Decoder::new(Config::default(), i)
288    }
289
290    /// Have we exhausted the iterator?
291    pub fn is_end(&mut self) -> bool {
292        self.skip_whitespace();
293        self.chars.peek().is_none()
294    }
295
296    pub fn into_iter(self) -> I {
297        self.chars.into_iter()
298    }
299
300    pub fn iter_mut(&mut self) -> &mut I {
301        self.chars.iter_mut()
302    }
303
304    /// Generic conversion from JSON to an instance of `FromJson`.
305    pub fn from_json<T: FromJson>(&mut self) -> DecodeResult<T> {
306        FromJson::decode(self)
307    }
308
309    /// Decode into `Json` AST value.
310    pub fn decode(&mut self) -> DecodeResult<Json> {
311        let n = self.config.max_nesting;
312        self.decode_json(n)
313    }
314
315    fn decode_json(&mut self, level: usize) -> DecodeResult<Json> {
316        if level == 0 {
317            return Err(DecodeError::MaxRecursion);
318        }
319        self.skip_whitespace();
320        match require!(self.chars.peek()) {
321            '"' => self.string().map(Json::String),
322            't' | 'f' => self.bool().map(Json::Bool),
323            'n' => self.null().map(|_| Json::Null),
324            '0'..='9' | '-' => self.i128().map(Json::I128),
325            '[' => {
326                self.array()?;
327                let mut v = Vec::new();
328                while self.has_more()? {
329                    let x = self.decode_json(level - 1)?;
330                    v.push(x)
331                }
332                Ok(Json::Array(v))
333            }
334            '{' => {
335                self.object()?;
336                let mut m = HashMap::new();
337                while self.has_more()? {
338                    let k = self.key()?;
339                    let v = self.decode_json(level - 1)?;
340                    m.insert(k, v);
341                }
342                Ok(Json::Object(m))
343            }
344            chr => return Err(DecodeError::Unexpected(chr)),
345        }
346    }
347
348    /// Decode as generic Json but ignore the result.
349    ///
350    /// Semantically this method is equivalent to `Decoder::decode`
351    /// but potentially more efficient as it tries to avoid allocating
352    /// intermediate values.
353    pub fn skip(&mut self) -> DecodeResult<()> {
354        let n = self.config.max_nesting;
355        let mut b = [0; 0];
356        let mut u = Utf8Buffer::new(&mut b);
357        self.skip_json(&mut u, n)
358    }
359
360    fn skip_json(&mut self, b: &mut Utf8Buffer, level: usize) -> DecodeResult<()> {
361        if level == 0 {
362            return Err(DecodeError::MaxRecursion);
363        }
364        self.skip_whitespace();
365        match require!(self.chars.peek()) {
366            '"' => self.str(b, false).map(|_| ()),
367            't' | 'f' => self.bool().map(|_| ()),
368            'n' => self.null().map(|_| ()),
369            '0'..='9' | '-' => self.i128().map(|_| ()),
370            '[' => {
371                self.array()?;
372                while self.has_more()? {
373                    self.skip_json(b, level - 1)?;
374                }
375                Ok(())
376            }
377            '{' => {
378                self.object()?;
379                while self.has_more()? {
380                    self.key_str(b, false)?;
381                    self.skip_json(b, level - 1)?;
382                }
383                Ok(())
384            }
385            chr => return Err(DecodeError::Unexpected(chr)),
386        }
387    }
388
389    integral!(u8, u8, false);
390    integral!(u16, u16, false);
391    integral!(u32, u32, false);
392    integral!(u64, u64, false);
393    integral!(u128, u128, false);
394    integral!(usize, usize, false);
395
396    integral!(i8, i8, true);
397    integral!(i16, i16, true);
398    integral!(i32, i32, true);
399    integral!(i64, i64, true);
400    integral!(i128, i128, true);
401    integral!(isize, isize, true);
402
403    // pub fn i128(&mut self) -> DecodeResult<i128> {
404    //     fn is_valid(x: char) -> bool {
405    //         match x {
406    //             '0'..='9' | '+' | '-' => true,
407    //             _ => false,
408    //         }
409    //     }
410    //     self.skip_whitespace();
411    //     let is_neg = match self.chars.peek() {
412    //         Some('-') => {
413    //             self.chars.next();
414    //             true
415    //         }
416    //         _ => false,
417    //     };
418    //     let d = require!(self.chars.next());
419    //     if !d.is_digit(10) {
420    //         return Err(DecodeError::Unexpected(d));
421    //     }
422    //     let mut buf = Utf8Buffer::new(&mut self.buffer);
423    //     buf.push(d);
424    //     while let Some(c) = self.chars.next() {
425    //         if is_valid(c) {
426    //             buf.push(c)
427    //         } else {
428    //             self.chars.put_back(c);
429    //             break;
430    //         }
431    //     }
432    //     match buf.as_str().parse::<i128>() {
433    //         Err(_) => Err(DecodeError::Number),
434    //         Ok(n) => Ok(if is_neg { -n } else { n }),
435    //     }
436    // }
437
438    pub fn null(&mut self) -> DecodeResult<()> {
439        self.skip_whitespace();
440        self.matches("null")
441    }
442
443    pub fn bool(&mut self) -> DecodeResult<bool> {
444        self.skip_whitespace();
445        match require!(self.chars.next()) {
446            't' => self.matches("rue").map(|_| true),
447            'f' => self.matches("alse").map(|_| false),
448            chr => Err(DecodeError::Unexpected(chr)),
449        }
450    }
451
452    pub fn string(&mut self) -> DecodeResult<String> {
453        let mut s = String::new();
454        self.string_into(&mut s, false)?;
455        Ok(s)
456    }
457
458    /// Decode a JSON string into the given `Utf8Buffer`.
459    ///
460    /// If the actual string does not fit into the provided buffer
461    /// and `overflow_err` is `true`, `DecodeError::BufferOverflow`
462    /// is returned immediately. If `overflow_err` is false we
463    /// continue decoding the string, but the buffer will only
464    /// contain as much as fits into it.
465    pub fn str(&mut self, b: &mut Utf8Buffer, overflow_err: bool) -> DecodeResult<()> {
466        self.string_into(b, overflow_err)
467    }
468
469    fn string_into<B: Buffer>(&mut self, s: &mut B, overflow_err: bool) -> DecodeResult<()> {
470        self.skip_whitespace();
471        if self.chars.next() != Some('"') {
472            return Err(DecodeError::Expected("\""));
473        }
474        let mut escaped = false;
475        loop {
476            match require!(self.chars.next()) {
477                chr if escaped => {
478                    match chr {
479                        '"' => s.push('"'),
480                        '/' => s.push('/'),
481                        '\\' => s.push('\\'),
482                        'b' => s.push('\x08'),
483                        'f' => s.push('\x0C'),
484                        'n' => s.push('\n'),
485                        'r' => s.push('\r'),
486                        't' => s.push('\t'),
487                        'u' => match self.hex_unicode()? {
488                            hi @ 0xD800..=0xDBFF => {
489                                self.matches("\\u")?;
490                                let lo = self.hex_unicode()?;
491                                if lo < 0xDC00 || lo > 0xDFFF {
492                                    return Err(DecodeError::UnicodeEscape);
493                                }
494                                let c =
495                                    (((hi - 0xD800) as u32) << 10 | (lo - 0xDC00) as u32) + 0x10000;
496                                s.push(char::from_u32(c).unwrap())
497                            }
498                            0xDC00..=0xDFFF => return Err(DecodeError::UnicodeEscape),
499                            x => match char::from_u32(x as u32) {
500                                Some(c) => s.push(c),
501                                None => return Err(DecodeError::UnicodeEscape),
502                            },
503                        },
504                        c => return Err(DecodeError::Unexpected(c)),
505                    }
506                    escaped = false
507                }
508                '\\' => escaped = true,
509                '"' => break,
510                chr => s.push(chr),
511            }
512            if overflow_err && s.is_full() {
513                return Err(DecodeError::BufferOverflow);
514            }
515        }
516        Ok(())
517    }
518
519    /// Decode `null` (which is mapped to `None`) or some other
520    /// JSON value (mapped to `Some`).
521    pub fn optional<A, F>(&mut self, mut f: F) -> DecodeResult<Option<A>>
522    where
523        F: FnMut(&mut Decoder<I>) -> DecodeResult<A>,
524    {
525        self.skip_whitespace();
526        match require!(self.chars.peek()) {
527            'n' => self.null().map(|_| None),
528            _ => f(self).map(Some),
529        }
530    }
531
532    /// When parsing JSON arrays and objects this needs to be
533    /// called in between to check if the array / object end has been
534    /// reached. E.g.
535    ///
536    /// ```
537    /// use json_codec_wasm::{Decoder, DecodeResult};
538    ///
539    /// fn foo<I: Iterator<Item=char>>(d: &mut Decoder<I>) -> DecodeResult<Vec<u8>> {
540    ///     d.array()?;
541    ///     let mut v = Vec::new();
542    ///     while d.has_more()? {
543    ///         v.push(d.u8()?)
544    ///     }
545    ///     Ok(v)
546    /// }
547    /// ```
548    pub fn has_more(&mut self) -> DecodeResult<bool> {
549        self.skip_whitespace();
550        match self.stack.top() {
551            Some(Scope::A(false)) => match self.chars.peek() {
552                Some(']') => {
553                    self.chars.next();
554                    self.stack.pop();
555                    Ok(false)
556                }
557                Some(_) => {
558                    self.stack.set();
559                    Ok(true)
560                }
561                None => Err(DecodeError::EndOfInput),
562            },
563            Some(Scope::A(true)) => match self.chars.next() {
564                Some(',') => Ok(true),
565                Some(']') => {
566                    self.stack.pop();
567                    Ok(false)
568                }
569                _ => Err(DecodeError::Expected("',' or ']'")),
570            },
571            Some(Scope::O(false)) => match self.chars.peek() {
572                Some('}') => {
573                    self.chars.next();
574                    self.stack.pop();
575                    Ok(false)
576                }
577                Some(_) => {
578                    self.stack.set();
579                    Ok(true)
580                }
581                None => Err(DecodeError::EndOfInput),
582            },
583            Some(Scope::O(true)) => match self.chars.next() {
584                Some(',') => Ok(true),
585                Some('}') => {
586                    self.stack.pop();
587                    Ok(false)
588                }
589                _ => Err(DecodeError::Expected("',' or '}'")),
590            },
591            None => Ok(false),
592        }
593    }
594
595    /// Begin parsing a JSON array.
596    ///
597    /// Before each element is parsed, `Decoder::has_more`
598    /// needs to be queried.
599    pub fn array(&mut self) -> DecodeResult<()> {
600        self.skip_whitespace();
601        self.matches("[")?;
602        self.stack.push(Scope::A(false));
603        Ok(())
604    }
605
606    /// Create a JSON array iterator to decode a homogenous array.
607    ///
608    /// ```
609    /// use json_codec_wasm::Decoder;
610    ///
611    /// let mut d = Decoder::default("[1,2,3,4]".chars());
612    /// let mut v: Vec<u8> = Vec::new();
613    ///
614    /// for i in d.array_iter().unwrap() {
615    ///     v.push(i.unwrap())
616    /// }
617    /// ```
618    pub fn array_iter<A: FromJson>(&mut self) -> DecodeResult<ArrayIter<A, I>> {
619        self.array()?;
620        Ok(ArrayIter::new(self))
621    }
622
623    /// Begin parsing a JSON object.
624    ///
625    /// Before each key is parsed, `Decoder::has_more`
626    /// needs to be queried.
627    pub fn object(&mut self) -> DecodeResult<()> {
628        self.skip_whitespace();
629        self.matches("{")?;
630        self.stack.push(Scope::O(false));
631        Ok(())
632    }
633
634    /// Decode a JSON object key.
635    pub fn key(&mut self) -> DecodeResult<String> {
636        let mut s = String::new();
637        self.key_into(&mut s, false)?;
638        Ok(s)
639    }
640
641    /// Decode a JSON object key into the given buffer.
642    pub fn key_str(&mut self, b: &mut Utf8Buffer, overflow_err: bool) -> DecodeResult<()> {
643        self.key_into(b, overflow_err)
644    }
645
646    fn key_into<B: Buffer>(&mut self, s: &mut B, overflow_err: bool) -> DecodeResult<()> {
647        self.string_into(s, overflow_err)?;
648        self.skip_whitespace();
649        if self.chars.next() != Some(':') {
650            return Err(DecodeError::Expected(":"));
651        }
652        Ok(())
653    }
654
655    fn skip_whitespace(&mut self) {
656        while let Some(c) = self.chars.next() {
657            if !c.is_whitespace() {
658                self.chars.put_back(c);
659                break;
660            }
661        }
662    }
663
664    fn hex_unicode(&mut self) -> DecodeResult<u16> {
665        let a = hex_byte(require!(self.chars.next()))?;
666        let b = hex_byte(require!(self.chars.next()))?;
667        let c = hex_byte(require!(self.chars.next()))?;
668        let d = hex_byte(require!(self.chars.next()))?;
669        Ok(a as u16 * 0x1000 + b as u16 * 0x100 + c as u16 * 0x10 + d as u16)
670    }
671
672    fn matches(&mut self, pattern: &str) -> DecodeResult<()> {
673        for a in pattern.chars() {
674            let b = require!(self.chars.next());
675            if a != b {
676                return Err(DecodeError::Unexpected(b));
677            }
678        }
679        Ok(())
680    }
681}
682
683fn hex_byte(digit: char) -> DecodeResult<u8> {
684    match digit {
685        '0' => Ok(0),
686        '1' => Ok(1),
687        '2' => Ok(2),
688        '3' => Ok(3),
689        '4' => Ok(4),
690        '5' => Ok(5),
691        '6' => Ok(6),
692        '7' => Ok(7),
693        '8' => Ok(8),
694        '9' => Ok(9),
695        'A' | 'a' => Ok(10),
696        'B' | 'b' => Ok(11),
697        'C' | 'c' => Ok(12),
698        'D' | 'd' => Ok(13),
699        'E' | 'e' => Ok(14),
700        'F' | 'f' => Ok(15),
701        chr => Err(DecodeError::Unexpected(chr)),
702    }
703}
704
705// DecodeError //////////////////////////////////////////////////////////////
706
707pub type DecodeResult<A> = Result<A, DecodeError>;
708
709#[derive(Debug)]
710pub enum DecodeError {
711    /// The underlying iterator stopped giving us new values.
712    EndOfInput,
713    /// An expectation was not met while decoding.
714    Expected(&'static str),
715    /// During generic JSON decoding too many nesting levels had
716    /// been encountered. See `Config` for details.
717    MaxRecursion,
718    /// When decoding integer values, the actual JSON digits exceeded
719    /// the possible value range.
720    IntOverflow,
721    /// An unexpected character was encountered.
722    Unexpected(char),
723    /// An invalid unicode escape sequence was found when
724    /// decoding a JSON string.
725    UnicodeEscape,
726    /// Decoding a numeric value failed.
727    Number,
728    /// A JSON string did not fit into the provided `Utf8Buffer`.
729    BufferOverflow,
730    /// Generic error message.
731    Message(&'static str),
732    /// Some other `Error` trait impl.
733    Other(Box<dyn Error + Send + Sync>),
734}
735
736impl fmt::Display for DecodeError {
737    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
738        match *self {
739            DecodeError::EndOfInput => write!(f, "end of input"),
740            DecodeError::Expected(e) => write!(f, "expected \"{}\"", e),
741            DecodeError::MaxRecursion => write!(f, "max. number of recursions reached"),
742            DecodeError::IntOverflow => write!(f, "integer overflow"),
743            DecodeError::Unexpected(c) => write!(f, "unexpected \"{}\"", c),
744            DecodeError::UnicodeEscape => write!(f, "invalid unicode escape"),
745            DecodeError::Number => write!(f, "failed to parse number"),
746            DecodeError::BufferOverflow => write!(f, "buffer overflow"),
747            DecodeError::Message(m) => write!(f, "error: {}", m),
748            DecodeError::Other(ref e) => write!(f, "other: {}", e),
749        }
750    }
751}
752
753impl Error for DecodeError {
754    fn description(&self) -> &str {
755        match *self {
756            DecodeError::EndOfInput => "end of input",
757            DecodeError::Expected(_) => "expected some string",
758            DecodeError::MaxRecursion => "objects/arrays are too deeply nested",
759            DecodeError::IntOverflow => "integer overflow",
760            DecodeError::Unexpected(_) => "unexpected char",
761            DecodeError::UnicodeEscape => "invalid unicode escape sequence",
762            DecodeError::Number => "failed to decode number",
763            DecodeError::BufferOverflow => "buffer too small to hold value",
764            DecodeError::Message(_) => "generic error message",
765            DecodeError::Other(_) => "other error",
766        }
767    }
768
769    fn cause(&self) -> Option<&dyn Error> {
770        match *self {
771            DecodeError::Other(ref e) => Some(&**e),
772            _ => None,
773        }
774    }
775}
776
777// Source ///////////////////////////////////////////////////////////////////
778
779struct Source<I: Iterator> {
780    iter: I,
781    front: Option<I::Item>,
782}
783
784impl<I: Iterator> Source<I> {
785    fn new(i: I) -> Source<I> {
786        Source {
787            iter: i,
788            front: None,
789        }
790    }
791
792    fn put_back(&mut self, i: I::Item) {
793        self.front = Some(i)
794    }
795
796    fn peek(&mut self) -> Option<I::Item>
797    where
798        I::Item: Copy,
799    {
800        self.next().map(|c| {
801            self.put_back(c);
802            c
803        })
804    }
805
806    fn into_iter(self) -> I {
807        self.iter
808    }
809
810    fn iter_mut(&mut self) -> &mut I {
811        &mut self.iter
812    }
813}
814
815impl<I: Iterator> Iterator for Source<I> {
816    type Item = I::Item;
817
818    fn next(&mut self) -> Option<I::Item> {
819        self.front.take().or_else(|| self.iter.next())
820    }
821}
822
823// ArrayIter /////////////////////////////////////////////////////////////////
824
825/// Iterator over JSON array elements.
826///
827/// Used in conjunction with homogenous JSON arrays.
828pub struct ArrayIter<'r, A, I>
829where
830    I: Iterator<Item = char> + 'r,
831{
832    dec: &'r mut Decoder<I>,
833    _ty: PhantomData<A>,
834}
835
836impl<'r, A, I> ArrayIter<'r, A, I>
837where
838    I: Iterator<Item = char> + 'r,
839{
840    fn new(d: &'r mut Decoder<I>) -> ArrayIter<'r, A, I> {
841        ArrayIter {
842            dec: d,
843            _ty: PhantomData,
844        }
845    }
846}
847
848impl<'r, A, I> Iterator for ArrayIter<'r, A, I>
849where
850    I: Iterator<Item = char> + 'r,
851    A: FromJson,
852{
853    type Item = DecodeResult<A>;
854
855    fn next(&mut self) -> Option<DecodeResult<A>> {
856        match self.dec.has_more() {
857            Ok(true) => Some(self.dec.from_json()),
858            Ok(false) => None,
859            Err(e) => Some(Err(e)),
860        }
861    }
862}
863
864// ReadIter /////////////////////////////////////////////////////////////////
865
866/// Iterator over characters read from any `Read`-type.
867///
868/// Uses the `chars` method of the `Read` interface. Since `Decoder`s
869/// expect iterators over plain `char`s this iterator yields `None`
870/// whenever the underlying `Chars` iterator returned an error.
871pub struct ReadIter<R: Read> {
872    iter: utf8::Chars<R>,
873    error: Option<ReadError>,
874}
875
876impl<R: Read> ReadIter<R> {
877    pub fn new(r: R) -> ReadIter<R> {
878        ReadIter {
879            iter: utf8::Chars::new(r),
880            error: None,
881        }
882    }
883
884    /// Get the error that has been encountered (if any).
885    pub fn error(&self) -> Option<&utf8::ReadError> {
886        self.error.as_ref()
887    }
888
889    /// Get and remove the error that has been encountered (if any).
890    pub fn take_error(&mut self) -> Option<utf8::ReadError> {
891        self.error.take()
892    }
893}
894
895impl<R: Read> Iterator for ReadIter<R> {
896    type Item = char;
897
898    fn next(&mut self) -> Option<char> {
899        match self.iter.next() {
900            Some(Ok(c)) => Some(c),
901            Some(Err(e)) => {
902                self.error = Some(e);
903                None
904            }
905            None => None,
906        }
907    }
908}
909
910// Tests ////////////////////////////////////////////////////////////////////
911
912#[cfg(test)]
913mod tests {
914    use super::*;
915
916    #[test]
917    fn empty1() {
918        let mut d = Decoder::default(r#"[{}]"#.chars());
919        d.decode().unwrap();
920    }
921
922    #[test]
923    fn empty2() {
924        let mut d = Decoder::default(r#"[]"#.chars());
925        d.array().unwrap();
926        assert!(!d.has_more().unwrap())
927    }
928
929    #[test]
930    fn object() {
931        let mut d = Decoder::default(r#" {"outer": {"inner": 32 } } "#.chars());
932        d.object().unwrap();
933        assert!(d.has_more().unwrap());
934        let k = d.key().unwrap();
935        assert_eq!("outer", &k);
936        d.object().unwrap();
937        assert!(d.has_more().unwrap());
938        let k = d.key().unwrap();
939        assert_eq!("inner", &k);
940        let v = d.isize().unwrap();
941        assert_eq!(32, v);
942        assert!(!d.has_more().unwrap());
943        assert!(!d.has_more().unwrap());
944    }
945
946    #[test]
947    fn array() {
948        let mut d = Decoder::default(" [ [   [1, 2  ,3], 42 ] ]   ".chars());
949        d.array().unwrap();
950        assert!(d.has_more().unwrap());
951        d.array().unwrap();
952        assert!(d.has_more().unwrap());
953        d.array().unwrap();
954        assert!(d.has_more().unwrap());
955        let a = d.isize().unwrap();
956        assert!(d.has_more().unwrap());
957        let b = d.isize().unwrap();
958        assert!(d.has_more().unwrap());
959        let c = d.isize().unwrap();
960        assert!(!d.has_more().unwrap());
961        assert!(d.has_more().unwrap());
962        let x = d.isize().unwrap();
963        assert!(!d.has_more().unwrap());
964        assert!(!d.has_more().unwrap());
965        assert_eq!((1, 2, 3, 42), (a, b, c, x))
966    }
967
968    #[test]
969    fn array_iter() {
970        let mut d = Decoder::new(Config::default(), "[ true, false, true ]".chars());
971        let mut v = Vec::new();
972        for x in d.array_iter().unwrap() {
973            v.push(x.unwrap())
974        }
975        assert_eq!(vec![true, false, true], v)
976    }
977
978    #[test]
979    fn array_macro() {
980        fn read_array() -> DecodeResult<Vec<bool>> {
981            let mut d = Decoder::new(Config::default(), "[ true, false, true ]".chars());
982            array!(d, d.bool())
983        }
984        assert_eq!(vec![true, false, true], read_array().unwrap())
985    }
986
987    #[test]
988    fn numbers() {
989        let mut d =
990            Decoder::default("1 0 -1 18446744073709551615 -9223372036854775808 255 256".chars());
991        assert_eq!(1, d.u8().unwrap());
992        assert_eq!(0, d.u8().unwrap());
993        assert_eq!(-1, d.i8().unwrap());
994        assert_eq!(18446744073709551615, d.u64().unwrap());
995        assert_eq!(-9223372036854775808, d.i64().unwrap());
996        assert_eq!(255, d.u8().unwrap());
997        match d.u8() {
998            Err(DecodeError::IntOverflow) => (),
999            other => panic!("unexpected result: {:?}", other),
1000        }
1001    }
1002
1003    #[test]
1004    fn generic() {
1005        let mut d = Decoder::default("null".chars());
1006        assert_eq!(None, d.from_json::<Option<u8>>().unwrap());
1007
1008        let mut d = Decoder::default("100".chars());
1009        assert_eq!(Some(100u8), d.from_json().unwrap())
1010    }
1011}