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