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