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 {}
350 impl fmt::Display for EncoderError {
351 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
352 // FIXME this should be a nicer error
353 fmt::Debug::fmt(self, f)
357 impl std::error::Error for EncoderError {}
359 impl From<fmt::Error> for EncoderError {
360 /// Converts a [`fmt::Error`] into `EncoderError`
362 /// This conversion does not allocate memory.
363 fn from(err: fmt::Error) -> EncoderError {
364 EncoderError::FmtError(err)
368 pub type EncodeResult = Result<(), EncoderError>;
369 pub type DecodeResult<T> = Result<T, DecoderError>;
371 fn escape_str(wr: &mut dyn fmt::Write, v: &str) -> EncodeResult {
376 for (i, byte) in v.bytes().enumerate() {
377 let escaped = match byte {
380 b'\x00' => "\\u0000",
381 b'\x01' => "\\u0001",
382 b'\x02' => "\\u0002",
383 b'\x03' => "\\u0003",
384 b'\x04' => "\\u0004",
385 b'\x05' => "\\u0005",
386 b'\x06' => "\\u0006",
387 b'\x07' => "\\u0007",
391 b'\x0b' => "\\u000b",
394 b'\x0e' => "\\u000e",
395 b'\x0f' => "\\u000f",
396 b'\x10' => "\\u0010",
397 b'\x11' => "\\u0011",
398 b'\x12' => "\\u0012",
399 b'\x13' => "\\u0013",
400 b'\x14' => "\\u0014",
401 b'\x15' => "\\u0015",
402 b'\x16' => "\\u0016",
403 b'\x17' => "\\u0017",
404 b'\x18' => "\\u0018",
405 b'\x19' => "\\u0019",
406 b'\x1a' => "\\u001a",
407 b'\x1b' => "\\u001b",
408 b'\x1c' => "\\u001c",
409 b'\x1d' => "\\u001d",
410 b'\x1e' => "\\u001e",
411 b'\x1f' => "\\u001f",
412 b'\x7f' => "\\u007f",
419 wr.write_str(&v[start..i])?;
422 wr.write_str(escaped)?;
427 if start != v.len() {
428 wr.write_str(&v[start..])?;
435 fn escape_char(writer: &mut dyn fmt::Write, v: char) -> EncodeResult {
436 escape_str(writer, v.encode_utf8(&mut [0; 4]))
439 fn spaces(wr: &mut dyn fmt::Write, mut n: usize) -> EncodeResult {
440 const BUF: &str = " ";
442 while n >= BUF.len() {
448 wr.write_str(&BUF[..n])?;
453 fn fmt_number_or_null(v: f64) -> string::String {
455 Fp::Nan | Fp::Infinite => string::String::from("null"),
456 _ if v.fract() != 0f64 => v.to_string(),
457 _ => v.to_string() + ".0",
461 /// A structure for implementing serialization to JSON.
462 pub struct Encoder<'a> {
463 writer: &'a mut (dyn fmt::Write + 'a),
464 is_emitting_map_key: bool,
467 impl<'a> Encoder<'a> {
468 /// Creates a new JSON encoder whose output will be written to the writer
470 pub fn new(writer: &'a mut dyn fmt::Write) -> Encoder<'a> {
471 Encoder { writer, is_emitting_map_key: false }
475 macro_rules! emit_enquoted_if_mapkey {
476 ($enc:ident,$e:expr) => {{
477 if $enc.is_emitting_map_key {
478 write!($enc.writer, "\"{}\"", $e)?;
480 write!($enc.writer, "{}", $e)?;
486 impl<'a> crate::Encoder for Encoder<'a> {
487 type Error = EncoderError;
489 fn emit_unit(&mut self) -> EncodeResult {
490 if self.is_emitting_map_key {
491 return Err(EncoderError::BadHashmapKey);
493 write!(self.writer, "null")?;
497 fn emit_usize(&mut self, v: usize) -> EncodeResult {
498 emit_enquoted_if_mapkey!(self, v)
500 fn emit_u128(&mut self, v: u128) -> EncodeResult {
501 emit_enquoted_if_mapkey!(self, v)
503 fn emit_u64(&mut self, v: u64) -> EncodeResult {
504 emit_enquoted_if_mapkey!(self, v)
506 fn emit_u32(&mut self, v: u32) -> EncodeResult {
507 emit_enquoted_if_mapkey!(self, v)
509 fn emit_u16(&mut self, v: u16) -> EncodeResult {
510 emit_enquoted_if_mapkey!(self, v)
512 fn emit_u8(&mut self, v: u8) -> EncodeResult {
513 emit_enquoted_if_mapkey!(self, v)
516 fn emit_isize(&mut self, v: isize) -> EncodeResult {
517 emit_enquoted_if_mapkey!(self, v)
519 fn emit_i128(&mut self, v: i128) -> EncodeResult {
520 emit_enquoted_if_mapkey!(self, v)
522 fn emit_i64(&mut self, v: i64) -> EncodeResult {
523 emit_enquoted_if_mapkey!(self, v)
525 fn emit_i32(&mut self, v: i32) -> EncodeResult {
526 emit_enquoted_if_mapkey!(self, v)
528 fn emit_i16(&mut self, v: i16) -> EncodeResult {
529 emit_enquoted_if_mapkey!(self, v)
531 fn emit_i8(&mut self, v: i8) -> EncodeResult {
532 emit_enquoted_if_mapkey!(self, v)
535 fn emit_bool(&mut self, v: bool) -> EncodeResult {
536 if self.is_emitting_map_key {
537 return Err(EncoderError::BadHashmapKey);
540 write!(self.writer, "true")?;
542 write!(self.writer, "false")?;
547 fn emit_f64(&mut self, v: f64) -> EncodeResult {
548 emit_enquoted_if_mapkey!(self, fmt_number_or_null(v))
550 fn emit_f32(&mut self, v: f32) -> EncodeResult {
551 self.emit_f64(f64::from(v))
554 fn emit_char(&mut self, v: char) -> EncodeResult {
555 escape_char(self.writer, v)
557 fn emit_str(&mut self, v: &str) -> EncodeResult {
558 escape_str(self.writer, v)
561 fn emit_enum<F>(&mut self, _name: &str, f: F) -> EncodeResult
563 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
568 fn emit_enum_variant<F>(&mut self, name: &str, _id: usize, cnt: usize, f: F) -> EncodeResult
570 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
572 // enums are encoded as strings or objects
574 // Kangaroo(34,"William") => {"variant": "Kangaroo", "fields": [34,"William"]}
576 escape_str(self.writer, name)
578 if self.is_emitting_map_key {
579 return Err(EncoderError::BadHashmapKey);
581 write!(self.writer, "{{\"variant\":")?;
582 escape_str(self.writer, name)?;
583 write!(self.writer, ",\"fields\":[")?;
585 write!(self.writer, "]}}")?;
590 fn emit_enum_variant_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult
592 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
594 if self.is_emitting_map_key {
595 return Err(EncoderError::BadHashmapKey);
598 write!(self.writer, ",")?;
603 fn emit_enum_struct_variant<F>(
611 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
613 if self.is_emitting_map_key {
614 return Err(EncoderError::BadHashmapKey);
616 self.emit_enum_variant(name, id, cnt, f)
619 fn emit_enum_struct_variant_field<F>(&mut self, _: &str, idx: usize, f: F) -> EncodeResult
621 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
623 if self.is_emitting_map_key {
624 return Err(EncoderError::BadHashmapKey);
626 self.emit_enum_variant_arg(idx, f)
629 fn emit_struct<F>(&mut self, _: &str, _: usize, f: F) -> EncodeResult
631 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
633 if self.is_emitting_map_key {
634 return Err(EncoderError::BadHashmapKey);
636 write!(self.writer, "{{")?;
638 write!(self.writer, "}}")?;
642 fn emit_struct_field<F>(&mut self, name: &str, idx: usize, f: F) -> EncodeResult
644 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
646 if self.is_emitting_map_key {
647 return Err(EncoderError::BadHashmapKey);
650 write!(self.writer, ",")?;
652 escape_str(self.writer, name)?;
653 write!(self.writer, ":")?;
657 fn emit_tuple<F>(&mut self, len: usize, f: F) -> EncodeResult
659 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
661 if self.is_emitting_map_key {
662 return Err(EncoderError::BadHashmapKey);
664 self.emit_seq(len, f)
666 fn emit_tuple_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult
668 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
670 if self.is_emitting_map_key {
671 return Err(EncoderError::BadHashmapKey);
673 self.emit_seq_elt(idx, f)
676 fn emit_tuple_struct<F>(&mut self, _name: &str, len: usize, f: F) -> EncodeResult
678 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
680 if self.is_emitting_map_key {
681 return Err(EncoderError::BadHashmapKey);
683 self.emit_seq(len, f)
685 fn emit_tuple_struct_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult
687 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
689 if self.is_emitting_map_key {
690 return Err(EncoderError::BadHashmapKey);
692 self.emit_seq_elt(idx, f)
695 fn emit_option<F>(&mut self, f: F) -> EncodeResult
697 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
699 if self.is_emitting_map_key {
700 return Err(EncoderError::BadHashmapKey);
704 fn emit_option_none(&mut self) -> EncodeResult {
705 if self.is_emitting_map_key {
706 return Err(EncoderError::BadHashmapKey);
710 fn emit_option_some<F>(&mut self, f: F) -> EncodeResult
712 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
714 if self.is_emitting_map_key {
715 return Err(EncoderError::BadHashmapKey);
720 fn emit_seq<F>(&mut self, _len: usize, f: F) -> EncodeResult
722 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
724 if self.is_emitting_map_key {
725 return Err(EncoderError::BadHashmapKey);
727 write!(self.writer, "[")?;
729 write!(self.writer, "]")?;
733 fn emit_seq_elt<F>(&mut self, idx: usize, f: F) -> EncodeResult
735 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
737 if self.is_emitting_map_key {
738 return Err(EncoderError::BadHashmapKey);
741 write!(self.writer, ",")?;
746 fn emit_map<F>(&mut self, _len: usize, f: F) -> EncodeResult
748 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
750 if self.is_emitting_map_key {
751 return Err(EncoderError::BadHashmapKey);
753 write!(self.writer, "{{")?;
755 write!(self.writer, "}}")?;
759 fn emit_map_elt_key<F>(&mut self, idx: usize, f: F) -> EncodeResult
761 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
763 if self.is_emitting_map_key {
764 return Err(EncoderError::BadHashmapKey);
767 write!(self.writer, ",")?
769 self.is_emitting_map_key = true;
771 self.is_emitting_map_key = false;
775 fn emit_map_elt_val<F>(&mut self, _idx: usize, f: F) -> EncodeResult
777 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
779 if self.is_emitting_map_key {
780 return Err(EncoderError::BadHashmapKey);
782 write!(self.writer, ":")?;
787 /// Another encoder for JSON, but prints out human-readable JSON instead of
789 pub struct PrettyEncoder<'a> {
790 writer: &'a mut (dyn fmt::Write + 'a),
793 is_emitting_map_key: bool,
796 impl<'a> PrettyEncoder<'a> {
797 /// Creates a new encoder whose output will be written to the specified writer
798 pub fn new(writer: &'a mut dyn fmt::Write) -> PrettyEncoder<'a> {
799 PrettyEncoder { writer, curr_indent: 0, indent: 2, is_emitting_map_key: false }
802 /// Sets the number of spaces to indent for each level.
803 /// This is safe to set during encoding.
804 pub fn set_indent(&mut self, indent: usize) {
805 // self.indent very well could be 0 so we need to use checked division.
806 let level = self.curr_indent.checked_div(self.indent).unwrap_or(0);
807 self.indent = indent;
808 self.curr_indent = level * self.indent;
812 impl<'a> crate::Encoder for PrettyEncoder<'a> {
813 type Error = EncoderError;
815 fn emit_unit(&mut self) -> EncodeResult {
816 if self.is_emitting_map_key {
817 return Err(EncoderError::BadHashmapKey);
819 write!(self.writer, "null")?;
823 fn emit_usize(&mut self, v: usize) -> EncodeResult {
824 emit_enquoted_if_mapkey!(self, v)
826 fn emit_u128(&mut self, v: u128) -> EncodeResult {
827 emit_enquoted_if_mapkey!(self, v)
829 fn emit_u64(&mut self, v: u64) -> EncodeResult {
830 emit_enquoted_if_mapkey!(self, v)
832 fn emit_u32(&mut self, v: u32) -> EncodeResult {
833 emit_enquoted_if_mapkey!(self, v)
835 fn emit_u16(&mut self, v: u16) -> EncodeResult {
836 emit_enquoted_if_mapkey!(self, v)
838 fn emit_u8(&mut self, v: u8) -> EncodeResult {
839 emit_enquoted_if_mapkey!(self, v)
842 fn emit_isize(&mut self, v: isize) -> EncodeResult {
843 emit_enquoted_if_mapkey!(self, v)
845 fn emit_i128(&mut self, v: i128) -> EncodeResult {
846 emit_enquoted_if_mapkey!(self, v)
848 fn emit_i64(&mut self, v: i64) -> EncodeResult {
849 emit_enquoted_if_mapkey!(self, v)
851 fn emit_i32(&mut self, v: i32) -> EncodeResult {
852 emit_enquoted_if_mapkey!(self, v)
854 fn emit_i16(&mut self, v: i16) -> EncodeResult {
855 emit_enquoted_if_mapkey!(self, v)
857 fn emit_i8(&mut self, v: i8) -> EncodeResult {
858 emit_enquoted_if_mapkey!(self, v)
861 fn emit_bool(&mut self, v: bool) -> EncodeResult {
862 if self.is_emitting_map_key {
863 return Err(EncoderError::BadHashmapKey);
866 write!(self.writer, "true")?;
868 write!(self.writer, "false")?;
873 fn emit_f64(&mut self, v: f64) -> EncodeResult {
874 emit_enquoted_if_mapkey!(self, fmt_number_or_null(v))
876 fn emit_f32(&mut self, v: f32) -> EncodeResult {
877 self.emit_f64(f64::from(v))
880 fn emit_char(&mut self, v: char) -> EncodeResult {
881 escape_char(self.writer, v)
883 fn emit_str(&mut self, v: &str) -> EncodeResult {
884 escape_str(self.writer, v)
887 fn emit_enum<F>(&mut self, _name: &str, f: F) -> EncodeResult
889 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
894 fn emit_enum_variant<F>(&mut self, name: &str, _id: usize, cnt: usize, f: F) -> EncodeResult
896 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
899 escape_str(self.writer, name)
901 if self.is_emitting_map_key {
902 return Err(EncoderError::BadHashmapKey);
904 writeln!(self.writer, "{{")?;
905 self.curr_indent += self.indent;
906 spaces(self.writer, self.curr_indent)?;
907 write!(self.writer, "\"variant\": ")?;
908 escape_str(self.writer, name)?;
909 writeln!(self.writer, ",")?;
910 spaces(self.writer, self.curr_indent)?;
911 writeln!(self.writer, "\"fields\": [")?;
912 self.curr_indent += self.indent;
914 self.curr_indent -= self.indent;
915 writeln!(self.writer)?;
916 spaces(self.writer, self.curr_indent)?;
917 self.curr_indent -= self.indent;
918 writeln!(self.writer, "]")?;
919 spaces(self.writer, self.curr_indent)?;
920 write!(self.writer, "}}")?;
925 fn emit_enum_variant_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult
927 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
929 if self.is_emitting_map_key {
930 return Err(EncoderError::BadHashmapKey);
933 writeln!(self.writer, ",")?;
935 spaces(self.writer, self.curr_indent)?;
939 fn emit_enum_struct_variant<F>(
947 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
949 if self.is_emitting_map_key {
950 return Err(EncoderError::BadHashmapKey);
952 self.emit_enum_variant(name, id, cnt, f)
955 fn emit_enum_struct_variant_field<F>(&mut self, _: &str, idx: usize, f: F) -> EncodeResult
957 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
959 if self.is_emitting_map_key {
960 return Err(EncoderError::BadHashmapKey);
962 self.emit_enum_variant_arg(idx, f)
965 fn emit_struct<F>(&mut self, _: &str, len: usize, f: F) -> EncodeResult
967 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
969 if self.is_emitting_map_key {
970 return Err(EncoderError::BadHashmapKey);
973 write!(self.writer, "{{}}")?;
975 write!(self.writer, "{{")?;
976 self.curr_indent += self.indent;
978 self.curr_indent -= self.indent;
979 writeln!(self.writer)?;
980 spaces(self.writer, self.curr_indent)?;
981 write!(self.writer, "}}")?;
986 fn emit_struct_field<F>(&mut self, name: &str, idx: usize, f: F) -> EncodeResult
988 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
990 if self.is_emitting_map_key {
991 return Err(EncoderError::BadHashmapKey);
994 writeln!(self.writer)?;
996 writeln!(self.writer, ",")?;
998 spaces(self.writer, self.curr_indent)?;
999 escape_str(self.writer, name)?;
1000 write!(self.writer, ": ")?;
1004 fn emit_tuple<F>(&mut self, len: usize, f: F) -> EncodeResult
1006 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
1008 if self.is_emitting_map_key {
1009 return Err(EncoderError::BadHashmapKey);
1011 self.emit_seq(len, f)
1013 fn emit_tuple_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult
1015 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
1017 if self.is_emitting_map_key {
1018 return Err(EncoderError::BadHashmapKey);
1020 self.emit_seq_elt(idx, f)
1023 fn emit_tuple_struct<F>(&mut self, _: &str, len: usize, f: F) -> EncodeResult
1025 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
1027 if self.is_emitting_map_key {
1028 return Err(EncoderError::BadHashmapKey);
1030 self.emit_seq(len, f)
1032 fn emit_tuple_struct_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult
1034 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
1036 if self.is_emitting_map_key {
1037 return Err(EncoderError::BadHashmapKey);
1039 self.emit_seq_elt(idx, f)
1042 fn emit_option<F>(&mut self, f: F) -> EncodeResult
1044 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
1046 if self.is_emitting_map_key {
1047 return Err(EncoderError::BadHashmapKey);
1051 fn emit_option_none(&mut self) -> EncodeResult {
1052 if self.is_emitting_map_key {
1053 return Err(EncoderError::BadHashmapKey);
1057 fn emit_option_some<F>(&mut self, f: F) -> EncodeResult
1059 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
1061 if self.is_emitting_map_key {
1062 return Err(EncoderError::BadHashmapKey);
1067 fn emit_seq<F>(&mut self, len: usize, f: F) -> EncodeResult
1069 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
1071 if self.is_emitting_map_key {
1072 return Err(EncoderError::BadHashmapKey);
1075 write!(self.writer, "[]")?;
1077 write!(self.writer, "[")?;
1078 self.curr_indent += self.indent;
1080 self.curr_indent -= self.indent;
1081 writeln!(self.writer)?;
1082 spaces(self.writer, self.curr_indent)?;
1083 write!(self.writer, "]")?;
1088 fn emit_seq_elt<F>(&mut self, idx: usize, f: F) -> EncodeResult
1090 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
1092 if self.is_emitting_map_key {
1093 return Err(EncoderError::BadHashmapKey);
1096 writeln!(self.writer)?;
1098 writeln!(self.writer, ",")?;
1100 spaces(self.writer, self.curr_indent)?;
1104 fn emit_map<F>(&mut self, len: usize, f: F) -> EncodeResult
1106 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
1108 if self.is_emitting_map_key {
1109 return Err(EncoderError::BadHashmapKey);
1112 write!(self.writer, "{{}}")?;
1114 write!(self.writer, "{{")?;
1115 self.curr_indent += self.indent;
1117 self.curr_indent -= self.indent;
1118 writeln!(self.writer)?;
1119 spaces(self.writer, self.curr_indent)?;
1120 write!(self.writer, "}}")?;
1125 fn emit_map_elt_key<F>(&mut self, idx: usize, f: F) -> EncodeResult
1127 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
1129 if self.is_emitting_map_key {
1130 return Err(EncoderError::BadHashmapKey);
1133 writeln!(self.writer)?;
1135 writeln!(self.writer, ",")?;
1137 spaces(self.writer, self.curr_indent)?;
1138 self.is_emitting_map_key = true;
1140 self.is_emitting_map_key = false;
1144 fn emit_map_elt_val<F>(&mut self, _idx: usize, f: F) -> EncodeResult
1146 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
1148 if self.is_emitting_map_key {
1149 return Err(EncoderError::BadHashmapKey);
1151 write!(self.writer, ": ")?;
1156 impl Encodable for Json {
1157 fn encode<E: crate::Encoder>(&self, e: &mut E) -> Result<(), E::Error> {
1159 Json::I64(v) => v.encode(e),
1160 Json::U64(v) => v.encode(e),
1161 Json::F64(v) => v.encode(e),
1162 Json::String(ref v) => v.encode(e),
1163 Json::Boolean(v) => v.encode(e),
1164 Json::Array(ref v) => v.encode(e),
1165 Json::Object(ref v) => v.encode(e),
1166 Json::Null => e.emit_unit(),
1171 /// Creates an `AsJson` wrapper which can be used to print a value as JSON
1172 /// on-the-fly via `write!`
1173 pub fn as_json<T>(t: &T) -> AsJson<'_, T> {
1177 /// Creates an `AsPrettyJson` wrapper which can be used to print a value as JSON
1178 /// on-the-fly via `write!`
1179 pub fn as_pretty_json<T>(t: &T) -> AsPrettyJson<'_, T> {
1180 AsPrettyJson { inner: t, indent: None }
1184 /// Borrow this json object as a pretty object to generate a pretty
1185 /// representation for it via `Display`.
1186 pub fn pretty(&self) -> PrettyJson<'_> {
1187 PrettyJson { inner: self }
1190 /// If the Json value is an Object, returns the value associated with the provided key.
1191 /// Otherwise, returns None.
1192 pub fn find(&self, key: &str) -> Option<&Json> {
1194 Json::Object(ref map) => map.get(key),
1199 /// Attempts to get a nested Json Object for each key in `keys`.
1200 /// If any key is found not to exist, `find_path` will return `None`.
1201 /// Otherwise, it will return the Json value associated with the final key.
1202 pub fn find_path<'a>(&'a self, keys: &[&str]) -> Option<&'a Json> {
1203 let mut target = self;
1205 target = target.find(*key)?;
1210 /// If the Json value is an Object, performs a depth-first search until
1211 /// a value associated with the provided key is found. If no value is found
1212 /// or the Json value is not an Object, returns `None`.
1213 pub fn search(&self, key: &str) -> Option<&Json> {
1215 Json::Object(ref map) => match map.get(key) {
1216 Some(json_value) => Some(json_value),
1218 for v in map.values() {
1219 match v.search(key) {
1220 x if x.is_some() => return x,
1231 /// Returns `true` if the Json value is an `Object`.
1232 pub fn is_object(&self) -> bool {
1233 self.as_object().is_some()
1236 /// If the Json value is an `Object`, returns the associated `BTreeMap`;
1237 /// returns `None` otherwise.
1238 pub fn as_object(&self) -> Option<&Object> {
1240 Json::Object(ref map) => Some(map),
1245 /// Returns `true` if the Json value is an `Array`.
1246 pub fn is_array(&self) -> bool {
1247 self.as_array().is_some()
1250 /// If the Json value is an `Array`, returns the associated vector;
1251 /// returns `None` otherwise.
1252 pub fn as_array(&self) -> Option<&Array> {
1254 Json::Array(ref array) => Some(&*array),
1259 /// Returns `true` if the Json value is a `String`.
1260 pub fn is_string(&self) -> bool {
1261 self.as_string().is_some()
1264 /// If the Json value is a `String`, returns the associated `str`;
1265 /// returns `None` otherwise.
1266 pub fn as_string(&self) -> Option<&str> {
1268 Json::String(ref s) => Some(&s[..]),
1273 /// Returns `true` if the Json value is a `Number`.
1274 pub fn is_number(&self) -> bool {
1276 Json::I64(_) | Json::U64(_) | Json::F64(_) => true,
1281 /// Returns `true` if the Json value is a `i64`.
1282 pub fn is_i64(&self) -> bool {
1284 Json::I64(_) => true,
1289 /// Returns `true` if the Json value is a `u64`.
1290 pub fn is_u64(&self) -> bool {
1292 Json::U64(_) => true,
1297 /// Returns `true` if the Json value is a `f64`.
1298 pub fn is_f64(&self) -> bool {
1300 Json::F64(_) => true,
1305 /// If the Json value is a number, returns or cast it to a `i64`;
1306 /// returns `None` otherwise.
1307 pub fn as_i64(&self) -> Option<i64> {
1309 Json::I64(n) => Some(n),
1310 Json::U64(n) => Some(n as i64),
1315 /// If the Json value is a number, returns or cast it to a `u64`;
1316 /// returns `None` otherwise.
1317 pub fn as_u64(&self) -> Option<u64> {
1319 Json::I64(n) => Some(n as u64),
1320 Json::U64(n) => Some(n),
1325 /// If the Json value is a number, returns or cast it to a `f64`;
1326 /// returns `None` otherwise.
1327 pub fn as_f64(&self) -> Option<f64> {
1329 Json::I64(n) => Some(n as f64),
1330 Json::U64(n) => Some(n as f64),
1331 Json::F64(n) => Some(n),
1336 /// Returns `true` if the Json value is a `Boolean`.
1337 pub fn is_boolean(&self) -> bool {
1338 self.as_boolean().is_some()
1341 /// If the Json value is a `Boolean`, returns the associated `bool`;
1342 /// returns `None` otherwise.
1343 pub fn as_boolean(&self) -> Option<bool> {
1345 Json::Boolean(b) => Some(b),
1350 /// Returns `true` if the Json value is a `Null`.
1351 pub fn is_null(&self) -> bool {
1352 self.as_null().is_some()
1355 /// If the Json value is a `Null`, returns `()`;
1356 /// returns `None` otherwise.
1357 pub fn as_null(&self) -> Option<()> {
1359 Json::Null => Some(()),
1365 impl<'a> Index<&'a str> for Json {
1368 fn index(&self, idx: &'a str) -> &Json {
1369 self.find(idx).unwrap()
1373 impl Index<usize> for Json {
1376 fn index(&self, idx: usize) -> &Json {
1378 Json::Array(ref v) => &v[idx],
1379 _ => panic!("can only index Json with usize if it is an array"),
1384 /// The output of the streaming parser.
1385 #[derive(PartialEq, Clone, Debug)]
1386 pub enum JsonEvent {
1395 StringValue(string::String),
1400 #[derive(PartialEq, Debug)]
1402 // Parse a value in an array, true means first element.
1404 // Parse ',' or ']' after an element in an array.
1406 // Parse a key:value in an object, true means first element.
1408 // Parse ',' or ']' after an element in an object.
1412 // Expecting the stream to end.
1414 // Parsing can't continue.
1418 /// A Stack represents the current position of the parser in the logical
1419 /// structure of the JSON stream.
1420 /// For example foo.bar[3].x
1422 stack: Vec<InternalStackElement>,
1423 str_buffer: Vec<u8>,
1426 /// StackElements compose a Stack.
1427 /// For example, StackElement::Key("foo"), StackElement::Key("bar"),
1428 /// StackElement::Index(3) and StackElement::Key("x") are the
1429 /// StackElements compositing the stack that represents foo.bar[3].x
1430 #[derive(PartialEq, Clone, Debug)]
1431 pub enum StackElement<'l> {
1436 // Internally, Key elements are stored as indices in a buffer to avoid
1437 // allocating a string for every member of an object.
1438 #[derive(PartialEq, Clone, Debug)]
1439 enum InternalStackElement {
1441 InternalKey(u16, u16), // start, size
1445 pub fn new() -> Stack {
1446 Stack { stack: Vec::new(), str_buffer: Vec::new() }
1449 /// Returns The number of elements in the Stack.
1450 pub fn len(&self) -> usize {
1454 /// Returns `true` if the stack is empty.
1455 pub fn is_empty(&self) -> bool {
1456 self.stack.is_empty()
1459 /// Provides access to the StackElement at a given index.
1460 /// lower indices are at the bottom of the stack while higher indices are
1462 pub fn get(&self, idx: usize) -> StackElement<'_> {
1463 match self.stack[idx] {
1464 InternalIndex(i) => StackElement::Index(i),
1465 InternalKey(start, size) => StackElement::Key(
1466 str::from_utf8(&self.str_buffer[start as usize..start as usize + size as usize])
1472 /// Compares this stack with an array of StackElement<'_>s.
1473 pub fn is_equal_to(&self, rhs: &[StackElement<'_>]) -> bool {
1474 if self.stack.len() != rhs.len() {
1477 for (i, r) in rhs.iter().enumerate() {
1478 if self.get(i) != *r {
1485 /// Returns `true` if the bottom-most elements of this stack are the same as
1486 /// the ones passed as parameter.
1487 pub fn starts_with(&self, rhs: &[StackElement<'_>]) -> bool {
1488 if self.stack.len() < rhs.len() {
1491 for (i, r) in rhs.iter().enumerate() {
1492 if self.get(i) != *r {
1499 /// Returns `true` if the top-most elements of this stack are the same as
1500 /// the ones passed as parameter.
1501 pub fn ends_with(&self, rhs: &[StackElement<'_>]) -> bool {
1502 if self.stack.len() < rhs.len() {
1505 let offset = self.stack.len() - rhs.len();
1506 for (i, r) in rhs.iter().enumerate() {
1507 if self.get(i + offset) != *r {
1514 /// Returns the top-most element (if any).
1515 pub fn top(&self) -> Option<StackElement<'_>> {
1516 match self.stack.last() {
1518 Some(&InternalIndex(i)) => Some(StackElement::Index(i)),
1519 Some(&InternalKey(start, size)) => Some(StackElement::Key(
1520 str::from_utf8(&self.str_buffer[start as usize..(start + size) as usize]).unwrap(),
1525 // Used by Parser to insert StackElement::Key elements at the top of the stack.
1526 fn push_key(&mut self, key: string::String) {
1527 self.stack.push(InternalKey(self.str_buffer.len() as u16, key.len() as u16));
1528 self.str_buffer.extend(key.as_bytes());
1531 // Used by Parser to insert StackElement::Index elements at the top of the stack.
1532 fn push_index(&mut self, index: u32) {
1533 self.stack.push(InternalIndex(index));
1536 // Used by Parser to remove the top-most element of the stack.
1538 assert!(!self.is_empty());
1539 match *self.stack.last().unwrap() {
1540 InternalKey(_, sz) => {
1541 let new_size = self.str_buffer.len() - sz as usize;
1542 self.str_buffer.truncate(new_size);
1544 InternalIndex(_) => {}
1549 // Used by Parser to test whether the top-most element is an index.
1550 fn last_is_index(&self) -> bool {
1551 match self.stack.last() {
1552 Some(InternalIndex(_)) => true,
1557 // Used by Parser to increment the index of the top-most element.
1558 fn bump_index(&mut self) {
1559 let len = self.stack.len();
1560 let idx = match *self.stack.last().unwrap() {
1561 InternalIndex(i) => i + 1,
1566 self.stack[len - 1] = InternalIndex(idx);
1570 /// A streaming JSON parser implemented as an iterator of JsonEvent, consuming
1571 /// an iterator of char.
1572 pub struct Parser<T> {
1577 // We maintain a stack representing where we are in the logical structure
1578 // of the JSON stream.
1580 // A state machine is kept to make it possible to interrupt and resume parsing.
1584 impl<T: Iterator<Item = char>> Iterator for Parser<T> {
1585 type Item = JsonEvent;
1587 fn next(&mut self) -> Option<JsonEvent> {
1588 if self.state == ParseFinished {
1592 if self.state == ParseBeforeFinish {
1593 self.parse_whitespace();
1594 // Make sure there is no trailing characters.
1596 self.state = ParseFinished;
1599 return Some(self.error_event(TrailingCharacters));
1607 impl<T: Iterator<Item = char>> Parser<T> {
1608 /// Creates the JSON parser.
1609 pub fn new(rdr: T) -> Parser<T> {
1610 let mut p = Parser {
1615 stack: Stack::new(),
1622 /// Provides access to the current position in the logical structure of the
1624 pub fn stack(&self) -> &Stack {
1628 fn eof(&self) -> bool {
1631 fn ch_or_null(&self) -> char {
1632 self.ch.unwrap_or('\x00')
1634 fn bump(&mut self) {
1635 self.ch = self.rdr.next();
1637 if self.ch_is('\n') {
1645 fn next_char(&mut self) -> Option<char> {
1649 fn ch_is(&self, c: char) -> bool {
1653 fn error<U>(&self, reason: ErrorCode) -> Result<U, ParserError> {
1654 Err(SyntaxError(reason, self.line, self.col))
1657 fn parse_whitespace(&mut self) {
1658 while self.ch_is(' ') || self.ch_is('\n') || self.ch_is('\t') || self.ch_is('\r') {
1663 fn parse_number(&mut self) -> JsonEvent {
1664 let neg = if self.ch_is('-') {
1671 let res = match self.parse_u64() {
1678 if self.ch_is('.') || self.ch_is('e') || self.ch_is('E') {
1679 let mut res = res as f64;
1681 if self.ch_is('.') {
1682 res = match self.parse_decimal(res) {
1690 if self.ch_is('e') || self.ch_is('E') {
1691 res = match self.parse_exponent(res) {
1705 let res = (res as i64).wrapping_neg();
1707 // Make sure we didn't underflow.
1709 Error(SyntaxError(InvalidNumber, self.line, self.col))
1718 fn parse_u64(&mut self) -> Result<u64, ParserError> {
1719 let mut accum = 0u64;
1720 let last_accum = 0; // necessary to detect overflow.
1722 match self.ch_or_null() {
1726 // A leading '0' must be the only digit before the decimal point.
1727 if let '0'..='9' = self.ch_or_null() {
1728 return self.error(InvalidNumber);
1733 match self.ch_or_null() {
1735 accum = accum.wrapping_mul(10);
1736 accum = accum.wrapping_add((c as u64) - ('0' as u64));
1738 // Detect overflow by comparing to the last value.
1739 if accum <= last_accum {
1740 return self.error(InvalidNumber);
1749 _ => return self.error(InvalidNumber),
1755 fn parse_decimal(&mut self, mut res: f64) -> Result<f64, ParserError> {
1758 // Make sure a digit follows the decimal place.
1759 match self.ch_or_null() {
1761 _ => return self.error(InvalidNumber),
1766 match self.ch_or_null() {
1769 res += (((c as isize) - ('0' as isize)) as f64) * dec;
1779 fn parse_exponent(&mut self, mut res: f64) -> Result<f64, ParserError> {
1783 let mut neg_exp = false;
1785 if self.ch_is('+') {
1787 } else if self.ch_is('-') {
1792 // Make sure a digit follows the exponent place.
1793 match self.ch_or_null() {
1795 _ => return self.error(InvalidNumber),
1798 match self.ch_or_null() {
1801 exp += (c as usize) - ('0' as usize);
1809 let exp = 10_f64.powi(exp as i32);
1819 fn decode_hex_escape(&mut self) -> Result<u16, ParserError> {
1822 while i < 4 && !self.eof() {
1824 n = match self.ch_or_null() {
1825 c @ '0'..='9' => n * 16 + ((c as u16) - ('0' as u16)),
1826 'a' | 'A' => n * 16 + 10,
1827 'b' | 'B' => n * 16 + 11,
1828 'c' | 'C' => n * 16 + 12,
1829 'd' | 'D' => n * 16 + 13,
1830 'e' | 'E' => n * 16 + 14,
1831 'f' | 'F' => n * 16 + 15,
1832 _ => return self.error(InvalidEscape),
1838 // Error out if we didn't parse 4 digits.
1840 return self.error(InvalidEscape);
1846 fn parse_str(&mut self) -> Result<string::String, ParserError> {
1847 let mut escape = false;
1848 let mut res = string::String::new();
1853 return self.error(EOFWhileParsingString);
1857 match self.ch_or_null() {
1858 '"' => res.push('"'),
1859 '\\' => res.push('\\'),
1860 '/' => res.push('/'),
1861 'b' => res.push('\x08'),
1862 'f' => res.push('\x0c'),
1863 'n' => res.push('\n'),
1864 'r' => res.push('\r'),
1865 't' => res.push('\t'),
1866 'u' => match self.decode_hex_escape()? {
1867 0xDC00..=0xDFFF => return self.error(LoneLeadingSurrogateInHexEscape),
1869 // Non-BMP characters are encoded as a sequence of
1870 // two hex escapes, representing UTF-16 surrogates.
1871 n1 @ 0xD800..=0xDBFF => {
1872 match (self.next_char(), self.next_char()) {
1873 (Some('\\'), Some('u')) => (),
1874 _ => return self.error(UnexpectedEndOfHexEscape),
1877 let n2 = self.decode_hex_escape()?;
1878 if n2 < 0xDC00 || n2 > 0xDFFF {
1879 return self.error(LoneLeadingSurrogateInHexEscape);
1882 (u32::from(n1 - 0xD800) << 10 | u32::from(n2 - 0xDC00)) + 0x1_0000;
1883 res.push(char::from_u32(c).unwrap());
1886 n => match char::from_u32(u32::from(n)) {
1887 Some(c) => res.push(c),
1888 None => return self.error(InvalidUnicodeCodePoint),
1891 _ => return self.error(InvalidEscape),
1894 } else if self.ch_is('\\') {
1902 Some(c) => res.push(c),
1903 None => unreachable!(),
1909 // Invoked at each iteration, consumes the stream until it has enough
1910 // information to return a JsonEvent.
1911 // Manages an internal state so that parsing can be interrupted and resumed.
1912 // Also keeps track of the position in the logical structure of the json
1913 // stream isize the form of a stack that can be queried by the user using the
1915 fn parse(&mut self) -> JsonEvent {
1917 // The only paths where the loop can spin a new iteration
1918 // are in the cases ParseArrayComma and ParseObjectComma if ','
1919 // is parsed. In these cases the state is set to (respectively)
1920 // ParseArray(false) and ParseObject(false), which always return,
1921 // so there is no risk of getting stuck in an infinite loop.
1922 // All other paths return before the end of the loop's iteration.
1923 self.parse_whitespace();
1927 return self.parse_start();
1929 ParseArray(first) => {
1930 return self.parse_array(first);
1932 ParseArrayComma => {
1933 if let Some(evt) = self.parse_array_comma_or_end() {
1937 ParseObject(first) => {
1938 return self.parse_object(first);
1940 ParseObjectComma => {
1942 if self.ch_is(',') {
1943 self.state = ParseObject(false);
1946 return self.parse_object_end();
1950 return self.error_event(InvalidSyntax);
1956 fn parse_start(&mut self) -> JsonEvent {
1957 let val = self.parse_value();
1958 self.state = match val {
1959 Error(_) => ParseFinished,
1960 ArrayStart => ParseArray(true),
1961 ObjectStart => ParseObject(true),
1962 _ => ParseBeforeFinish,
1967 fn parse_array(&mut self, first: bool) -> JsonEvent {
1968 if self.ch_is(']') {
1970 self.error_event(InvalidSyntax)
1972 self.state = if self.stack.is_empty() {
1974 } else if self.stack.last_is_index() {
1984 self.stack.push_index(0);
1986 let val = self.parse_value();
1987 self.state = match val {
1988 Error(_) => ParseFinished,
1989 ArrayStart => ParseArray(true),
1990 ObjectStart => ParseObject(true),
1991 _ => ParseArrayComma,
1997 fn parse_array_comma_or_end(&mut self) -> Option<JsonEvent> {
1998 if self.ch_is(',') {
1999 self.stack.bump_index();
2000 self.state = ParseArray(false);
2003 } else if self.ch_is(']') {
2005 self.state = if self.stack.is_empty() {
2007 } else if self.stack.last_is_index() {
2014 } else if self.eof() {
2015 Some(self.error_event(EOFWhileParsingArray))
2017 Some(self.error_event(InvalidSyntax))
2021 fn parse_object(&mut self, first: bool) -> JsonEvent {
2022 if self.ch_is('}') {
2024 if self.stack.is_empty() {
2025 return self.error_event(TrailingComma);
2030 self.state = if self.stack.is_empty() {
2032 } else if self.stack.last_is_index() {
2041 return self.error_event(EOFWhileParsingObject);
2043 if !self.ch_is('"') {
2044 return self.error_event(KeyMustBeAString);
2046 let s = match self.parse_str() {
2049 self.state = ParseFinished;
2053 self.parse_whitespace();
2055 return self.error_event(EOFWhileParsingObject);
2056 } else if self.ch_or_null() != ':' {
2057 return self.error_event(ExpectedColon);
2059 self.stack.push_key(s);
2061 self.parse_whitespace();
2063 let val = self.parse_value();
2065 self.state = match val {
2066 Error(_) => ParseFinished,
2067 ArrayStart => ParseArray(true),
2068 ObjectStart => ParseObject(true),
2069 _ => ParseObjectComma,
2074 fn parse_object_end(&mut self) -> JsonEvent {
2075 if self.ch_is('}') {
2076 self.state = if self.stack.is_empty() {
2078 } else if self.stack.last_is_index() {
2085 } else if self.eof() {
2086 self.error_event(EOFWhileParsingObject)
2088 self.error_event(InvalidSyntax)
2092 fn parse_value(&mut self) -> JsonEvent {
2094 return self.error_event(EOFWhileParsingValue);
2096 match self.ch_or_null() {
2097 'n' => self.parse_ident("ull", NullValue),
2098 't' => self.parse_ident("rue", BooleanValue(true)),
2099 'f' => self.parse_ident("alse", BooleanValue(false)),
2100 '0'..='9' | '-' => self.parse_number(),
2101 '"' => match self.parse_str() {
2102 Ok(s) => StringValue(s),
2113 _ => self.error_event(InvalidSyntax),
2117 fn parse_ident(&mut self, ident: &str, value: JsonEvent) -> JsonEvent {
2118 if ident.chars().all(|c| Some(c) == self.next_char()) {
2122 Error(SyntaxError(InvalidSyntax, self.line, self.col))
2126 fn error_event(&mut self, reason: ErrorCode) -> JsonEvent {
2127 self.state = ParseFinished;
2128 Error(SyntaxError(reason, self.line, self.col))
2132 /// A Builder consumes a json::Parser to create a generic Json structure.
2133 pub struct Builder<T> {
2135 token: Option<JsonEvent>,
2138 impl<T: Iterator<Item = char>> Builder<T> {
2139 /// Creates a JSON Builder.
2140 pub fn new(src: T) -> Builder<T> {
2141 Builder { parser: Parser::new(src), token: None }
2144 // Decode a Json value from a Parser.
2145 pub fn build(&mut self) -> Result<Json, BuilderError> {
2147 let result = self.build_value();
2151 Some(Error(ref e)) => {
2152 return Err(e.clone());
2155 panic!("unexpected token {:?}", tok.clone());
2161 fn bump(&mut self) {
2162 self.token = self.parser.next();
2165 fn build_value(&mut self) -> Result<Json, BuilderError> {
2167 Some(NullValue) => Ok(Json::Null),
2168 Some(I64Value(n)) => Ok(Json::I64(n)),
2169 Some(U64Value(n)) => Ok(Json::U64(n)),
2170 Some(F64Value(n)) => Ok(Json::F64(n)),
2171 Some(BooleanValue(b)) => Ok(Json::Boolean(b)),
2172 Some(StringValue(ref mut s)) => {
2173 let mut temp = string::String::new();
2175 Ok(Json::String(temp))
2177 Some(Error(ref e)) => Err(e.clone()),
2178 Some(ArrayStart) => self.build_array(),
2179 Some(ObjectStart) => self.build_object(),
2180 Some(ObjectEnd) => self.parser.error(InvalidSyntax),
2181 Some(ArrayEnd) => self.parser.error(InvalidSyntax),
2182 None => self.parser.error(EOFWhileParsingValue),
2186 fn build_array(&mut self) -> Result<Json, BuilderError> {
2188 let mut values = Vec::new();
2191 if self.token == Some(ArrayEnd) {
2192 return Ok(Json::Array(values.into_iter().collect()));
2194 match self.build_value() {
2195 Ok(v) => values.push(v),
2196 Err(e) => return Err(e),
2202 fn build_object(&mut self) -> Result<Json, BuilderError> {
2205 let mut values = BTreeMap::new();
2209 Some(ObjectEnd) => {
2210 return Ok(Json::Object(values));
2212 Some(Error(ref e)) => {
2213 return Err(e.clone());
2220 let key = match self.parser.stack().top() {
2221 Some(StackElement::Key(k)) => k.to_owned(),
2223 panic!("invalid state");
2226 match self.build_value() {
2228 values.insert(key, value);
2236 self.parser.error(EOFWhileParsingObject)
2240 /// Decodes a json value from an `&mut io::Read`
2241 pub fn from_reader(rdr: &mut dyn Read) -> Result<Json, BuilderError> {
2242 let mut contents = Vec::new();
2243 match rdr.read_to_end(&mut contents) {
2245 Err(e) => return Err(io_error_to_error(e)),
2247 let s = match str::from_utf8(&contents).ok() {
2249 _ => return Err(SyntaxError(NotUtf8, 0, 0)),
2251 let mut builder = Builder::new(s.chars());
2255 /// Decodes a json value from a string
2256 pub fn from_str(s: &str) -> Result<Json, BuilderError> {
2257 let mut builder = Builder::new(s.chars());
2261 /// A structure to decode JSON to values in rust.
2262 pub struct Decoder {
2267 /// Creates a new decoder instance for decoding the specified JSON value.
2268 pub fn new(json: Json) -> Decoder {
2269 Decoder { stack: vec![json] }
2272 fn pop(&mut self) -> Json {
2273 self.stack.pop().unwrap()
2277 macro_rules! expect {
2278 ($e:expr, Null) => {{
2280 Json::Null => Ok(()),
2281 other => Err(ExpectedError("Null".to_owned(), other.to_string())),
2284 ($e:expr, $t:ident) => {{
2286 Json::$t(v) => Ok(v),
2287 other => Err(ExpectedError(stringify!($t).to_owned(), other.to_string())),
2292 macro_rules! read_primitive {
2293 ($name:ident, $ty:ty) => {
2294 fn $name(&mut self) -> DecodeResult<$ty> {
2296 Json::I64(f) => Ok(f as $ty),
2297 Json::U64(f) => Ok(f as $ty),
2298 Json::F64(f) => Err(ExpectedError("Integer".to_owned(), f.to_string())),
2299 // re: #12967.. a type w/ numeric keys (ie HashMap<usize, V> etc)
2300 // is going to have a string here, as per JSON spec.
2301 Json::String(s) => match s.parse().ok() {
2303 None => Err(ExpectedError("Number".to_owned(), s)),
2305 value => Err(ExpectedError("Number".to_owned(), value.to_string())),
2311 impl crate::Decoder for Decoder {
2312 type Error = DecoderError;
2314 fn read_nil(&mut self) -> DecodeResult<()> {
2315 expect!(self.pop(), Null)
2318 read_primitive! { read_usize, usize }
2319 read_primitive! { read_u8, u8 }
2320 read_primitive! { read_u16, u16 }
2321 read_primitive! { read_u32, u32 }
2322 read_primitive! { read_u64, u64 }
2323 read_primitive! { read_u128, u128 }
2324 read_primitive! { read_isize, isize }
2325 read_primitive! { read_i8, i8 }
2326 read_primitive! { read_i16, i16 }
2327 read_primitive! { read_i32, i32 }
2328 read_primitive! { read_i64, i64 }
2329 read_primitive! { read_i128, i128 }
2331 fn read_f32(&mut self) -> DecodeResult<f32> {
2332 self.read_f64().map(|x| x as f32)
2335 fn read_f64(&mut self) -> DecodeResult<f64> {
2337 Json::I64(f) => Ok(f as f64),
2338 Json::U64(f) => Ok(f as f64),
2339 Json::F64(f) => Ok(f),
2340 Json::String(s) => {
2341 // re: #12967.. a type w/ numeric keys (ie HashMap<usize, V> etc)
2342 // is going to have a string here, as per JSON spec.
2343 match s.parse().ok() {
2345 None => Err(ExpectedError("Number".to_owned(), s)),
2348 Json::Null => Ok(f64::NAN),
2349 value => Err(ExpectedError("Number".to_owned(), value.to_string())),
2353 fn read_bool(&mut self) -> DecodeResult<bool> {
2354 expect!(self.pop(), Boolean)
2357 fn read_char(&mut self) -> DecodeResult<char> {
2358 let s = self.read_str()?;
2360 let mut it = s.chars();
2361 if let (Some(c), None) = (it.next(), it.next()) {
2362 // exactly one character
2366 Err(ExpectedError("single character string".to_owned(), s.to_string()))
2369 fn read_str(&mut self) -> DecodeResult<Cow<'_, str>> {
2370 expect!(self.pop(), String).map(Cow::Owned)
2373 fn read_enum<T, F>(&mut self, _name: &str, f: F) -> DecodeResult<T>
2375 F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2380 fn read_enum_variant<T, F>(&mut self, names: &[&str], mut f: F) -> DecodeResult<T>
2382 F: FnMut(&mut Decoder, usize) -> DecodeResult<T>,
2384 let name = match self.pop() {
2385 Json::String(s) => s,
2386 Json::Object(mut o) => {
2387 let n = match o.remove(&"variant".to_owned()) {
2388 Some(Json::String(s)) => s,
2389 Some(val) => return Err(ExpectedError("String".to_owned(), val.to_string())),
2390 None => return Err(MissingFieldError("variant".to_owned())),
2392 match o.remove(&"fields".to_string()) {
2393 Some(Json::Array(l)) => {
2394 self.stack.extend(l.into_iter().rev());
2396 Some(val) => return Err(ExpectedError("Array".to_owned(), val.to_string())),
2397 None => return Err(MissingFieldError("fields".to_owned())),
2401 json => return Err(ExpectedError("String or Object".to_owned(), json.to_string())),
2403 let idx = match names.iter().position(|n| *n == &name[..]) {
2405 None => return Err(UnknownVariantError(name)),
2410 fn read_enum_variant_arg<T, F>(&mut self, _idx: usize, f: F) -> DecodeResult<T>
2412 F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2417 fn read_enum_struct_variant<T, F>(&mut self, names: &[&str], f: F) -> DecodeResult<T>
2419 F: FnMut(&mut Decoder, usize) -> DecodeResult<T>,
2421 self.read_enum_variant(names, f)
2424 fn read_enum_struct_variant_field<T, F>(
2429 ) -> DecodeResult<T>
2431 F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2433 self.read_enum_variant_arg(idx, f)
2436 fn read_struct<T, F>(&mut self, _name: &str, _len: usize, f: F) -> DecodeResult<T>
2438 F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2440 let value = f(self)?;
2445 fn read_struct_field<T, F>(&mut self, name: &str, _idx: usize, f: F) -> DecodeResult<T>
2447 F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2449 let mut obj = expect!(self.pop(), Object)?;
2451 let value = match obj.remove(&name.to_string()) {
2453 // Add a Null and try to parse it as an Option<_>
2454 // to get None as a default value.
2455 self.stack.push(Json::Null);
2458 Err(_) => return Err(MissingFieldError(name.to_string())),
2462 self.stack.push(json);
2466 self.stack.push(Json::Object(obj));
2470 fn read_tuple<T, F>(&mut self, tuple_len: usize, f: F) -> DecodeResult<T>
2472 F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2474 self.read_seq(move |d, len| {
2475 if len == tuple_len {
2478 Err(ExpectedError(format!("Tuple{}", tuple_len), format!("Tuple{}", len)))
2483 fn read_tuple_arg<T, F>(&mut self, idx: usize, f: F) -> DecodeResult<T>
2485 F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2487 self.read_seq_elt(idx, f)
2490 fn read_tuple_struct<T, F>(&mut self, _name: &str, len: usize, f: F) -> DecodeResult<T>
2492 F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2494 self.read_tuple(len, f)
2497 fn read_tuple_struct_arg<T, F>(&mut self, idx: usize, f: F) -> DecodeResult<T>
2499 F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2501 self.read_tuple_arg(idx, f)
2504 fn read_option<T, F>(&mut self, mut f: F) -> DecodeResult<T>
2506 F: FnMut(&mut Decoder, bool) -> DecodeResult<T>,
2509 Json::Null => f(self, false),
2511 self.stack.push(value);
2517 fn read_seq<T, F>(&mut self, f: F) -> DecodeResult<T>
2519 F: FnOnce(&mut Decoder, usize) -> DecodeResult<T>,
2521 let array = expect!(self.pop(), Array)?;
2522 let len = array.len();
2523 self.stack.extend(array.into_iter().rev());
2527 fn read_seq_elt<T, F>(&mut self, _idx: usize, f: F) -> DecodeResult<T>
2529 F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2534 fn read_map<T, F>(&mut self, f: F) -> DecodeResult<T>
2536 F: FnOnce(&mut Decoder, usize) -> DecodeResult<T>,
2538 let obj = expect!(self.pop(), Object)?;
2539 let len = obj.len();
2540 for (key, value) in obj {
2541 self.stack.push(value);
2542 self.stack.push(Json::String(key));
2547 fn read_map_elt_key<T, F>(&mut self, _idx: usize, f: F) -> DecodeResult<T>
2549 F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2554 fn read_map_elt_val<T, F>(&mut self, _idx: usize, f: F) -> DecodeResult<T>
2556 F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2561 fn error(&mut self, err: &str) -> DecoderError {
2562 ApplicationError(err.to_string())
2566 /// A trait for converting values to JSON
2568 /// Converts the value of `self` to an instance of JSON
2569 fn to_json(&self) -> Json;
2572 macro_rules! to_json_impl_i64 {
2574 $(impl ToJson for $t {
2575 fn to_json(&self) -> Json {
2576 Json::I64(*self as i64)
2582 to_json_impl_i64! { isize, i8, i16, i32, i64 }
2584 macro_rules! to_json_impl_u64 {
2586 $(impl ToJson for $t {
2587 fn to_json(&self) -> Json {
2588 Json::U64(*self as u64)
2594 to_json_impl_u64! { usize, u8, u16, u32, u64 }
2596 impl ToJson for Json {
2597 fn to_json(&self) -> Json {
2602 impl ToJson for f32 {
2603 fn to_json(&self) -> Json {
2604 f64::from(*self).to_json()
2608 impl ToJson for f64 {
2609 fn to_json(&self) -> Json {
2610 match self.classify() {
2611 Fp::Nan | Fp::Infinite => Json::Null,
2612 _ => Json::F64(*self),
2617 impl ToJson for () {
2618 fn to_json(&self) -> Json {
2623 impl ToJson for bool {
2624 fn to_json(&self) -> Json {
2625 Json::Boolean(*self)
2629 impl ToJson for str {
2630 fn to_json(&self) -> Json {
2631 Json::String(self.to_string())
2635 impl ToJson for string::String {
2636 fn to_json(&self) -> Json {
2637 Json::String((*self).clone())
2641 macro_rules! tuple_impl {
2642 // use variables to indicate the arity of the tuple
2643 ($($tyvar:ident),* ) => {
2644 // the trailing commas are for the 1 tuple
2646 $( $tyvar : ToJson ),*
2647 > ToJson for ( $( $tyvar ),* , ) {
2650 #[allow(non_snake_case)]
2651 fn to_json(&self) -> Json {
2653 ($(ref $tyvar),*,) => Json::Array(vec![$($tyvar.to_json()),*])
2662 tuple_impl! {A, B, C}
2663 tuple_impl! {A, B, C, D}
2664 tuple_impl! {A, B, C, D, E}
2665 tuple_impl! {A, B, C, D, E, F}
2666 tuple_impl! {A, B, C, D, E, F, G}
2667 tuple_impl! {A, B, C, D, E, F, G, H}
2668 tuple_impl! {A, B, C, D, E, F, G, H, I}
2669 tuple_impl! {A, B, C, D, E, F, G, H, I, J}
2670 tuple_impl! {A, B, C, D, E, F, G, H, I, J, K}
2671 tuple_impl! {A, B, C, D, E, F, G, H, I, J, K, L}
2673 impl<A: ToJson> ToJson for [A] {
2674 fn to_json(&self) -> Json {
2675 Json::Array(self.iter().map(|elt| elt.to_json()).collect())
2679 impl<A: ToJson> ToJson for Vec<A> {
2680 fn to_json(&self) -> Json {
2681 Json::Array(self.iter().map(|elt| elt.to_json()).collect())
2685 impl<A: ToJson> ToJson for BTreeMap<string::String, A> {
2686 fn to_json(&self) -> Json {
2687 let mut d = BTreeMap::new();
2688 for (key, value) in self {
2689 d.insert((*key).clone(), value.to_json());
2695 impl<A: ToJson> ToJson for HashMap<string::String, A> {
2696 fn to_json(&self) -> Json {
2697 let mut d = BTreeMap::new();
2698 for (key, value) in self {
2699 d.insert((*key).clone(), value.to_json());
2705 impl<A: ToJson> ToJson for Option<A> {
2706 fn to_json(&self) -> Json {
2709 Some(ref value) => value.to_json(),
2714 struct FormatShim<'a, 'b> {
2715 inner: &'a mut fmt::Formatter<'b>,
2718 impl<'a, 'b> fmt::Write for FormatShim<'a, 'b> {
2719 fn write_str(&mut self, s: &str) -> fmt::Result {
2720 match self.inner.write_str(s) {
2722 Err(_) => Err(fmt::Error),
2727 impl fmt::Display for Json {
2728 /// Encodes a json value into a string
2729 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2730 let mut shim = FormatShim { inner: f };
2731 let mut encoder = Encoder::new(&mut shim);
2732 match self.encode(&mut encoder) {
2734 Err(_) => Err(fmt::Error),
2739 impl<'a> fmt::Display for PrettyJson<'a> {
2740 /// Encodes a json value into a string
2741 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2742 let mut shim = FormatShim { inner: f };
2743 let mut encoder = PrettyEncoder::new(&mut shim);
2744 match self.inner.encode(&mut encoder) {
2746 Err(_) => Err(fmt::Error),
2751 impl<'a, T: Encodable> fmt::Display for AsJson<'a, T> {
2752 /// Encodes a json value into a string
2753 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2754 let mut shim = FormatShim { inner: f };
2755 let mut encoder = Encoder::new(&mut shim);
2756 match self.inner.encode(&mut encoder) {
2758 Err(_) => Err(fmt::Error),
2763 impl<'a, T> AsPrettyJson<'a, T> {
2764 /// Sets the indentation level for the emitted JSON
2765 pub fn indent(mut self, indent: usize) -> AsPrettyJson<'a, T> {
2766 self.indent = Some(indent);
2771 impl<'a, T: Encodable> fmt::Display for AsPrettyJson<'a, T> {
2772 /// Encodes a json value into a string
2773 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2774 let mut shim = FormatShim { inner: f };
2775 let mut encoder = PrettyEncoder::new(&mut shim);
2776 if let Some(n) = self.indent {
2777 encoder.set_indent(n);
2779 match self.inner.encode(&mut encoder) {
2781 Err(_) => Err(fmt::Error),
2786 impl FromStr for Json {
2787 type Err = BuilderError;
2788 fn from_str(s: &str) -> Result<Json, BuilderError> {