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