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 //! use rustc_serialize::json;
73 //! // Automatically generate `Decodable` and `Encodable` trait implementations
74 //! #[derive(RustcDecodable, RustcEncodable)]
75 //! pub struct TestStruct {
78 //! 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();
96 //! ## Using the `ToJson` trait
98 //! The examples above use the `ToJson` trait to generate the JSON string, which is required
99 //! for custom mappings.
101 //! ### Simple example of `ToJson` usage
104 //! # #![feature(rustc_private)]
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 `RustcEncodable` trait implementation
121 //! #[derive(RustcEncodable)]
122 //! pub struct ComplexNumRecord {
129 //! let num = ComplexNum { a: 0.0001, b: 12.539 };
130 //! let data: String = json::encode(&ComplexNumRecord{
132 //! dsc: "test".to_string(),
133 //! val: num.to_json(),
135 //! println!("data: {}", data);
136 //! // data: {"uid":1,"dsc":"test","val":"0.0001+12.539i"};
140 //! ### Verbose example of `ToJson` usage
143 //! # #![feature(rustc_private)]
144 //! use std::collections::BTreeMap;
145 //! use rustc_serialize::json::{self, Json, ToJson};
147 //! // Only generate `RustcDecodable` trait implementation
148 //! #[derive(RustcDecodable)]
149 //! pub struct TestStruct {
151 //! data_str: String,
152 //! data_vector: Vec<u8>,
155 //! // Specify encoding method manually
156 //! impl ToJson for TestStruct {
157 //! fn to_json(&self) -> Json {
158 //! let mut d = BTreeMap::new();
159 //! // All standard types implement `to_json()`, so use it
160 //! d.insert("data_int".to_string(), self.data_int.to_json());
161 //! d.insert("data_str".to_string(), self.data_str.to_json());
162 //! d.insert("data_vector".to_string(), self.data_vector.to_json());
168 //! // Serialize using `ToJson`
169 //! let input_data = TestStruct {
171 //! data_str: "madoka".to_string(),
172 //! data_vector: vec![2,3,4,5],
174 //! let json_obj: Json = input_data.to_json();
175 //! let json_str: String = json_obj.to_string();
177 //! // Deserialize like before
178 //! let decoded: TestStruct = json::decode(&json_str).unwrap();
182 use self::DecoderError::*;
183 use self::ErrorCode::*;
184 use self::InternalStackElement::*;
185 use self::JsonEvent::*;
186 use self::ParserError::*;
187 use self::ParserState::*;
189 use std::borrow::Cow;
190 use std::collections::{BTreeMap, HashMap};
192 use std::io::prelude::*;
194 use std::num::FpCategory as Fp;
196 use std::str::FromStr;
198 use std::{char, fmt, str};
200 use crate::Encodable;
202 /// Represents a json value
203 #[derive(Clone, PartialEq, PartialOrd, Debug)]
208 String(string::String),
211 Object(self::Object),
215 pub type Array = Vec<Json>;
216 pub type Object = BTreeMap<string::String, Json>;
218 pub struct PrettyJson<'a> {
222 pub struct AsJson<'a, T> {
225 pub struct AsPrettyJson<'a, T> {
227 indent: Option<usize>,
230 /// The errors that can arise while parsing a JSON stream.
231 #[derive(Clone, Copy, PartialEq, Debug)]
235 EOFWhileParsingObject,
236 EOFWhileParsingArray,
237 EOFWhileParsingValue,
238 EOFWhileParsingString,
244 InvalidUnicodeCodePoint,
245 LoneLeadingSurrogateInHexEscape,
246 UnexpectedEndOfHexEscape,
252 #[derive(Clone, PartialEq, Debug)]
253 pub enum ParserError {
255 SyntaxError(ErrorCode, usize, usize),
256 IoError(io::ErrorKind, String),
259 // Builder and Parser have the same errors.
260 pub type BuilderError = ParserError;
262 #[derive(Clone, PartialEq, Debug)]
263 pub enum DecoderError {
264 ParseError(ParserError),
265 ExpectedError(string::String, string::String),
266 MissingFieldError(string::String),
267 UnknownVariantError(string::String),
268 ApplicationError(string::String),
271 #[derive(Copy, Clone, Debug)]
272 pub enum EncoderError {
273 FmtError(fmt::Error),
277 /// Returns a readable error string for a given error code.
278 pub fn error_str(error: ErrorCode) -> &'static str {
280 InvalidSyntax => "invalid syntax",
281 InvalidNumber => "invalid number",
282 EOFWhileParsingObject => "EOF While parsing object",
283 EOFWhileParsingArray => "EOF While parsing array",
284 EOFWhileParsingValue => "EOF While parsing value",
285 EOFWhileParsingString => "EOF While parsing string",
286 KeyMustBeAString => "key must be a string",
287 ExpectedColon => "expected `:`",
288 TrailingCharacters => "trailing characters",
289 TrailingComma => "trailing comma",
290 InvalidEscape => "invalid escape",
291 UnrecognizedHex => "invalid \\u{ esc}ape (unrecognized hex)",
292 NotFourDigit => "invalid \\u{ esc}ape (not four digits)",
293 NotUtf8 => "contents not utf-8",
294 InvalidUnicodeCodePoint => "invalid Unicode code point",
295 LoneLeadingSurrogateInHexEscape => "lone leading surrogate in hex escape",
296 UnexpectedEndOfHexEscape => "unexpected end of hex escape",
300 /// Shortcut function to decode a JSON `&str` into an object
301 pub fn decode<T: crate::Decodable>(s: &str) -> DecodeResult<T> {
302 let json = match from_str(s) {
304 Err(e) => return Err(ParseError(e)),
307 let mut decoder = Decoder::new(json);
308 crate::Decodable::decode(&mut decoder)
311 /// Shortcut function to encode a `T` into a JSON `String`
312 pub fn encode<T: crate::Encodable>(object: &T) -> Result<string::String, EncoderError> {
313 let mut s = String::new();
315 let mut encoder = Encoder::new(&mut s);
316 object.encode(&mut encoder)?;
321 impl fmt::Display for ErrorCode {
322 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
323 error_str(*self).fmt(f)
327 fn io_error_to_error(io: io::Error) -> ParserError {
328 IoError(io.kind(), io.to_string())
331 impl fmt::Display for ParserError {
332 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
333 // FIXME this should be a nicer error
334 fmt::Debug::fmt(self, f)
338 impl fmt::Display for DecoderError {
339 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
340 // FIXME this should be a nicer error
341 fmt::Debug::fmt(self, f)
345 impl std::error::Error for DecoderError {}
347 impl fmt::Display for EncoderError {
348 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
349 // FIXME this should be a nicer error
350 fmt::Debug::fmt(self, f)
354 impl std::error::Error for EncoderError {}
356 impl From<fmt::Error> for EncoderError {
357 /// Converts a [`fmt::Error`] into `EncoderError`
359 /// This conversion does not allocate memory.
360 fn from(err: fmt::Error) -> EncoderError {
361 EncoderError::FmtError(err)
365 pub type EncodeResult = Result<(), EncoderError>;
366 pub type DecodeResult<T> = Result<T, DecoderError>;
368 fn escape_str(wr: &mut dyn fmt::Write, v: &str) -> EncodeResult {
373 for (i, byte) in v.bytes().enumerate() {
374 let escaped = match byte {
377 b'\x00' => "\\u0000",
378 b'\x01' => "\\u0001",
379 b'\x02' => "\\u0002",
380 b'\x03' => "\\u0003",
381 b'\x04' => "\\u0004",
382 b'\x05' => "\\u0005",
383 b'\x06' => "\\u0006",
384 b'\x07' => "\\u0007",
388 b'\x0b' => "\\u000b",
391 b'\x0e' => "\\u000e",
392 b'\x0f' => "\\u000f",
393 b'\x10' => "\\u0010",
394 b'\x11' => "\\u0011",
395 b'\x12' => "\\u0012",
396 b'\x13' => "\\u0013",
397 b'\x14' => "\\u0014",
398 b'\x15' => "\\u0015",
399 b'\x16' => "\\u0016",
400 b'\x17' => "\\u0017",
401 b'\x18' => "\\u0018",
402 b'\x19' => "\\u0019",
403 b'\x1a' => "\\u001a",
404 b'\x1b' => "\\u001b",
405 b'\x1c' => "\\u001c",
406 b'\x1d' => "\\u001d",
407 b'\x1e' => "\\u001e",
408 b'\x1f' => "\\u001f",
409 b'\x7f' => "\\u007f",
416 wr.write_str(&v[start..i])?;
419 wr.write_str(escaped)?;
424 if start != v.len() {
425 wr.write_str(&v[start..])?;
432 fn escape_char(writer: &mut dyn fmt::Write, v: char) -> EncodeResult {
433 escape_str(writer, v.encode_utf8(&mut [0; 4]))
436 fn spaces(wr: &mut dyn fmt::Write, mut n: usize) -> EncodeResult {
437 const BUF: &str = " ";
439 while n >= BUF.len() {
445 wr.write_str(&BUF[..n])?;
450 fn fmt_number_or_null(v: f64) -> string::String {
452 Fp::Nan | Fp::Infinite => string::String::from("null"),
453 _ if v.fract() != 0f64 => v.to_string(),
454 _ => v.to_string() + ".0",
458 /// A structure for implementing serialization to JSON.
459 pub struct Encoder<'a> {
460 writer: &'a mut (dyn fmt::Write + 'a),
461 is_emitting_map_key: bool,
464 impl<'a> Encoder<'a> {
465 /// Creates a new JSON encoder whose output will be written to the writer
467 pub fn new(writer: &'a mut dyn fmt::Write) -> Encoder<'a> {
468 Encoder { writer, is_emitting_map_key: false }
472 macro_rules! emit_enquoted_if_mapkey {
473 ($enc:ident,$e:expr) => {{
474 if $enc.is_emitting_map_key {
475 write!($enc.writer, "\"{}\"", $e)?;
477 write!($enc.writer, "{}", $e)?;
483 impl<'a> crate::Encoder for Encoder<'a> {
484 type Error = EncoderError;
486 fn emit_unit(&mut self) -> EncodeResult {
487 if self.is_emitting_map_key {
488 return Err(EncoderError::BadHashmapKey);
490 write!(self.writer, "null")?;
494 fn emit_usize(&mut self, v: usize) -> EncodeResult {
495 emit_enquoted_if_mapkey!(self, v)
497 fn emit_u128(&mut self, v: u128) -> EncodeResult {
498 emit_enquoted_if_mapkey!(self, v)
500 fn emit_u64(&mut self, v: u64) -> EncodeResult {
501 emit_enquoted_if_mapkey!(self, v)
503 fn emit_u32(&mut self, v: u32) -> EncodeResult {
504 emit_enquoted_if_mapkey!(self, v)
506 fn emit_u16(&mut self, v: u16) -> EncodeResult {
507 emit_enquoted_if_mapkey!(self, v)
509 fn emit_u8(&mut self, v: u8) -> EncodeResult {
510 emit_enquoted_if_mapkey!(self, v)
513 fn emit_isize(&mut self, v: isize) -> EncodeResult {
514 emit_enquoted_if_mapkey!(self, v)
516 fn emit_i128(&mut self, v: i128) -> EncodeResult {
517 emit_enquoted_if_mapkey!(self, v)
519 fn emit_i64(&mut self, v: i64) -> EncodeResult {
520 emit_enquoted_if_mapkey!(self, v)
522 fn emit_i32(&mut self, v: i32) -> EncodeResult {
523 emit_enquoted_if_mapkey!(self, v)
525 fn emit_i16(&mut self, v: i16) -> EncodeResult {
526 emit_enquoted_if_mapkey!(self, v)
528 fn emit_i8(&mut self, v: i8) -> EncodeResult {
529 emit_enquoted_if_mapkey!(self, v)
532 fn emit_bool(&mut self, v: bool) -> EncodeResult {
533 if self.is_emitting_map_key {
534 return Err(EncoderError::BadHashmapKey);
537 write!(self.writer, "true")?;
539 write!(self.writer, "false")?;
544 fn emit_f64(&mut self, v: f64) -> EncodeResult {
545 emit_enquoted_if_mapkey!(self, fmt_number_or_null(v))
547 fn emit_f32(&mut self, v: f32) -> EncodeResult {
548 self.emit_f64(f64::from(v))
551 fn emit_char(&mut self, v: char) -> EncodeResult {
552 escape_char(self.writer, v)
554 fn emit_str(&mut self, v: &str) -> EncodeResult {
555 escape_str(self.writer, v)
558 fn emit_enum<F>(&mut self, _name: &str, f: F) -> EncodeResult
560 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
565 fn emit_enum_variant<F>(&mut self, name: &str, _id: usize, cnt: usize, f: F) -> EncodeResult
567 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
569 // enums are encoded as strings or objects
571 // Kangaroo(34,"William") => {"variant": "Kangaroo", "fields": [34,"William"]}
573 escape_str(self.writer, name)
575 if self.is_emitting_map_key {
576 return Err(EncoderError::BadHashmapKey);
578 write!(self.writer, "{{\"variant\":")?;
579 escape_str(self.writer, name)?;
580 write!(self.writer, ",\"fields\":[")?;
582 write!(self.writer, "]}}")?;
587 fn emit_enum_variant_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult
589 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
591 if self.is_emitting_map_key {
592 return Err(EncoderError::BadHashmapKey);
595 write!(self.writer, ",")?;
600 fn emit_enum_struct_variant<F>(
608 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
610 if self.is_emitting_map_key {
611 return Err(EncoderError::BadHashmapKey);
613 self.emit_enum_variant(name, id, cnt, f)
616 fn emit_enum_struct_variant_field<F>(&mut self, _: &str, idx: usize, f: F) -> EncodeResult
618 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
620 if self.is_emitting_map_key {
621 return Err(EncoderError::BadHashmapKey);
623 self.emit_enum_variant_arg(idx, f)
626 fn emit_struct<F>(&mut self, _: &str, _: usize, f: F) -> EncodeResult
628 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
630 if self.is_emitting_map_key {
631 return Err(EncoderError::BadHashmapKey);
633 write!(self.writer, "{{")?;
635 write!(self.writer, "}}")?;
639 fn emit_struct_field<F>(&mut self, name: &str, idx: usize, f: F) -> EncodeResult
641 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
643 if self.is_emitting_map_key {
644 return Err(EncoderError::BadHashmapKey);
647 write!(self.writer, ",")?;
649 escape_str(self.writer, name)?;
650 write!(self.writer, ":")?;
654 fn emit_tuple<F>(&mut self, len: usize, f: F) -> EncodeResult
656 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
658 if self.is_emitting_map_key {
659 return Err(EncoderError::BadHashmapKey);
661 self.emit_seq(len, f)
663 fn emit_tuple_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult
665 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
667 if self.is_emitting_map_key {
668 return Err(EncoderError::BadHashmapKey);
670 self.emit_seq_elt(idx, f)
673 fn emit_tuple_struct<F>(&mut self, _name: &str, len: usize, f: F) -> EncodeResult
675 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
677 if self.is_emitting_map_key {
678 return Err(EncoderError::BadHashmapKey);
680 self.emit_seq(len, f)
682 fn emit_tuple_struct_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult
684 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
686 if self.is_emitting_map_key {
687 return Err(EncoderError::BadHashmapKey);
689 self.emit_seq_elt(idx, f)
692 fn emit_option<F>(&mut self, f: F) -> EncodeResult
694 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
696 if self.is_emitting_map_key {
697 return Err(EncoderError::BadHashmapKey);
701 fn emit_option_none(&mut self) -> EncodeResult {
702 if self.is_emitting_map_key {
703 return Err(EncoderError::BadHashmapKey);
707 fn emit_option_some<F>(&mut self, f: F) -> EncodeResult
709 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
711 if self.is_emitting_map_key {
712 return Err(EncoderError::BadHashmapKey);
717 fn emit_seq<F>(&mut self, _len: usize, f: F) -> EncodeResult
719 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
721 if self.is_emitting_map_key {
722 return Err(EncoderError::BadHashmapKey);
724 write!(self.writer, "[")?;
726 write!(self.writer, "]")?;
730 fn emit_seq_elt<F>(&mut self, idx: usize, f: F) -> EncodeResult
732 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
734 if self.is_emitting_map_key {
735 return Err(EncoderError::BadHashmapKey);
738 write!(self.writer, ",")?;
743 fn emit_map<F>(&mut self, _len: usize, f: F) -> EncodeResult
745 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
747 if self.is_emitting_map_key {
748 return Err(EncoderError::BadHashmapKey);
750 write!(self.writer, "{{")?;
752 write!(self.writer, "}}")?;
756 fn emit_map_elt_key<F>(&mut self, idx: usize, f: F) -> EncodeResult
758 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
760 if self.is_emitting_map_key {
761 return Err(EncoderError::BadHashmapKey);
764 write!(self.writer, ",")?
766 self.is_emitting_map_key = true;
768 self.is_emitting_map_key = false;
772 fn emit_map_elt_val<F>(&mut self, _idx: usize, f: F) -> EncodeResult
774 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
776 if self.is_emitting_map_key {
777 return Err(EncoderError::BadHashmapKey);
779 write!(self.writer, ":")?;
784 /// Another encoder for JSON, but prints out human-readable JSON instead of
786 pub struct PrettyEncoder<'a> {
787 writer: &'a mut (dyn fmt::Write + 'a),
790 is_emitting_map_key: bool,
793 impl<'a> PrettyEncoder<'a> {
794 /// Creates a new encoder whose output will be written to the specified writer
795 pub fn new(writer: &'a mut dyn fmt::Write) -> PrettyEncoder<'a> {
796 PrettyEncoder { writer, curr_indent: 0, indent: 2, is_emitting_map_key: false }
799 /// Sets the number of spaces to indent for each level.
800 /// This is safe to set during encoding.
801 pub fn set_indent(&mut self, indent: usize) {
802 // self.indent very well could be 0 so we need to use checked division.
803 let level = self.curr_indent.checked_div(self.indent).unwrap_or(0);
804 self.indent = indent;
805 self.curr_indent = level * self.indent;
809 impl<'a> crate::Encoder for PrettyEncoder<'a> {
810 type Error = EncoderError;
812 fn emit_unit(&mut self) -> EncodeResult {
813 if self.is_emitting_map_key {
814 return Err(EncoderError::BadHashmapKey);
816 write!(self.writer, "null")?;
820 fn emit_usize(&mut self, v: usize) -> EncodeResult {
821 emit_enquoted_if_mapkey!(self, v)
823 fn emit_u128(&mut self, v: u128) -> EncodeResult {
824 emit_enquoted_if_mapkey!(self, v)
826 fn emit_u64(&mut self, v: u64) -> EncodeResult {
827 emit_enquoted_if_mapkey!(self, v)
829 fn emit_u32(&mut self, v: u32) -> EncodeResult {
830 emit_enquoted_if_mapkey!(self, v)
832 fn emit_u16(&mut self, v: u16) -> EncodeResult {
833 emit_enquoted_if_mapkey!(self, v)
835 fn emit_u8(&mut self, v: u8) -> EncodeResult {
836 emit_enquoted_if_mapkey!(self, v)
839 fn emit_isize(&mut self, v: isize) -> EncodeResult {
840 emit_enquoted_if_mapkey!(self, v)
842 fn emit_i128(&mut self, v: i128) -> EncodeResult {
843 emit_enquoted_if_mapkey!(self, v)
845 fn emit_i64(&mut self, v: i64) -> EncodeResult {
846 emit_enquoted_if_mapkey!(self, v)
848 fn emit_i32(&mut self, v: i32) -> EncodeResult {
849 emit_enquoted_if_mapkey!(self, v)
851 fn emit_i16(&mut self, v: i16) -> EncodeResult {
852 emit_enquoted_if_mapkey!(self, v)
854 fn emit_i8(&mut self, v: i8) -> EncodeResult {
855 emit_enquoted_if_mapkey!(self, v)
858 fn emit_bool(&mut self, v: bool) -> EncodeResult {
859 if self.is_emitting_map_key {
860 return Err(EncoderError::BadHashmapKey);
863 write!(self.writer, "true")?;
865 write!(self.writer, "false")?;
870 fn emit_f64(&mut self, v: f64) -> EncodeResult {
871 emit_enquoted_if_mapkey!(self, fmt_number_or_null(v))
873 fn emit_f32(&mut self, v: f32) -> EncodeResult {
874 self.emit_f64(f64::from(v))
877 fn emit_char(&mut self, v: char) -> EncodeResult {
878 escape_char(self.writer, v)
880 fn emit_str(&mut self, v: &str) -> EncodeResult {
881 escape_str(self.writer, v)
884 fn emit_enum<F>(&mut self, _name: &str, f: F) -> EncodeResult
886 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
891 fn emit_enum_variant<F>(&mut self, name: &str, _id: usize, cnt: usize, f: F) -> EncodeResult
893 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
896 escape_str(self.writer, name)
898 if self.is_emitting_map_key {
899 return Err(EncoderError::BadHashmapKey);
901 writeln!(self.writer, "{{")?;
902 self.curr_indent += self.indent;
903 spaces(self.writer, self.curr_indent)?;
904 write!(self.writer, "\"variant\": ")?;
905 escape_str(self.writer, name)?;
906 writeln!(self.writer, ",")?;
907 spaces(self.writer, self.curr_indent)?;
908 writeln!(self.writer, "\"fields\": [")?;
909 self.curr_indent += self.indent;
911 self.curr_indent -= self.indent;
912 writeln!(self.writer)?;
913 spaces(self.writer, self.curr_indent)?;
914 self.curr_indent -= self.indent;
915 writeln!(self.writer, "]")?;
916 spaces(self.writer, self.curr_indent)?;
917 write!(self.writer, "}}")?;
922 fn emit_enum_variant_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult
924 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
926 if self.is_emitting_map_key {
927 return Err(EncoderError::BadHashmapKey);
930 writeln!(self.writer, ",")?;
932 spaces(self.writer, self.curr_indent)?;
936 fn emit_enum_struct_variant<F>(
944 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
946 if self.is_emitting_map_key {
947 return Err(EncoderError::BadHashmapKey);
949 self.emit_enum_variant(name, id, cnt, f)
952 fn emit_enum_struct_variant_field<F>(&mut self, _: &str, idx: usize, f: F) -> EncodeResult
954 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
956 if self.is_emitting_map_key {
957 return Err(EncoderError::BadHashmapKey);
959 self.emit_enum_variant_arg(idx, f)
962 fn emit_struct<F>(&mut self, _: &str, len: usize, f: F) -> EncodeResult
964 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
966 if self.is_emitting_map_key {
967 return Err(EncoderError::BadHashmapKey);
970 write!(self.writer, "{{}}")?;
972 write!(self.writer, "{{")?;
973 self.curr_indent += self.indent;
975 self.curr_indent -= self.indent;
976 writeln!(self.writer)?;
977 spaces(self.writer, self.curr_indent)?;
978 write!(self.writer, "}}")?;
983 fn emit_struct_field<F>(&mut self, name: &str, idx: usize, f: F) -> EncodeResult
985 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
987 if self.is_emitting_map_key {
988 return Err(EncoderError::BadHashmapKey);
991 writeln!(self.writer)?;
993 writeln!(self.writer, ",")?;
995 spaces(self.writer, self.curr_indent)?;
996 escape_str(self.writer, name)?;
997 write!(self.writer, ": ")?;
1001 fn emit_tuple<F>(&mut self, len: usize, f: F) -> EncodeResult
1003 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
1005 if self.is_emitting_map_key {
1006 return Err(EncoderError::BadHashmapKey);
1008 self.emit_seq(len, f)
1010 fn emit_tuple_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult
1012 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
1014 if self.is_emitting_map_key {
1015 return Err(EncoderError::BadHashmapKey);
1017 self.emit_seq_elt(idx, f)
1020 fn emit_tuple_struct<F>(&mut self, _: &str, len: usize, f: F) -> EncodeResult
1022 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
1024 if self.is_emitting_map_key {
1025 return Err(EncoderError::BadHashmapKey);
1027 self.emit_seq(len, f)
1029 fn emit_tuple_struct_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult
1031 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
1033 if self.is_emitting_map_key {
1034 return Err(EncoderError::BadHashmapKey);
1036 self.emit_seq_elt(idx, f)
1039 fn emit_option<F>(&mut self, f: F) -> EncodeResult
1041 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
1043 if self.is_emitting_map_key {
1044 return Err(EncoderError::BadHashmapKey);
1048 fn emit_option_none(&mut self) -> EncodeResult {
1049 if self.is_emitting_map_key {
1050 return Err(EncoderError::BadHashmapKey);
1054 fn emit_option_some<F>(&mut self, f: F) -> EncodeResult
1056 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
1058 if self.is_emitting_map_key {
1059 return Err(EncoderError::BadHashmapKey);
1064 fn emit_seq<F>(&mut self, len: usize, f: F) -> EncodeResult
1066 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
1068 if self.is_emitting_map_key {
1069 return Err(EncoderError::BadHashmapKey);
1072 write!(self.writer, "[]")?;
1074 write!(self.writer, "[")?;
1075 self.curr_indent += self.indent;
1077 self.curr_indent -= self.indent;
1078 writeln!(self.writer)?;
1079 spaces(self.writer, self.curr_indent)?;
1080 write!(self.writer, "]")?;
1085 fn emit_seq_elt<F>(&mut self, idx: usize, f: F) -> EncodeResult
1087 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
1089 if self.is_emitting_map_key {
1090 return Err(EncoderError::BadHashmapKey);
1093 writeln!(self.writer)?;
1095 writeln!(self.writer, ",")?;
1097 spaces(self.writer, self.curr_indent)?;
1101 fn emit_map<F>(&mut self, len: usize, f: F) -> EncodeResult
1103 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
1105 if self.is_emitting_map_key {
1106 return Err(EncoderError::BadHashmapKey);
1109 write!(self.writer, "{{}}")?;
1111 write!(self.writer, "{{")?;
1112 self.curr_indent += self.indent;
1114 self.curr_indent -= self.indent;
1115 writeln!(self.writer)?;
1116 spaces(self.writer, self.curr_indent)?;
1117 write!(self.writer, "}}")?;
1122 fn emit_map_elt_key<F>(&mut self, idx: usize, f: F) -> EncodeResult
1124 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
1126 if self.is_emitting_map_key {
1127 return Err(EncoderError::BadHashmapKey);
1130 writeln!(self.writer)?;
1132 writeln!(self.writer, ",")?;
1134 spaces(self.writer, self.curr_indent)?;
1135 self.is_emitting_map_key = true;
1137 self.is_emitting_map_key = false;
1141 fn emit_map_elt_val<F>(&mut self, _idx: usize, f: F) -> EncodeResult
1143 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
1145 if self.is_emitting_map_key {
1146 return Err(EncoderError::BadHashmapKey);
1148 write!(self.writer, ": ")?;
1153 impl Encodable for Json {
1154 fn encode<E: crate::Encoder>(&self, e: &mut E) -> Result<(), E::Error> {
1156 Json::I64(v) => v.encode(e),
1157 Json::U64(v) => v.encode(e),
1158 Json::F64(v) => v.encode(e),
1159 Json::String(ref v) => v.encode(e),
1160 Json::Boolean(v) => v.encode(e),
1161 Json::Array(ref v) => v.encode(e),
1162 Json::Object(ref v) => v.encode(e),
1163 Json::Null => e.emit_unit(),
1168 /// Creates an `AsJson` wrapper which can be used to print a value as JSON
1169 /// on-the-fly via `write!`
1170 pub fn as_json<T>(t: &T) -> AsJson<'_, T> {
1174 /// Creates an `AsPrettyJson` wrapper which can be used to print a value as JSON
1175 /// on-the-fly via `write!`
1176 pub fn as_pretty_json<T>(t: &T) -> AsPrettyJson<'_, T> {
1177 AsPrettyJson { inner: t, indent: None }
1181 /// Borrow this json object as a pretty object to generate a pretty
1182 /// representation for it via `Display`.
1183 pub fn pretty(&self) -> PrettyJson<'_> {
1184 PrettyJson { inner: self }
1187 /// If the Json value is an Object, returns the value associated with the provided key.
1188 /// Otherwise, returns None.
1189 pub fn find(&self, key: &str) -> Option<&Json> {
1191 Json::Object(ref map) => map.get(key),
1196 /// Attempts to get a nested Json Object for each key in `keys`.
1197 /// If any key is found not to exist, `find_path` will return `None`.
1198 /// Otherwise, it will return the Json value associated with the final key.
1199 pub fn find_path<'a>(&'a self, keys: &[&str]) -> Option<&'a Json> {
1200 let mut target = self;
1202 target = target.find(*key)?;
1207 /// If the Json value is an Object, performs a depth-first search until
1208 /// a value associated with the provided key is found. If no value is found
1209 /// or the Json value is not an Object, returns `None`.
1210 pub fn search(&self, key: &str) -> Option<&Json> {
1212 Json::Object(ref map) => match map.get(key) {
1213 Some(json_value) => Some(json_value),
1215 for v in map.values() {
1216 match v.search(key) {
1217 x if x.is_some() => return x,
1228 /// Returns `true` if the Json value is an `Object`.
1229 pub fn is_object(&self) -> bool {
1230 self.as_object().is_some()
1233 /// If the Json value is an `Object`, returns the associated `BTreeMap`;
1234 /// returns `None` otherwise.
1235 pub fn as_object(&self) -> Option<&Object> {
1237 Json::Object(ref map) => Some(map),
1242 /// Returns `true` if the Json value is an `Array`.
1243 pub fn is_array(&self) -> bool {
1244 self.as_array().is_some()
1247 /// If the Json value is an `Array`, returns the associated vector;
1248 /// returns `None` otherwise.
1249 pub fn as_array(&self) -> Option<&Array> {
1251 Json::Array(ref array) => Some(&*array),
1256 /// Returns `true` if the Json value is a `String`.
1257 pub fn is_string(&self) -> bool {
1258 self.as_string().is_some()
1261 /// If the Json value is a `String`, returns the associated `str`;
1262 /// returns `None` otherwise.
1263 pub fn as_string(&self) -> Option<&str> {
1265 Json::String(ref s) => Some(&s[..]),
1270 /// Returns `true` if the Json value is a `Number`.
1271 pub fn is_number(&self) -> bool {
1273 Json::I64(_) | Json::U64(_) | Json::F64(_) => true,
1278 /// Returns `true` if the Json value is a `i64`.
1279 pub fn is_i64(&self) -> bool {
1281 Json::I64(_) => true,
1286 /// Returns `true` if the Json value is a `u64`.
1287 pub fn is_u64(&self) -> bool {
1289 Json::U64(_) => true,
1294 /// Returns `true` if the Json value is a `f64`.
1295 pub fn is_f64(&self) -> bool {
1297 Json::F64(_) => true,
1302 /// If the Json value is a number, returns or cast it to a `i64`;
1303 /// returns `None` otherwise.
1304 pub fn as_i64(&self) -> Option<i64> {
1306 Json::I64(n) => Some(n),
1307 Json::U64(n) => Some(n as i64),
1312 /// If the Json value is a number, returns or cast it to a `u64`;
1313 /// returns `None` otherwise.
1314 pub fn as_u64(&self) -> Option<u64> {
1316 Json::I64(n) => Some(n as u64),
1317 Json::U64(n) => Some(n),
1322 /// If the Json value is a number, returns or cast it to a `f64`;
1323 /// returns `None` otherwise.
1324 pub fn as_f64(&self) -> Option<f64> {
1326 Json::I64(n) => Some(n as f64),
1327 Json::U64(n) => Some(n as f64),
1328 Json::F64(n) => Some(n),
1333 /// Returns `true` if the Json value is a `Boolean`.
1334 pub fn is_boolean(&self) -> bool {
1335 self.as_boolean().is_some()
1338 /// If the Json value is a `Boolean`, returns the associated `bool`;
1339 /// returns `None` otherwise.
1340 pub fn as_boolean(&self) -> Option<bool> {
1342 Json::Boolean(b) => Some(b),
1347 /// Returns `true` if the Json value is a `Null`.
1348 pub fn is_null(&self) -> bool {
1349 self.as_null().is_some()
1352 /// If the Json value is a `Null`, returns `()`;
1353 /// returns `None` otherwise.
1354 pub fn as_null(&self) -> Option<()> {
1356 Json::Null => Some(()),
1362 impl<'a> Index<&'a str> for Json {
1365 fn index(&self, idx: &'a str) -> &Json {
1366 self.find(idx).unwrap()
1370 impl Index<usize> for Json {
1373 fn index(&self, idx: usize) -> &Json {
1375 Json::Array(ref v) => &v[idx],
1376 _ => panic!("can only index Json with usize if it is an array"),
1381 /// The output of the streaming parser.
1382 #[derive(PartialEq, Clone, Debug)]
1383 pub enum JsonEvent {
1392 StringValue(string::String),
1397 #[derive(PartialEq, Debug)]
1399 // Parse a value in an array, true means first element.
1401 // Parse ',' or ']' after an element in an array.
1403 // Parse a key:value in an object, true means first element.
1405 // Parse ',' or ']' after an element in an object.
1409 // Expecting the stream to end.
1411 // Parsing can't continue.
1415 /// A Stack represents the current position of the parser in the logical
1416 /// structure of the JSON stream.
1418 /// An example is `foo.bar[3].x`.
1420 stack: Vec<InternalStackElement>,
1421 str_buffer: Vec<u8>,
1424 /// StackElements compose a Stack.
1426 /// As an example, `StackElement::Key("foo")`, `StackElement::Key("bar")`,
1427 /// `StackElement::Index(3)`, and `StackElement::Key("x")` are the
1428 /// StackElements composing the stack that represents `foo.bar[3].x`.
1429 #[derive(PartialEq, Clone, Debug)]
1430 pub enum StackElement<'l> {
1435 // Internally, Key elements are stored as indices in a buffer to avoid
1436 // allocating a string for every member of an object.
1437 #[derive(PartialEq, Clone, Debug)]
1438 enum InternalStackElement {
1440 InternalKey(u16, u16), // start, size
1444 pub fn new() -> Stack {
1445 Stack { stack: Vec::new(), str_buffer: Vec::new() }
1448 /// Returns The number of elements in the Stack.
1449 pub fn len(&self) -> usize {
1453 /// Returns `true` if the stack is empty.
1454 pub fn is_empty(&self) -> bool {
1455 self.stack.is_empty()
1458 /// Provides access to the StackElement at a given index.
1459 /// lower indices are at the bottom of the stack while higher indices are
1461 pub fn get(&self, idx: usize) -> StackElement<'_> {
1462 match self.stack[idx] {
1463 InternalIndex(i) => StackElement::Index(i),
1464 InternalKey(start, size) => StackElement::Key(
1465 str::from_utf8(&self.str_buffer[start as usize..start as usize + size as usize])
1471 /// Compares this stack with an array of StackElement<'_>s.
1472 pub fn is_equal_to(&self, rhs: &[StackElement<'_>]) -> bool {
1473 if self.stack.len() != rhs.len() {
1476 for (i, r) in rhs.iter().enumerate() {
1477 if self.get(i) != *r {
1484 /// Returns `true` if the bottom-most elements of this stack are the same as
1485 /// the ones passed as parameter.
1486 pub fn starts_with(&self, rhs: &[StackElement<'_>]) -> bool {
1487 if self.stack.len() < rhs.len() {
1490 for (i, r) in rhs.iter().enumerate() {
1491 if self.get(i) != *r {
1498 /// Returns `true` if the top-most elements of this stack are the same as
1499 /// the ones passed as parameter.
1500 pub fn ends_with(&self, rhs: &[StackElement<'_>]) -> bool {
1501 if self.stack.len() < rhs.len() {
1504 let offset = self.stack.len() - rhs.len();
1505 for (i, r) in rhs.iter().enumerate() {
1506 if self.get(i + offset) != *r {
1513 /// Returns the top-most element (if any).
1514 pub fn top(&self) -> Option<StackElement<'_>> {
1515 match self.stack.last() {
1517 Some(&InternalIndex(i)) => Some(StackElement::Index(i)),
1518 Some(&InternalKey(start, size)) => Some(StackElement::Key(
1519 str::from_utf8(&self.str_buffer[start as usize..(start + size) as usize]).unwrap(),
1524 // Used by Parser to insert StackElement::Key elements at the top of the stack.
1525 fn push_key(&mut self, key: string::String) {
1526 self.stack.push(InternalKey(self.str_buffer.len() as u16, key.len() as u16));
1527 self.str_buffer.extend(key.as_bytes());
1530 // Used by Parser to insert StackElement::Index elements at the top of the stack.
1531 fn push_index(&mut self, index: u32) {
1532 self.stack.push(InternalIndex(index));
1535 // Used by Parser to remove the top-most element of the stack.
1537 assert!(!self.is_empty());
1538 match *self.stack.last().unwrap() {
1539 InternalKey(_, sz) => {
1540 let new_size = self.str_buffer.len() - sz as usize;
1541 self.str_buffer.truncate(new_size);
1543 InternalIndex(_) => {}
1548 // Used by Parser to test whether the top-most element is an index.
1549 fn last_is_index(&self) -> bool {
1550 match self.stack.last() {
1551 Some(InternalIndex(_)) => true,
1556 // Used by Parser to increment the index of the top-most element.
1557 fn bump_index(&mut self) {
1558 let len = self.stack.len();
1559 let idx = match *self.stack.last().unwrap() {
1560 InternalIndex(i) => i + 1,
1565 self.stack[len - 1] = InternalIndex(idx);
1569 /// A streaming JSON parser implemented as an iterator of JsonEvent, consuming
1570 /// an iterator of char.
1571 pub struct Parser<T> {
1576 // We maintain a stack representing where we are in the logical structure
1577 // of the JSON stream.
1579 // A state machine is kept to make it possible to interrupt and resume parsing.
1583 impl<T: Iterator<Item = char>> Iterator for Parser<T> {
1584 type Item = JsonEvent;
1586 fn next(&mut self) -> Option<JsonEvent> {
1587 if self.state == ParseFinished {
1591 if self.state == ParseBeforeFinish {
1592 self.parse_whitespace();
1593 // Make sure there is no trailing characters.
1595 self.state = ParseFinished;
1598 return Some(self.error_event(TrailingCharacters));
1606 impl<T: Iterator<Item = char>> Parser<T> {
1607 /// Creates the JSON parser.
1608 pub fn new(rdr: T) -> Parser<T> {
1609 let mut p = Parser {
1614 stack: Stack::new(),
1621 /// Provides access to the current position in the logical structure of the
1623 pub fn stack(&self) -> &Stack {
1627 fn eof(&self) -> bool {
1630 fn ch_or_null(&self) -> char {
1631 self.ch.unwrap_or('\x00')
1633 fn bump(&mut self) {
1634 self.ch = self.rdr.next();
1636 if self.ch_is('\n') {
1644 fn next_char(&mut self) -> Option<char> {
1648 fn ch_is(&self, c: char) -> bool {
1652 fn error<U>(&self, reason: ErrorCode) -> Result<U, ParserError> {
1653 Err(SyntaxError(reason, self.line, self.col))
1656 fn parse_whitespace(&mut self) {
1657 while self.ch_is(' ') || self.ch_is('\n') || self.ch_is('\t') || self.ch_is('\r') {
1662 fn parse_number(&mut self) -> JsonEvent {
1663 let neg = if self.ch_is('-') {
1670 let res = match self.parse_u64() {
1677 if self.ch_is('.') || self.ch_is('e') || self.ch_is('E') {
1678 let mut res = res as f64;
1680 if self.ch_is('.') {
1681 res = match self.parse_decimal(res) {
1689 if self.ch_is('e') || self.ch_is('E') {
1690 res = match self.parse_exponent(res) {
1704 let res = (res as i64).wrapping_neg();
1706 // Make sure we didn't underflow.
1708 Error(SyntaxError(InvalidNumber, self.line, self.col))
1717 fn parse_u64(&mut self) -> Result<u64, ParserError> {
1718 let mut accum = 0u64;
1719 let last_accum = 0; // necessary to detect overflow.
1721 match self.ch_or_null() {
1725 // A leading '0' must be the only digit before the decimal point.
1726 if let '0'..='9' = self.ch_or_null() {
1727 return self.error(InvalidNumber);
1732 match self.ch_or_null() {
1734 accum = accum.wrapping_mul(10);
1735 accum = accum.wrapping_add((c as u64) - ('0' as u64));
1737 // Detect overflow by comparing to the last value.
1738 if accum <= last_accum {
1739 return self.error(InvalidNumber);
1748 _ => return self.error(InvalidNumber),
1754 fn parse_decimal(&mut self, mut res: f64) -> Result<f64, ParserError> {
1757 // Make sure a digit follows the decimal place.
1758 match self.ch_or_null() {
1760 _ => return self.error(InvalidNumber),
1765 match self.ch_or_null() {
1768 res += (((c as isize) - ('0' as isize)) as f64) * dec;
1778 fn parse_exponent(&mut self, mut res: f64) -> Result<f64, ParserError> {
1782 let mut neg_exp = false;
1784 if self.ch_is('+') {
1786 } else if self.ch_is('-') {
1791 // Make sure a digit follows the exponent place.
1792 match self.ch_or_null() {
1794 _ => return self.error(InvalidNumber),
1797 match self.ch_or_null() {
1800 exp += (c as usize) - ('0' as usize);
1808 let exp = 10_f64.powi(exp as i32);
1818 fn decode_hex_escape(&mut self) -> Result<u16, ParserError> {
1821 while i < 4 && !self.eof() {
1823 n = match self.ch_or_null() {
1824 c @ '0'..='9' => n * 16 + ((c as u16) - ('0' as u16)),
1825 'a' | 'A' => n * 16 + 10,
1826 'b' | 'B' => n * 16 + 11,
1827 'c' | 'C' => n * 16 + 12,
1828 'd' | 'D' => n * 16 + 13,
1829 'e' | 'E' => n * 16 + 14,
1830 'f' | 'F' => n * 16 + 15,
1831 _ => return self.error(InvalidEscape),
1837 // Error out if we didn't parse 4 digits.
1839 return self.error(InvalidEscape);
1845 fn parse_str(&mut self) -> Result<string::String, ParserError> {
1846 let mut escape = false;
1847 let mut res = string::String::new();
1852 return self.error(EOFWhileParsingString);
1856 match self.ch_or_null() {
1857 '"' => res.push('"'),
1858 '\\' => res.push('\\'),
1859 '/' => res.push('/'),
1860 'b' => res.push('\x08'),
1861 'f' => res.push('\x0c'),
1862 'n' => res.push('\n'),
1863 'r' => res.push('\r'),
1864 't' => res.push('\t'),
1865 'u' => match self.decode_hex_escape()? {
1866 0xDC00..=0xDFFF => return self.error(LoneLeadingSurrogateInHexEscape),
1868 // Non-BMP characters are encoded as a sequence of
1869 // two hex escapes, representing UTF-16 surrogates.
1870 n1 @ 0xD800..=0xDBFF => {
1871 match (self.next_char(), self.next_char()) {
1872 (Some('\\'), Some('u')) => (),
1873 _ => return self.error(UnexpectedEndOfHexEscape),
1876 let n2 = self.decode_hex_escape()?;
1877 if n2 < 0xDC00 || n2 > 0xDFFF {
1878 return self.error(LoneLeadingSurrogateInHexEscape);
1881 (u32::from(n1 - 0xD800) << 10 | u32::from(n2 - 0xDC00)) + 0x1_0000;
1882 res.push(char::from_u32(c).unwrap());
1885 n => match char::from_u32(u32::from(n)) {
1886 Some(c) => res.push(c),
1887 None => return self.error(InvalidUnicodeCodePoint),
1890 _ => return self.error(InvalidEscape),
1893 } else if self.ch_is('\\') {
1901 Some(c) => res.push(c),
1902 None => unreachable!(),
1908 // Invoked at each iteration, consumes the stream until it has enough
1909 // information to return a JsonEvent.
1910 // Manages an internal state so that parsing can be interrupted and resumed.
1911 // Also keeps track of the position in the logical structure of the json
1912 // stream isize the form of a stack that can be queried by the user using the
1914 fn parse(&mut self) -> JsonEvent {
1916 // The only paths where the loop can spin a new iteration
1917 // are in the cases ParseArrayComma and ParseObjectComma if ','
1918 // is parsed. In these cases the state is set to (respectively)
1919 // ParseArray(false) and ParseObject(false), which always return,
1920 // so there is no risk of getting stuck in an infinite loop.
1921 // All other paths return before the end of the loop's iteration.
1922 self.parse_whitespace();
1926 return self.parse_start();
1928 ParseArray(first) => {
1929 return self.parse_array(first);
1931 ParseArrayComma => {
1932 if let Some(evt) = self.parse_array_comma_or_end() {
1936 ParseObject(first) => {
1937 return self.parse_object(first);
1939 ParseObjectComma => {
1941 if self.ch_is(',') {
1942 self.state = ParseObject(false);
1945 return self.parse_object_end();
1949 return self.error_event(InvalidSyntax);
1955 fn parse_start(&mut self) -> JsonEvent {
1956 let val = self.parse_value();
1957 self.state = match val {
1958 Error(_) => ParseFinished,
1959 ArrayStart => ParseArray(true),
1960 ObjectStart => ParseObject(true),
1961 _ => ParseBeforeFinish,
1966 fn parse_array(&mut self, first: bool) -> JsonEvent {
1967 if self.ch_is(']') {
1969 self.error_event(InvalidSyntax)
1971 self.state = if self.stack.is_empty() {
1973 } else if self.stack.last_is_index() {
1983 self.stack.push_index(0);
1985 let val = self.parse_value();
1986 self.state = match val {
1987 Error(_) => ParseFinished,
1988 ArrayStart => ParseArray(true),
1989 ObjectStart => ParseObject(true),
1990 _ => ParseArrayComma,
1996 fn parse_array_comma_or_end(&mut self) -> Option<JsonEvent> {
1997 if self.ch_is(',') {
1998 self.stack.bump_index();
1999 self.state = ParseArray(false);
2002 } else if self.ch_is(']') {
2004 self.state = if self.stack.is_empty() {
2006 } else if self.stack.last_is_index() {
2013 } else if self.eof() {
2014 Some(self.error_event(EOFWhileParsingArray))
2016 Some(self.error_event(InvalidSyntax))
2020 fn parse_object(&mut self, first: bool) -> JsonEvent {
2021 if self.ch_is('}') {
2023 if self.stack.is_empty() {
2024 return self.error_event(TrailingComma);
2029 self.state = if self.stack.is_empty() {
2031 } else if self.stack.last_is_index() {
2040 return self.error_event(EOFWhileParsingObject);
2042 if !self.ch_is('"') {
2043 return self.error_event(KeyMustBeAString);
2045 let s = match self.parse_str() {
2048 self.state = ParseFinished;
2052 self.parse_whitespace();
2054 return self.error_event(EOFWhileParsingObject);
2055 } else if self.ch_or_null() != ':' {
2056 return self.error_event(ExpectedColon);
2058 self.stack.push_key(s);
2060 self.parse_whitespace();
2062 let val = self.parse_value();
2064 self.state = match val {
2065 Error(_) => ParseFinished,
2066 ArrayStart => ParseArray(true),
2067 ObjectStart => ParseObject(true),
2068 _ => ParseObjectComma,
2073 fn parse_object_end(&mut self) -> JsonEvent {
2074 if self.ch_is('}') {
2075 self.state = if self.stack.is_empty() {
2077 } else if self.stack.last_is_index() {
2084 } else if self.eof() {
2085 self.error_event(EOFWhileParsingObject)
2087 self.error_event(InvalidSyntax)
2091 fn parse_value(&mut self) -> JsonEvent {
2093 return self.error_event(EOFWhileParsingValue);
2095 match self.ch_or_null() {
2096 'n' => self.parse_ident("ull", NullValue),
2097 't' => self.parse_ident("rue", BooleanValue(true)),
2098 'f' => self.parse_ident("alse", BooleanValue(false)),
2099 '0'..='9' | '-' => self.parse_number(),
2100 '"' => match self.parse_str() {
2101 Ok(s) => StringValue(s),
2112 _ => self.error_event(InvalidSyntax),
2116 fn parse_ident(&mut self, ident: &str, value: JsonEvent) -> JsonEvent {
2117 if ident.chars().all(|c| Some(c) == self.next_char()) {
2121 Error(SyntaxError(InvalidSyntax, self.line, self.col))
2125 fn error_event(&mut self, reason: ErrorCode) -> JsonEvent {
2126 self.state = ParseFinished;
2127 Error(SyntaxError(reason, self.line, self.col))
2131 /// A Builder consumes a json::Parser to create a generic Json structure.
2132 pub struct Builder<T> {
2134 token: Option<JsonEvent>,
2137 impl<T: Iterator<Item = char>> Builder<T> {
2138 /// Creates a JSON Builder.
2139 pub fn new(src: T) -> Builder<T> {
2140 Builder { parser: Parser::new(src), token: None }
2143 // Decode a Json value from a Parser.
2144 pub fn build(&mut self) -> Result<Json, BuilderError> {
2146 let result = self.build_value();
2150 Some(Error(ref e)) => {
2151 return Err(e.clone());
2154 panic!("unexpected token {:?}", tok.clone());
2160 fn bump(&mut self) {
2161 self.token = self.parser.next();
2164 fn build_value(&mut self) -> Result<Json, BuilderError> {
2166 Some(NullValue) => Ok(Json::Null),
2167 Some(I64Value(n)) => Ok(Json::I64(n)),
2168 Some(U64Value(n)) => Ok(Json::U64(n)),
2169 Some(F64Value(n)) => Ok(Json::F64(n)),
2170 Some(BooleanValue(b)) => Ok(Json::Boolean(b)),
2171 Some(StringValue(ref mut s)) => {
2172 let mut temp = string::String::new();
2174 Ok(Json::String(temp))
2176 Some(Error(ref e)) => Err(e.clone()),
2177 Some(ArrayStart) => self.build_array(),
2178 Some(ObjectStart) => self.build_object(),
2179 Some(ObjectEnd) => self.parser.error(InvalidSyntax),
2180 Some(ArrayEnd) => self.parser.error(InvalidSyntax),
2181 None => self.parser.error(EOFWhileParsingValue),
2185 fn build_array(&mut self) -> Result<Json, BuilderError> {
2187 let mut values = Vec::new();
2190 if self.token == Some(ArrayEnd) {
2191 return Ok(Json::Array(values.into_iter().collect()));
2193 match self.build_value() {
2194 Ok(v) => values.push(v),
2195 Err(e) => return Err(e),
2201 fn build_object(&mut self) -> Result<Json, BuilderError> {
2204 let mut values = BTreeMap::new();
2208 Some(ObjectEnd) => {
2209 return Ok(Json::Object(values));
2211 Some(Error(ref e)) => {
2212 return Err(e.clone());
2219 let key = match self.parser.stack().top() {
2220 Some(StackElement::Key(k)) => k.to_owned(),
2222 panic!("invalid state");
2225 match self.build_value() {
2227 values.insert(key, value);
2235 self.parser.error(EOFWhileParsingObject)
2239 /// Decodes a json value from an `&mut io::Read`
2240 pub fn from_reader(rdr: &mut dyn Read) -> Result<Json, BuilderError> {
2241 let mut contents = Vec::new();
2242 match rdr.read_to_end(&mut contents) {
2244 Err(e) => return Err(io_error_to_error(e)),
2246 let s = match str::from_utf8(&contents).ok() {
2248 _ => return Err(SyntaxError(NotUtf8, 0, 0)),
2250 let mut builder = Builder::new(s.chars());
2254 /// Decodes a json value from a string
2255 pub fn from_str(s: &str) -> Result<Json, BuilderError> {
2256 let mut builder = Builder::new(s.chars());
2260 /// A structure to decode JSON to values in rust.
2261 pub struct Decoder {
2266 /// Creates a new decoder instance for decoding the specified JSON value.
2267 pub fn new(json: Json) -> Decoder {
2268 Decoder { stack: vec![json] }
2271 fn pop(&mut self) -> Json {
2272 self.stack.pop().unwrap()
2276 macro_rules! expect {
2277 ($e:expr, Null) => {{
2279 Json::Null => Ok(()),
2280 other => Err(ExpectedError("Null".to_owned(), other.to_string())),
2283 ($e:expr, $t:ident) => {{
2285 Json::$t(v) => Ok(v),
2286 other => Err(ExpectedError(stringify!($t).to_owned(), other.to_string())),
2291 macro_rules! read_primitive {
2292 ($name:ident, $ty:ty) => {
2293 fn $name(&mut self) -> DecodeResult<$ty> {
2295 Json::I64(f) => Ok(f as $ty),
2296 Json::U64(f) => Ok(f as $ty),
2297 Json::F64(f) => Err(ExpectedError("Integer".to_owned(), f.to_string())),
2298 // re: #12967.. a type w/ numeric keys (ie HashMap<usize, V> etc)
2299 // is going to have a string here, as per JSON spec.
2300 Json::String(s) => match s.parse().ok() {
2302 None => Err(ExpectedError("Number".to_owned(), s)),
2304 value => Err(ExpectedError("Number".to_owned(), value.to_string())),
2310 impl crate::Decoder for Decoder {
2311 type Error = DecoderError;
2313 fn read_nil(&mut self) -> DecodeResult<()> {
2314 expect!(self.pop(), Null)
2317 read_primitive! { read_usize, usize }
2318 read_primitive! { read_u8, u8 }
2319 read_primitive! { read_u16, u16 }
2320 read_primitive! { read_u32, u32 }
2321 read_primitive! { read_u64, u64 }
2322 read_primitive! { read_u128, u128 }
2323 read_primitive! { read_isize, isize }
2324 read_primitive! { read_i8, i8 }
2325 read_primitive! { read_i16, i16 }
2326 read_primitive! { read_i32, i32 }
2327 read_primitive! { read_i64, i64 }
2328 read_primitive! { read_i128, i128 }
2330 fn read_f32(&mut self) -> DecodeResult<f32> {
2331 self.read_f64().map(|x| x as f32)
2334 fn read_f64(&mut self) -> DecodeResult<f64> {
2336 Json::I64(f) => Ok(f as f64),
2337 Json::U64(f) => Ok(f as f64),
2338 Json::F64(f) => Ok(f),
2339 Json::String(s) => {
2340 // re: #12967.. a type w/ numeric keys (ie HashMap<usize, V> etc)
2341 // is going to have a string here, as per JSON spec.
2342 match s.parse().ok() {
2344 None => Err(ExpectedError("Number".to_owned(), s)),
2347 Json::Null => Ok(f64::NAN),
2348 value => Err(ExpectedError("Number".to_owned(), value.to_string())),
2352 fn read_bool(&mut self) -> DecodeResult<bool> {
2353 expect!(self.pop(), Boolean)
2356 fn read_char(&mut self) -> DecodeResult<char> {
2357 let s = self.read_str()?;
2359 let mut it = s.chars();
2360 if let (Some(c), None) = (it.next(), it.next()) {
2361 // exactly one character
2365 Err(ExpectedError("single character string".to_owned(), s.to_string()))
2368 fn read_str(&mut self) -> DecodeResult<Cow<'_, str>> {
2369 expect!(self.pop(), String).map(Cow::Owned)
2372 fn read_enum<T, F>(&mut self, _name: &str, f: F) -> DecodeResult<T>
2374 F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2379 fn read_enum_variant<T, F>(&mut self, names: &[&str], mut f: F) -> DecodeResult<T>
2381 F: FnMut(&mut Decoder, usize) -> DecodeResult<T>,
2383 let name = match self.pop() {
2384 Json::String(s) => s,
2385 Json::Object(mut o) => {
2386 let n = match o.remove(&"variant".to_owned()) {
2387 Some(Json::String(s)) => s,
2388 Some(val) => return Err(ExpectedError("String".to_owned(), val.to_string())),
2389 None => return Err(MissingFieldError("variant".to_owned())),
2391 match o.remove(&"fields".to_string()) {
2392 Some(Json::Array(l)) => {
2393 self.stack.extend(l.into_iter().rev());
2395 Some(val) => return Err(ExpectedError("Array".to_owned(), val.to_string())),
2396 None => return Err(MissingFieldError("fields".to_owned())),
2400 json => return Err(ExpectedError("String or Object".to_owned(), json.to_string())),
2402 let idx = match names.iter().position(|n| *n == &name[..]) {
2404 None => return Err(UnknownVariantError(name)),
2409 fn read_enum_variant_arg<T, F>(&mut self, _idx: usize, f: F) -> DecodeResult<T>
2411 F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2416 fn read_enum_struct_variant<T, F>(&mut self, names: &[&str], f: F) -> DecodeResult<T>
2418 F: FnMut(&mut Decoder, usize) -> DecodeResult<T>,
2420 self.read_enum_variant(names, f)
2423 fn read_enum_struct_variant_field<T, F>(
2428 ) -> DecodeResult<T>
2430 F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2432 self.read_enum_variant_arg(idx, f)
2435 fn read_struct<T, F>(&mut self, _name: &str, _len: usize, f: F) -> DecodeResult<T>
2437 F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2439 let value = f(self)?;
2444 fn read_struct_field<T, F>(&mut self, name: &str, _idx: usize, f: F) -> DecodeResult<T>
2446 F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2448 let mut obj = expect!(self.pop(), Object)?;
2450 let value = match obj.remove(&name.to_string()) {
2452 // Add a Null and try to parse it as an Option<_>
2453 // to get None as a default value.
2454 self.stack.push(Json::Null);
2457 Err(_) => return Err(MissingFieldError(name.to_string())),
2461 self.stack.push(json);
2465 self.stack.push(Json::Object(obj));
2469 fn read_tuple<T, F>(&mut self, tuple_len: usize, f: F) -> DecodeResult<T>
2471 F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2473 self.read_seq(move |d, len| {
2474 if len == tuple_len {
2477 Err(ExpectedError(format!("Tuple{}", tuple_len), format!("Tuple{}", len)))
2482 fn read_tuple_arg<T, F>(&mut self, idx: usize, f: F) -> DecodeResult<T>
2484 F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2486 self.read_seq_elt(idx, f)
2489 fn read_tuple_struct<T, F>(&mut self, _name: &str, len: usize, f: F) -> DecodeResult<T>
2491 F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2493 self.read_tuple(len, f)
2496 fn read_tuple_struct_arg<T, F>(&mut self, idx: usize, f: F) -> DecodeResult<T>
2498 F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2500 self.read_tuple_arg(idx, f)
2503 fn read_option<T, F>(&mut self, mut f: F) -> DecodeResult<T>
2505 F: FnMut(&mut Decoder, bool) -> DecodeResult<T>,
2508 Json::Null => f(self, false),
2510 self.stack.push(value);
2516 fn read_seq<T, F>(&mut self, f: F) -> DecodeResult<T>
2518 F: FnOnce(&mut Decoder, usize) -> DecodeResult<T>,
2520 let array = expect!(self.pop(), Array)?;
2521 let len = array.len();
2522 self.stack.extend(array.into_iter().rev());
2526 fn read_seq_elt<T, F>(&mut self, _idx: usize, f: F) -> DecodeResult<T>
2528 F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2533 fn read_map<T, F>(&mut self, f: F) -> DecodeResult<T>
2535 F: FnOnce(&mut Decoder, usize) -> DecodeResult<T>,
2537 let obj = expect!(self.pop(), Object)?;
2538 let len = obj.len();
2539 for (key, value) in obj {
2540 self.stack.push(value);
2541 self.stack.push(Json::String(key));
2546 fn read_map_elt_key<T, F>(&mut self, _idx: usize, f: F) -> DecodeResult<T>
2548 F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2553 fn read_map_elt_val<T, F>(&mut self, _idx: usize, f: F) -> DecodeResult<T>
2555 F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2560 fn error(&mut self, err: &str) -> DecoderError {
2561 ApplicationError(err.to_string())
2565 /// A trait for converting values to JSON
2567 /// Converts the value of `self` to an instance of JSON
2568 fn to_json(&self) -> Json;
2571 macro_rules! to_json_impl_i64 {
2573 $(impl ToJson for $t {
2574 fn to_json(&self) -> Json {
2575 Json::I64(*self as i64)
2581 to_json_impl_i64! { isize, i8, i16, i32, i64 }
2583 macro_rules! to_json_impl_u64 {
2585 $(impl ToJson for $t {
2586 fn to_json(&self) -> Json {
2587 Json::U64(*self as u64)
2593 to_json_impl_u64! { usize, u8, u16, u32, u64 }
2595 impl ToJson for Json {
2596 fn to_json(&self) -> Json {
2601 impl ToJson for f32 {
2602 fn to_json(&self) -> Json {
2603 f64::from(*self).to_json()
2607 impl ToJson for f64 {
2608 fn to_json(&self) -> Json {
2609 match self.classify() {
2610 Fp::Nan | Fp::Infinite => Json::Null,
2611 _ => Json::F64(*self),
2616 impl ToJson for () {
2617 fn to_json(&self) -> Json {
2622 impl ToJson for bool {
2623 fn to_json(&self) -> Json {
2624 Json::Boolean(*self)
2628 impl ToJson for str {
2629 fn to_json(&self) -> Json {
2630 Json::String(self.to_string())
2634 impl ToJson for string::String {
2635 fn to_json(&self) -> Json {
2636 Json::String((*self).clone())
2640 macro_rules! tuple_impl {
2641 // use variables to indicate the arity of the tuple
2642 ($($tyvar:ident),* ) => {
2643 // the trailing commas are for the 1 tuple
2645 $( $tyvar : ToJson ),*
2646 > ToJson for ( $( $tyvar ),* , ) {
2649 #[allow(non_snake_case)]
2650 fn to_json(&self) -> Json {
2652 ($(ref $tyvar),*,) => Json::Array(vec![$($tyvar.to_json()),*])
2661 tuple_impl! {A, B, C}
2662 tuple_impl! {A, B, C, D}
2663 tuple_impl! {A, B, C, D, E}
2664 tuple_impl! {A, B, C, D, E, F}
2665 tuple_impl! {A, B, C, D, E, F, G}
2666 tuple_impl! {A, B, C, D, E, F, G, H}
2667 tuple_impl! {A, B, C, D, E, F, G, H, I}
2668 tuple_impl! {A, B, C, D, E, F, G, H, I, J}
2669 tuple_impl! {A, B, C, D, E, F, G, H, I, J, K}
2670 tuple_impl! {A, B, C, D, E, F, G, H, I, J, K, L}
2672 impl<A: ToJson> ToJson for [A] {
2673 fn to_json(&self) -> Json {
2674 Json::Array(self.iter().map(|elt| elt.to_json()).collect())
2678 impl<A: ToJson> ToJson for Vec<A> {
2679 fn to_json(&self) -> Json {
2680 Json::Array(self.iter().map(|elt| elt.to_json()).collect())
2684 impl<T: ToString, A: ToJson> ToJson for BTreeMap<T, A> {
2685 fn to_json(&self) -> Json {
2686 let mut d = BTreeMap::new();
2687 for (key, value) in self {
2688 d.insert(key.to_string(), value.to_json());
2694 impl<A: ToJson> ToJson for HashMap<string::String, A> {
2695 fn to_json(&self) -> Json {
2696 let mut d = BTreeMap::new();
2697 for (key, value) in self {
2698 d.insert((*key).clone(), value.to_json());
2704 impl<A: ToJson> ToJson for Option<A> {
2705 fn to_json(&self) -> Json {
2708 Some(ref value) => value.to_json(),
2713 struct FormatShim<'a, 'b> {
2714 inner: &'a mut fmt::Formatter<'b>,
2717 impl<'a, 'b> fmt::Write for FormatShim<'a, 'b> {
2718 fn write_str(&mut self, s: &str) -> fmt::Result {
2719 match self.inner.write_str(s) {
2721 Err(_) => Err(fmt::Error),
2726 impl fmt::Display for Json {
2727 /// Encodes a json value into a string
2728 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2729 let mut shim = FormatShim { inner: f };
2730 let mut encoder = Encoder::new(&mut shim);
2731 match self.encode(&mut encoder) {
2733 Err(_) => Err(fmt::Error),
2738 impl<'a> fmt::Display for PrettyJson<'a> {
2739 /// Encodes a json value into a string
2740 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2741 let mut shim = FormatShim { inner: f };
2742 let mut encoder = PrettyEncoder::new(&mut shim);
2743 match self.inner.encode(&mut encoder) {
2745 Err(_) => Err(fmt::Error),
2750 impl<'a, T: Encodable> fmt::Display for AsJson<'a, T> {
2751 /// Encodes a json value into a string
2752 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2753 let mut shim = FormatShim { inner: f };
2754 let mut encoder = Encoder::new(&mut shim);
2755 match self.inner.encode(&mut encoder) {
2757 Err(_) => Err(fmt::Error),
2762 impl<'a, T> AsPrettyJson<'a, T> {
2763 /// Sets the indentation level for the emitted JSON
2764 pub fn indent(mut self, indent: usize) -> AsPrettyJson<'a, T> {
2765 self.indent = Some(indent);
2770 impl<'a, T: Encodable> fmt::Display for AsPrettyJson<'a, T> {
2771 /// Encodes a json value into a string
2772 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2773 let mut shim = FormatShim { inner: f };
2774 let mut encoder = PrettyEncoder::new(&mut shim);
2775 if let Some(n) = self.indent {
2776 encoder.set_indent(n);
2778 match self.inner.encode(&mut encoder) {
2780 Err(_) => Err(fmt::Error),
2785 impl FromStr for Json {
2786 type Err = BuilderError;
2787 fn from_str(s: &str) -> Result<Json, BuilderError> {