]> git.lizzy.rs Git - rust.git/blob - src/libserialize/json.rs
syntax: Migrate built-in macros to the regular stability checking
[rust.git] / src / libserialize / json.rs
1 // Rust JSON serialization library.
2 // Copyright (c) 2011 Google Inc.
3
4 #![forbid(non_camel_case_types)]
5 #![allow(missing_docs)]
6
7 //! JSON parsing and serialization
8 //!
9 //! # What is JSON?
10 //!
11 //! JSON (JavaScript Object Notation) is a way to write data in Javascript.
12 //! Like XML, it allows to encode structured data in a text format that can be easily read by humans
13 //! Its simple syntax and native compatibility with JavaScript have made it a widely used format.
14 //!
15 //! Data types that can be encoded are JavaScript types (see the `Json` enum for more details):
16 //!
17 //! * `Boolean`: equivalent to rust's `bool`
18 //! * `Number`: equivalent to rust's `f64`
19 //! * `String`: equivalent to rust's `String`
20 //! * `Array`: equivalent to rust's `Vec<T>`, but also allowing objects of different types in the
21 //!   same array
22 //! * `Object`: equivalent to rust's `BTreeMap<String, json::Json>`
23 //! * `Null`
24 //!
25 //! An object is a series of string keys mapping to values, in `"key": value` format.
26 //! Arrays are enclosed in square brackets ([ ... ]) and objects in curly brackets ({ ... }).
27 //! A simple JSON document encoding a person, their age, address and phone numbers could look like
28 //!
29 //! ```json
30 //! {
31 //!     "FirstName": "John",
32 //!     "LastName": "Doe",
33 //!     "Age": 43,
34 //!     "Address": {
35 //!         "Street": "Downing Street 10",
36 //!         "City": "London",
37 //!         "Country": "Great Britain"
38 //!     },
39 //!     "PhoneNumbers": [
40 //!         "+44 1234567",
41 //!         "+44 2345678"
42 //!     ]
43 //! }
44 //! ```
45 //!
46 //! # Rust Type-based Encoding and Decoding
47 //!
48 //! Rust provides a mechanism for low boilerplate encoding & decoding of values to and from JSON via
49 //! the serialization API.
50 //! To be able to encode a piece of data, it must implement the `serialize::RustcEncodable` trait.
51 //! To be able to decode a piece of data, it must implement the `serialize::RustcDecodable` trait.
52 //! The Rust compiler provides an annotation to automatically generate the code for these traits:
53 //! `#[derive(RustcDecodable, RustcEncodable)]`
54 //!
55 //! The JSON API provides an enum `json::Json` and a trait `ToJson` to encode objects.
56 //! The `ToJson` trait provides a `to_json` method to convert an object into a `json::Json` value.
57 //! A `json::Json` value can be encoded as a string or buffer using the functions described above.
58 //! You can also use the `json::Encoder` object, which implements the `Encoder` trait.
59 //!
60 //! When using `ToJson` the `RustcEncodable` trait implementation is not mandatory.
61 //!
62 //! # Examples of use
63 //!
64 //! ## Using Autoserialization
65 //!
66 //! Create a struct called `TestStruct` and serialize and deserialize it to and from JSON using the
67 //! serialization API, using the derived serialization code.
68 //!
69 //! ```rust
70 //! # #![feature(rustc_private)]
71 //! extern crate serialize as rustc_serialize; // for the deriving below
72 //! use rustc_serialize::json;
73 //!
74 //! // Automatically generate `Decodable` and `Encodable` trait implementations
75 //! #[derive(RustcDecodable, RustcEncodable)]
76 //! pub struct TestStruct  {
77 //!     data_int: u8,
78 //!     data_str: String,
79 //!     data_vector: Vec<u8>,
80 //! }
81 //!
82 //! fn main() {
83 //!     let object = TestStruct {
84 //!         data_int: 1,
85 //!         data_str: "homura".to_string(),
86 //!         data_vector: vec![2,3,4,5],
87 //!     };
88 //!
89 //!     // Serialize using `json::encode`
90 //!     let encoded = json::encode(&object).unwrap();
91 //!
92 //!     // Deserialize using `json::decode`
93 //!     let decoded: TestStruct = json::decode(&encoded[..]).unwrap();
94 //! }
95 //! ```
96 //!
97 //! ## Using the `ToJson` trait
98 //!
99 //! The examples above use the `ToJson` trait to generate the JSON string, which is required
100 //! for custom mappings.
101 //!
102 //! ### Simple example of `ToJson` usage
103 //!
104 //! ```rust
105 //! # #![feature(rustc_private)]
106 //! # #![allow(deprecated)]
107 //! extern crate serialize;
108 //! use serialize::json::{self, ToJson, Json};
109 //!
110 //! // A custom data structure
111 //! struct ComplexNum {
112 //!     a: f64,
113 //!     b: f64,
114 //! }
115 //!
116 //! // JSON value representation
117 //! impl ToJson for ComplexNum {
118 //!     fn to_json(&self) -> Json {
119 //!         Json::String(format!("{}+{}i", self.a, self.b))
120 //!     }
121 //! }
122 //!
123 //! // Only generate `RustcEncodable` trait implementation
124 //! #[derive(Encodable)]
125 //! pub struct ComplexNumRecord {
126 //!     uid: u8,
127 //!     dsc: String,
128 //!     val: Json,
129 //! }
130 //!
131 //! fn main() {
132 //!     let num = ComplexNum { a: 0.0001, b: 12.539 };
133 //!     let data: String = json::encode(&ComplexNumRecord{
134 //!         uid: 1,
135 //!         dsc: "test".to_string(),
136 //!         val: num.to_json(),
137 //!     }).unwrap();
138 //!     println!("data: {}", data);
139 //!     // data: {"uid":1,"dsc":"test","val":"0.0001+12.539i"};
140 //! }
141 //! ```
142 //!
143 //! ### Verbose example of `ToJson` usage
144 //!
145 //! ```rust
146 //! # #![feature(rustc_private)]
147 //! # #![allow(deprecated)]
148 //! extern crate serialize;
149 //! use std::collections::BTreeMap;
150 //! use serialize::json::{self, Json, ToJson};
151 //!
152 //! // Only generate `Decodable` trait implementation
153 //! #[derive(Decodable)]
154 //! pub struct TestStruct {
155 //!     data_int: u8,
156 //!     data_str: String,
157 //!     data_vector: Vec<u8>,
158 //! }
159 //!
160 //! // Specify encoding method manually
161 //! impl ToJson for TestStruct {
162 //!     fn to_json(&self) -> Json {
163 //!         let mut d = BTreeMap::new();
164 //!         // All standard types implement `to_json()`, so use it
165 //!         d.insert("data_int".to_string(), self.data_int.to_json());
166 //!         d.insert("data_str".to_string(), self.data_str.to_json());
167 //!         d.insert("data_vector".to_string(), self.data_vector.to_json());
168 //!         Json::Object(d)
169 //!     }
170 //! }
171 //!
172 //! fn main() {
173 //!     // Serialize using `ToJson`
174 //!     let input_data = TestStruct {
175 //!         data_int: 1,
176 //!         data_str: "madoka".to_string(),
177 //!         data_vector: vec![2,3,4,5],
178 //!     };
179 //!     let json_obj: Json = input_data.to_json();
180 //!     let json_str: String = json_obj.to_string();
181 //!
182 //!     // Deserialize like before
183 //!     let decoded: TestStruct = json::decode(&json_str).unwrap();
184 //! }
185 //! ```
186
187 use self::JsonEvent::*;
188 use self::ErrorCode::*;
189 use self::ParserError::*;
190 use self::DecoderError::*;
191 use self::ParserState::*;
192 use self::InternalStackElement::*;
193
194 use std::borrow::Cow;
195 use std::collections::{HashMap, BTreeMap};
196 use std::io::prelude::*;
197 use std::io;
198 use std::mem::swap;
199 use std::num::FpCategory as Fp;
200 use std::ops::Index;
201 use std::str::FromStr;
202 use std::string;
203 use std::{char, f64, fmt, str};
204
205 use crate::Encodable;
206
207 /// Represents a json value
208 #[derive(Clone, PartialEq, PartialOrd, Debug)]
209 pub enum Json {
210     I64(i64),
211     U64(u64),
212     F64(f64),
213     String(string::String),
214     Boolean(bool),
215     Array(self::Array),
216     Object(self::Object),
217     Null,
218 }
219
220 pub type Array = Vec<Json>;
221 pub type Object = BTreeMap<string::String, Json>;
222
223 pub struct PrettyJson<'a> { inner: &'a Json }
224
225 pub struct AsJson<'a, T> { inner: &'a T }
226 pub struct AsPrettyJson<'a, T> { inner: &'a T, indent: Option<usize> }
227
228 /// The errors that can arise while parsing a JSON stream.
229 #[derive(Clone, Copy, PartialEq, Debug)]
230 pub enum ErrorCode {
231     InvalidSyntax,
232     InvalidNumber,
233     EOFWhileParsingObject,
234     EOFWhileParsingArray,
235     EOFWhileParsingValue,
236     EOFWhileParsingString,
237     KeyMustBeAString,
238     ExpectedColon,
239     TrailingCharacters,
240     TrailingComma,
241     InvalidEscape,
242     InvalidUnicodeCodePoint,
243     LoneLeadingSurrogateInHexEscape,
244     UnexpectedEndOfHexEscape,
245     UnrecognizedHex,
246     NotFourDigit,
247     NotUtf8,
248 }
249
250 #[derive(Clone, PartialEq, Debug)]
251 pub enum ParserError {
252     /// msg, line, col
253     SyntaxError(ErrorCode, usize, usize),
254     IoError(io::ErrorKind, String),
255 }
256
257 // Builder and Parser have the same errors.
258 pub type BuilderError = ParserError;
259
260 #[derive(Clone, PartialEq, Debug)]
261 pub enum DecoderError {
262     ParseError(ParserError),
263     ExpectedError(string::String, string::String),
264     MissingFieldError(string::String),
265     UnknownVariantError(string::String),
266     ApplicationError(string::String)
267 }
268
269 #[derive(Copy, Clone, Debug)]
270 pub enum EncoderError {
271     FmtError(fmt::Error),
272     BadHashmapKey,
273 }
274
275 /// Returns a readable error string for a given error code.
276 pub fn error_str(error: ErrorCode) -> &'static str {
277     match error {
278         InvalidSyntax => "invalid syntax",
279         InvalidNumber => "invalid number",
280         EOFWhileParsingObject => "EOF While parsing object",
281         EOFWhileParsingArray => "EOF While parsing array",
282         EOFWhileParsingValue => "EOF While parsing value",
283         EOFWhileParsingString => "EOF While parsing string",
284         KeyMustBeAString => "key must be a string",
285         ExpectedColon => "expected `:`",
286         TrailingCharacters => "trailing characters",
287         TrailingComma => "trailing comma",
288         InvalidEscape => "invalid escape",
289         UnrecognizedHex => "invalid \\u{ esc}ape (unrecognized hex)",
290         NotFourDigit => "invalid \\u{ esc}ape (not four digits)",
291         NotUtf8 => "contents not utf-8",
292         InvalidUnicodeCodePoint => "invalid Unicode code point",
293         LoneLeadingSurrogateInHexEscape => "lone leading surrogate in hex escape",
294         UnexpectedEndOfHexEscape => "unexpected end of hex escape",
295     }
296 }
297
298 /// Shortcut function to decode a JSON `&str` into an object
299 pub fn decode<T: crate::Decodable>(s: &str) -> DecodeResult<T> {
300     let json = match from_str(s) {
301         Ok(x) => x,
302         Err(e) => return Err(ParseError(e))
303     };
304
305     let mut decoder = Decoder::new(json);
306     crate::Decodable::decode(&mut decoder)
307 }
308
309 /// Shortcut function to encode a `T` into a JSON `String`
310 pub fn encode<T: crate::Encodable>(object: &T) -> Result<string::String, EncoderError> {
311     let mut s = String::new();
312     {
313         let mut encoder = Encoder::new(&mut s);
314         object.encode(&mut encoder)?;
315     }
316     Ok(s)
317 }
318
319 impl fmt::Display for ErrorCode {
320     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
321         error_str(*self).fmt(f)
322     }
323 }
324
325 fn io_error_to_error(io: io::Error) -> ParserError {
326     IoError(io.kind(), io.to_string())
327 }
328
329 impl fmt::Display for ParserError {
330     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
331         // FIXME this should be a nicer error
332         fmt::Debug::fmt(self, f)
333     }
334 }
335
336 impl fmt::Display for DecoderError {
337     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
338         // FIXME this should be a nicer error
339         fmt::Debug::fmt(self, f)
340     }
341 }
342
343 impl std::error::Error for DecoderError {
344     fn description(&self) -> &str { "decoder error" }
345 }
346
347 impl fmt::Display for EncoderError {
348     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
349         // FIXME this should be a nicer error
350         fmt::Debug::fmt(self, f)
351     }
352 }
353
354 impl std::error::Error for EncoderError {
355     fn description(&self) -> &str { "encoder error" }
356 }
357
358 impl From<fmt::Error> for EncoderError {
359     /// Converts a [`fmt::Error`] into `EncoderError`
360     ///
361     /// This conversion does not allocate memory.
362     fn from(err: fmt::Error) -> EncoderError { EncoderError::FmtError(err) }
363 }
364
365 pub type EncodeResult = Result<(), EncoderError>;
366 pub type DecodeResult<T> = Result<T, DecoderError>;
367
368 fn escape_str(wr: &mut dyn fmt::Write, v: &str) -> EncodeResult {
369     wr.write_str("\"")?;
370
371     let mut start = 0;
372
373     for (i, byte) in v.bytes().enumerate() {
374         let escaped = match byte {
375             b'"' => "\\\"",
376             b'\\' => "\\\\",
377             b'\x00' => "\\u0000",
378             b'\x01' => "\\u0001",
379             b'\x02' => "\\u0002",
380             b'\x03' => "\\u0003",
381             b'\x04' => "\\u0004",
382             b'\x05' => "\\u0005",
383             b'\x06' => "\\u0006",
384             b'\x07' => "\\u0007",
385             b'\x08' => "\\b",
386             b'\t' => "\\t",
387             b'\n' => "\\n",
388             b'\x0b' => "\\u000b",
389             b'\x0c' => "\\f",
390             b'\r' => "\\r",
391             b'\x0e' => "\\u000e",
392             b'\x0f' => "\\u000f",
393             b'\x10' => "\\u0010",
394             b'\x11' => "\\u0011",
395             b'\x12' => "\\u0012",
396             b'\x13' => "\\u0013",
397             b'\x14' => "\\u0014",
398             b'\x15' => "\\u0015",
399             b'\x16' => "\\u0016",
400             b'\x17' => "\\u0017",
401             b'\x18' => "\\u0018",
402             b'\x19' => "\\u0019",
403             b'\x1a' => "\\u001a",
404             b'\x1b' => "\\u001b",
405             b'\x1c' => "\\u001c",
406             b'\x1d' => "\\u001d",
407             b'\x1e' => "\\u001e",
408             b'\x1f' => "\\u001f",
409             b'\x7f' => "\\u007f",
410             _ => { continue; }
411         };
412
413         if start < i {
414             wr.write_str(&v[start..i])?;
415         }
416
417         wr.write_str(escaped)?;
418
419         start = i + 1;
420     }
421
422     if start != v.len() {
423         wr.write_str(&v[start..])?;
424     }
425
426     wr.write_str("\"")?;
427     Ok(())
428 }
429
430 fn escape_char(writer: &mut dyn fmt::Write, v: char) -> EncodeResult {
431     escape_str(writer, v.encode_utf8(&mut [0; 4]))
432 }
433
434 fn spaces(wr: &mut dyn fmt::Write, mut n: usize) -> EncodeResult {
435     const BUF: &str = "                ";
436
437     while n >= BUF.len() {
438         wr.write_str(BUF)?;
439         n -= BUF.len();
440     }
441
442     if n > 0 {
443         wr.write_str(&BUF[..n])?;
444     }
445     Ok(())
446 }
447
448 fn fmt_number_or_null(v: f64) -> string::String {
449     match v.classify() {
450         Fp::Nan | Fp::Infinite => string::String::from("null"),
451         _ if v.fract() != 0f64 => v.to_string(),
452         _ => v.to_string() + ".0",
453     }
454 }
455
456 /// A structure for implementing serialization to JSON.
457 pub struct Encoder<'a> {
458     writer: &'a mut (dyn fmt::Write+'a),
459     is_emitting_map_key: bool,
460 }
461
462 impl<'a> Encoder<'a> {
463     /// Creates a new JSON encoder whose output will be written to the writer
464     /// specified.
465     pub fn new(writer: &'a mut dyn fmt::Write) -> Encoder<'a> {
466         Encoder { writer, is_emitting_map_key: false, }
467     }
468 }
469
470 macro_rules! emit_enquoted_if_mapkey {
471     ($enc:ident,$e:expr) => ({
472         if $enc.is_emitting_map_key {
473             write!($enc.writer, "\"{}\"", $e)?;
474         } else {
475             write!($enc.writer, "{}", $e)?;
476         }
477         Ok(())
478     })
479 }
480
481 impl<'a> crate::Encoder for Encoder<'a> {
482     type Error = EncoderError;
483
484     fn emit_unit(&mut self) -> EncodeResult {
485         if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
486         write!(self.writer, "null")?;
487         Ok(())
488     }
489
490     fn emit_usize(&mut self, v: usize) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
491     fn emit_u128(&mut self, v: u128) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
492     fn emit_u64(&mut self, v: u64) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
493     fn emit_u32(&mut self, v: u32) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
494     fn emit_u16(&mut self, v: u16) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
495     fn emit_u8(&mut self, v: u8) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
496
497     fn emit_isize(&mut self, v: isize) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
498     fn emit_i128(&mut self, v: i128) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
499     fn emit_i64(&mut self, v: i64) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
500     fn emit_i32(&mut self, v: i32) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
501     fn emit_i16(&mut self, v: i16) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
502     fn emit_i8(&mut self, v: i8) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
503
504     fn emit_bool(&mut self, v: bool) -> EncodeResult {
505         if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
506         if v {
507             write!(self.writer, "true")?;
508         } else {
509             write!(self.writer, "false")?;
510         }
511         Ok(())
512     }
513
514     fn emit_f64(&mut self, v: f64) -> EncodeResult {
515         emit_enquoted_if_mapkey!(self, fmt_number_or_null(v))
516     }
517     fn emit_f32(&mut self, v: f32) -> EncodeResult {
518         self.emit_f64(f64::from(v))
519     }
520
521     fn emit_char(&mut self, v: char) -> EncodeResult {
522         escape_char(self.writer, v)
523     }
524     fn emit_str(&mut self, v: &str) -> EncodeResult {
525         escape_str(self.writer, v)
526     }
527
528     fn emit_enum<F>(&mut self, _name: &str, f: F) -> EncodeResult where
529         F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
530     {
531         f(self)
532     }
533
534     fn emit_enum_variant<F>(&mut self,
535                             name: &str,
536                             _id: usize,
537                             cnt: usize,
538                             f: F) -> EncodeResult where
539         F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
540     {
541         // enums are encoded as strings or objects
542         // Bunny => "Bunny"
543         // Kangaroo(34,"William") => {"variant": "Kangaroo", "fields": [34,"William"]}
544         if cnt == 0 {
545             escape_str(self.writer, name)
546         } else {
547             if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
548             write!(self.writer, "{{\"variant\":")?;
549             escape_str(self.writer, name)?;
550             write!(self.writer, ",\"fields\":[")?;
551             f(self)?;
552             write!(self.writer, "]}}")?;
553             Ok(())
554         }
555     }
556
557     fn emit_enum_variant_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult where
558         F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
559     {
560         if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
561         if idx != 0 {
562             write!(self.writer, ",")?;
563         }
564         f(self)
565     }
566
567     fn emit_enum_struct_variant<F>(&mut self,
568                                    name: &str,
569                                    id: usize,
570                                    cnt: usize,
571                                    f: F) -> EncodeResult where
572         F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
573     {
574         if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
575         self.emit_enum_variant(name, id, cnt, f)
576     }
577
578     fn emit_enum_struct_variant_field<F>(&mut self,
579                                          _: &str,
580                                          idx: usize,
581                                          f: F) -> EncodeResult where
582         F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
583     {
584         if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
585         self.emit_enum_variant_arg(idx, f)
586     }
587
588     fn emit_struct<F>(&mut self, _: &str, _: usize, f: F) -> EncodeResult where
589         F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
590     {
591         if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
592         write!(self.writer, "{{")?;
593         f(self)?;
594         write!(self.writer, "}}")?;
595         Ok(())
596     }
597
598     fn emit_struct_field<F>(&mut self, name: &str, idx: usize, f: F) -> EncodeResult where
599         F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
600     {
601         if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
602         if idx != 0 { write!(self.writer, ",")?; }
603         escape_str(self.writer, name)?;
604         write!(self.writer, ":")?;
605         f(self)
606     }
607
608     fn emit_tuple<F>(&mut self, len: usize, f: F) -> EncodeResult where
609         F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
610     {
611         if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
612         self.emit_seq(len, f)
613     }
614     fn emit_tuple_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult where
615         F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
616     {
617         if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
618         self.emit_seq_elt(idx, f)
619     }
620
621     fn emit_tuple_struct<F>(&mut self, _name: &str, len: usize, f: F) -> EncodeResult where
622         F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
623     {
624         if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
625         self.emit_seq(len, f)
626     }
627     fn emit_tuple_struct_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult where
628         F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
629     {
630         if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
631         self.emit_seq_elt(idx, f)
632     }
633
634     fn emit_option<F>(&mut self, f: F) -> EncodeResult where
635         F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
636     {
637         if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
638         f(self)
639     }
640     fn emit_option_none(&mut self) -> EncodeResult {
641         if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
642         self.emit_unit()
643     }
644     fn emit_option_some<F>(&mut self, f: F) -> EncodeResult where
645         F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
646     {
647         if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
648         f(self)
649     }
650
651     fn emit_seq<F>(&mut self, _len: usize, f: F) -> EncodeResult where
652         F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
653     {
654         if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
655         write!(self.writer, "[")?;
656         f(self)?;
657         write!(self.writer, "]")?;
658         Ok(())
659     }
660
661     fn emit_seq_elt<F>(&mut self, idx: usize, f: F) -> EncodeResult where
662         F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
663     {
664         if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
665         if idx != 0 {
666             write!(self.writer, ",")?;
667         }
668         f(self)
669     }
670
671     fn emit_map<F>(&mut self, _len: usize, f: F) -> EncodeResult where
672         F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
673     {
674         if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
675         write!(self.writer, "{{")?;
676         f(self)?;
677         write!(self.writer, "}}")?;
678         Ok(())
679     }
680
681     fn emit_map_elt_key<F>(&mut self, idx: usize, f: F) -> EncodeResult where
682         F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
683     {
684         if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
685         if idx != 0 { write!(self.writer, ",")? }
686         self.is_emitting_map_key = true;
687         f(self)?;
688         self.is_emitting_map_key = false;
689         Ok(())
690     }
691
692     fn emit_map_elt_val<F>(&mut self, _idx: usize, f: F) -> EncodeResult where
693         F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
694     {
695         if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
696         write!(self.writer, ":")?;
697         f(self)
698     }
699 }
700
701 /// Another encoder for JSON, but prints out human-readable JSON instead of
702 /// compact data
703 pub struct PrettyEncoder<'a> {
704     writer: &'a mut (dyn fmt::Write+'a),
705     curr_indent: usize,
706     indent: usize,
707     is_emitting_map_key: bool,
708 }
709
710 impl<'a> PrettyEncoder<'a> {
711     /// Creates a new encoder whose output will be written to the specified writer
712     pub fn new(writer: &'a mut dyn fmt::Write) -> PrettyEncoder<'a> {
713         PrettyEncoder {
714             writer,
715             curr_indent: 0,
716             indent: 2,
717             is_emitting_map_key: false,
718         }
719     }
720
721     /// Sets the number of spaces to indent for each level.
722     /// This is safe to set during encoding.
723     pub fn set_indent(&mut self, indent: usize) {
724         // self.indent very well could be 0 so we need to use checked division.
725         let level = self.curr_indent.checked_div(self.indent).unwrap_or(0);
726         self.indent = indent;
727         self.curr_indent = level * self.indent;
728     }
729 }
730
731 impl<'a> crate::Encoder for PrettyEncoder<'a> {
732     type Error = EncoderError;
733
734     fn emit_unit(&mut self) -> EncodeResult {
735         if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
736         write!(self.writer, "null")?;
737         Ok(())
738     }
739
740     fn emit_usize(&mut self, v: usize) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
741     fn emit_u128(&mut self, v: u128) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
742     fn emit_u64(&mut self, v: u64) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
743     fn emit_u32(&mut self, v: u32) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
744     fn emit_u16(&mut self, v: u16) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
745     fn emit_u8(&mut self, v: u8) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
746
747     fn emit_isize(&mut self, v: isize) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
748     fn emit_i128(&mut self, v: i128) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
749     fn emit_i64(&mut self, v: i64) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
750     fn emit_i32(&mut self, v: i32) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
751     fn emit_i16(&mut self, v: i16) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
752     fn emit_i8(&mut self, v: i8) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
753
754     fn emit_bool(&mut self, v: bool) -> EncodeResult {
755         if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
756         if v {
757             write!(self.writer, "true")?;
758         } else {
759             write!(self.writer, "false")?;
760         }
761         Ok(())
762     }
763
764     fn emit_f64(&mut self, v: f64) -> EncodeResult {
765         emit_enquoted_if_mapkey!(self, fmt_number_or_null(v))
766     }
767     fn emit_f32(&mut self, v: f32) -> EncodeResult {
768         self.emit_f64(f64::from(v))
769     }
770
771     fn emit_char(&mut self, v: char) -> EncodeResult {
772         escape_char(self.writer, v)
773     }
774     fn emit_str(&mut self, v: &str) -> EncodeResult {
775         escape_str(self.writer, v)
776     }
777
778     fn emit_enum<F>(&mut self, _name: &str, f: F) -> EncodeResult where
779         F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
780     {
781         f(self)
782     }
783
784     fn emit_enum_variant<F>(&mut self,
785                             name: &str,
786                             _id: usize,
787                             cnt: usize,
788                             f: F)
789                             -> EncodeResult where
790         F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
791     {
792         if cnt == 0 {
793             escape_str(self.writer, name)
794         } else {
795             if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
796             writeln!(self.writer, "{{")?;
797             self.curr_indent += self.indent;
798             spaces(self.writer, self.curr_indent)?;
799             write!(self.writer, "\"variant\": ")?;
800             escape_str(self.writer, name)?;
801             writeln!(self.writer, ",")?;
802             spaces(self.writer, self.curr_indent)?;
803             writeln!(self.writer, "\"fields\": [")?;
804             self.curr_indent += self.indent;
805             f(self)?;
806             self.curr_indent -= self.indent;
807             writeln!(self.writer)?;
808             spaces(self.writer, self.curr_indent)?;
809             self.curr_indent -= self.indent;
810             writeln!(self.writer, "]")?;
811             spaces(self.writer, self.curr_indent)?;
812             write!(self.writer, "}}")?;
813             Ok(())
814         }
815     }
816
817     fn emit_enum_variant_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult where
818         F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
819     {
820         if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
821         if idx != 0 {
822             writeln!(self.writer, ",")?;
823         }
824         spaces(self.writer, self.curr_indent)?;
825         f(self)
826     }
827
828     fn emit_enum_struct_variant<F>(&mut self,
829                                    name: &str,
830                                    id: usize,
831                                    cnt: usize,
832                                    f: F) -> EncodeResult where
833         F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
834     {
835         if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
836         self.emit_enum_variant(name, id, cnt, f)
837     }
838
839     fn emit_enum_struct_variant_field<F>(&mut self,
840                                          _: &str,
841                                          idx: usize,
842                                          f: F) -> EncodeResult where
843         F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
844     {
845         if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
846         self.emit_enum_variant_arg(idx, f)
847     }
848
849
850     fn emit_struct<F>(&mut self, _: &str, len: usize, f: F) -> EncodeResult where
851         F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
852     {
853         if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
854         if len == 0 {
855             write!(self.writer, "{{}}")?;
856         } else {
857             write!(self.writer, "{{")?;
858             self.curr_indent += self.indent;
859             f(self)?;
860             self.curr_indent -= self.indent;
861             writeln!(self.writer)?;
862             spaces(self.writer, self.curr_indent)?;
863             write!(self.writer, "}}")?;
864         }
865         Ok(())
866     }
867
868     fn emit_struct_field<F>(&mut self, name: &str, idx: usize, f: F) -> EncodeResult where
869         F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
870     {
871         if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
872         if idx == 0 {
873             writeln!(self.writer)?;
874         } else {
875             writeln!(self.writer, ",")?;
876         }
877         spaces(self.writer, self.curr_indent)?;
878         escape_str(self.writer, name)?;
879         write!(self.writer, ": ")?;
880         f(self)
881     }
882
883     fn emit_tuple<F>(&mut self, len: usize, f: F) -> EncodeResult where
884         F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
885     {
886         if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
887         self.emit_seq(len, f)
888     }
889     fn emit_tuple_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult where
890         F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
891     {
892         if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
893         self.emit_seq_elt(idx, f)
894     }
895
896     fn emit_tuple_struct<F>(&mut self, _: &str, len: usize, f: F) -> EncodeResult where
897         F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
898     {
899         if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
900         self.emit_seq(len, f)
901     }
902     fn emit_tuple_struct_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult where
903         F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
904     {
905         if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
906         self.emit_seq_elt(idx, f)
907     }
908
909     fn emit_option<F>(&mut self, f: F) -> EncodeResult where
910         F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
911     {
912         if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
913         f(self)
914     }
915     fn emit_option_none(&mut self) -> EncodeResult {
916         if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
917         self.emit_unit()
918     }
919     fn emit_option_some<F>(&mut self, f: F) -> EncodeResult where
920         F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
921     {
922         if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
923         f(self)
924     }
925
926     fn emit_seq<F>(&mut self, len: usize, f: F) -> EncodeResult where
927         F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
928     {
929         if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
930         if len == 0 {
931             write!(self.writer, "[]")?;
932         } else {
933             write!(self.writer, "[")?;
934             self.curr_indent += self.indent;
935             f(self)?;
936             self.curr_indent -= self.indent;
937             writeln!(self.writer)?;
938             spaces(self.writer, self.curr_indent)?;
939             write!(self.writer, "]")?;
940         }
941         Ok(())
942     }
943
944     fn emit_seq_elt<F>(&mut self, idx: usize, f: F) -> EncodeResult where
945         F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
946     {
947         if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
948         if idx == 0 {
949             writeln!(self.writer)?;
950         } else {
951             writeln!(self.writer, ",")?;
952         }
953         spaces(self.writer, self.curr_indent)?;
954         f(self)
955     }
956
957     fn emit_map<F>(&mut self, len: usize, f: F) -> EncodeResult where
958         F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
959     {
960         if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
961         if len == 0 {
962             write!(self.writer, "{{}}")?;
963         } else {
964             write!(self.writer, "{{")?;
965             self.curr_indent += self.indent;
966             f(self)?;
967             self.curr_indent -= self.indent;
968             writeln!(self.writer)?;
969             spaces(self.writer, self.curr_indent)?;
970             write!(self.writer, "}}")?;
971         }
972         Ok(())
973     }
974
975     fn emit_map_elt_key<F>(&mut self, idx: usize, f: F) -> EncodeResult where
976         F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
977     {
978         if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
979         if idx == 0 {
980             writeln!(self.writer)?;
981         } else {
982             writeln!(self.writer, ",")?;
983         }
984         spaces(self.writer, self.curr_indent)?;
985         self.is_emitting_map_key = true;
986         f(self)?;
987         self.is_emitting_map_key = false;
988         Ok(())
989     }
990
991     fn emit_map_elt_val<F>(&mut self, _idx: usize, f: F) -> EncodeResult where
992         F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
993     {
994         if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
995         write!(self.writer, ": ")?;
996         f(self)
997     }
998 }
999
1000 impl Encodable for Json {
1001     fn encode<E: crate::Encoder>(&self, e: &mut E) -> Result<(), E::Error> {
1002         match *self {
1003             Json::I64(v) => v.encode(e),
1004             Json::U64(v) => v.encode(e),
1005             Json::F64(v) => v.encode(e),
1006             Json::String(ref v) => v.encode(e),
1007             Json::Boolean(v) => v.encode(e),
1008             Json::Array(ref v) => v.encode(e),
1009             Json::Object(ref v) => v.encode(e),
1010             Json::Null => e.emit_unit(),
1011         }
1012     }
1013 }
1014
1015 /// Creates an `AsJson` wrapper which can be used to print a value as JSON
1016 /// on-the-fly via `write!`
1017 pub fn as_json<T>(t: &T) -> AsJson<'_, T> {
1018     AsJson { inner: t }
1019 }
1020
1021 /// Creates an `AsPrettyJson` wrapper which can be used to print a value as JSON
1022 /// on-the-fly via `write!`
1023 pub fn as_pretty_json<T>(t: &T) -> AsPrettyJson<'_, T> {
1024     AsPrettyJson { inner: t, indent: None }
1025 }
1026
1027 impl Json {
1028     /// Borrow this json object as a pretty object to generate a pretty
1029     /// representation for it via `Display`.
1030     pub fn pretty(&self) -> PrettyJson<'_> {
1031         PrettyJson { inner: self }
1032     }
1033
1034      /// If the Json value is an Object, returns the value associated with the provided key.
1035     /// Otherwise, returns None.
1036     pub fn find(&self, key: &str) -> Option<&Json> {
1037         match *self {
1038             Json::Object(ref map) => map.get(key),
1039             _ => None
1040         }
1041     }
1042
1043     /// Attempts to get a nested Json Object for each key in `keys`.
1044     /// If any key is found not to exist, `find_path` will return `None`.
1045     /// Otherwise, it will return the Json value associated with the final key.
1046     pub fn find_path<'a>(&'a self, keys: &[&str]) -> Option<&'a Json>{
1047         let mut target = self;
1048         for key in keys {
1049             target = target.find(*key)?;
1050         }
1051         Some(target)
1052     }
1053
1054     /// If the Json value is an Object, performs a depth-first search until
1055     /// a value associated with the provided key is found. If no value is found
1056     /// or the Json value is not an Object, returns `None`.
1057     pub fn search(&self, key: &str) -> Option<&Json> {
1058         match self {
1059             &Json::Object(ref map) => {
1060                 match map.get(key) {
1061                     Some(json_value) => Some(json_value),
1062                     None => {
1063                         for (_, v) in map {
1064                             match v.search(key) {
1065                                 x if x.is_some() => return x,
1066                                 _ => ()
1067                             }
1068                         }
1069                         None
1070                     }
1071                 }
1072             },
1073             _ => None
1074         }
1075     }
1076
1077     /// Returns `true` if the Json value is an `Object`.
1078     pub fn is_object(&self) -> bool {
1079         self.as_object().is_some()
1080     }
1081
1082     /// If the Json value is an `Object`, returns the associated `BTreeMap`;
1083     /// returns `None` otherwise.
1084     pub fn as_object(&self) -> Option<&Object> {
1085         match *self {
1086             Json::Object(ref map) => Some(map),
1087             _ => None
1088         }
1089     }
1090
1091     /// Returns `true` if the Json value is an `Array`.
1092     pub fn is_array(&self) -> bool {
1093         self.as_array().is_some()
1094     }
1095
1096     /// If the Json value is an `Array`, returns the associated vector;
1097     /// returns `None` otherwise.
1098     pub fn as_array(&self) -> Option<&Array> {
1099         match *self {
1100             Json::Array(ref array) => Some(&*array),
1101             _ => None
1102         }
1103     }
1104
1105     /// Returns `true` if the Json value is a `String`.
1106     pub fn is_string(&self) -> bool {
1107         self.as_string().is_some()
1108     }
1109
1110     /// If the Json value is a `String`, returns the associated `str`;
1111     /// returns `None` otherwise.
1112     pub fn as_string(&self) -> Option<&str> {
1113         match *self {
1114             Json::String(ref s) => Some(&s[..]),
1115             _ => None
1116         }
1117     }
1118
1119     /// Returns `true` if the Json value is a `Number`.
1120     pub fn is_number(&self) -> bool {
1121         match *self {
1122             Json::I64(_) | Json::U64(_) | Json::F64(_) => true,
1123             _ => false,
1124         }
1125     }
1126
1127     /// Returns `true` if the Json value is a `i64`.
1128     pub fn is_i64(&self) -> bool {
1129         match *self {
1130             Json::I64(_) => true,
1131             _ => false,
1132         }
1133     }
1134
1135     /// Returns `true` if the Json value is a `u64`.
1136     pub fn is_u64(&self) -> bool {
1137         match *self {
1138             Json::U64(_) => true,
1139             _ => false,
1140         }
1141     }
1142
1143     /// Returns `true` if the Json value is a `f64`.
1144     pub fn is_f64(&self) -> bool {
1145         match *self {
1146             Json::F64(_) => true,
1147             _ => false,
1148         }
1149     }
1150
1151     /// If the Json value is a number, returns or cast it to a `i64`;
1152     /// returns `None` otherwise.
1153     pub fn as_i64(&self) -> Option<i64> {
1154         match *self {
1155             Json::I64(n) => Some(n),
1156             Json::U64(n) => Some(n as i64),
1157             _ => None
1158         }
1159     }
1160
1161     /// If the Json value is a number, returns or cast it to a `u64`;
1162     /// returns `None` otherwise.
1163     pub fn as_u64(&self) -> Option<u64> {
1164         match *self {
1165             Json::I64(n) => Some(n as u64),
1166             Json::U64(n) => Some(n),
1167             _ => None
1168         }
1169     }
1170
1171     /// If the Json value is a number, returns or cast it to a `f64`;
1172     /// returns `None` otherwise.
1173     pub fn as_f64(&self) -> Option<f64> {
1174         match *self {
1175             Json::I64(n) => Some(n as f64),
1176             Json::U64(n) => Some(n as f64),
1177             Json::F64(n) => Some(n),
1178             _ => None
1179         }
1180     }
1181
1182     /// Returns `true` if the Json value is a `Boolean`.
1183     pub fn is_boolean(&self) -> bool {
1184         self.as_boolean().is_some()
1185     }
1186
1187     /// If the Json value is a `Boolean`, returns the associated `bool`;
1188     /// returns `None` otherwise.
1189     pub fn as_boolean(&self) -> Option<bool> {
1190         match *self {
1191             Json::Boolean(b) => Some(b),
1192             _ => None
1193         }
1194     }
1195
1196     /// Returns `true` if the Json value is a `Null`.
1197     pub fn is_null(&self) -> bool {
1198         self.as_null().is_some()
1199     }
1200
1201     /// If the Json value is a `Null`, returns `()`;
1202     /// returns `None` otherwise.
1203     pub fn as_null(&self) -> Option<()> {
1204         match *self {
1205             Json::Null => Some(()),
1206             _ => None
1207         }
1208     }
1209 }
1210
1211 impl<'a> Index<&'a str>  for Json {
1212     type Output = Json;
1213
1214     fn index(&self, idx: &'a str) -> &Json {
1215         self.find(idx).unwrap()
1216     }
1217 }
1218
1219 impl Index<usize> for Json {
1220     type Output = Json;
1221
1222     fn index(&self, idx: usize) -> &Json {
1223         match *self {
1224             Json::Array(ref v) => &v[idx],
1225             _ => panic!("can only index Json with usize if it is an array")
1226         }
1227     }
1228 }
1229
1230 /// The output of the streaming parser.
1231 #[derive(PartialEq, Clone, Debug)]
1232 pub enum JsonEvent {
1233     ObjectStart,
1234     ObjectEnd,
1235     ArrayStart,
1236     ArrayEnd,
1237     BooleanValue(bool),
1238     I64Value(i64),
1239     U64Value(u64),
1240     F64Value(f64),
1241     StringValue(string::String),
1242     NullValue,
1243     Error(ParserError),
1244 }
1245
1246 #[derive(PartialEq, Debug)]
1247 enum ParserState {
1248     // Parse a value in an array, true means first element.
1249     ParseArray(bool),
1250     // Parse ',' or ']' after an element in an array.
1251     ParseArrayComma,
1252     // Parse a key:value in an object, true means first element.
1253     ParseObject(bool),
1254     // Parse ',' or ']' after an element in an object.
1255     ParseObjectComma,
1256     // Initial state.
1257     ParseStart,
1258     // Expecting the stream to end.
1259     ParseBeforeFinish,
1260     // Parsing can't continue.
1261     ParseFinished,
1262 }
1263
1264 /// A Stack represents the current position of the parser in the logical
1265 /// structure of the JSON stream.
1266 /// For example foo.bar[3].x
1267 pub struct Stack {
1268     stack: Vec<InternalStackElement>,
1269     str_buffer: Vec<u8>,
1270 }
1271
1272 /// StackElements compose a Stack.
1273 /// For example, StackElement::Key("foo"), StackElement::Key("bar"),
1274 /// StackElement::Index(3) and StackElement::Key("x") are the
1275 /// StackElements compositing the stack that represents foo.bar[3].x
1276 #[derive(PartialEq, Clone, Debug)]
1277 pub enum StackElement<'l> {
1278     Index(u32),
1279     Key(&'l str),
1280 }
1281
1282 // Internally, Key elements are stored as indices in a buffer to avoid
1283 // allocating a string for every member of an object.
1284 #[derive(PartialEq, Clone, Debug)]
1285 enum InternalStackElement {
1286     InternalIndex(u32),
1287     InternalKey(u16, u16), // start, size
1288 }
1289
1290 impl Stack {
1291     pub fn new() -> Stack {
1292         Stack { stack: Vec::new(), str_buffer: Vec::new() }
1293     }
1294
1295     /// Returns The number of elements in the Stack.
1296     pub fn len(&self) -> usize { self.stack.len() }
1297
1298     /// Returns `true` if the stack is empty.
1299     pub fn is_empty(&self) -> bool { self.stack.is_empty() }
1300
1301     /// Provides access to the StackElement at a given index.
1302     /// lower indices are at the bottom of the stack while higher indices are
1303     /// at the top.
1304     pub fn get(&self, idx: usize) -> StackElement<'_> {
1305         match self.stack[idx] {
1306             InternalIndex(i) => StackElement::Index(i),
1307             InternalKey(start, size) => {
1308                 StackElement::Key(str::from_utf8(
1309                     &self.str_buffer[start as usize .. start as usize + size as usize])
1310                         .unwrap())
1311             }
1312         }
1313     }
1314
1315     /// Compares this stack with an array of StackElement<'_>s.
1316     pub fn is_equal_to(&self, rhs: &[StackElement<'_>]) -> bool {
1317         if self.stack.len() != rhs.len() { return false; }
1318         for (i, r) in rhs.iter().enumerate() {
1319             if self.get(i) != *r { return false; }
1320         }
1321         true
1322     }
1323
1324     /// Returns `true` if the bottom-most elements of this stack are the same as
1325     /// the ones passed as parameter.
1326     pub fn starts_with(&self, rhs: &[StackElement<'_>]) -> bool {
1327         if self.stack.len() < rhs.len() { return false; }
1328         for (i, r) in rhs.iter().enumerate() {
1329             if self.get(i) != *r { return false; }
1330         }
1331         true
1332     }
1333
1334     /// Returns `true` if the top-most elements of this stack are the same as
1335     /// the ones passed as parameter.
1336     pub fn ends_with(&self, rhs: &[StackElement<'_>]) -> bool {
1337         if self.stack.len() < rhs.len() { return false; }
1338         let offset = self.stack.len() - rhs.len();
1339         for (i, r) in rhs.iter().enumerate() {
1340             if self.get(i + offset) != *r { return false; }
1341         }
1342         true
1343     }
1344
1345     /// Returns the top-most element (if any).
1346     pub fn top(&self) -> Option<StackElement<'_>> {
1347         match self.stack.last() {
1348             None => None,
1349             Some(&InternalIndex(i)) => Some(StackElement::Index(i)),
1350             Some(&InternalKey(start, size)) => {
1351                 Some(StackElement::Key(str::from_utf8(
1352                     &self.str_buffer[start as usize .. (start+size) as usize]
1353                 ).unwrap()))
1354             }
1355         }
1356     }
1357
1358     // Used by Parser to insert StackElement::Key elements at the top of the stack.
1359     fn push_key(&mut self, key: string::String) {
1360         self.stack.push(InternalKey(self.str_buffer.len() as u16, key.len() as u16));
1361         self.str_buffer.extend(key.as_bytes());
1362     }
1363
1364     // Used by Parser to insert StackElement::Index elements at the top of the stack.
1365     fn push_index(&mut self, index: u32) {
1366         self.stack.push(InternalIndex(index));
1367     }
1368
1369     // Used by Parser to remove the top-most element of the stack.
1370     fn pop(&mut self) {
1371         assert!(!self.is_empty());
1372         match *self.stack.last().unwrap() {
1373             InternalKey(_, sz) => {
1374                 let new_size = self.str_buffer.len() - sz as usize;
1375                 self.str_buffer.truncate(new_size);
1376             }
1377             InternalIndex(_) => {}
1378         }
1379         self.stack.pop();
1380     }
1381
1382     // Used by Parser to test whether the top-most element is an index.
1383     fn last_is_index(&self) -> bool {
1384         match self.stack.last() {
1385             Some(InternalIndex(_)) => true,
1386             _ => false,
1387         }
1388     }
1389
1390     // Used by Parser to increment the index of the top-most element.
1391     fn bump_index(&mut self) {
1392         let len = self.stack.len();
1393         let idx = match *self.stack.last().unwrap() {
1394             InternalIndex(i) => { i + 1 }
1395             _ => { panic!(); }
1396         };
1397         self.stack[len - 1] = InternalIndex(idx);
1398     }
1399 }
1400
1401 /// A streaming JSON parser implemented as an iterator of JsonEvent, consuming
1402 /// an iterator of char.
1403 pub struct Parser<T> {
1404     rdr: T,
1405     ch: Option<char>,
1406     line: usize,
1407     col: usize,
1408     // We maintain a stack representing where we are in the logical structure
1409     // of the JSON stream.
1410     stack: Stack,
1411     // A state machine is kept to make it possible to interrupt and resume parsing.
1412     state: ParserState,
1413 }
1414
1415 impl<T: Iterator<Item=char>> Iterator for Parser<T> {
1416     type Item = JsonEvent;
1417
1418     fn next(&mut self) -> Option<JsonEvent> {
1419         if self.state == ParseFinished {
1420             return None;
1421         }
1422
1423         if self.state == ParseBeforeFinish {
1424             self.parse_whitespace();
1425             // Make sure there is no trailing characters.
1426             if self.eof() {
1427                 self.state = ParseFinished;
1428                 return None;
1429             } else {
1430                 return Some(self.error_event(TrailingCharacters));
1431             }
1432         }
1433
1434         Some(self.parse())
1435     }
1436 }
1437
1438 impl<T: Iterator<Item=char>> Parser<T> {
1439     /// Creates the JSON parser.
1440     pub fn new(rdr: T) -> Parser<T> {
1441         let mut p = Parser {
1442             rdr,
1443             ch: Some('\x00'),
1444             line: 1,
1445             col: 0,
1446             stack: Stack::new(),
1447             state: ParseStart,
1448         };
1449         p.bump();
1450         p
1451     }
1452
1453     /// Provides access to the current position in the logical structure of the
1454     /// JSON stream.
1455     pub fn stack(&self) -> &Stack {
1456         &self.stack
1457     }
1458
1459     fn eof(&self) -> bool { self.ch.is_none() }
1460     fn ch_or_null(&self) -> char { self.ch.unwrap_or('\x00') }
1461     fn bump(&mut self) {
1462         self.ch = self.rdr.next();
1463
1464         if self.ch_is('\n') {
1465             self.line += 1;
1466             self.col = 1;
1467         } else {
1468             self.col += 1;
1469         }
1470     }
1471
1472     fn next_char(&mut self) -> Option<char> {
1473         self.bump();
1474         self.ch
1475     }
1476     fn ch_is(&self, c: char) -> bool {
1477         self.ch == Some(c)
1478     }
1479
1480     fn error<U>(&self, reason: ErrorCode) -> Result<U, ParserError> {
1481         Err(SyntaxError(reason, self.line, self.col))
1482     }
1483
1484     fn parse_whitespace(&mut self) {
1485         while self.ch_is(' ') ||
1486               self.ch_is('\n') ||
1487               self.ch_is('\t') ||
1488               self.ch_is('\r') { self.bump(); }
1489     }
1490
1491     fn parse_number(&mut self) -> JsonEvent {
1492         let mut neg = false;
1493
1494         if self.ch_is('-') {
1495             self.bump();
1496             neg = true;
1497         }
1498
1499         let res = match self.parse_u64() {
1500             Ok(res) => res,
1501             Err(e) => { return Error(e); }
1502         };
1503
1504         if self.ch_is('.') || self.ch_is('e') || self.ch_is('E') {
1505             let mut res = res as f64;
1506
1507             if self.ch_is('.') {
1508                 res = match self.parse_decimal(res) {
1509                     Ok(res) => res,
1510                     Err(e) => { return Error(e); }
1511                 };
1512             }
1513
1514             if self.ch_is('e') || self.ch_is('E') {
1515                 res = match self.parse_exponent(res) {
1516                     Ok(res) => res,
1517                     Err(e) => { return Error(e); }
1518                 };
1519             }
1520
1521             if neg {
1522                 res *= -1.0;
1523             }
1524
1525             F64Value(res)
1526         } else if neg {
1527             let res = (res as i64).wrapping_neg();
1528
1529             // Make sure we didn't underflow.
1530             if res > 0 {
1531                 Error(SyntaxError(InvalidNumber, self.line, self.col))
1532             } else {
1533                 I64Value(res)
1534             }
1535         } else {
1536             U64Value(res)
1537         }
1538     }
1539
1540     fn parse_u64(&mut self) -> Result<u64, ParserError> {
1541         let mut accum = 0u64;
1542         let last_accum = 0; // necessary to detect overflow.
1543
1544         match self.ch_or_null() {
1545             '0' => {
1546                 self.bump();
1547
1548                 // A leading '0' must be the only digit before the decimal point.
1549                 if let '0' ..= '9' = self.ch_or_null() {
1550                     return self.error(InvalidNumber)
1551                 }
1552             },
1553             '1' ..= '9' => {
1554                 while !self.eof() {
1555                     match self.ch_or_null() {
1556                         c @ '0' ..= '9' => {
1557                             accum = accum.wrapping_mul(10);
1558                             accum = accum.wrapping_add((c as u64) - ('0' as u64));
1559
1560                             // Detect overflow by comparing to the last value.
1561                             if accum <= last_accum { return self.error(InvalidNumber); }
1562
1563                             self.bump();
1564                         }
1565                         _ => break,
1566                     }
1567                 }
1568             }
1569             _ => return self.error(InvalidNumber),
1570         }
1571
1572         Ok(accum)
1573     }
1574
1575     fn parse_decimal(&mut self, mut res: f64) -> Result<f64, ParserError> {
1576         self.bump();
1577
1578         // Make sure a digit follows the decimal place.
1579         match self.ch_or_null() {
1580             '0' ..= '9' => (),
1581              _ => return self.error(InvalidNumber)
1582         }
1583
1584         let mut dec = 1.0;
1585         while !self.eof() {
1586             match self.ch_or_null() {
1587                 c @ '0' ..= '9' => {
1588                     dec /= 10.0;
1589                     res += (((c as isize) - ('0' as isize)) as f64) * dec;
1590                     self.bump();
1591                 }
1592                 _ => break,
1593             }
1594         }
1595
1596         Ok(res)
1597     }
1598
1599     fn parse_exponent(&mut self, mut res: f64) -> Result<f64, ParserError> {
1600         self.bump();
1601
1602         let mut exp = 0;
1603         let mut neg_exp = false;
1604
1605         if self.ch_is('+') {
1606             self.bump();
1607         } else if self.ch_is('-') {
1608             self.bump();
1609             neg_exp = true;
1610         }
1611
1612         // Make sure a digit follows the exponent place.
1613         match self.ch_or_null() {
1614             '0' ..= '9' => (),
1615             _ => return self.error(InvalidNumber)
1616         }
1617         while !self.eof() {
1618             match self.ch_or_null() {
1619                 c @ '0' ..= '9' => {
1620                     exp *= 10;
1621                     exp += (c as usize) - ('0' as usize);
1622
1623                     self.bump();
1624                 }
1625                 _ => break
1626             }
1627         }
1628
1629         let exp = 10_f64.powi(exp as i32);
1630         if neg_exp {
1631             res /= exp;
1632         } else {
1633             res *= exp;
1634         }
1635
1636         Ok(res)
1637     }
1638
1639     fn decode_hex_escape(&mut self) -> Result<u16, ParserError> {
1640         let mut i = 0;
1641         let mut n = 0;
1642         while i < 4 && !self.eof() {
1643             self.bump();
1644             n = match self.ch_or_null() {
1645                 c @ '0' ..= '9' => n * 16 + ((c as u16) - ('0' as u16)),
1646                 'a' | 'A' => n * 16 + 10,
1647                 'b' | 'B' => n * 16 + 11,
1648                 'c' | 'C' => n * 16 + 12,
1649                 'd' | 'D' => n * 16 + 13,
1650                 'e' | 'E' => n * 16 + 14,
1651                 'f' | 'F' => n * 16 + 15,
1652                 _ => return self.error(InvalidEscape)
1653             };
1654
1655             i += 1;
1656         }
1657
1658         // Error out if we didn't parse 4 digits.
1659         if i != 4 {
1660             return self.error(InvalidEscape);
1661         }
1662
1663         Ok(n)
1664     }
1665
1666     fn parse_str(&mut self) -> Result<string::String, ParserError> {
1667         let mut escape = false;
1668         let mut res = string::String::new();
1669
1670         loop {
1671             self.bump();
1672             if self.eof() {
1673                 return self.error(EOFWhileParsingString);
1674             }
1675
1676             if escape {
1677                 match self.ch_or_null() {
1678                     '"' => res.push('"'),
1679                     '\\' => res.push('\\'),
1680                     '/' => res.push('/'),
1681                     'b' => res.push('\x08'),
1682                     'f' => res.push('\x0c'),
1683                     'n' => res.push('\n'),
1684                     'r' => res.push('\r'),
1685                     't' => res.push('\t'),
1686                     'u' => match self.decode_hex_escape()? {
1687                         0xDC00 ..= 0xDFFF => {
1688                             return self.error(LoneLeadingSurrogateInHexEscape)
1689                         }
1690
1691                         // Non-BMP characters are encoded as a sequence of
1692                         // two hex escapes, representing UTF-16 surrogates.
1693                         n1 @ 0xD800 ..= 0xDBFF => {
1694                             match (self.next_char(), self.next_char()) {
1695                                 (Some('\\'), Some('u')) => (),
1696                                 _ => return self.error(UnexpectedEndOfHexEscape),
1697                             }
1698
1699                             let n2 = self.decode_hex_escape()?;
1700                             if n2 < 0xDC00 || n2 > 0xDFFF {
1701                                 return self.error(LoneLeadingSurrogateInHexEscape)
1702                             }
1703                             let c = (u32::from(n1 - 0xD800) << 10 |
1704                                      u32::from(n2 - 0xDC00)) + 0x1_0000;
1705                             res.push(char::from_u32(c).unwrap());
1706                         }
1707
1708                         n => match char::from_u32(u32::from(n)) {
1709                             Some(c) => res.push(c),
1710                             None => return self.error(InvalidUnicodeCodePoint),
1711                         },
1712                     },
1713                     _ => return self.error(InvalidEscape),
1714                 }
1715                 escape = false;
1716             } else if self.ch_is('\\') {
1717                 escape = true;
1718             } else {
1719                 match self.ch {
1720                     Some('"') => {
1721                         self.bump();
1722                         return Ok(res);
1723                     },
1724                     Some(c) => res.push(c),
1725                     None => unreachable!()
1726                 }
1727             }
1728         }
1729     }
1730
1731     // Invoked at each iteration, consumes the stream until it has enough
1732     // information to return a JsonEvent.
1733     // Manages an internal state so that parsing can be interrupted and resumed.
1734     // Also keeps track of the position in the logical structure of the json
1735     // stream isize the form of a stack that can be queried by the user using the
1736     // stack() method.
1737     fn parse(&mut self) -> JsonEvent {
1738         loop {
1739             // The only paths where the loop can spin a new iteration
1740             // are in the cases ParseArrayComma and ParseObjectComma if ','
1741             // is parsed. In these cases the state is set to (respectively)
1742             // ParseArray(false) and ParseObject(false), which always return,
1743             // so there is no risk of getting stuck in an infinite loop.
1744             // All other paths return before the end of the loop's iteration.
1745             self.parse_whitespace();
1746
1747             match self.state {
1748                 ParseStart => {
1749                     return self.parse_start();
1750                 }
1751                 ParseArray(first) => {
1752                     return self.parse_array(first);
1753                 }
1754                 ParseArrayComma => {
1755                     if let Some(evt) = self.parse_array_comma_or_end() {
1756                         return evt;
1757                     }
1758                 }
1759                 ParseObject(first) => {
1760                     return self.parse_object(first);
1761                 }
1762                 ParseObjectComma => {
1763                     self.stack.pop();
1764                     if self.ch_is(',') {
1765                         self.state = ParseObject(false);
1766                         self.bump();
1767                     } else {
1768                         return self.parse_object_end();
1769                     }
1770                 }
1771                 _ => {
1772                     return self.error_event(InvalidSyntax);
1773                 }
1774             }
1775         }
1776     }
1777
1778     fn parse_start(&mut self) -> JsonEvent {
1779         let val = self.parse_value();
1780         self.state = match val {
1781             Error(_) => ParseFinished,
1782             ArrayStart => ParseArray(true),
1783             ObjectStart => ParseObject(true),
1784             _ => ParseBeforeFinish,
1785         };
1786         val
1787     }
1788
1789     fn parse_array(&mut self, first: bool) -> JsonEvent {
1790         if self.ch_is(']') {
1791             if !first {
1792                 self.error_event(InvalidSyntax)
1793             } else {
1794                 self.state = if self.stack.is_empty() {
1795                     ParseBeforeFinish
1796                 } else if self.stack.last_is_index() {
1797                     ParseArrayComma
1798                 } else {
1799                     ParseObjectComma
1800                 };
1801                 self.bump();
1802                 ArrayEnd
1803             }
1804         } else {
1805             if first {
1806                 self.stack.push_index(0);
1807             }
1808             let val = self.parse_value();
1809             self.state = match val {
1810                 Error(_) => ParseFinished,
1811                 ArrayStart => ParseArray(true),
1812                 ObjectStart => ParseObject(true),
1813                 _ => ParseArrayComma,
1814             };
1815             val
1816         }
1817     }
1818
1819     fn parse_array_comma_or_end(&mut self) -> Option<JsonEvent> {
1820         if self.ch_is(',') {
1821             self.stack.bump_index();
1822             self.state = ParseArray(false);
1823             self.bump();
1824             None
1825         } else if self.ch_is(']') {
1826             self.stack.pop();
1827             self.state = if self.stack.is_empty() {
1828                 ParseBeforeFinish
1829             } else if self.stack.last_is_index() {
1830                 ParseArrayComma
1831             } else {
1832                 ParseObjectComma
1833             };
1834             self.bump();
1835             Some(ArrayEnd)
1836         } else if self.eof() {
1837             Some(self.error_event(EOFWhileParsingArray))
1838         } else {
1839             Some(self.error_event(InvalidSyntax))
1840         }
1841     }
1842
1843     fn parse_object(&mut self, first: bool) -> JsonEvent {
1844         if self.ch_is('}') {
1845             if !first {
1846                 if self.stack.is_empty() {
1847                     return self.error_event(TrailingComma);
1848                 } else {
1849                     self.stack.pop();
1850                 }
1851             }
1852             self.state = if self.stack.is_empty() {
1853                 ParseBeforeFinish
1854             } else if self.stack.last_is_index() {
1855                 ParseArrayComma
1856             } else {
1857                 ParseObjectComma
1858             };
1859             self.bump();
1860             return ObjectEnd;
1861         }
1862         if self.eof() {
1863             return self.error_event(EOFWhileParsingObject);
1864         }
1865         if !self.ch_is('"') {
1866             return self.error_event(KeyMustBeAString);
1867         }
1868         let s = match self.parse_str() {
1869             Ok(s) => s,
1870             Err(e) => {
1871                 self.state = ParseFinished;
1872                 return Error(e);
1873             }
1874         };
1875         self.parse_whitespace();
1876         if self.eof() {
1877             return self.error_event(EOFWhileParsingObject);
1878         } else if self.ch_or_null() != ':' {
1879             return self.error_event(ExpectedColon);
1880         }
1881         self.stack.push_key(s);
1882         self.bump();
1883         self.parse_whitespace();
1884
1885         let val = self.parse_value();
1886
1887         self.state = match val {
1888             Error(_) => ParseFinished,
1889             ArrayStart => ParseArray(true),
1890             ObjectStart => ParseObject(true),
1891             _ => ParseObjectComma,
1892         };
1893         val
1894     }
1895
1896     fn parse_object_end(&mut self) -> JsonEvent {
1897         if self.ch_is('}') {
1898             self.state = if self.stack.is_empty() {
1899                 ParseBeforeFinish
1900             } else if self.stack.last_is_index() {
1901                 ParseArrayComma
1902             } else {
1903                 ParseObjectComma
1904             };
1905             self.bump();
1906             ObjectEnd
1907         } else if self.eof() {
1908             self.error_event(EOFWhileParsingObject)
1909         } else {
1910             self.error_event(InvalidSyntax)
1911         }
1912     }
1913
1914     fn parse_value(&mut self) -> JsonEvent {
1915         if self.eof() { return self.error_event(EOFWhileParsingValue); }
1916         match self.ch_or_null() {
1917             'n' => { self.parse_ident("ull", NullValue) }
1918             't' => { self.parse_ident("rue", BooleanValue(true)) }
1919             'f' => { self.parse_ident("alse", BooleanValue(false)) }
1920             '0' ..= '9' | '-' => self.parse_number(),
1921             '"' => match self.parse_str() {
1922                 Ok(s) => StringValue(s),
1923                 Err(e) => Error(e),
1924             },
1925             '[' => {
1926                 self.bump();
1927                 ArrayStart
1928             }
1929             '{' => {
1930                 self.bump();
1931                 ObjectStart
1932             }
1933             _ => { self.error_event(InvalidSyntax) }
1934         }
1935     }
1936
1937     fn parse_ident(&mut self, ident: &str, value: JsonEvent) -> JsonEvent {
1938         if ident.chars().all(|c| Some(c) == self.next_char()) {
1939             self.bump();
1940             value
1941         } else {
1942             Error(SyntaxError(InvalidSyntax, self.line, self.col))
1943         }
1944     }
1945
1946     fn error_event(&mut self, reason: ErrorCode) -> JsonEvent {
1947         self.state = ParseFinished;
1948         Error(SyntaxError(reason, self.line, self.col))
1949     }
1950 }
1951
1952 /// A Builder consumes a json::Parser to create a generic Json structure.
1953 pub struct Builder<T> {
1954     parser: Parser<T>,
1955     token: Option<JsonEvent>,
1956 }
1957
1958 impl<T: Iterator<Item=char>> Builder<T> {
1959     /// Creates a JSON Builder.
1960     pub fn new(src: T) -> Builder<T> {
1961         Builder { parser: Parser::new(src), token: None, }
1962     }
1963
1964     // Decode a Json value from a Parser.
1965     pub fn build(&mut self) -> Result<Json, BuilderError> {
1966         self.bump();
1967         let result = self.build_value();
1968         self.bump();
1969         match self.token {
1970             None => {}
1971             Some(Error(ref e)) => { return Err(e.clone()); }
1972             ref tok => { panic!("unexpected token {:?}", tok.clone()); }
1973         }
1974         result
1975     }
1976
1977     fn bump(&mut self) {
1978         self.token = self.parser.next();
1979     }
1980
1981     fn build_value(&mut self) -> Result<Json, BuilderError> {
1982         match self.token {
1983             Some(NullValue) => Ok(Json::Null),
1984             Some(I64Value(n)) => Ok(Json::I64(n)),
1985             Some(U64Value(n)) => Ok(Json::U64(n)),
1986             Some(F64Value(n)) => Ok(Json::F64(n)),
1987             Some(BooleanValue(b)) => Ok(Json::Boolean(b)),
1988             Some(StringValue(ref mut s)) => {
1989                 let mut temp = string::String::new();
1990                 swap(s, &mut temp);
1991                 Ok(Json::String(temp))
1992             }
1993             Some(Error(ref e)) => Err(e.clone()),
1994             Some(ArrayStart) => self.build_array(),
1995             Some(ObjectStart) => self.build_object(),
1996             Some(ObjectEnd) => self.parser.error(InvalidSyntax),
1997             Some(ArrayEnd) => self.parser.error(InvalidSyntax),
1998             None => self.parser.error(EOFWhileParsingValue),
1999         }
2000     }
2001
2002     fn build_array(&mut self) -> Result<Json, BuilderError> {
2003         self.bump();
2004         let mut values = Vec::new();
2005
2006         loop {
2007             if self.token == Some(ArrayEnd) {
2008                 return Ok(Json::Array(values.into_iter().collect()));
2009             }
2010             match self.build_value() {
2011                 Ok(v) => values.push(v),
2012                 Err(e) => { return Err(e) }
2013             }
2014             self.bump();
2015         }
2016     }
2017
2018     fn build_object(&mut self) -> Result<Json, BuilderError> {
2019         self.bump();
2020
2021         let mut values = BTreeMap::new();
2022
2023         loop {
2024             match self.token {
2025                 Some(ObjectEnd) => { return Ok(Json::Object(values)); }
2026                 Some(Error(ref e)) => { return Err(e.clone()); }
2027                 None => { break; }
2028                 _ => {}
2029             }
2030             let key = match self.parser.stack().top() {
2031                 Some(StackElement::Key(k)) => { k.to_owned() }
2032                 _ => { panic!("invalid state"); }
2033             };
2034             match self.build_value() {
2035                 Ok(value) => { values.insert(key, value); }
2036                 Err(e) => { return Err(e); }
2037             }
2038             self.bump();
2039         }
2040         self.parser.error(EOFWhileParsingObject)
2041     }
2042 }
2043
2044 /// Decodes a json value from an `&mut io::Read`
2045 pub fn from_reader(rdr: &mut dyn Read) -> Result<Json, BuilderError> {
2046     let mut contents = Vec::new();
2047     match rdr.read_to_end(&mut contents) {
2048         Ok(c)  => c,
2049         Err(e) => return Err(io_error_to_error(e))
2050     };
2051     let s = match str::from_utf8(&contents).ok() {
2052         Some(s) => s,
2053         _       => return Err(SyntaxError(NotUtf8, 0, 0))
2054     };
2055     let mut builder = Builder::new(s.chars());
2056     builder.build()
2057 }
2058
2059 /// Decodes a json value from a string
2060 pub fn from_str(s: &str) -> Result<Json, BuilderError> {
2061     let mut builder = Builder::new(s.chars());
2062     builder.build()
2063 }
2064
2065 /// A structure to decode JSON to values in rust.
2066 pub struct Decoder {
2067     stack: Vec<Json>,
2068 }
2069
2070 impl Decoder {
2071     /// Creates a new decoder instance for decoding the specified JSON value.
2072     pub fn new(json: Json) -> Decoder {
2073         Decoder { stack: vec![json] }
2074     }
2075
2076     fn pop(&mut self) -> Json {
2077         self.stack.pop().unwrap()
2078     }
2079 }
2080
2081 macro_rules! expect {
2082     ($e:expr, Null) => ({
2083         match $e {
2084             Json::Null => Ok(()),
2085             other => Err(ExpectedError("Null".to_owned(),
2086                                        other.to_string()))
2087         }
2088     });
2089     ($e:expr, $t:ident) => ({
2090         match $e {
2091             Json::$t(v) => Ok(v),
2092             other => {
2093                 Err(ExpectedError(stringify!($t).to_owned(),
2094                                   other.to_string()))
2095             }
2096         }
2097     })
2098 }
2099
2100 macro_rules! read_primitive {
2101     ($name:ident, $ty:ty) => {
2102         fn $name(&mut self) -> DecodeResult<$ty> {
2103             match self.pop() {
2104                 Json::I64(f) => Ok(f as $ty),
2105                 Json::U64(f) => Ok(f as $ty),
2106                 Json::F64(f) => Err(ExpectedError("Integer".to_owned(), f.to_string())),
2107                 // re: #12967.. a type w/ numeric keys (ie HashMap<usize, V> etc)
2108                 // is going to have a string here, as per JSON spec.
2109                 Json::String(s) => match s.parse().ok() {
2110                     Some(f) => Ok(f),
2111                     None => Err(ExpectedError("Number".to_owned(), s)),
2112                 },
2113                 value => Err(ExpectedError("Number".to_owned(), value.to_string())),
2114             }
2115         }
2116     }
2117 }
2118
2119 impl crate::Decoder for Decoder {
2120     type Error = DecoderError;
2121
2122     fn read_nil(&mut self) -> DecodeResult<()> {
2123         expect!(self.pop(), Null)
2124     }
2125
2126     read_primitive! { read_usize, usize }
2127     read_primitive! { read_u8, u8 }
2128     read_primitive! { read_u16, u16 }
2129     read_primitive! { read_u32, u32 }
2130     read_primitive! { read_u64, u64 }
2131     read_primitive! { read_u128, u128 }
2132     read_primitive! { read_isize, isize }
2133     read_primitive! { read_i8, i8 }
2134     read_primitive! { read_i16, i16 }
2135     read_primitive! { read_i32, i32 }
2136     read_primitive! { read_i64, i64 }
2137     read_primitive! { read_i128, i128 }
2138
2139     fn read_f32(&mut self) -> DecodeResult<f32> { self.read_f64().map(|x| x as f32) }
2140
2141     fn read_f64(&mut self) -> DecodeResult<f64> {
2142         match self.pop() {
2143             Json::I64(f) => Ok(f as f64),
2144             Json::U64(f) => Ok(f as f64),
2145             Json::F64(f) => Ok(f),
2146             Json::String(s) => {
2147                 // re: #12967.. a type w/ numeric keys (ie HashMap<usize, V> etc)
2148                 // is going to have a string here, as per JSON spec.
2149                 match s.parse().ok() {
2150                     Some(f) => Ok(f),
2151                     None => Err(ExpectedError("Number".to_owned(), s)),
2152                 }
2153             },
2154             Json::Null => Ok(f64::NAN),
2155             value => Err(ExpectedError("Number".to_owned(), value.to_string()))
2156         }
2157     }
2158
2159     fn read_bool(&mut self) -> DecodeResult<bool> {
2160         expect!(self.pop(), Boolean)
2161     }
2162
2163     fn read_char(&mut self) -> DecodeResult<char> {
2164         let s = self.read_str()?;
2165         {
2166             let mut it = s.chars();
2167             match (it.next(), it.next()) {
2168                 // exactly one character
2169                 (Some(c), None) => return Ok(c),
2170                 _ => ()
2171             }
2172         }
2173         Err(ExpectedError("single character string".to_owned(), s.to_string()))
2174     }
2175
2176     fn read_str(&mut self) -> DecodeResult<Cow<'_, str>> {
2177         expect!(self.pop(), String).map(Cow::Owned)
2178     }
2179
2180     fn read_enum<T, F>(&mut self, _name: &str, f: F) -> DecodeResult<T> where
2181         F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2182     {
2183         f(self)
2184     }
2185
2186     fn read_enum_variant<T, F>(&mut self, names: &[&str],
2187                                mut f: F) -> DecodeResult<T>
2188         where F: FnMut(&mut Decoder, usize) -> DecodeResult<T>,
2189     {
2190         let name = match self.pop() {
2191             Json::String(s) => s,
2192             Json::Object(mut o) => {
2193                 let n = match o.remove(&"variant".to_owned()) {
2194                     Some(Json::String(s)) => s,
2195                     Some(val) => {
2196                         return Err(ExpectedError("String".to_owned(), val.to_string()))
2197                     }
2198                     None => {
2199                         return Err(MissingFieldError("variant".to_owned()))
2200                     }
2201                 };
2202                 match o.remove(&"fields".to_string()) {
2203                     Some(Json::Array(l)) => {
2204                         self.stack.extend(l.into_iter().rev());
2205                     },
2206                     Some(val) => {
2207                         return Err(ExpectedError("Array".to_owned(), val.to_string()))
2208                     }
2209                     None => {
2210                         return Err(MissingFieldError("fields".to_owned()))
2211                     }
2212                 }
2213                 n
2214             }
2215             json => {
2216                 return Err(ExpectedError("String or Object".to_owned(), json.to_string()))
2217             }
2218         };
2219         let idx = match names.iter().position(|n| *n == &name[..]) {
2220             Some(idx) => idx,
2221             None => return Err(UnknownVariantError(name))
2222         };
2223         f(self, idx)
2224     }
2225
2226     fn read_enum_variant_arg<T, F>(&mut self, _idx: usize, f: F) -> DecodeResult<T> where
2227         F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2228     {
2229         f(self)
2230     }
2231
2232     fn read_enum_struct_variant<T, F>(&mut self, names: &[&str], f: F) -> DecodeResult<T> where
2233         F: FnMut(&mut Decoder, usize) -> DecodeResult<T>,
2234     {
2235         self.read_enum_variant(names, f)
2236     }
2237
2238
2239     fn read_enum_struct_variant_field<T, F>(&mut self,
2240                                          _name: &str,
2241                                          idx: usize,
2242                                          f: F)
2243                                          -> DecodeResult<T> where
2244         F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2245     {
2246         self.read_enum_variant_arg(idx, f)
2247     }
2248
2249     fn read_struct<T, F>(&mut self, _name: &str, _len: usize, f: F) -> DecodeResult<T> where
2250         F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2251     {
2252         let value = f(self)?;
2253         self.pop();
2254         Ok(value)
2255     }
2256
2257     fn read_struct_field<T, F>(&mut self,
2258                                name: &str,
2259                                _idx: usize,
2260                                f: F)
2261                                -> DecodeResult<T> where
2262         F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2263     {
2264         let mut obj = expect!(self.pop(), Object)?;
2265
2266         let value = match obj.remove(&name.to_string()) {
2267             None => {
2268                 // Add a Null and try to parse it as an Option<_>
2269                 // to get None as a default value.
2270                 self.stack.push(Json::Null);
2271                 match f(self) {
2272                     Ok(x) => x,
2273                     Err(_) => return Err(MissingFieldError(name.to_string())),
2274                 }
2275             },
2276             Some(json) => {
2277                 self.stack.push(json);
2278                 f(self)?
2279             }
2280         };
2281         self.stack.push(Json::Object(obj));
2282         Ok(value)
2283     }
2284
2285     fn read_tuple<T, F>(&mut self, tuple_len: usize, f: F) -> DecodeResult<T> where
2286         F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2287     {
2288         self.read_seq(move |d, len| {
2289             if len == tuple_len {
2290                 f(d)
2291             } else {
2292                 Err(ExpectedError(format!("Tuple{}", tuple_len), format!("Tuple{}", len)))
2293             }
2294         })
2295     }
2296
2297     fn read_tuple_arg<T, F>(&mut self, idx: usize, f: F) -> DecodeResult<T> where
2298         F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2299     {
2300         self.read_seq_elt(idx, f)
2301     }
2302
2303     fn read_tuple_struct<T, F>(&mut self,
2304                                _name: &str,
2305                                len: usize,
2306                                f: F)
2307                                -> DecodeResult<T> where
2308         F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2309     {
2310         self.read_tuple(len, f)
2311     }
2312
2313     fn read_tuple_struct_arg<T, F>(&mut self,
2314                                    idx: usize,
2315                                    f: F)
2316                                    -> DecodeResult<T> where
2317         F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2318     {
2319         self.read_tuple_arg(idx, f)
2320     }
2321
2322     fn read_option<T, F>(&mut self, mut f: F) -> DecodeResult<T> where
2323         F: FnMut(&mut Decoder, bool) -> DecodeResult<T>,
2324     {
2325         match self.pop() {
2326             Json::Null => f(self, false),
2327             value => { self.stack.push(value); f(self, true) }
2328         }
2329     }
2330
2331     fn read_seq<T, F>(&mut self, f: F) -> DecodeResult<T> where
2332         F: FnOnce(&mut Decoder, usize) -> DecodeResult<T>,
2333     {
2334         let array = expect!(self.pop(), Array)?;
2335         let len = array.len();
2336         self.stack.extend(array.into_iter().rev());
2337         f(self, len)
2338     }
2339
2340     fn read_seq_elt<T, F>(&mut self, _idx: usize, f: F) -> DecodeResult<T> where
2341         F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2342     {
2343         f(self)
2344     }
2345
2346     fn read_map<T, F>(&mut self, f: F) -> DecodeResult<T> where
2347         F: FnOnce(&mut Decoder, usize) -> DecodeResult<T>,
2348     {
2349         let obj = expect!(self.pop(), Object)?;
2350         let len = obj.len();
2351         for (key, value) in obj {
2352             self.stack.push(value);
2353             self.stack.push(Json::String(key));
2354         }
2355         f(self, len)
2356     }
2357
2358     fn read_map_elt_key<T, F>(&mut self, _idx: usize, f: F) -> DecodeResult<T> where
2359        F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2360     {
2361         f(self)
2362     }
2363
2364     fn read_map_elt_val<T, F>(&mut self, _idx: usize, f: F) -> DecodeResult<T> where
2365        F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2366     {
2367         f(self)
2368     }
2369
2370     fn error(&mut self, err: &str) -> DecoderError {
2371         ApplicationError(err.to_string())
2372     }
2373 }
2374
2375 /// A trait for converting values to JSON
2376 pub trait ToJson {
2377     /// Converts the value of `self` to an instance of JSON
2378     fn to_json(&self) -> Json;
2379 }
2380
2381 macro_rules! to_json_impl_i64 {
2382     ($($t:ty), +) => (
2383         $(impl ToJson for $t {
2384             fn to_json(&self) -> Json {
2385                 Json::I64(*self as i64)
2386             }
2387         })+
2388     )
2389 }
2390
2391 to_json_impl_i64! { isize, i8, i16, i32, i64 }
2392
2393 macro_rules! to_json_impl_u64 {
2394     ($($t:ty), +) => (
2395         $(impl ToJson for $t {
2396             fn to_json(&self) -> Json {
2397                 Json::U64(*self as u64)
2398             }
2399         })+
2400     )
2401 }
2402
2403 to_json_impl_u64! { usize, u8, u16, u32, u64 }
2404
2405 impl ToJson for Json {
2406     fn to_json(&self) -> Json { self.clone() }
2407 }
2408
2409 impl ToJson for f32 {
2410     fn to_json(&self) -> Json { f64::from(*self).to_json() }
2411 }
2412
2413 impl ToJson for f64 {
2414     fn to_json(&self) -> Json {
2415         match self.classify() {
2416             Fp::Nan | Fp::Infinite => Json::Null,
2417             _                  => Json::F64(*self)
2418         }
2419     }
2420 }
2421
2422 impl ToJson for () {
2423     fn to_json(&self) -> Json { Json::Null }
2424 }
2425
2426 impl ToJson for bool {
2427     fn to_json(&self) -> Json { Json::Boolean(*self) }
2428 }
2429
2430 impl ToJson for str {
2431     fn to_json(&self) -> Json { Json::String(self.to_string()) }
2432 }
2433
2434 impl ToJson for string::String {
2435     fn to_json(&self) -> Json { Json::String((*self).clone()) }
2436 }
2437
2438 macro_rules! tuple_impl {
2439     // use variables to indicate the arity of the tuple
2440     ($($tyvar:ident),* ) => {
2441         // the trailing commas are for the 1 tuple
2442         impl<
2443             $( $tyvar : ToJson ),*
2444             > ToJson for ( $( $tyvar ),* , ) {
2445
2446             #[inline]
2447             #[allow(non_snake_case)]
2448             fn to_json(&self) -> Json {
2449                 match *self {
2450                     ($(ref $tyvar),*,) => Json::Array(vec![$($tyvar.to_json()),*])
2451                 }
2452             }
2453         }
2454     }
2455 }
2456
2457 tuple_impl!{A}
2458 tuple_impl!{A, B}
2459 tuple_impl!{A, B, C}
2460 tuple_impl!{A, B, C, D}
2461 tuple_impl!{A, B, C, D, E}
2462 tuple_impl!{A, B, C, D, E, F}
2463 tuple_impl!{A, B, C, D, E, F, G}
2464 tuple_impl!{A, B, C, D, E, F, G, H}
2465 tuple_impl!{A, B, C, D, E, F, G, H, I}
2466 tuple_impl!{A, B, C, D, E, F, G, H, I, J}
2467 tuple_impl!{A, B, C, D, E, F, G, H, I, J, K}
2468 tuple_impl!{A, B, C, D, E, F, G, H, I, J, K, L}
2469
2470 impl<A: ToJson> ToJson for [A] {
2471     fn to_json(&self) -> Json { Json::Array(self.iter().map(|elt| elt.to_json()).collect()) }
2472 }
2473
2474 impl<A: ToJson> ToJson for Vec<A> {
2475     fn to_json(&self) -> Json { Json::Array(self.iter().map(|elt| elt.to_json()).collect()) }
2476 }
2477
2478 impl<A: ToJson> ToJson for BTreeMap<string::String, A> {
2479     fn to_json(&self) -> Json {
2480         let mut d = BTreeMap::new();
2481         for (key, value) in self {
2482             d.insert((*key).clone(), value.to_json());
2483         }
2484         Json::Object(d)
2485     }
2486 }
2487
2488 impl<A: ToJson> ToJson for HashMap<string::String, A> {
2489     fn to_json(&self) -> Json {
2490         let mut d = BTreeMap::new();
2491         for (key, value) in self {
2492             d.insert((*key).clone(), value.to_json());
2493         }
2494         Json::Object(d)
2495     }
2496 }
2497
2498 impl<A:ToJson> ToJson for Option<A> {
2499     fn to_json(&self) -> Json {
2500         match *self {
2501             None => Json::Null,
2502             Some(ref value) => value.to_json()
2503         }
2504     }
2505 }
2506
2507 struct FormatShim<'a, 'b> {
2508     inner: &'a mut fmt::Formatter<'b>,
2509 }
2510
2511 impl<'a, 'b> fmt::Write for FormatShim<'a, 'b> {
2512     fn write_str(&mut self, s: &str) -> fmt::Result {
2513         match self.inner.write_str(s) {
2514             Ok(_) => Ok(()),
2515             Err(_) => Err(fmt::Error)
2516         }
2517     }
2518 }
2519
2520 impl fmt::Display for Json {
2521     /// Encodes a json value into a string
2522     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2523         let mut shim = FormatShim { inner: f };
2524         let mut encoder = Encoder::new(&mut shim);
2525         match self.encode(&mut encoder) {
2526             Ok(_) => Ok(()),
2527             Err(_) => Err(fmt::Error)
2528         }
2529     }
2530 }
2531
2532 impl<'a> fmt::Display for PrettyJson<'a> {
2533     /// Encodes a json value into a string
2534     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2535         let mut shim = FormatShim { inner: f };
2536         let mut encoder = PrettyEncoder::new(&mut shim);
2537         match self.inner.encode(&mut encoder) {
2538             Ok(_) => Ok(()),
2539             Err(_) => Err(fmt::Error)
2540         }
2541     }
2542 }
2543
2544 impl<'a, T: Encodable> fmt::Display for AsJson<'a, T> {
2545     /// Encodes a json value into a string
2546     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2547         let mut shim = FormatShim { inner: f };
2548         let mut encoder = Encoder::new(&mut shim);
2549         match self.inner.encode(&mut encoder) {
2550             Ok(_) => Ok(()),
2551             Err(_) => Err(fmt::Error)
2552         }
2553     }
2554 }
2555
2556 impl<'a, T> AsPrettyJson<'a, T> {
2557     /// Sets the indentation level for the emitted JSON
2558     pub fn indent(mut self, indent: usize) -> AsPrettyJson<'a, T> {
2559         self.indent = Some(indent);
2560         self
2561     }
2562 }
2563
2564 impl<'a, T: Encodable> fmt::Display for AsPrettyJson<'a, T> {
2565     /// Encodes a json value into a string
2566     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2567         let mut shim = FormatShim { inner: f };
2568         let mut encoder = PrettyEncoder::new(&mut shim);
2569         if let Some(n) = self.indent {
2570             encoder.set_indent(n);
2571         }
2572         match self.inner.encode(&mut encoder) {
2573             Ok(_) => Ok(()),
2574             Err(_) => Err(fmt::Error)
2575         }
2576     }
2577 }
2578
2579 impl FromStr for Json {
2580     type Err = BuilderError;
2581     fn from_str(s: &str) -> Result<Json, BuilderError> {
2582         from_str(s)
2583     }
2584 }
2585
2586 #[cfg(test)]
2587 mod tests;