1 // Rust JSON serialization library.
2 // Copyright (c) 2011 Google Inc.
4 #![forbid(non_camel_case_types)]
5 #![allow(missing_docs)]
7 //! JSON parsing and serialization
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.
15 //! Data types that can be encoded are JavaScript types (see the `Json` enum for more details):
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
22 //! * `Object`: equivalent to rust's `BTreeMap<String, json::Json>`
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
31 //! "FirstName": "John",
32 //! "LastName": "Doe",
35 //! "Street": "Downing Street 10",
37 //! "Country": "Great Britain"
46 //! # Rust Type-based Encoding and Decoding
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)]`
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.
60 //! When using `ToJson` the `RustcEncodable` trait implementation is not mandatory.
64 //! ## Using Autoserialization
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.
70 //! # #![feature(rustc_private)]
71 //! extern crate serialize as rustc_serialize; // for the deriving below
72 //! use rustc_serialize::json;
74 //! // Automatically generate `Decodable` and `Encodable` trait implementations
75 //! #[derive(RustcDecodable, RustcEncodable)]
76 //! pub struct TestStruct {
79 //! data_vector: Vec<u8>,
83 //! let object = TestStruct {
85 //! data_str: "homura".to_string(),
86 //! data_vector: vec![2,3,4,5],
89 //! // Serialize using `json::encode`
90 //! let encoded = json::encode(&object).unwrap();
92 //! // Deserialize using `json::decode`
93 //! let decoded: TestStruct = json::decode(&encoded[..]).unwrap();
97 //! ## Using the `ToJson` trait
99 //! The examples above use the `ToJson` trait to generate the JSON string, which is required
100 //! for custom mappings.
102 //! ### Simple example of `ToJson` usage
105 //! # #![feature(rustc_private)]
106 //! extern crate serialize as rustc_serialize;
107 //! use rustc_serialize::json::{self, ToJson, Json};
109 //! // A custom data structure
110 //! struct ComplexNum {
115 //! // JSON value representation
116 //! impl ToJson for ComplexNum {
117 //! fn to_json(&self) -> Json {
118 //! Json::String(format!("{}+{}i", self.a, self.b))
122 //! // Only generate `RustcEncodable` trait implementation
123 //! #[derive(RustcEncodable)]
124 //! pub struct ComplexNumRecord {
131 //! let num = ComplexNum { a: 0.0001, b: 12.539 };
132 //! let data: String = json::encode(&ComplexNumRecord{
134 //! dsc: "test".to_string(),
135 //! val: num.to_json(),
137 //! println!("data: {}", data);
138 //! // data: {"uid":1,"dsc":"test","val":"0.0001+12.539i"};
142 //! ### Verbose example of `ToJson` usage
145 //! # #![feature(rustc_private)]
146 //! extern crate serialize as rustc_serialize;
147 //! use std::collections::BTreeMap;
148 //! use rustc_serialize::json::{self, Json, ToJson};
150 //! // Only generate `RustcDecodable` trait implementation
151 //! #[derive(RustcDecodable)]
152 //! pub struct TestStruct {
154 //! data_str: String,
155 //! data_vector: Vec<u8>,
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());
171 //! // Serialize using `ToJson`
172 //! let input_data = TestStruct {
174 //! data_str: "madoka".to_string(),
175 //! data_vector: vec![2,3,4,5],
177 //! let json_obj: Json = input_data.to_json();
178 //! let json_str: String = json_obj.to_string();
180 //! // Deserialize like before
181 //! let decoded: TestStruct = json::decode(&json_str).unwrap();
185 use self::DecoderError::*;
186 use self::ErrorCode::*;
187 use self::InternalStackElement::*;
188 use self::JsonEvent::*;
189 use self::ParserError::*;
190 use self::ParserState::*;
192 use std::borrow::Cow;
193 use std::collections::{BTreeMap, HashMap};
195 use std::io::prelude::*;
197 use std::num::FpCategory as Fp;
199 use std::str::FromStr;
201 use std::{char, f64, fmt, str};
203 use crate::Encodable;
205 /// Represents a json value
206 #[derive(Clone, PartialEq, PartialOrd, Debug)]
211 String(string::String),
214 Object(self::Object),
218 pub type Array = Vec<Json>;
219 pub type Object = BTreeMap<string::String, Json>;
221 pub struct PrettyJson<'a> {
225 pub struct AsJson<'a, T> {
228 pub struct AsPrettyJson<'a, T> {
230 indent: Option<usize>,
233 /// The errors that can arise while parsing a JSON stream.
234 #[derive(Clone, Copy, PartialEq, Debug)]
238 EOFWhileParsingObject,
239 EOFWhileParsingArray,
240 EOFWhileParsingValue,
241 EOFWhileParsingString,
247 InvalidUnicodeCodePoint,
248 LoneLeadingSurrogateInHexEscape,
249 UnexpectedEndOfHexEscape,
255 #[derive(Clone, PartialEq, Debug)]
256 pub enum ParserError {
258 SyntaxError(ErrorCode, usize, usize),
259 IoError(io::ErrorKind, String),
262 // Builder and Parser have the same errors.
263 pub type BuilderError = ParserError;
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),
274 #[derive(Copy, Clone, Debug)]
275 pub enum EncoderError {
276 FmtError(fmt::Error),
280 /// Returns a readable error string for a given error code.
281 pub fn error_str(error: ErrorCode) -> &'static str {
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",
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) {
307 Err(e) => return Err(ParseError(e)),
310 let mut decoder = Decoder::new(json);
311 crate::Decodable::decode(&mut decoder)
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();
318 let mut encoder = Encoder::new(&mut s);
319 object.encode(&mut encoder)?;
324 impl fmt::Display for ErrorCode {
325 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
326 error_str(*self).fmt(f)
330 fn io_error_to_error(io: io::Error) -> ParserError {
331 IoError(io.kind(), io.to_string())
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)
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)
348 impl std::error::Error for DecoderError {
349 fn description(&self) -> &str {
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)
361 impl std::error::Error for EncoderError {
362 fn description(&self) -> &str {
367 impl From<fmt::Error> for EncoderError {
368 /// Converts a [`fmt::Error`] into `EncoderError`
370 /// This conversion does not allocate memory.
371 fn from(err: fmt::Error) -> EncoderError {
372 EncoderError::FmtError(err)
376 pub type EncodeResult = Result<(), EncoderError>;
377 pub type DecodeResult<T> = Result<T, DecoderError>;
379 fn escape_str(wr: &mut dyn fmt::Write, v: &str) -> EncodeResult {
384 for (i, byte) in v.bytes().enumerate() {
385 let escaped = match byte {
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",
399 b'\x0b' => "\\u000b",
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",
427 wr.write_str(&v[start..i])?;
430 wr.write_str(escaped)?;
435 if start != v.len() {
436 wr.write_str(&v[start..])?;
443 fn escape_char(writer: &mut dyn fmt::Write, v: char) -> EncodeResult {
444 escape_str(writer, v.encode_utf8(&mut [0; 4]))
447 fn spaces(wr: &mut dyn fmt::Write, mut n: usize) -> EncodeResult {
448 const BUF: &str = " ";
450 while n >= BUF.len() {
456 wr.write_str(&BUF[..n])?;
461 fn fmt_number_or_null(v: f64) -> string::String {
463 Fp::Nan | Fp::Infinite => string::String::from("null"),
464 _ if v.fract() != 0f64 => v.to_string(),
465 _ => v.to_string() + ".0",
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,
475 impl<'a> Encoder<'a> {
476 /// Creates a new JSON encoder whose output will be written to the writer
478 pub fn new(writer: &'a mut dyn fmt::Write) -> Encoder<'a> {
479 Encoder { writer, is_emitting_map_key: false }
483 macro_rules! emit_enquoted_if_mapkey {
484 ($enc:ident,$e:expr) => {{
485 if $enc.is_emitting_map_key {
486 write!($enc.writer, "\"{}\"", $e)?;
488 write!($enc.writer, "{}", $e)?;
494 impl<'a> crate::Encoder for Encoder<'a> {
495 type Error = EncoderError;
497 fn emit_unit(&mut self) -> EncodeResult {
498 if self.is_emitting_map_key {
499 return Err(EncoderError::BadHashmapKey);
501 write!(self.writer, "null")?;
505 fn emit_usize(&mut self, v: usize) -> EncodeResult {
506 emit_enquoted_if_mapkey!(self, v)
508 fn emit_u128(&mut self, v: u128) -> EncodeResult {
509 emit_enquoted_if_mapkey!(self, v)
511 fn emit_u64(&mut self, v: u64) -> EncodeResult {
512 emit_enquoted_if_mapkey!(self, v)
514 fn emit_u32(&mut self, v: u32) -> EncodeResult {
515 emit_enquoted_if_mapkey!(self, v)
517 fn emit_u16(&mut self, v: u16) -> EncodeResult {
518 emit_enquoted_if_mapkey!(self, v)
520 fn emit_u8(&mut self, v: u8) -> EncodeResult {
521 emit_enquoted_if_mapkey!(self, v)
524 fn emit_isize(&mut self, v: isize) -> EncodeResult {
525 emit_enquoted_if_mapkey!(self, v)
527 fn emit_i128(&mut self, v: i128) -> EncodeResult {
528 emit_enquoted_if_mapkey!(self, v)
530 fn emit_i64(&mut self, v: i64) -> EncodeResult {
531 emit_enquoted_if_mapkey!(self, v)
533 fn emit_i32(&mut self, v: i32) -> EncodeResult {
534 emit_enquoted_if_mapkey!(self, v)
536 fn emit_i16(&mut self, v: i16) -> EncodeResult {
537 emit_enquoted_if_mapkey!(self, v)
539 fn emit_i8(&mut self, v: i8) -> EncodeResult {
540 emit_enquoted_if_mapkey!(self, v)
543 fn emit_bool(&mut self, v: bool) -> EncodeResult {
544 if self.is_emitting_map_key {
545 return Err(EncoderError::BadHashmapKey);
548 write!(self.writer, "true")?;
550 write!(self.writer, "false")?;
555 fn emit_f64(&mut self, v: f64) -> EncodeResult {
556 emit_enquoted_if_mapkey!(self, fmt_number_or_null(v))
558 fn emit_f32(&mut self, v: f32) -> EncodeResult {
559 self.emit_f64(f64::from(v))
562 fn emit_char(&mut self, v: char) -> EncodeResult {
563 escape_char(self.writer, v)
565 fn emit_str(&mut self, v: &str) -> EncodeResult {
566 escape_str(self.writer, v)
569 fn emit_enum<F>(&mut self, _name: &str, f: F) -> EncodeResult
571 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
576 fn emit_enum_variant<F>(&mut self, name: &str, _id: usize, cnt: usize, f: F) -> EncodeResult
578 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
580 // enums are encoded as strings or objects
582 // Kangaroo(34,"William") => {"variant": "Kangaroo", "fields": [34,"William"]}
584 escape_str(self.writer, name)
586 if self.is_emitting_map_key {
587 return Err(EncoderError::BadHashmapKey);
589 write!(self.writer, "{{\"variant\":")?;
590 escape_str(self.writer, name)?;
591 write!(self.writer, ",\"fields\":[")?;
593 write!(self.writer, "]}}")?;
598 fn emit_enum_variant_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult
600 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
602 if self.is_emitting_map_key {
603 return Err(EncoderError::BadHashmapKey);
606 write!(self.writer, ",")?;
611 fn emit_enum_struct_variant<F>(
619 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
621 if self.is_emitting_map_key {
622 return Err(EncoderError::BadHashmapKey);
624 self.emit_enum_variant(name, id, cnt, f)
627 fn emit_enum_struct_variant_field<F>(&mut self, _: &str, idx: usize, f: F) -> EncodeResult
629 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
631 if self.is_emitting_map_key {
632 return Err(EncoderError::BadHashmapKey);
634 self.emit_enum_variant_arg(idx, f)
637 fn emit_struct<F>(&mut self, _: &str, _: usize, f: F) -> EncodeResult
639 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
641 if self.is_emitting_map_key {
642 return Err(EncoderError::BadHashmapKey);
644 write!(self.writer, "{{")?;
646 write!(self.writer, "}}")?;
650 fn emit_struct_field<F>(&mut self, name: &str, idx: usize, f: F) -> EncodeResult
652 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
654 if self.is_emitting_map_key {
655 return Err(EncoderError::BadHashmapKey);
658 write!(self.writer, ",")?;
660 escape_str(self.writer, name)?;
661 write!(self.writer, ":")?;
665 fn emit_tuple<F>(&mut self, len: usize, f: F) -> EncodeResult
667 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
669 if self.is_emitting_map_key {
670 return Err(EncoderError::BadHashmapKey);
672 self.emit_seq(len, f)
674 fn emit_tuple_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult
676 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
678 if self.is_emitting_map_key {
679 return Err(EncoderError::BadHashmapKey);
681 self.emit_seq_elt(idx, f)
684 fn emit_tuple_struct<F>(&mut self, _name: &str, len: usize, f: F) -> EncodeResult
686 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
688 if self.is_emitting_map_key {
689 return Err(EncoderError::BadHashmapKey);
691 self.emit_seq(len, f)
693 fn emit_tuple_struct_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult
695 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
697 if self.is_emitting_map_key {
698 return Err(EncoderError::BadHashmapKey);
700 self.emit_seq_elt(idx, f)
703 fn emit_option<F>(&mut self, f: F) -> EncodeResult
705 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
707 if self.is_emitting_map_key {
708 return Err(EncoderError::BadHashmapKey);
712 fn emit_option_none(&mut self) -> EncodeResult {
713 if self.is_emitting_map_key {
714 return Err(EncoderError::BadHashmapKey);
718 fn emit_option_some<F>(&mut self, f: F) -> EncodeResult
720 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
722 if self.is_emitting_map_key {
723 return Err(EncoderError::BadHashmapKey);
728 fn emit_seq<F>(&mut self, _len: usize, f: F) -> EncodeResult
730 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
732 if self.is_emitting_map_key {
733 return Err(EncoderError::BadHashmapKey);
735 write!(self.writer, "[")?;
737 write!(self.writer, "]")?;
741 fn emit_seq_elt<F>(&mut self, idx: usize, f: F) -> EncodeResult
743 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
745 if self.is_emitting_map_key {
746 return Err(EncoderError::BadHashmapKey);
749 write!(self.writer, ",")?;
754 fn emit_map<F>(&mut self, _len: usize, f: F) -> EncodeResult
756 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
758 if self.is_emitting_map_key {
759 return Err(EncoderError::BadHashmapKey);
761 write!(self.writer, "{{")?;
763 write!(self.writer, "}}")?;
767 fn emit_map_elt_key<F>(&mut self, idx: usize, f: F) -> EncodeResult
769 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
771 if self.is_emitting_map_key {
772 return Err(EncoderError::BadHashmapKey);
775 write!(self.writer, ",")?
777 self.is_emitting_map_key = true;
779 self.is_emitting_map_key = false;
783 fn emit_map_elt_val<F>(&mut self, _idx: usize, f: F) -> EncodeResult
785 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
787 if self.is_emitting_map_key {
788 return Err(EncoderError::BadHashmapKey);
790 write!(self.writer, ":")?;
795 /// Another encoder for JSON, but prints out human-readable JSON instead of
797 pub struct PrettyEncoder<'a> {
798 writer: &'a mut (dyn fmt::Write + 'a),
801 is_emitting_map_key: bool,
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 }
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;
820 impl<'a> crate::Encoder for PrettyEncoder<'a> {
821 type Error = EncoderError;
823 fn emit_unit(&mut self) -> EncodeResult {
824 if self.is_emitting_map_key {
825 return Err(EncoderError::BadHashmapKey);
827 write!(self.writer, "null")?;
831 fn emit_usize(&mut self, v: usize) -> EncodeResult {
832 emit_enquoted_if_mapkey!(self, v)
834 fn emit_u128(&mut self, v: u128) -> EncodeResult {
835 emit_enquoted_if_mapkey!(self, v)
837 fn emit_u64(&mut self, v: u64) -> EncodeResult {
838 emit_enquoted_if_mapkey!(self, v)
840 fn emit_u32(&mut self, v: u32) -> EncodeResult {
841 emit_enquoted_if_mapkey!(self, v)
843 fn emit_u16(&mut self, v: u16) -> EncodeResult {
844 emit_enquoted_if_mapkey!(self, v)
846 fn emit_u8(&mut self, v: u8) -> EncodeResult {
847 emit_enquoted_if_mapkey!(self, v)
850 fn emit_isize(&mut self, v: isize) -> EncodeResult {
851 emit_enquoted_if_mapkey!(self, v)
853 fn emit_i128(&mut self, v: i128) -> EncodeResult {
854 emit_enquoted_if_mapkey!(self, v)
856 fn emit_i64(&mut self, v: i64) -> EncodeResult {
857 emit_enquoted_if_mapkey!(self, v)
859 fn emit_i32(&mut self, v: i32) -> EncodeResult {
860 emit_enquoted_if_mapkey!(self, v)
862 fn emit_i16(&mut self, v: i16) -> EncodeResult {
863 emit_enquoted_if_mapkey!(self, v)
865 fn emit_i8(&mut self, v: i8) -> EncodeResult {
866 emit_enquoted_if_mapkey!(self, v)
869 fn emit_bool(&mut self, v: bool) -> EncodeResult {
870 if self.is_emitting_map_key {
871 return Err(EncoderError::BadHashmapKey);
874 write!(self.writer, "true")?;
876 write!(self.writer, "false")?;
881 fn emit_f64(&mut self, v: f64) -> EncodeResult {
882 emit_enquoted_if_mapkey!(self, fmt_number_or_null(v))
884 fn emit_f32(&mut self, v: f32) -> EncodeResult {
885 self.emit_f64(f64::from(v))
888 fn emit_char(&mut self, v: char) -> EncodeResult {
889 escape_char(self.writer, v)
891 fn emit_str(&mut self, v: &str) -> EncodeResult {
892 escape_str(self.writer, v)
895 fn emit_enum<F>(&mut self, _name: &str, f: F) -> EncodeResult
897 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
902 fn emit_enum_variant<F>(&mut self, name: &str, _id: usize, cnt: usize, f: F) -> EncodeResult
904 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
907 escape_str(self.writer, name)
909 if self.is_emitting_map_key {
910 return Err(EncoderError::BadHashmapKey);
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;
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, "}}")?;
933 fn emit_enum_variant_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult
935 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
937 if self.is_emitting_map_key {
938 return Err(EncoderError::BadHashmapKey);
941 writeln!(self.writer, ",")?;
943 spaces(self.writer, self.curr_indent)?;
947 fn emit_enum_struct_variant<F>(
955 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
957 if self.is_emitting_map_key {
958 return Err(EncoderError::BadHashmapKey);
960 self.emit_enum_variant(name, id, cnt, f)
963 fn emit_enum_struct_variant_field<F>(&mut self, _: &str, idx: usize, f: F) -> EncodeResult
965 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
967 if self.is_emitting_map_key {
968 return Err(EncoderError::BadHashmapKey);
970 self.emit_enum_variant_arg(idx, f)
973 fn emit_struct<F>(&mut self, _: &str, len: usize, f: F) -> EncodeResult
975 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
977 if self.is_emitting_map_key {
978 return Err(EncoderError::BadHashmapKey);
981 write!(self.writer, "{{}}")?;
983 write!(self.writer, "{{")?;
984 self.curr_indent += self.indent;
986 self.curr_indent -= self.indent;
987 writeln!(self.writer)?;
988 spaces(self.writer, self.curr_indent)?;
989 write!(self.writer, "}}")?;
994 fn emit_struct_field<F>(&mut self, name: &str, idx: usize, f: F) -> EncodeResult
996 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
998 if self.is_emitting_map_key {
999 return Err(EncoderError::BadHashmapKey);
1002 writeln!(self.writer)?;
1004 writeln!(self.writer, ",")?;
1006 spaces(self.writer, self.curr_indent)?;
1007 escape_str(self.writer, name)?;
1008 write!(self.writer, ": ")?;
1012 fn emit_tuple<F>(&mut self, len: usize, f: F) -> EncodeResult
1014 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
1016 if self.is_emitting_map_key {
1017 return Err(EncoderError::BadHashmapKey);
1019 self.emit_seq(len, f)
1021 fn emit_tuple_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult
1023 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
1025 if self.is_emitting_map_key {
1026 return Err(EncoderError::BadHashmapKey);
1028 self.emit_seq_elt(idx, f)
1031 fn emit_tuple_struct<F>(&mut self, _: &str, len: usize, f: F) -> EncodeResult
1033 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
1035 if self.is_emitting_map_key {
1036 return Err(EncoderError::BadHashmapKey);
1038 self.emit_seq(len, f)
1040 fn emit_tuple_struct_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult
1042 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
1044 if self.is_emitting_map_key {
1045 return Err(EncoderError::BadHashmapKey);
1047 self.emit_seq_elt(idx, f)
1050 fn emit_option<F>(&mut self, f: F) -> EncodeResult
1052 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
1054 if self.is_emitting_map_key {
1055 return Err(EncoderError::BadHashmapKey);
1059 fn emit_option_none(&mut self) -> EncodeResult {
1060 if self.is_emitting_map_key {
1061 return Err(EncoderError::BadHashmapKey);
1065 fn emit_option_some<F>(&mut self, f: F) -> EncodeResult
1067 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
1069 if self.is_emitting_map_key {
1070 return Err(EncoderError::BadHashmapKey);
1075 fn emit_seq<F>(&mut self, len: usize, f: F) -> EncodeResult
1077 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
1079 if self.is_emitting_map_key {
1080 return Err(EncoderError::BadHashmapKey);
1083 write!(self.writer, "[]")?;
1085 write!(self.writer, "[")?;
1086 self.curr_indent += self.indent;
1088 self.curr_indent -= self.indent;
1089 writeln!(self.writer)?;
1090 spaces(self.writer, self.curr_indent)?;
1091 write!(self.writer, "]")?;
1096 fn emit_seq_elt<F>(&mut self, idx: usize, f: F) -> EncodeResult
1098 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
1100 if self.is_emitting_map_key {
1101 return Err(EncoderError::BadHashmapKey);
1104 writeln!(self.writer)?;
1106 writeln!(self.writer, ",")?;
1108 spaces(self.writer, self.curr_indent)?;
1112 fn emit_map<F>(&mut self, len: usize, f: F) -> EncodeResult
1114 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
1116 if self.is_emitting_map_key {
1117 return Err(EncoderError::BadHashmapKey);
1120 write!(self.writer, "{{}}")?;
1122 write!(self.writer, "{{")?;
1123 self.curr_indent += self.indent;
1125 self.curr_indent -= self.indent;
1126 writeln!(self.writer)?;
1127 spaces(self.writer, self.curr_indent)?;
1128 write!(self.writer, "}}")?;
1133 fn emit_map_elt_key<F>(&mut self, idx: usize, f: F) -> EncodeResult
1135 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
1137 if self.is_emitting_map_key {
1138 return Err(EncoderError::BadHashmapKey);
1141 writeln!(self.writer)?;
1143 writeln!(self.writer, ",")?;
1145 spaces(self.writer, self.curr_indent)?;
1146 self.is_emitting_map_key = true;
1148 self.is_emitting_map_key = false;
1152 fn emit_map_elt_val<F>(&mut self, _idx: usize, f: F) -> EncodeResult
1154 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
1156 if self.is_emitting_map_key {
1157 return Err(EncoderError::BadHashmapKey);
1159 write!(self.writer, ": ")?;
1164 impl Encodable for Json {
1165 fn encode<E: crate::Encoder>(&self, e: &mut E) -> Result<(), E::Error> {
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(),
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> {
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 }
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 }
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> {
1202 Json::Object(ref map) => map.get(key),
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;
1213 target = target.find(*key)?;
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> {
1223 Json::Object(ref map) => match map.get(key) {
1224 Some(json_value) => Some(json_value),
1226 for v in map.values() {
1227 match v.search(key) {
1228 x if x.is_some() => return x,
1239 /// Returns `true` if the Json value is an `Object`.
1240 pub fn is_object(&self) -> bool {
1241 self.as_object().is_some()
1244 /// If the Json value is an `Object`, returns the associated `BTreeMap`;
1245 /// returns `None` otherwise.
1246 pub fn as_object(&self) -> Option<&Object> {
1248 Json::Object(ref map) => Some(map),
1253 /// Returns `true` if the Json value is an `Array`.
1254 pub fn is_array(&self) -> bool {
1255 self.as_array().is_some()
1258 /// If the Json value is an `Array`, returns the associated vector;
1259 /// returns `None` otherwise.
1260 pub fn as_array(&self) -> Option<&Array> {
1262 Json::Array(ref array) => Some(&*array),
1267 /// Returns `true` if the Json value is a `String`.
1268 pub fn is_string(&self) -> bool {
1269 self.as_string().is_some()
1272 /// If the Json value is a `String`, returns the associated `str`;
1273 /// returns `None` otherwise.
1274 pub fn as_string(&self) -> Option<&str> {
1276 Json::String(ref s) => Some(&s[..]),
1281 /// Returns `true` if the Json value is a `Number`.
1282 pub fn is_number(&self) -> bool {
1284 Json::I64(_) | Json::U64(_) | Json::F64(_) => true,
1289 /// Returns `true` if the Json value is a `i64`.
1290 pub fn is_i64(&self) -> bool {
1292 Json::I64(_) => true,
1297 /// Returns `true` if the Json value is a `u64`.
1298 pub fn is_u64(&self) -> bool {
1300 Json::U64(_) => true,
1305 /// Returns `true` if the Json value is a `f64`.
1306 pub fn is_f64(&self) -> bool {
1308 Json::F64(_) => true,
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> {
1317 Json::I64(n) => Some(n),
1318 Json::U64(n) => Some(n as i64),
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> {
1327 Json::I64(n) => Some(n as u64),
1328 Json::U64(n) => Some(n),
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> {
1337 Json::I64(n) => Some(n as f64),
1338 Json::U64(n) => Some(n as f64),
1339 Json::F64(n) => Some(n),
1344 /// Returns `true` if the Json value is a `Boolean`.
1345 pub fn is_boolean(&self) -> bool {
1346 self.as_boolean().is_some()
1349 /// If the Json value is a `Boolean`, returns the associated `bool`;
1350 /// returns `None` otherwise.
1351 pub fn as_boolean(&self) -> Option<bool> {
1353 Json::Boolean(b) => Some(b),
1358 /// Returns `true` if the Json value is a `Null`.
1359 pub fn is_null(&self) -> bool {
1360 self.as_null().is_some()
1363 /// If the Json value is a `Null`, returns `()`;
1364 /// returns `None` otherwise.
1365 pub fn as_null(&self) -> Option<()> {
1367 Json::Null => Some(()),
1373 impl<'a> Index<&'a str> for Json {
1376 fn index(&self, idx: &'a str) -> &Json {
1377 self.find(idx).unwrap()
1381 impl Index<usize> for Json {
1384 fn index(&self, idx: usize) -> &Json {
1386 Json::Array(ref v) => &v[idx],
1387 _ => panic!("can only index Json with usize if it is an array"),
1392 /// The output of the streaming parser.
1393 #[derive(PartialEq, Clone, Debug)]
1394 pub enum JsonEvent {
1403 StringValue(string::String),
1408 #[derive(PartialEq, Debug)]
1410 // Parse a value in an array, true means first element.
1412 // Parse ',' or ']' after an element in an array.
1414 // Parse a key:value in an object, true means first element.
1416 // Parse ',' or ']' after an element in an object.
1420 // Expecting the stream to end.
1422 // Parsing can't continue.
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
1430 stack: Vec<InternalStackElement>,
1431 str_buffer: Vec<u8>,
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> {
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 {
1449 InternalKey(u16, u16), // start, size
1453 pub fn new() -> Stack {
1454 Stack { stack: Vec::new(), str_buffer: Vec::new() }
1457 /// Returns The number of elements in the Stack.
1458 pub fn len(&self) -> usize {
1462 /// Returns `true` if the stack is empty.
1463 pub fn is_empty(&self) -> bool {
1464 self.stack.is_empty()
1467 /// Provides access to the StackElement at a given index.
1468 /// lower indices are at the bottom of the stack while higher indices are
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])
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() {
1485 for (i, r) in rhs.iter().enumerate() {
1486 if self.get(i) != *r {
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() {
1499 for (i, r) in rhs.iter().enumerate() {
1500 if self.get(i) != *r {
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() {
1513 let offset = self.stack.len() - rhs.len();
1514 for (i, r) in rhs.iter().enumerate() {
1515 if self.get(i + offset) != *r {
1522 /// Returns the top-most element (if any).
1523 pub fn top(&self) -> Option<StackElement<'_>> {
1524 match self.stack.last() {
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(),
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());
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));
1544 // Used by Parser to remove the top-most element of the stack.
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);
1552 InternalIndex(_) => {}
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,
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,
1574 self.stack[len - 1] = InternalIndex(idx);
1578 /// A streaming JSON parser implemented as an iterator of JsonEvent, consuming
1579 /// an iterator of char.
1580 pub struct Parser<T> {
1585 // We maintain a stack representing where we are in the logical structure
1586 // of the JSON stream.
1588 // A state machine is kept to make it possible to interrupt and resume parsing.
1592 impl<T: Iterator<Item = char>> Iterator for Parser<T> {
1593 type Item = JsonEvent;
1595 fn next(&mut self) -> Option<JsonEvent> {
1596 if self.state == ParseFinished {
1600 if self.state == ParseBeforeFinish {
1601 self.parse_whitespace();
1602 // Make sure there is no trailing characters.
1604 self.state = ParseFinished;
1607 return Some(self.error_event(TrailingCharacters));
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 {
1623 stack: Stack::new(),
1630 /// Provides access to the current position in the logical structure of the
1632 pub fn stack(&self) -> &Stack {
1636 fn eof(&self) -> bool {
1639 fn ch_or_null(&self) -> char {
1640 self.ch.unwrap_or('\x00')
1642 fn bump(&mut self) {
1643 self.ch = self.rdr.next();
1645 if self.ch_is('\n') {
1653 fn next_char(&mut self) -> Option<char> {
1657 fn ch_is(&self, c: char) -> bool {
1661 fn error<U>(&self, reason: ErrorCode) -> Result<U, ParserError> {
1662 Err(SyntaxError(reason, self.line, self.col))
1665 fn parse_whitespace(&mut self) {
1666 while self.ch_is(' ') || self.ch_is('\n') || self.ch_is('\t') || self.ch_is('\r') {
1671 fn parse_number(&mut self) -> JsonEvent {
1672 let neg = if self.ch_is('-') {
1679 let res = match self.parse_u64() {
1686 if self.ch_is('.') || self.ch_is('e') || self.ch_is('E') {
1687 let mut res = res as f64;
1689 if self.ch_is('.') {
1690 res = match self.parse_decimal(res) {
1698 if self.ch_is('e') || self.ch_is('E') {
1699 res = match self.parse_exponent(res) {
1713 let res = (res as i64).wrapping_neg();
1715 // Make sure we didn't underflow.
1717 Error(SyntaxError(InvalidNumber, self.line, self.col))
1726 fn parse_u64(&mut self) -> Result<u64, ParserError> {
1727 let mut accum = 0u64;
1728 let last_accum = 0; // necessary to detect overflow.
1730 match self.ch_or_null() {
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);
1741 match self.ch_or_null() {
1743 accum = accum.wrapping_mul(10);
1744 accum = accum.wrapping_add((c as u64) - ('0' as u64));
1746 // Detect overflow by comparing to the last value.
1747 if accum <= last_accum {
1748 return self.error(InvalidNumber);
1757 _ => return self.error(InvalidNumber),
1763 fn parse_decimal(&mut self, mut res: f64) -> Result<f64, ParserError> {
1766 // Make sure a digit follows the decimal place.
1767 match self.ch_or_null() {
1769 _ => return self.error(InvalidNumber),
1774 match self.ch_or_null() {
1777 res += (((c as isize) - ('0' as isize)) as f64) * dec;
1787 fn parse_exponent(&mut self, mut res: f64) -> Result<f64, ParserError> {
1791 let mut neg_exp = false;
1793 if self.ch_is('+') {
1795 } else if self.ch_is('-') {
1800 // Make sure a digit follows the exponent place.
1801 match self.ch_or_null() {
1803 _ => return self.error(InvalidNumber),
1806 match self.ch_or_null() {
1809 exp += (c as usize) - ('0' as usize);
1817 let exp = 10_f64.powi(exp as i32);
1827 fn decode_hex_escape(&mut self) -> Result<u16, ParserError> {
1830 while i < 4 && !self.eof() {
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),
1846 // Error out if we didn't parse 4 digits.
1848 return self.error(InvalidEscape);
1854 fn parse_str(&mut self) -> Result<string::String, ParserError> {
1855 let mut escape = false;
1856 let mut res = string::String::new();
1861 return self.error(EOFWhileParsingString);
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),
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),
1885 let n2 = self.decode_hex_escape()?;
1886 if n2 < 0xDC00 || n2 > 0xDFFF {
1887 return self.error(LoneLeadingSurrogateInHexEscape);
1890 (u32::from(n1 - 0xD800) << 10 | u32::from(n2 - 0xDC00)) + 0x1_0000;
1891 res.push(char::from_u32(c).unwrap());
1894 n => match char::from_u32(u32::from(n)) {
1895 Some(c) => res.push(c),
1896 None => return self.error(InvalidUnicodeCodePoint),
1899 _ => return self.error(InvalidEscape),
1902 } else if self.ch_is('\\') {
1910 Some(c) => res.push(c),
1911 None => unreachable!(),
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
1923 fn parse(&mut self) -> JsonEvent {
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();
1935 return self.parse_start();
1937 ParseArray(first) => {
1938 return self.parse_array(first);
1940 ParseArrayComma => {
1941 if let Some(evt) = self.parse_array_comma_or_end() {
1945 ParseObject(first) => {
1946 return self.parse_object(first);
1948 ParseObjectComma => {
1950 if self.ch_is(',') {
1951 self.state = ParseObject(false);
1954 return self.parse_object_end();
1958 return self.error_event(InvalidSyntax);
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,
1975 fn parse_array(&mut self, first: bool) -> JsonEvent {
1976 if self.ch_is(']') {
1978 self.error_event(InvalidSyntax)
1980 self.state = if self.stack.is_empty() {
1982 } else if self.stack.last_is_index() {
1992 self.stack.push_index(0);
1994 let val = self.parse_value();
1995 self.state = match val {
1996 Error(_) => ParseFinished,
1997 ArrayStart => ParseArray(true),
1998 ObjectStart => ParseObject(true),
1999 _ => ParseArrayComma,
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);
2011 } else if self.ch_is(']') {
2013 self.state = if self.stack.is_empty() {
2015 } else if self.stack.last_is_index() {
2022 } else if self.eof() {
2023 Some(self.error_event(EOFWhileParsingArray))
2025 Some(self.error_event(InvalidSyntax))
2029 fn parse_object(&mut self, first: bool) -> JsonEvent {
2030 if self.ch_is('}') {
2032 if self.stack.is_empty() {
2033 return self.error_event(TrailingComma);
2038 self.state = if self.stack.is_empty() {
2040 } else if self.stack.last_is_index() {
2049 return self.error_event(EOFWhileParsingObject);
2051 if !self.ch_is('"') {
2052 return self.error_event(KeyMustBeAString);
2054 let s = match self.parse_str() {
2057 self.state = ParseFinished;
2061 self.parse_whitespace();
2063 return self.error_event(EOFWhileParsingObject);
2064 } else if self.ch_or_null() != ':' {
2065 return self.error_event(ExpectedColon);
2067 self.stack.push_key(s);
2069 self.parse_whitespace();
2071 let val = self.parse_value();
2073 self.state = match val {
2074 Error(_) => ParseFinished,
2075 ArrayStart => ParseArray(true),
2076 ObjectStart => ParseObject(true),
2077 _ => ParseObjectComma,
2082 fn parse_object_end(&mut self) -> JsonEvent {
2083 if self.ch_is('}') {
2084 self.state = if self.stack.is_empty() {
2086 } else if self.stack.last_is_index() {
2093 } else if self.eof() {
2094 self.error_event(EOFWhileParsingObject)
2096 self.error_event(InvalidSyntax)
2100 fn parse_value(&mut self) -> JsonEvent {
2102 return self.error_event(EOFWhileParsingValue);
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),
2121 _ => self.error_event(InvalidSyntax),
2125 fn parse_ident(&mut self, ident: &str, value: JsonEvent) -> JsonEvent {
2126 if ident.chars().all(|c| Some(c) == self.next_char()) {
2130 Error(SyntaxError(InvalidSyntax, self.line, self.col))
2134 fn error_event(&mut self, reason: ErrorCode) -> JsonEvent {
2135 self.state = ParseFinished;
2136 Error(SyntaxError(reason, self.line, self.col))
2140 /// A Builder consumes a json::Parser to create a generic Json structure.
2141 pub struct Builder<T> {
2143 token: Option<JsonEvent>,
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 }
2152 // Decode a Json value from a Parser.
2153 pub fn build(&mut self) -> Result<Json, BuilderError> {
2155 let result = self.build_value();
2159 Some(Error(ref e)) => {
2160 return Err(e.clone());
2163 panic!("unexpected token {:?}", tok.clone());
2169 fn bump(&mut self) {
2170 self.token = self.parser.next();
2173 fn build_value(&mut self) -> Result<Json, BuilderError> {
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();
2183 Ok(Json::String(temp))
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),
2194 fn build_array(&mut self) -> Result<Json, BuilderError> {
2196 let mut values = Vec::new();
2199 if self.token == Some(ArrayEnd) {
2200 return Ok(Json::Array(values.into_iter().collect()));
2202 match self.build_value() {
2203 Ok(v) => values.push(v),
2204 Err(e) => return Err(e),
2210 fn build_object(&mut self) -> Result<Json, BuilderError> {
2213 let mut values = BTreeMap::new();
2217 Some(ObjectEnd) => {
2218 return Ok(Json::Object(values));
2220 Some(Error(ref e)) => {
2221 return Err(e.clone());
2228 let key = match self.parser.stack().top() {
2229 Some(StackElement::Key(k)) => k.to_owned(),
2231 panic!("invalid state");
2234 match self.build_value() {
2236 values.insert(key, value);
2244 self.parser.error(EOFWhileParsingObject)
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) {
2253 Err(e) => return Err(io_error_to_error(e)),
2255 let s = match str::from_utf8(&contents).ok() {
2257 _ => return Err(SyntaxError(NotUtf8, 0, 0)),
2259 let mut builder = Builder::new(s.chars());
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());
2269 /// A structure to decode JSON to values in rust.
2270 pub struct 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] }
2280 fn pop(&mut self) -> Json {
2281 self.stack.pop().unwrap()
2285 macro_rules! expect {
2286 ($e:expr, Null) => {{
2288 Json::Null => Ok(()),
2289 other => Err(ExpectedError("Null".to_owned(), other.to_string())),
2292 ($e:expr, $t:ident) => {{
2294 Json::$t(v) => Ok(v),
2295 other => Err(ExpectedError(stringify!($t).to_owned(), other.to_string())),
2300 macro_rules! read_primitive {
2301 ($name:ident, $ty:ty) => {
2302 fn $name(&mut self) -> DecodeResult<$ty> {
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() {
2311 None => Err(ExpectedError("Number".to_owned(), s)),
2313 value => Err(ExpectedError("Number".to_owned(), value.to_string())),
2319 impl crate::Decoder for Decoder {
2320 type Error = DecoderError;
2322 fn read_nil(&mut self) -> DecodeResult<()> {
2323 expect!(self.pop(), Null)
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 }
2339 fn read_f32(&mut self) -> DecodeResult<f32> {
2340 self.read_f64().map(|x| x as f32)
2343 fn read_f64(&mut self) -> DecodeResult<f64> {
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() {
2353 None => Err(ExpectedError("Number".to_owned(), s)),
2356 Json::Null => Ok(f64::NAN),
2357 value => Err(ExpectedError("Number".to_owned(), value.to_string())),
2361 fn read_bool(&mut self) -> DecodeResult<bool> {
2362 expect!(self.pop(), Boolean)
2365 fn read_char(&mut self) -> DecodeResult<char> {
2366 let s = self.read_str()?;
2368 let mut it = s.chars();
2369 if let (Some(c), None) = (it.next(), it.next()) {
2370 // exactly one character
2374 Err(ExpectedError("single character string".to_owned(), s.to_string()))
2377 fn read_str(&mut self) -> DecodeResult<Cow<'_, str>> {
2378 expect!(self.pop(), String).map(Cow::Owned)
2381 fn read_enum<T, F>(&mut self, _name: &str, f: F) -> DecodeResult<T>
2383 F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2388 fn read_enum_variant<T, F>(&mut self, names: &[&str], mut f: F) -> DecodeResult<T>
2390 F: FnMut(&mut Decoder, usize) -> DecodeResult<T>,
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())),
2400 match o.remove(&"fields".to_string()) {
2401 Some(Json::Array(l)) => {
2402 self.stack.extend(l.into_iter().rev());
2404 Some(val) => return Err(ExpectedError("Array".to_owned(), val.to_string())),
2405 None => return Err(MissingFieldError("fields".to_owned())),
2409 json => return Err(ExpectedError("String or Object".to_owned(), json.to_string())),
2411 let idx = match names.iter().position(|n| *n == &name[..]) {
2413 None => return Err(UnknownVariantError(name)),
2418 fn read_enum_variant_arg<T, F>(&mut self, _idx: usize, f: F) -> DecodeResult<T>
2420 F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2425 fn read_enum_struct_variant<T, F>(&mut self, names: &[&str], f: F) -> DecodeResult<T>
2427 F: FnMut(&mut Decoder, usize) -> DecodeResult<T>,
2429 self.read_enum_variant(names, f)
2432 fn read_enum_struct_variant_field<T, F>(
2437 ) -> DecodeResult<T>
2439 F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2441 self.read_enum_variant_arg(idx, f)
2444 fn read_struct<T, F>(&mut self, _name: &str, _len: usize, f: F) -> DecodeResult<T>
2446 F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2448 let value = f(self)?;
2453 fn read_struct_field<T, F>(&mut self, name: &str, _idx: usize, f: F) -> DecodeResult<T>
2455 F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2457 let mut obj = expect!(self.pop(), Object)?;
2459 let value = match obj.remove(&name.to_string()) {
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);
2466 Err(_) => return Err(MissingFieldError(name.to_string())),
2470 self.stack.push(json);
2474 self.stack.push(Json::Object(obj));
2478 fn read_tuple<T, F>(&mut self, tuple_len: usize, f: F) -> DecodeResult<T>
2480 F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2482 self.read_seq(move |d, len| {
2483 if len == tuple_len {
2486 Err(ExpectedError(format!("Tuple{}", tuple_len), format!("Tuple{}", len)))
2491 fn read_tuple_arg<T, F>(&mut self, idx: usize, f: F) -> DecodeResult<T>
2493 F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2495 self.read_seq_elt(idx, f)
2498 fn read_tuple_struct<T, F>(&mut self, _name: &str, len: usize, f: F) -> DecodeResult<T>
2500 F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2502 self.read_tuple(len, f)
2505 fn read_tuple_struct_arg<T, F>(&mut self, idx: usize, f: F) -> DecodeResult<T>
2507 F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2509 self.read_tuple_arg(idx, f)
2512 fn read_option<T, F>(&mut self, mut f: F) -> DecodeResult<T>
2514 F: FnMut(&mut Decoder, bool) -> DecodeResult<T>,
2517 Json::Null => f(self, false),
2519 self.stack.push(value);
2525 fn read_seq<T, F>(&mut self, f: F) -> DecodeResult<T>
2527 F: FnOnce(&mut Decoder, usize) -> DecodeResult<T>,
2529 let array = expect!(self.pop(), Array)?;
2530 let len = array.len();
2531 self.stack.extend(array.into_iter().rev());
2535 fn read_seq_elt<T, F>(&mut self, _idx: usize, f: F) -> DecodeResult<T>
2537 F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2542 fn read_map<T, F>(&mut self, f: F) -> DecodeResult<T>
2544 F: FnOnce(&mut Decoder, usize) -> DecodeResult<T>,
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));
2555 fn read_map_elt_key<T, F>(&mut self, _idx: usize, f: F) -> DecodeResult<T>
2557 F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2562 fn read_map_elt_val<T, F>(&mut self, _idx: usize, f: F) -> DecodeResult<T>
2564 F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2569 fn error(&mut self, err: &str) -> DecoderError {
2570 ApplicationError(err.to_string())
2574 /// A trait for converting values to JSON
2576 /// Converts the value of `self` to an instance of JSON
2577 fn to_json(&self) -> Json;
2580 macro_rules! to_json_impl_i64 {
2582 $(impl ToJson for $t {
2583 fn to_json(&self) -> Json {
2584 Json::I64(*self as i64)
2590 to_json_impl_i64! { isize, i8, i16, i32, i64 }
2592 macro_rules! to_json_impl_u64 {
2594 $(impl ToJson for $t {
2595 fn to_json(&self) -> Json {
2596 Json::U64(*self as u64)
2602 to_json_impl_u64! { usize, u8, u16, u32, u64 }
2604 impl ToJson for Json {
2605 fn to_json(&self) -> Json {
2610 impl ToJson for f32 {
2611 fn to_json(&self) -> Json {
2612 f64::from(*self).to_json()
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),
2625 impl ToJson for () {
2626 fn to_json(&self) -> Json {
2631 impl ToJson for bool {
2632 fn to_json(&self) -> Json {
2633 Json::Boolean(*self)
2637 impl ToJson for str {
2638 fn to_json(&self) -> Json {
2639 Json::String(self.to_string())
2643 impl ToJson for string::String {
2644 fn to_json(&self) -> Json {
2645 Json::String((*self).clone())
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
2654 $( $tyvar : ToJson ),*
2655 > ToJson for ( $( $tyvar ),* , ) {
2658 #[allow(non_snake_case)]
2659 fn to_json(&self) -> Json {
2661 ($(ref $tyvar),*,) => Json::Array(vec![$($tyvar.to_json()),*])
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}
2681 impl<A: ToJson> ToJson for [A] {
2682 fn to_json(&self) -> Json {
2683 Json::Array(self.iter().map(|elt| elt.to_json()).collect())
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())
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());
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());
2713 impl<A: ToJson> ToJson for Option<A> {
2714 fn to_json(&self) -> Json {
2717 Some(ref value) => value.to_json(),
2722 struct FormatShim<'a, 'b> {
2723 inner: &'a mut fmt::Formatter<'b>,
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) {
2730 Err(_) => Err(fmt::Error),
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) {
2742 Err(_) => Err(fmt::Error),
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) {
2754 Err(_) => Err(fmt::Error),
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) {
2766 Err(_) => Err(fmt::Error),
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);
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);
2787 match self.inner.encode(&mut encoder) {
2789 Err(_) => Err(fmt::Error),
2794 impl FromStr for Json {
2795 type Err = BuilderError;
2796 fn from_str(s: &str) -> Result<Json, BuilderError> {