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::Encodable` trait.
51 //! To be able to decode a piece of data, it must implement the `serialize::Decodable` trait.
52 //! The Rust compiler provides an annotation to automatically generate the code for these traits:
53 //! `#[derive(Decodable, Encodable)]`
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 `Encodable` 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 //! use rustc_macros::{Decodable, Encodable};
72 //! use rustc_serialize::json;
74 //! // Automatically generate `Decodable` and `Encodable` trait implementations
75 //! #[derive(Decodable, Encodable)]
76 //! pub struct TestStruct {
79 //! data_vector: Vec<u8>,
82 //! let object = TestStruct {
84 //! data_str: "homura".to_string(),
85 //! data_vector: vec![2,3,4,5],
88 //! // Serialize using `json::encode`
89 //! let encoded = json::encode(&object).unwrap();
91 //! // Deserialize using `json::decode`
92 //! let decoded: TestStruct = json::decode(&encoded[..]).unwrap();
95 //! ## Using the `ToJson` trait
97 //! The examples above use the `ToJson` trait to generate the JSON string, which is required
98 //! for custom mappings.
100 //! ### Simple example of `ToJson` usage
103 //! # #![feature(rustc_private)]
104 //! use rustc_macros::Encodable;
105 //! use rustc_serialize::json::{self, ToJson, Json};
107 //! // A custom data structure
108 //! struct ComplexNum {
113 //! // JSON value representation
114 //! impl ToJson for ComplexNum {
115 //! fn to_json(&self) -> Json {
116 //! Json::String(format!("{}+{}i", self.a, self.b))
120 //! // Only generate `Encodable` trait implementation
121 //! #[derive(Encodable)]
122 //! pub struct ComplexNumRecord {
128 //! let num = ComplexNum { a: 0.0001, b: 12.539 };
129 //! let data: String = json::encode(&ComplexNumRecord{
131 //! dsc: "test".to_string(),
132 //! val: num.to_json(),
134 //! println!("data: {}", data);
135 //! // data: {"uid":1,"dsc":"test","val":"0.0001+12.539i"};
138 //! ### Verbose example of `ToJson` usage
141 //! # #![feature(rustc_private)]
142 //! use rustc_macros::Decodable;
143 //! use std::collections::BTreeMap;
144 //! use rustc_serialize::json::{self, Json, ToJson};
146 //! // Only generate `Decodable` trait implementation
147 //! #[derive(Decodable)]
148 //! pub struct TestStruct {
150 //! data_str: String,
151 //! data_vector: Vec<u8>,
154 //! // Specify encoding method manually
155 //! impl ToJson for TestStruct {
156 //! fn to_json(&self) -> Json {
157 //! let mut d = BTreeMap::new();
158 //! // All standard types implement `to_json()`, so use it
159 //! d.insert("data_int".to_string(), self.data_int.to_json());
160 //! d.insert("data_str".to_string(), self.data_str.to_json());
161 //! d.insert("data_vector".to_string(), self.data_vector.to_json());
166 //! // Serialize using `ToJson`
167 //! let input_data = TestStruct {
169 //! data_str: "madoka".to_string(),
170 //! data_vector: vec![2,3,4,5],
172 //! let json_obj: Json = input_data.to_json();
173 //! let json_str: String = json_obj.to_string();
175 //! // Deserialize like before
176 //! let decoded: TestStruct = json::decode(&json_str).unwrap();
179 use self::DecoderError::*;
180 use self::ErrorCode::*;
181 use self::InternalStackElement::*;
182 use self::JsonEvent::*;
183 use self::ParserError::*;
184 use self::ParserState::*;
186 use std::borrow::Cow;
187 use std::collections::{BTreeMap, HashMap};
189 use std::io::prelude::*;
191 use std::num::FpCategory as Fp;
193 use std::str::FromStr;
195 use std::{char, fmt, str};
197 use crate::Encodable;
199 /// Represents a json value
200 #[derive(Clone, PartialEq, PartialOrd, Debug)]
205 String(string::String),
208 Object(self::Object),
212 pub type Array = Vec<Json>;
213 pub type Object = BTreeMap<string::String, Json>;
215 pub struct PrettyJson<'a> {
219 pub struct AsJson<'a, T> {
222 pub struct AsPrettyJson<'a, T> {
224 indent: Option<usize>,
227 /// The errors that can arise while parsing a JSON stream.
228 #[derive(Clone, Copy, PartialEq, Debug)]
232 EOFWhileParsingObject,
233 EOFWhileParsingArray,
234 EOFWhileParsingValue,
235 EOFWhileParsingString,
241 InvalidUnicodeCodePoint,
242 LoneLeadingSurrogateInHexEscape,
243 UnexpectedEndOfHexEscape,
249 #[derive(Clone, PartialEq, Debug)]
250 pub enum ParserError {
252 SyntaxError(ErrorCode, usize, usize),
253 IoError(io::ErrorKind, String),
256 // Builder and Parser have the same errors.
257 pub type BuilderError = ParserError;
259 #[derive(Clone, PartialEq, Debug)]
260 pub enum DecoderError {
261 ParseError(ParserError),
262 ExpectedError(string::String, string::String),
263 MissingFieldError(string::String),
264 UnknownVariantError(string::String),
265 ApplicationError(string::String),
268 #[derive(Copy, Clone, Debug)]
269 pub enum EncoderError {
270 FmtError(fmt::Error),
274 /// Returns a readable error string for a given error code.
275 pub fn error_str(error: ErrorCode) -> &'static str {
277 InvalidSyntax => "invalid syntax",
278 InvalidNumber => "invalid number",
279 EOFWhileParsingObject => "EOF While parsing object",
280 EOFWhileParsingArray => "EOF While parsing array",
281 EOFWhileParsingValue => "EOF While parsing value",
282 EOFWhileParsingString => "EOF While parsing string",
283 KeyMustBeAString => "key must be a string",
284 ExpectedColon => "expected `:`",
285 TrailingCharacters => "trailing characters",
286 TrailingComma => "trailing comma",
287 InvalidEscape => "invalid escape",
288 UnrecognizedHex => "invalid \\u{ esc}ape (unrecognized hex)",
289 NotFourDigit => "invalid \\u{ esc}ape (not four digits)",
290 NotUtf8 => "contents not utf-8",
291 InvalidUnicodeCodePoint => "invalid Unicode code point",
292 LoneLeadingSurrogateInHexEscape => "lone leading surrogate in hex escape",
293 UnexpectedEndOfHexEscape => "unexpected end of hex escape",
297 /// Shortcut function to decode a JSON `&str` into an object
298 pub fn decode<T: crate::Decodable<Decoder>>(s: &str) -> DecodeResult<T> {
299 let json = match from_str(s) {
301 Err(e) => return Err(ParseError(e)),
304 let mut decoder = Decoder::new(json);
305 crate::Decodable::decode(&mut decoder)
308 /// Shortcut function to encode a `T` into a JSON `String`
309 pub fn encode<T: for<'r> crate::Encodable<Encoder<'r>>>(
311 ) -> Result<string::String, EncoderError> {
312 let mut s = String::new();
314 let mut encoder = Encoder::new(&mut s);
315 object.encode(&mut encoder)?;
320 impl fmt::Display for ErrorCode {
321 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
322 error_str(*self).fmt(f)
326 fn io_error_to_error(io: io::Error) -> ParserError {
327 IoError(io.kind(), io.to_string())
330 impl fmt::Display for ParserError {
331 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
332 // FIXME this should be a nicer error
333 fmt::Debug::fmt(self, f)
337 impl fmt::Display for DecoderError {
338 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
339 // FIXME this should be a nicer error
340 fmt::Debug::fmt(self, f)
344 impl std::error::Error for DecoderError {}
346 impl fmt::Display for EncoderError {
347 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
348 // FIXME this should be a nicer error
349 fmt::Debug::fmt(self, f)
353 impl std::error::Error for EncoderError {}
355 impl From<fmt::Error> for EncoderError {
356 /// Converts a [`fmt::Error`] into `EncoderError`
358 /// This conversion does not allocate memory.
359 fn from(err: fmt::Error) -> EncoderError {
360 EncoderError::FmtError(err)
364 pub type EncodeResult = Result<(), EncoderError>;
365 pub type DecodeResult<T> = Result<T, DecoderError>;
367 fn escape_str(wr: &mut dyn fmt::Write, v: &str) -> EncodeResult {
372 for (i, byte) in v.bytes().enumerate() {
373 let escaped = match byte {
376 b'\x00' => "\\u0000",
377 b'\x01' => "\\u0001",
378 b'\x02' => "\\u0002",
379 b'\x03' => "\\u0003",
380 b'\x04' => "\\u0004",
381 b'\x05' => "\\u0005",
382 b'\x06' => "\\u0006",
383 b'\x07' => "\\u0007",
387 b'\x0b' => "\\u000b",
390 b'\x0e' => "\\u000e",
391 b'\x0f' => "\\u000f",
392 b'\x10' => "\\u0010",
393 b'\x11' => "\\u0011",
394 b'\x12' => "\\u0012",
395 b'\x13' => "\\u0013",
396 b'\x14' => "\\u0014",
397 b'\x15' => "\\u0015",
398 b'\x16' => "\\u0016",
399 b'\x17' => "\\u0017",
400 b'\x18' => "\\u0018",
401 b'\x19' => "\\u0019",
402 b'\x1a' => "\\u001a",
403 b'\x1b' => "\\u001b",
404 b'\x1c' => "\\u001c",
405 b'\x1d' => "\\u001d",
406 b'\x1e' => "\\u001e",
407 b'\x1f' => "\\u001f",
408 b'\x7f' => "\\u007f",
415 wr.write_str(&v[start..i])?;
418 wr.write_str(escaped)?;
423 if start != v.len() {
424 wr.write_str(&v[start..])?;
431 fn escape_char(writer: &mut dyn fmt::Write, v: char) -> EncodeResult {
432 escape_str(writer, v.encode_utf8(&mut [0; 4]))
435 fn spaces(wr: &mut dyn fmt::Write, mut n: usize) -> EncodeResult {
436 const BUF: &str = " ";
438 while n >= BUF.len() {
444 wr.write_str(&BUF[..n])?;
449 fn fmt_number_or_null(v: f64) -> string::String {
451 Fp::Nan | Fp::Infinite => string::String::from("null"),
452 _ if v.fract() != 0f64 => v.to_string(),
453 _ => v.to_string() + ".0",
457 /// A structure for implementing serialization to JSON.
458 pub struct Encoder<'a> {
459 writer: &'a mut (dyn fmt::Write + 'a),
460 is_emitting_map_key: bool,
463 impl<'a> Encoder<'a> {
464 /// Creates a new JSON encoder whose output will be written to the writer
466 pub fn new(writer: &'a mut dyn fmt::Write) -> Encoder<'a> {
467 Encoder { writer, is_emitting_map_key: false }
471 macro_rules! emit_enquoted_if_mapkey {
472 ($enc:ident,$e:expr) => {{
473 if $enc.is_emitting_map_key {
474 write!($enc.writer, "\"{}\"", $e)?;
476 write!($enc.writer, "{}", $e)?;
482 impl<'a> crate::Encoder for Encoder<'a> {
483 type Error = EncoderError;
485 fn emit_unit(&mut self) -> EncodeResult {
486 if self.is_emitting_map_key {
487 return Err(EncoderError::BadHashmapKey);
489 write!(self.writer, "null")?;
493 fn emit_usize(&mut self, v: usize) -> EncodeResult {
494 emit_enquoted_if_mapkey!(self, v)
496 fn emit_u128(&mut self, v: u128) -> EncodeResult {
497 emit_enquoted_if_mapkey!(self, v)
499 fn emit_u64(&mut self, v: u64) -> EncodeResult {
500 emit_enquoted_if_mapkey!(self, v)
502 fn emit_u32(&mut self, v: u32) -> EncodeResult {
503 emit_enquoted_if_mapkey!(self, v)
505 fn emit_u16(&mut self, v: u16) -> EncodeResult {
506 emit_enquoted_if_mapkey!(self, v)
508 fn emit_u8(&mut self, v: u8) -> EncodeResult {
509 emit_enquoted_if_mapkey!(self, v)
512 fn emit_isize(&mut self, v: isize) -> EncodeResult {
513 emit_enquoted_if_mapkey!(self, v)
515 fn emit_i128(&mut self, v: i128) -> EncodeResult {
516 emit_enquoted_if_mapkey!(self, v)
518 fn emit_i64(&mut self, v: i64) -> EncodeResult {
519 emit_enquoted_if_mapkey!(self, v)
521 fn emit_i32(&mut self, v: i32) -> EncodeResult {
522 emit_enquoted_if_mapkey!(self, v)
524 fn emit_i16(&mut self, v: i16) -> EncodeResult {
525 emit_enquoted_if_mapkey!(self, v)
527 fn emit_i8(&mut self, v: i8) -> EncodeResult {
528 emit_enquoted_if_mapkey!(self, v)
531 fn emit_bool(&mut self, v: bool) -> EncodeResult {
532 if self.is_emitting_map_key {
533 return Err(EncoderError::BadHashmapKey);
536 write!(self.writer, "true")?;
538 write!(self.writer, "false")?;
543 fn emit_f64(&mut self, v: f64) -> EncodeResult {
544 emit_enquoted_if_mapkey!(self, fmt_number_or_null(v))
546 fn emit_f32(&mut self, v: f32) -> EncodeResult {
547 self.emit_f64(f64::from(v))
550 fn emit_char(&mut self, v: char) -> EncodeResult {
551 escape_char(self.writer, v)
553 fn emit_str(&mut self, v: &str) -> EncodeResult {
554 escape_str(self.writer, v)
557 fn emit_enum<F>(&mut self, _name: &str, f: F) -> EncodeResult
559 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
564 fn emit_enum_variant<F>(&mut self, name: &str, _id: usize, cnt: usize, f: F) -> EncodeResult
566 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
568 // enums are encoded as strings or objects
570 // Kangaroo(34,"William") => {"variant": "Kangaroo", "fields": [34,"William"]}
572 escape_str(self.writer, name)
574 if self.is_emitting_map_key {
575 return Err(EncoderError::BadHashmapKey);
577 write!(self.writer, "{{\"variant\":")?;
578 escape_str(self.writer, name)?;
579 write!(self.writer, ",\"fields\":[")?;
581 write!(self.writer, "]}}")?;
586 fn emit_enum_variant_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult
588 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
590 if self.is_emitting_map_key {
591 return Err(EncoderError::BadHashmapKey);
594 write!(self.writer, ",")?;
599 fn emit_enum_struct_variant<F>(
607 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
609 if self.is_emitting_map_key {
610 return Err(EncoderError::BadHashmapKey);
612 self.emit_enum_variant(name, id, cnt, f)
615 fn emit_enum_struct_variant_field<F>(&mut self, _: &str, idx: usize, f: F) -> EncodeResult
617 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
619 if self.is_emitting_map_key {
620 return Err(EncoderError::BadHashmapKey);
622 self.emit_enum_variant_arg(idx, f)
625 fn emit_struct<F>(&mut self, _: &str, _: usize, f: F) -> EncodeResult
627 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
629 if self.is_emitting_map_key {
630 return Err(EncoderError::BadHashmapKey);
632 write!(self.writer, "{{")?;
634 write!(self.writer, "}}")?;
638 fn emit_struct_field<F>(&mut self, name: &str, idx: usize, f: F) -> EncodeResult
640 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
642 if self.is_emitting_map_key {
643 return Err(EncoderError::BadHashmapKey);
646 write!(self.writer, ",")?;
648 escape_str(self.writer, name)?;
649 write!(self.writer, ":")?;
653 fn emit_tuple<F>(&mut self, len: usize, f: F) -> EncodeResult
655 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
657 if self.is_emitting_map_key {
658 return Err(EncoderError::BadHashmapKey);
660 self.emit_seq(len, f)
662 fn emit_tuple_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult
664 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
666 if self.is_emitting_map_key {
667 return Err(EncoderError::BadHashmapKey);
669 self.emit_seq_elt(idx, f)
672 fn emit_tuple_struct<F>(&mut self, _name: &str, len: usize, f: F) -> EncodeResult
674 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
676 if self.is_emitting_map_key {
677 return Err(EncoderError::BadHashmapKey);
679 self.emit_seq(len, f)
681 fn emit_tuple_struct_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult
683 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
685 if self.is_emitting_map_key {
686 return Err(EncoderError::BadHashmapKey);
688 self.emit_seq_elt(idx, f)
691 fn emit_option<F>(&mut self, f: F) -> EncodeResult
693 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
695 if self.is_emitting_map_key {
696 return Err(EncoderError::BadHashmapKey);
700 fn emit_option_none(&mut self) -> EncodeResult {
701 if self.is_emitting_map_key {
702 return Err(EncoderError::BadHashmapKey);
706 fn emit_option_some<F>(&mut self, f: F) -> EncodeResult
708 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
710 if self.is_emitting_map_key {
711 return Err(EncoderError::BadHashmapKey);
716 fn emit_seq<F>(&mut self, _len: usize, f: F) -> EncodeResult
718 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
720 if self.is_emitting_map_key {
721 return Err(EncoderError::BadHashmapKey);
723 write!(self.writer, "[")?;
725 write!(self.writer, "]")?;
729 fn emit_seq_elt<F>(&mut self, idx: usize, f: F) -> EncodeResult
731 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
733 if self.is_emitting_map_key {
734 return Err(EncoderError::BadHashmapKey);
737 write!(self.writer, ",")?;
742 fn emit_map<F>(&mut self, _len: usize, f: F) -> EncodeResult
744 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
746 if self.is_emitting_map_key {
747 return Err(EncoderError::BadHashmapKey);
749 write!(self.writer, "{{")?;
751 write!(self.writer, "}}")?;
755 fn emit_map_elt_key<F>(&mut self, idx: usize, f: F) -> EncodeResult
757 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
759 if self.is_emitting_map_key {
760 return Err(EncoderError::BadHashmapKey);
763 write!(self.writer, ",")?
765 self.is_emitting_map_key = true;
767 self.is_emitting_map_key = false;
771 fn emit_map_elt_val<F>(&mut self, _idx: usize, f: F) -> EncodeResult
773 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
775 if self.is_emitting_map_key {
776 return Err(EncoderError::BadHashmapKey);
778 write!(self.writer, ":")?;
783 /// Another encoder for JSON, but prints out human-readable JSON instead of
785 pub struct PrettyEncoder<'a> {
786 writer: &'a mut (dyn fmt::Write + 'a),
789 is_emitting_map_key: bool,
792 impl<'a> PrettyEncoder<'a> {
793 /// Creates a new encoder whose output will be written to the specified writer
794 pub fn new(writer: &'a mut dyn fmt::Write) -> PrettyEncoder<'a> {
795 PrettyEncoder { writer, curr_indent: 0, indent: 2, is_emitting_map_key: false }
798 /// Sets the number of spaces to indent for each level.
799 /// This is safe to set during encoding.
800 pub fn set_indent(&mut self, indent: usize) {
801 // self.indent very well could be 0 so we need to use checked division.
802 let level = self.curr_indent.checked_div(self.indent).unwrap_or(0);
803 self.indent = indent;
804 self.curr_indent = level * self.indent;
808 impl<'a> crate::Encoder for PrettyEncoder<'a> {
809 type Error = EncoderError;
811 fn emit_unit(&mut self) -> EncodeResult {
812 if self.is_emitting_map_key {
813 return Err(EncoderError::BadHashmapKey);
815 write!(self.writer, "null")?;
819 fn emit_usize(&mut self, v: usize) -> EncodeResult {
820 emit_enquoted_if_mapkey!(self, v)
822 fn emit_u128(&mut self, v: u128) -> EncodeResult {
823 emit_enquoted_if_mapkey!(self, v)
825 fn emit_u64(&mut self, v: u64) -> EncodeResult {
826 emit_enquoted_if_mapkey!(self, v)
828 fn emit_u32(&mut self, v: u32) -> EncodeResult {
829 emit_enquoted_if_mapkey!(self, v)
831 fn emit_u16(&mut self, v: u16) -> EncodeResult {
832 emit_enquoted_if_mapkey!(self, v)
834 fn emit_u8(&mut self, v: u8) -> EncodeResult {
835 emit_enquoted_if_mapkey!(self, v)
838 fn emit_isize(&mut self, v: isize) -> EncodeResult {
839 emit_enquoted_if_mapkey!(self, v)
841 fn emit_i128(&mut self, v: i128) -> EncodeResult {
842 emit_enquoted_if_mapkey!(self, v)
844 fn emit_i64(&mut self, v: i64) -> EncodeResult {
845 emit_enquoted_if_mapkey!(self, v)
847 fn emit_i32(&mut self, v: i32) -> EncodeResult {
848 emit_enquoted_if_mapkey!(self, v)
850 fn emit_i16(&mut self, v: i16) -> EncodeResult {
851 emit_enquoted_if_mapkey!(self, v)
853 fn emit_i8(&mut self, v: i8) -> EncodeResult {
854 emit_enquoted_if_mapkey!(self, v)
857 fn emit_bool(&mut self, v: bool) -> EncodeResult {
858 if self.is_emitting_map_key {
859 return Err(EncoderError::BadHashmapKey);
862 write!(self.writer, "true")?;
864 write!(self.writer, "false")?;
869 fn emit_f64(&mut self, v: f64) -> EncodeResult {
870 emit_enquoted_if_mapkey!(self, fmt_number_or_null(v))
872 fn emit_f32(&mut self, v: f32) -> EncodeResult {
873 self.emit_f64(f64::from(v))
876 fn emit_char(&mut self, v: char) -> EncodeResult {
877 escape_char(self.writer, v)
879 fn emit_str(&mut self, v: &str) -> EncodeResult {
880 escape_str(self.writer, v)
883 fn emit_enum<F>(&mut self, _name: &str, f: F) -> EncodeResult
885 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
890 fn emit_enum_variant<F>(&mut self, name: &str, _id: usize, cnt: usize, f: F) -> EncodeResult
892 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
895 escape_str(self.writer, name)
897 if self.is_emitting_map_key {
898 return Err(EncoderError::BadHashmapKey);
900 writeln!(self.writer, "{{")?;
901 self.curr_indent += self.indent;
902 spaces(self.writer, self.curr_indent)?;
903 write!(self.writer, "\"variant\": ")?;
904 escape_str(self.writer, name)?;
905 writeln!(self.writer, ",")?;
906 spaces(self.writer, self.curr_indent)?;
907 writeln!(self.writer, "\"fields\": [")?;
908 self.curr_indent += self.indent;
910 self.curr_indent -= self.indent;
911 writeln!(self.writer)?;
912 spaces(self.writer, self.curr_indent)?;
913 self.curr_indent -= self.indent;
914 writeln!(self.writer, "]")?;
915 spaces(self.writer, self.curr_indent)?;
916 write!(self.writer, "}}")?;
921 fn emit_enum_variant_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult
923 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
925 if self.is_emitting_map_key {
926 return Err(EncoderError::BadHashmapKey);
929 writeln!(self.writer, ",")?;
931 spaces(self.writer, self.curr_indent)?;
935 fn emit_enum_struct_variant<F>(
943 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
945 if self.is_emitting_map_key {
946 return Err(EncoderError::BadHashmapKey);
948 self.emit_enum_variant(name, id, cnt, f)
951 fn emit_enum_struct_variant_field<F>(&mut self, _: &str, idx: usize, f: F) -> EncodeResult
953 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
955 if self.is_emitting_map_key {
956 return Err(EncoderError::BadHashmapKey);
958 self.emit_enum_variant_arg(idx, f)
961 fn emit_struct<F>(&mut self, _: &str, len: usize, f: F) -> EncodeResult
963 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
965 if self.is_emitting_map_key {
966 return Err(EncoderError::BadHashmapKey);
969 write!(self.writer, "{{}}")?;
971 write!(self.writer, "{{")?;
972 self.curr_indent += self.indent;
974 self.curr_indent -= self.indent;
975 writeln!(self.writer)?;
976 spaces(self.writer, self.curr_indent)?;
977 write!(self.writer, "}}")?;
982 fn emit_struct_field<F>(&mut self, name: &str, idx: usize, f: F) -> EncodeResult
984 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
986 if self.is_emitting_map_key {
987 return Err(EncoderError::BadHashmapKey);
990 writeln!(self.writer)?;
992 writeln!(self.writer, ",")?;
994 spaces(self.writer, self.curr_indent)?;
995 escape_str(self.writer, name)?;
996 write!(self.writer, ": ")?;
1000 fn emit_tuple<F>(&mut self, len: usize, f: F) -> EncodeResult
1002 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
1004 if self.is_emitting_map_key {
1005 return Err(EncoderError::BadHashmapKey);
1007 self.emit_seq(len, f)
1009 fn emit_tuple_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult
1011 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
1013 if self.is_emitting_map_key {
1014 return Err(EncoderError::BadHashmapKey);
1016 self.emit_seq_elt(idx, f)
1019 fn emit_tuple_struct<F>(&mut self, _: &str, len: usize, f: F) -> EncodeResult
1021 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
1023 if self.is_emitting_map_key {
1024 return Err(EncoderError::BadHashmapKey);
1026 self.emit_seq(len, f)
1028 fn emit_tuple_struct_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult
1030 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
1032 if self.is_emitting_map_key {
1033 return Err(EncoderError::BadHashmapKey);
1035 self.emit_seq_elt(idx, f)
1038 fn emit_option<F>(&mut self, f: F) -> EncodeResult
1040 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
1042 if self.is_emitting_map_key {
1043 return Err(EncoderError::BadHashmapKey);
1047 fn emit_option_none(&mut self) -> EncodeResult {
1048 if self.is_emitting_map_key {
1049 return Err(EncoderError::BadHashmapKey);
1053 fn emit_option_some<F>(&mut self, f: F) -> EncodeResult
1055 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
1057 if self.is_emitting_map_key {
1058 return Err(EncoderError::BadHashmapKey);
1063 fn emit_seq<F>(&mut self, len: usize, f: F) -> EncodeResult
1065 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
1067 if self.is_emitting_map_key {
1068 return Err(EncoderError::BadHashmapKey);
1071 write!(self.writer, "[]")?;
1073 write!(self.writer, "[")?;
1074 self.curr_indent += self.indent;
1076 self.curr_indent -= self.indent;
1077 writeln!(self.writer)?;
1078 spaces(self.writer, self.curr_indent)?;
1079 write!(self.writer, "]")?;
1084 fn emit_seq_elt<F>(&mut self, idx: usize, f: F) -> EncodeResult
1086 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
1088 if self.is_emitting_map_key {
1089 return Err(EncoderError::BadHashmapKey);
1092 writeln!(self.writer)?;
1094 writeln!(self.writer, ",")?;
1096 spaces(self.writer, self.curr_indent)?;
1100 fn emit_map<F>(&mut self, len: usize, f: F) -> EncodeResult
1102 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
1104 if self.is_emitting_map_key {
1105 return Err(EncoderError::BadHashmapKey);
1108 write!(self.writer, "{{}}")?;
1110 write!(self.writer, "{{")?;
1111 self.curr_indent += self.indent;
1113 self.curr_indent -= self.indent;
1114 writeln!(self.writer)?;
1115 spaces(self.writer, self.curr_indent)?;
1116 write!(self.writer, "}}")?;
1121 fn emit_map_elt_key<F>(&mut self, idx: usize, f: F) -> EncodeResult
1123 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
1125 if self.is_emitting_map_key {
1126 return Err(EncoderError::BadHashmapKey);
1129 writeln!(self.writer)?;
1131 writeln!(self.writer, ",")?;
1133 spaces(self.writer, self.curr_indent)?;
1134 self.is_emitting_map_key = true;
1136 self.is_emitting_map_key = false;
1140 fn emit_map_elt_val<F>(&mut self, _idx: usize, f: F) -> EncodeResult
1142 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
1144 if self.is_emitting_map_key {
1145 return Err(EncoderError::BadHashmapKey);
1147 write!(self.writer, ": ")?;
1152 impl<E: crate::Encoder> Encodable<E> for Json {
1153 fn encode(&self, e: &mut E) -> Result<(), E::Error> {
1155 Json::I64(v) => v.encode(e),
1156 Json::U64(v) => v.encode(e),
1157 Json::F64(v) => v.encode(e),
1158 Json::String(ref v) => v.encode(e),
1159 Json::Boolean(v) => v.encode(e),
1160 Json::Array(ref v) => v.encode(e),
1161 Json::Object(ref v) => v.encode(e),
1162 Json::Null => e.emit_unit(),
1167 /// Creates an `AsJson` wrapper which can be used to print a value as JSON
1168 /// on-the-fly via `write!`
1169 pub fn as_json<T>(t: &T) -> AsJson<'_, T> {
1173 /// Creates an `AsPrettyJson` wrapper which can be used to print a value as JSON
1174 /// on-the-fly via `write!`
1175 pub fn as_pretty_json<T>(t: &T) -> AsPrettyJson<'_, T> {
1176 AsPrettyJson { inner: t, indent: None }
1180 /// Borrow this json object as a pretty object to generate a pretty
1181 /// representation for it via `Display`.
1182 pub fn pretty(&self) -> PrettyJson<'_> {
1183 PrettyJson { inner: self }
1186 /// If the Json value is an Object, returns the value associated with the provided key.
1187 /// Otherwise, returns None.
1188 pub fn find(&self, key: &str) -> Option<&Json> {
1190 Json::Object(ref map) => map.get(key),
1195 /// Attempts to get a nested Json Object for each key in `keys`.
1196 /// If any key is found not to exist, `find_path` will return `None`.
1197 /// Otherwise, it will return the Json value associated with the final key.
1198 pub fn find_path<'a>(&'a self, keys: &[&str]) -> Option<&'a Json> {
1199 let mut target = self;
1201 target = target.find(*key)?;
1206 /// If the Json value is an Object, performs a depth-first search until
1207 /// a value associated with the provided key is found. If no value is found
1208 /// or the Json value is not an Object, returns `None`.
1209 pub fn search(&self, key: &str) -> Option<&Json> {
1211 Json::Object(ref map) => match map.get(key) {
1212 Some(json_value) => Some(json_value),
1214 for v in map.values() {
1215 match v.search(key) {
1216 x if x.is_some() => return x,
1227 /// Returns `true` if the Json value is an `Object`.
1228 pub fn is_object(&self) -> bool {
1229 self.as_object().is_some()
1232 /// If the Json value is an `Object`, returns the associated `BTreeMap`;
1233 /// returns `None` otherwise.
1234 pub fn as_object(&self) -> Option<&Object> {
1236 Json::Object(ref map) => Some(map),
1241 /// Returns `true` if the Json value is an `Array`.
1242 pub fn is_array(&self) -> bool {
1243 self.as_array().is_some()
1246 /// If the Json value is an `Array`, returns the associated vector;
1247 /// returns `None` otherwise.
1248 pub fn as_array(&self) -> Option<&Array> {
1250 Json::Array(ref array) => Some(&*array),
1255 /// Returns `true` if the Json value is a `String`.
1256 pub fn is_string(&self) -> bool {
1257 self.as_string().is_some()
1260 /// If the Json value is a `String`, returns the associated `str`;
1261 /// returns `None` otherwise.
1262 pub fn as_string(&self) -> Option<&str> {
1264 Json::String(ref s) => Some(&s[..]),
1269 /// Returns `true` if the Json value is a `Number`.
1270 pub fn is_number(&self) -> bool {
1271 matches!(*self, Json::I64(_) | Json::U64(_) | Json::F64(_))
1274 /// Returns `true` if the Json value is a `i64`.
1275 pub fn is_i64(&self) -> bool {
1276 matches!(*self, Json::I64(_))
1279 /// Returns `true` if the Json value is a `u64`.
1280 pub fn is_u64(&self) -> bool {
1281 matches!(*self, Json::U64(_))
1284 /// Returns `true` if the Json value is a `f64`.
1285 pub fn is_f64(&self) -> bool {
1286 matches!(*self, Json::F64(_))
1289 /// If the Json value is a number, returns or cast it to a `i64`;
1290 /// returns `None` otherwise.
1291 pub fn as_i64(&self) -> Option<i64> {
1293 Json::I64(n) => Some(n),
1294 Json::U64(n) => Some(n as i64),
1299 /// If the Json value is a number, returns or cast it to a `u64`;
1300 /// returns `None` otherwise.
1301 pub fn as_u64(&self) -> Option<u64> {
1303 Json::I64(n) => Some(n as u64),
1304 Json::U64(n) => Some(n),
1309 /// If the Json value is a number, returns or cast it to a `f64`;
1310 /// returns `None` otherwise.
1311 pub fn as_f64(&self) -> Option<f64> {
1313 Json::I64(n) => Some(n as f64),
1314 Json::U64(n) => Some(n as f64),
1315 Json::F64(n) => Some(n),
1320 /// Returns `true` if the Json value is a `Boolean`.
1321 pub fn is_boolean(&self) -> bool {
1322 self.as_boolean().is_some()
1325 /// If the Json value is a `Boolean`, returns the associated `bool`;
1326 /// returns `None` otherwise.
1327 pub fn as_boolean(&self) -> Option<bool> {
1329 Json::Boolean(b) => Some(b),
1334 /// Returns `true` if the Json value is a `Null`.
1335 pub fn is_null(&self) -> bool {
1336 self.as_null().is_some()
1339 /// If the Json value is a `Null`, returns `()`;
1340 /// returns `None` otherwise.
1341 pub fn as_null(&self) -> Option<()> {
1343 Json::Null => Some(()),
1349 impl<'a> Index<&'a str> for Json {
1352 fn index(&self, idx: &'a str) -> &Json {
1353 self.find(idx).unwrap()
1357 impl Index<usize> for Json {
1360 fn index(&self, idx: usize) -> &Json {
1362 Json::Array(ref v) => &v[idx],
1363 _ => panic!("can only index Json with usize if it is an array"),
1368 /// The output of the streaming parser.
1369 #[derive(PartialEq, Clone, Debug)]
1370 pub enum JsonEvent {
1379 StringValue(string::String),
1384 #[derive(PartialEq, Debug)]
1386 // Parse a value in an array, true means first element.
1388 // Parse ',' or ']' after an element in an array.
1390 // Parse a key:value in an object, true means first element.
1392 // Parse ',' or ']' after an element in an object.
1396 // Expecting the stream to end.
1398 // Parsing can't continue.
1402 /// A Stack represents the current position of the parser in the logical
1403 /// structure of the JSON stream.
1405 /// An example is `foo.bar[3].x`.
1408 stack: Vec<InternalStackElement>,
1409 str_buffer: Vec<u8>,
1412 /// StackElements compose a Stack.
1414 /// As an example, `StackElement::Key("foo")`, `StackElement::Key("bar")`,
1415 /// `StackElement::Index(3)`, and `StackElement::Key("x")` are the
1416 /// StackElements composing the stack that represents `foo.bar[3].x`.
1417 #[derive(PartialEq, Clone, Debug)]
1418 pub enum StackElement<'l> {
1423 // Internally, Key elements are stored as indices in a buffer to avoid
1424 // allocating a string for every member of an object.
1425 #[derive(PartialEq, Clone, Debug)]
1426 enum InternalStackElement {
1428 InternalKey(u16, u16), // start, size
1432 pub fn new() -> Stack {
1436 /// Returns The number of elements in the Stack.
1437 pub fn len(&self) -> usize {
1441 /// Returns `true` if the stack is empty.
1442 pub fn is_empty(&self) -> bool {
1443 self.stack.is_empty()
1446 /// Provides access to the StackElement at a given index.
1447 /// lower indices are at the bottom of the stack while higher indices are
1449 pub fn get(&self, idx: usize) -> StackElement<'_> {
1450 match self.stack[idx] {
1451 InternalIndex(i) => StackElement::Index(i),
1452 InternalKey(start, size) => StackElement::Key(
1453 str::from_utf8(&self.str_buffer[start as usize..start as usize + size as usize])
1459 /// Compares this stack with an array of StackElement<'_>s.
1460 pub fn is_equal_to(&self, rhs: &[StackElement<'_>]) -> bool {
1461 if self.stack.len() != rhs.len() {
1464 for (i, r) in rhs.iter().enumerate() {
1465 if self.get(i) != *r {
1472 /// Returns `true` if the bottom-most elements of this stack are the same as
1473 /// the ones passed as parameter.
1474 pub fn starts_with(&self, rhs: &[StackElement<'_>]) -> bool {
1475 if self.stack.len() < rhs.len() {
1478 for (i, r) in rhs.iter().enumerate() {
1479 if self.get(i) != *r {
1486 /// Returns `true` if the top-most elements of this stack are the same as
1487 /// the ones passed as parameter.
1488 pub fn ends_with(&self, rhs: &[StackElement<'_>]) -> bool {
1489 if self.stack.len() < rhs.len() {
1492 let offset = self.stack.len() - rhs.len();
1493 for (i, r) in rhs.iter().enumerate() {
1494 if self.get(i + offset) != *r {
1501 /// Returns the top-most element (if any).
1502 pub fn top(&self) -> Option<StackElement<'_>> {
1503 match self.stack.last() {
1505 Some(&InternalIndex(i)) => Some(StackElement::Index(i)),
1506 Some(&InternalKey(start, size)) => Some(StackElement::Key(
1507 str::from_utf8(&self.str_buffer[start as usize..(start + size) as usize]).unwrap(),
1512 // Used by Parser to insert StackElement::Key elements at the top of the stack.
1513 fn push_key(&mut self, key: string::String) {
1514 self.stack.push(InternalKey(self.str_buffer.len() as u16, key.len() as u16));
1515 self.str_buffer.extend(key.as_bytes());
1518 // Used by Parser to insert StackElement::Index elements at the top of the stack.
1519 fn push_index(&mut self, index: u32) {
1520 self.stack.push(InternalIndex(index));
1523 // Used by Parser to remove the top-most element of the stack.
1525 assert!(!self.is_empty());
1526 match *self.stack.last().unwrap() {
1527 InternalKey(_, sz) => {
1528 let new_size = self.str_buffer.len() - sz as usize;
1529 self.str_buffer.truncate(new_size);
1531 InternalIndex(_) => {}
1536 // Used by Parser to test whether the top-most element is an index.
1537 fn last_is_index(&self) -> bool {
1538 matches!(self.stack.last(), Some(InternalIndex(_)))
1541 // Used by Parser to increment the index of the top-most element.
1542 fn bump_index(&mut self) {
1543 let len = self.stack.len();
1544 let idx = match *self.stack.last().unwrap() {
1545 InternalIndex(i) => i + 1,
1550 self.stack[len - 1] = InternalIndex(idx);
1554 /// A streaming JSON parser implemented as an iterator of JsonEvent, consuming
1555 /// an iterator of char.
1556 pub struct Parser<T> {
1561 // We maintain a stack representing where we are in the logical structure
1562 // of the JSON stream.
1564 // A state machine is kept to make it possible to interrupt and resume parsing.
1568 impl<T: Iterator<Item = char>> Iterator for Parser<T> {
1569 type Item = JsonEvent;
1571 fn next(&mut self) -> Option<JsonEvent> {
1572 if self.state == ParseFinished {
1576 if self.state == ParseBeforeFinish {
1577 self.parse_whitespace();
1578 // Make sure there is no trailing characters.
1580 self.state = ParseFinished;
1583 return Some(self.error_event(TrailingCharacters));
1591 impl<T: Iterator<Item = char>> Parser<T> {
1592 /// Creates the JSON parser.
1593 pub fn new(rdr: T) -> Parser<T> {
1594 let mut p = Parser {
1599 stack: Stack::new(),
1606 /// Provides access to the current position in the logical structure of the
1608 pub fn stack(&self) -> &Stack {
1612 fn eof(&self) -> bool {
1615 fn ch_or_null(&self) -> char {
1616 self.ch.unwrap_or('\x00')
1618 fn bump(&mut self) {
1619 self.ch = self.rdr.next();
1621 if self.ch_is('\n') {
1629 fn next_char(&mut self) -> Option<char> {
1633 fn ch_is(&self, c: char) -> bool {
1637 fn error<U>(&self, reason: ErrorCode) -> Result<U, ParserError> {
1638 Err(SyntaxError(reason, self.line, self.col))
1641 fn parse_whitespace(&mut self) {
1642 while self.ch_is(' ') || self.ch_is('\n') || self.ch_is('\t') || self.ch_is('\r') {
1647 fn parse_number(&mut self) -> JsonEvent {
1648 let neg = if self.ch_is('-') {
1655 let res = match self.parse_u64() {
1662 if self.ch_is('.') || self.ch_is('e') || self.ch_is('E') {
1663 let mut res = res as f64;
1665 if self.ch_is('.') {
1666 res = match self.parse_decimal(res) {
1674 if self.ch_is('e') || self.ch_is('E') {
1675 res = match self.parse_exponent(res) {
1689 let res = (res as i64).wrapping_neg();
1691 // Make sure we didn't underflow.
1693 Error(SyntaxError(InvalidNumber, self.line, self.col))
1702 fn parse_u64(&mut self) -> Result<u64, ParserError> {
1703 let mut accum = 0u64;
1704 let last_accum = 0; // necessary to detect overflow.
1706 match self.ch_or_null() {
1710 // A leading '0' must be the only digit before the decimal point.
1711 if let '0'..='9' = self.ch_or_null() {
1712 return self.error(InvalidNumber);
1717 match self.ch_or_null() {
1719 accum = accum.wrapping_mul(10);
1720 accum = accum.wrapping_add((c as u64) - ('0' as u64));
1722 // Detect overflow by comparing to the last value.
1723 if accum <= last_accum {
1724 return self.error(InvalidNumber);
1733 _ => return self.error(InvalidNumber),
1739 fn parse_decimal(&mut self, mut res: f64) -> Result<f64, ParserError> {
1742 // Make sure a digit follows the decimal place.
1743 match self.ch_or_null() {
1745 _ => return self.error(InvalidNumber),
1750 match self.ch_or_null() {
1753 res += (((c as isize) - ('0' as isize)) as f64) * dec;
1763 fn parse_exponent(&mut self, mut res: f64) -> Result<f64, ParserError> {
1767 let mut neg_exp = false;
1769 if self.ch_is('+') {
1771 } else if self.ch_is('-') {
1776 // Make sure a digit follows the exponent place.
1777 match self.ch_or_null() {
1779 _ => return self.error(InvalidNumber),
1782 match self.ch_or_null() {
1785 exp += (c as usize) - ('0' as usize);
1793 let exp = 10_f64.powi(exp as i32);
1803 fn decode_hex_escape(&mut self) -> Result<u16, ParserError> {
1806 while i < 4 && !self.eof() {
1808 n = match self.ch_or_null() {
1809 c @ '0'..='9' => n * 16 + ((c as u16) - ('0' as u16)),
1810 'a' | 'A' => n * 16 + 10,
1811 'b' | 'B' => n * 16 + 11,
1812 'c' | 'C' => n * 16 + 12,
1813 'd' | 'D' => n * 16 + 13,
1814 'e' | 'E' => n * 16 + 14,
1815 'f' | 'F' => n * 16 + 15,
1816 _ => return self.error(InvalidEscape),
1822 // Error out if we didn't parse 4 digits.
1824 return self.error(InvalidEscape);
1830 fn parse_str(&mut self) -> Result<string::String, ParserError> {
1831 let mut escape = false;
1832 let mut res = string::String::new();
1837 return self.error(EOFWhileParsingString);
1841 match self.ch_or_null() {
1842 '"' => res.push('"'),
1843 '\\' => res.push('\\'),
1844 '/' => res.push('/'),
1845 'b' => res.push('\x08'),
1846 'f' => res.push('\x0c'),
1847 'n' => res.push('\n'),
1848 'r' => res.push('\r'),
1849 't' => res.push('\t'),
1850 'u' => match self.decode_hex_escape()? {
1851 0xDC00..=0xDFFF => return self.error(LoneLeadingSurrogateInHexEscape),
1853 // Non-BMP characters are encoded as a sequence of
1854 // two hex escapes, representing UTF-16 surrogates.
1855 n1 @ 0xD800..=0xDBFF => {
1856 match (self.next_char(), self.next_char()) {
1857 (Some('\\'), Some('u')) => (),
1858 _ => return self.error(UnexpectedEndOfHexEscape),
1861 let n2 = self.decode_hex_escape()?;
1862 if n2 < 0xDC00 || n2 > 0xDFFF {
1863 return self.error(LoneLeadingSurrogateInHexEscape);
1866 (u32::from(n1 - 0xD800) << 10 | u32::from(n2 - 0xDC00)) + 0x1_0000;
1867 res.push(char::from_u32(c).unwrap());
1870 n => match char::from_u32(u32::from(n)) {
1871 Some(c) => res.push(c),
1872 None => return self.error(InvalidUnicodeCodePoint),
1875 _ => return self.error(InvalidEscape),
1878 } else if self.ch_is('\\') {
1886 Some(c) => res.push(c),
1887 None => unreachable!(),
1893 // Invoked at each iteration, consumes the stream until it has enough
1894 // information to return a JsonEvent.
1895 // Manages an internal state so that parsing can be interrupted and resumed.
1896 // Also keeps track of the position in the logical structure of the json
1897 // stream isize the form of a stack that can be queried by the user using the
1899 fn parse(&mut self) -> JsonEvent {
1901 // The only paths where the loop can spin a new iteration
1902 // are in the cases ParseArrayComma and ParseObjectComma if ','
1903 // is parsed. In these cases the state is set to (respectively)
1904 // ParseArray(false) and ParseObject(false), which always return,
1905 // so there is no risk of getting stuck in an infinite loop.
1906 // All other paths return before the end of the loop's iteration.
1907 self.parse_whitespace();
1911 return self.parse_start();
1913 ParseArray(first) => {
1914 return self.parse_array(first);
1916 ParseArrayComma => {
1917 if let Some(evt) = self.parse_array_comma_or_end() {
1921 ParseObject(first) => {
1922 return self.parse_object(first);
1924 ParseObjectComma => {
1926 if self.ch_is(',') {
1927 self.state = ParseObject(false);
1930 return self.parse_object_end();
1934 return self.error_event(InvalidSyntax);
1940 fn parse_start(&mut self) -> JsonEvent {
1941 let val = self.parse_value();
1942 self.state = match val {
1943 Error(_) => ParseFinished,
1944 ArrayStart => ParseArray(true),
1945 ObjectStart => ParseObject(true),
1946 _ => ParseBeforeFinish,
1951 fn parse_array(&mut self, first: bool) -> JsonEvent {
1952 if self.ch_is(']') {
1954 self.error_event(InvalidSyntax)
1956 self.state = if self.stack.is_empty() {
1958 } else if self.stack.last_is_index() {
1968 self.stack.push_index(0);
1970 let val = self.parse_value();
1971 self.state = match val {
1972 Error(_) => ParseFinished,
1973 ArrayStart => ParseArray(true),
1974 ObjectStart => ParseObject(true),
1975 _ => ParseArrayComma,
1981 fn parse_array_comma_or_end(&mut self) -> Option<JsonEvent> {
1982 if self.ch_is(',') {
1983 self.stack.bump_index();
1984 self.state = ParseArray(false);
1987 } else if self.ch_is(']') {
1989 self.state = if self.stack.is_empty() {
1991 } else if self.stack.last_is_index() {
1998 } else if self.eof() {
1999 Some(self.error_event(EOFWhileParsingArray))
2001 Some(self.error_event(InvalidSyntax))
2005 fn parse_object(&mut self, first: bool) -> JsonEvent {
2006 if self.ch_is('}') {
2008 if self.stack.is_empty() {
2009 return self.error_event(TrailingComma);
2014 self.state = if self.stack.is_empty() {
2016 } else if self.stack.last_is_index() {
2025 return self.error_event(EOFWhileParsingObject);
2027 if !self.ch_is('"') {
2028 return self.error_event(KeyMustBeAString);
2030 let s = match self.parse_str() {
2033 self.state = ParseFinished;
2037 self.parse_whitespace();
2039 return self.error_event(EOFWhileParsingObject);
2040 } else if self.ch_or_null() != ':' {
2041 return self.error_event(ExpectedColon);
2043 self.stack.push_key(s);
2045 self.parse_whitespace();
2047 let val = self.parse_value();
2049 self.state = match val {
2050 Error(_) => ParseFinished,
2051 ArrayStart => ParseArray(true),
2052 ObjectStart => ParseObject(true),
2053 _ => ParseObjectComma,
2058 fn parse_object_end(&mut self) -> JsonEvent {
2059 if self.ch_is('}') {
2060 self.state = if self.stack.is_empty() {
2062 } else if self.stack.last_is_index() {
2069 } else if self.eof() {
2070 self.error_event(EOFWhileParsingObject)
2072 self.error_event(InvalidSyntax)
2076 fn parse_value(&mut self) -> JsonEvent {
2078 return self.error_event(EOFWhileParsingValue);
2080 match self.ch_or_null() {
2081 'n' => self.parse_ident("ull", NullValue),
2082 't' => self.parse_ident("rue", BooleanValue(true)),
2083 'f' => self.parse_ident("alse", BooleanValue(false)),
2084 '0'..='9' | '-' => self.parse_number(),
2085 '"' => match self.parse_str() {
2086 Ok(s) => StringValue(s),
2097 _ => self.error_event(InvalidSyntax),
2101 fn parse_ident(&mut self, ident: &str, value: JsonEvent) -> JsonEvent {
2102 if ident.chars().all(|c| Some(c) == self.next_char()) {
2106 Error(SyntaxError(InvalidSyntax, self.line, self.col))
2110 fn error_event(&mut self, reason: ErrorCode) -> JsonEvent {
2111 self.state = ParseFinished;
2112 Error(SyntaxError(reason, self.line, self.col))
2116 /// A Builder consumes a json::Parser to create a generic Json structure.
2117 pub struct Builder<T> {
2119 token: Option<JsonEvent>,
2122 impl<T: Iterator<Item = char>> Builder<T> {
2123 /// Creates a JSON Builder.
2124 pub fn new(src: T) -> Builder<T> {
2125 Builder { parser: Parser::new(src), token: None }
2128 // Decode a Json value from a Parser.
2129 pub fn build(&mut self) -> Result<Json, BuilderError> {
2131 let result = self.build_value();
2135 Some(Error(ref e)) => {
2136 return Err(e.clone());
2139 panic!("unexpected token {:?}", tok.clone());
2145 fn bump(&mut self) {
2146 self.token = self.parser.next();
2149 fn build_value(&mut self) -> Result<Json, BuilderError> {
2151 Some(NullValue) => Ok(Json::Null),
2152 Some(I64Value(n)) => Ok(Json::I64(n)),
2153 Some(U64Value(n)) => Ok(Json::U64(n)),
2154 Some(F64Value(n)) => Ok(Json::F64(n)),
2155 Some(BooleanValue(b)) => Ok(Json::Boolean(b)),
2156 Some(StringValue(ref mut s)) => {
2157 let mut temp = string::String::new();
2159 Ok(Json::String(temp))
2161 Some(Error(ref e)) => Err(e.clone()),
2162 Some(ArrayStart) => self.build_array(),
2163 Some(ObjectStart) => self.build_object(),
2164 Some(ObjectEnd) => self.parser.error(InvalidSyntax),
2165 Some(ArrayEnd) => self.parser.error(InvalidSyntax),
2166 None => self.parser.error(EOFWhileParsingValue),
2170 fn build_array(&mut self) -> Result<Json, BuilderError> {
2172 let mut values = Vec::new();
2175 if self.token == Some(ArrayEnd) {
2176 return Ok(Json::Array(values.into_iter().collect()));
2178 match self.build_value() {
2179 Ok(v) => values.push(v),
2180 Err(e) => return Err(e),
2186 fn build_object(&mut self) -> Result<Json, BuilderError> {
2189 let mut values = BTreeMap::new();
2193 Some(ObjectEnd) => {
2194 return Ok(Json::Object(values));
2196 Some(Error(ref e)) => {
2197 return Err(e.clone());
2204 let key = match self.parser.stack().top() {
2205 Some(StackElement::Key(k)) => k.to_owned(),
2207 panic!("invalid state");
2210 match self.build_value() {
2212 values.insert(key, value);
2220 self.parser.error(EOFWhileParsingObject)
2224 /// Decodes a json value from an `&mut io::Read`
2225 pub fn from_reader(rdr: &mut dyn Read) -> Result<Json, BuilderError> {
2226 let mut contents = Vec::new();
2227 match rdr.read_to_end(&mut contents) {
2229 Err(e) => return Err(io_error_to_error(e)),
2231 let s = match str::from_utf8(&contents).ok() {
2233 _ => return Err(SyntaxError(NotUtf8, 0, 0)),
2235 let mut builder = Builder::new(s.chars());
2239 /// Decodes a json value from a string
2240 pub fn from_str(s: &str) -> Result<Json, BuilderError> {
2241 let mut builder = Builder::new(s.chars());
2245 /// A structure to decode JSON to values in rust.
2246 pub struct Decoder {
2251 /// Creates a new decoder instance for decoding the specified JSON value.
2252 pub fn new(json: Json) -> Decoder {
2253 Decoder { stack: vec![json] }
2256 fn pop(&mut self) -> Json {
2257 self.stack.pop().unwrap()
2261 macro_rules! expect {
2262 ($e:expr, Null) => {{
2264 Json::Null => Ok(()),
2265 other => Err(ExpectedError("Null".to_owned(), other.to_string())),
2268 ($e:expr, $t:ident) => {{
2270 Json::$t(v) => Ok(v),
2271 other => Err(ExpectedError(stringify!($t).to_owned(), other.to_string())),
2276 macro_rules! read_primitive {
2277 ($name:ident, $ty:ty) => {
2278 fn $name(&mut self) -> DecodeResult<$ty> {
2280 Json::I64(f) => Ok(f as $ty),
2281 Json::U64(f) => Ok(f as $ty),
2282 Json::F64(f) => Err(ExpectedError("Integer".to_owned(), f.to_string())),
2283 // re: #12967.. a type w/ numeric keys (ie HashMap<usize, V> etc)
2284 // is going to have a string here, as per JSON spec.
2285 Json::String(s) => match s.parse().ok() {
2287 None => Err(ExpectedError("Number".to_owned(), s)),
2289 value => Err(ExpectedError("Number".to_owned(), value.to_string())),
2295 impl crate::Decoder for Decoder {
2296 type Error = DecoderError;
2298 fn read_nil(&mut self) -> DecodeResult<()> {
2299 expect!(self.pop(), Null)
2302 read_primitive! { read_usize, usize }
2303 read_primitive! { read_u8, u8 }
2304 read_primitive! { read_u16, u16 }
2305 read_primitive! { read_u32, u32 }
2306 read_primitive! { read_u64, u64 }
2307 read_primitive! { read_u128, u128 }
2308 read_primitive! { read_isize, isize }
2309 read_primitive! { read_i8, i8 }
2310 read_primitive! { read_i16, i16 }
2311 read_primitive! { read_i32, i32 }
2312 read_primitive! { read_i64, i64 }
2313 read_primitive! { read_i128, i128 }
2315 fn read_f32(&mut self) -> DecodeResult<f32> {
2316 self.read_f64().map(|x| x as f32)
2319 fn read_f64(&mut self) -> DecodeResult<f64> {
2321 Json::I64(f) => Ok(f as f64),
2322 Json::U64(f) => Ok(f as f64),
2323 Json::F64(f) => Ok(f),
2324 Json::String(s) => {
2325 // re: #12967.. a type w/ numeric keys (ie HashMap<usize, V> etc)
2326 // is going to have a string here, as per JSON spec.
2327 match s.parse().ok() {
2329 None => Err(ExpectedError("Number".to_owned(), s)),
2332 Json::Null => Ok(f64::NAN),
2333 value => Err(ExpectedError("Number".to_owned(), value.to_string())),
2337 fn read_bool(&mut self) -> DecodeResult<bool> {
2338 expect!(self.pop(), Boolean)
2341 fn read_char(&mut self) -> DecodeResult<char> {
2342 let s = self.read_str()?;
2344 let mut it = s.chars();
2345 if let (Some(c), None) = (it.next(), it.next()) {
2346 // exactly one character
2350 Err(ExpectedError("single character string".to_owned(), s.to_string()))
2353 fn read_str(&mut self) -> DecodeResult<Cow<'_, str>> {
2354 expect!(self.pop(), String).map(Cow::Owned)
2357 fn read_enum<T, F>(&mut self, _name: &str, f: F) -> DecodeResult<T>
2359 F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2364 fn read_enum_variant<T, F>(&mut self, names: &[&str], mut f: F) -> DecodeResult<T>
2366 F: FnMut(&mut Decoder, usize) -> DecodeResult<T>,
2368 let name = match self.pop() {
2369 Json::String(s) => s,
2370 Json::Object(mut o) => {
2371 let n = match o.remove(&"variant".to_owned()) {
2372 Some(Json::String(s)) => s,
2373 Some(val) => return Err(ExpectedError("String".to_owned(), val.to_string())),
2374 None => return Err(MissingFieldError("variant".to_owned())),
2376 match o.remove(&"fields".to_string()) {
2377 Some(Json::Array(l)) => {
2378 self.stack.extend(l.into_iter().rev());
2380 Some(val) => return Err(ExpectedError("Array".to_owned(), val.to_string())),
2381 None => return Err(MissingFieldError("fields".to_owned())),
2385 json => return Err(ExpectedError("String or Object".to_owned(), json.to_string())),
2387 let idx = match names.iter().position(|n| *n == &name[..]) {
2389 None => return Err(UnknownVariantError(name)),
2394 fn read_enum_variant_arg<T, F>(&mut self, _idx: usize, f: F) -> DecodeResult<T>
2396 F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2401 fn read_enum_struct_variant<T, F>(&mut self, names: &[&str], f: F) -> DecodeResult<T>
2403 F: FnMut(&mut Decoder, usize) -> DecodeResult<T>,
2405 self.read_enum_variant(names, f)
2408 fn read_enum_struct_variant_field<T, F>(
2413 ) -> DecodeResult<T>
2415 F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2417 self.read_enum_variant_arg(idx, f)
2420 fn read_struct<T, F>(&mut self, _name: &str, _len: usize, f: F) -> DecodeResult<T>
2422 F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2424 let value = f(self)?;
2429 fn read_struct_field<T, F>(&mut self, name: &str, _idx: usize, f: F) -> DecodeResult<T>
2431 F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2433 let mut obj = expect!(self.pop(), Object)?;
2435 let value = match obj.remove(&name.to_string()) {
2437 // Add a Null and try to parse it as an Option<_>
2438 // to get None as a default value.
2439 self.stack.push(Json::Null);
2442 Err(_) => return Err(MissingFieldError(name.to_string())),
2446 self.stack.push(json);
2450 self.stack.push(Json::Object(obj));
2454 fn read_tuple<T, F>(&mut self, tuple_len: usize, f: F) -> DecodeResult<T>
2456 F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2458 self.read_seq(move |d, len| {
2459 if len == tuple_len {
2462 Err(ExpectedError(format!("Tuple{}", tuple_len), format!("Tuple{}", len)))
2467 fn read_tuple_arg<T, F>(&mut self, idx: usize, f: F) -> DecodeResult<T>
2469 F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2471 self.read_seq_elt(idx, f)
2474 fn read_tuple_struct<T, F>(&mut self, _name: &str, len: usize, f: F) -> DecodeResult<T>
2476 F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2478 self.read_tuple(len, f)
2481 fn read_tuple_struct_arg<T, F>(&mut self, idx: usize, f: F) -> DecodeResult<T>
2483 F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2485 self.read_tuple_arg(idx, f)
2488 fn read_option<T, F>(&mut self, mut f: F) -> DecodeResult<T>
2490 F: FnMut(&mut Decoder, bool) -> DecodeResult<T>,
2493 Json::Null => f(self, false),
2495 self.stack.push(value);
2501 fn read_seq<T, F>(&mut self, f: F) -> DecodeResult<T>
2503 F: FnOnce(&mut Decoder, usize) -> DecodeResult<T>,
2505 let array = expect!(self.pop(), Array)?;
2506 let len = array.len();
2507 self.stack.extend(array.into_iter().rev());
2511 fn read_seq_elt<T, F>(&mut self, _idx: usize, f: F) -> DecodeResult<T>
2513 F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2518 fn read_map<T, F>(&mut self, f: F) -> DecodeResult<T>
2520 F: FnOnce(&mut Decoder, usize) -> DecodeResult<T>,
2522 let obj = expect!(self.pop(), Object)?;
2523 let len = obj.len();
2524 for (key, value) in obj {
2525 self.stack.push(value);
2526 self.stack.push(Json::String(key));
2531 fn read_map_elt_key<T, F>(&mut self, _idx: usize, f: F) -> DecodeResult<T>
2533 F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2538 fn read_map_elt_val<T, F>(&mut self, _idx: usize, f: F) -> DecodeResult<T>
2540 F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2545 fn error(&mut self, err: &str) -> DecoderError {
2546 ApplicationError(err.to_string())
2550 /// A trait for converting values to JSON
2552 /// Converts the value of `self` to an instance of JSON
2553 fn to_json(&self) -> Json;
2556 macro_rules! to_json_impl_i64 {
2558 $(impl ToJson for $t {
2559 fn to_json(&self) -> Json {
2560 Json::I64(*self as i64)
2566 to_json_impl_i64! { isize, i8, i16, i32, i64 }
2568 macro_rules! to_json_impl_u64 {
2570 $(impl ToJson for $t {
2571 fn to_json(&self) -> Json {
2572 Json::U64(*self as u64)
2578 to_json_impl_u64! { usize, u8, u16, u32, u64 }
2580 impl ToJson for Json {
2581 fn to_json(&self) -> Json {
2586 impl ToJson for f32 {
2587 fn to_json(&self) -> Json {
2588 f64::from(*self).to_json()
2592 impl ToJson for f64 {
2593 fn to_json(&self) -> Json {
2594 match self.classify() {
2595 Fp::Nan | Fp::Infinite => Json::Null,
2596 _ => Json::F64(*self),
2601 impl ToJson for () {
2602 fn to_json(&self) -> Json {
2607 impl ToJson for bool {
2608 fn to_json(&self) -> Json {
2609 Json::Boolean(*self)
2613 impl ToJson for str {
2614 fn to_json(&self) -> Json {
2615 Json::String(self.to_string())
2619 impl ToJson for string::String {
2620 fn to_json(&self) -> Json {
2621 Json::String((*self).clone())
2625 macro_rules! tuple_impl {
2626 // use variables to indicate the arity of the tuple
2627 ($($tyvar:ident),* ) => {
2628 // the trailing commas are for the 1 tuple
2630 $( $tyvar : ToJson ),*
2631 > ToJson for ( $( $tyvar ),* , ) {
2634 #[allow(non_snake_case)]
2635 fn to_json(&self) -> Json {
2637 ($(ref $tyvar),*,) => Json::Array(vec![$($tyvar.to_json()),*])
2646 tuple_impl! {A, B, C}
2647 tuple_impl! {A, B, C, D}
2648 tuple_impl! {A, B, C, D, E}
2649 tuple_impl! {A, B, C, D, E, F}
2650 tuple_impl! {A, B, C, D, E, F, G}
2651 tuple_impl! {A, B, C, D, E, F, G, H}
2652 tuple_impl! {A, B, C, D, E, F, G, H, I}
2653 tuple_impl! {A, B, C, D, E, F, G, H, I, J}
2654 tuple_impl! {A, B, C, D, E, F, G, H, I, J, K}
2655 tuple_impl! {A, B, C, D, E, F, G, H, I, J, K, L}
2657 impl<A: ToJson> ToJson for [A] {
2658 fn to_json(&self) -> Json {
2659 Json::Array(self.iter().map(|elt| elt.to_json()).collect())
2663 impl<A: ToJson> ToJson for Vec<A> {
2664 fn to_json(&self) -> Json {
2665 Json::Array(self.iter().map(|elt| elt.to_json()).collect())
2669 impl<T: ToString, A: ToJson> ToJson for BTreeMap<T, A> {
2670 fn to_json(&self) -> Json {
2671 let mut d = BTreeMap::new();
2672 for (key, value) in self {
2673 d.insert(key.to_string(), value.to_json());
2679 impl<A: ToJson> ToJson for HashMap<string::String, A> {
2680 fn to_json(&self) -> Json {
2681 let mut d = BTreeMap::new();
2682 for (key, value) in self {
2683 d.insert((*key).clone(), value.to_json());
2689 impl<A: ToJson> ToJson for Option<A> {
2690 fn to_json(&self) -> Json {
2693 Some(ref value) => value.to_json(),
2698 struct FormatShim<'a, 'b> {
2699 inner: &'a mut fmt::Formatter<'b>,
2702 impl<'a, 'b> fmt::Write for FormatShim<'a, 'b> {
2703 fn write_str(&mut self, s: &str) -> fmt::Result {
2704 match self.inner.write_str(s) {
2706 Err(_) => Err(fmt::Error),
2711 impl fmt::Display for Json {
2712 /// Encodes a json value into a string
2713 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2714 let mut shim = FormatShim { inner: f };
2715 let mut encoder = Encoder::new(&mut shim);
2716 match self.encode(&mut encoder) {
2718 Err(_) => Err(fmt::Error),
2723 impl<'a> fmt::Display for PrettyJson<'a> {
2724 /// Encodes a json value into a string
2725 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2726 let mut shim = FormatShim { inner: f };
2727 let mut encoder = PrettyEncoder::new(&mut shim);
2728 match self.inner.encode(&mut encoder) {
2730 Err(_) => Err(fmt::Error),
2735 impl<'a, T: for<'r> Encodable<Encoder<'r>>> fmt::Display for AsJson<'a, T> {
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.inner.encode(&mut encoder) {
2742 Err(_) => Err(fmt::Error),
2747 impl<'a, T> AsPrettyJson<'a, T> {
2748 /// Sets the indentation level for the emitted JSON
2749 pub fn indent(mut self, indent: usize) -> AsPrettyJson<'a, T> {
2750 self.indent = Some(indent);
2755 impl<'a, T: for<'x> Encodable<PrettyEncoder<'x>>> fmt::Display for AsPrettyJson<'a, T> {
2756 /// Encodes a json value into a string
2757 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2758 let mut shim = FormatShim { inner: f };
2759 let mut encoder = PrettyEncoder::new(&mut shim);
2760 if let Some(n) = self.indent {
2761 encoder.set_indent(n);
2763 match self.inner.encode(&mut encoder) {
2765 Err(_) => Err(fmt::Error),
2770 impl FromStr for Json {
2771 type Err = BuilderError;
2772 fn from_str(s: &str) -> Result<Json, BuilderError> {