1 // Rust JSON serialization library.
2 // Copyright (c) 2011 Google Inc.
4 #![forbid(non_camel_case_types)]
5 #![allow(missing_docs)]
7 //! JSON parsing and serialization
11 //! JSON (JavaScript Object Notation) is a way to write data in Javascript.
12 //! Like XML, it allows to encode structured data in a text format that can be easily read by humans
13 //! Its simple syntax and native compatibility with JavaScript have made it a widely used format.
15 //! Data types that can be encoded are JavaScript types (see the `Json` enum for more details):
17 //! * `Boolean`: equivalent to rust's `bool`
18 //! * `Number`: equivalent to rust's `f64`
19 //! * `String`: equivalent to rust's `String`
20 //! * `Array`: equivalent to rust's `Vec<T>`, but also allowing objects of different types in the
22 //! * `Object`: equivalent to rust's `BTreeMap<String, json::Json>`
25 //! An object is a series of string keys mapping to values, in `"key": value` format.
26 //! Arrays are enclosed in square brackets ([ ... ]) and objects in curly brackets ({ ... }).
27 //! A simple JSON document encoding a person, their age, address and phone numbers could look like
31 //! "FirstName": "John",
32 //! "LastName": "Doe",
35 //! "Street": "Downing Street 10",
37 //! "Country": "Great Britain"
46 //! # Rust Type-based Encoding and Decoding
48 //! Rust provides a mechanism for low boilerplate encoding & decoding of values to and from JSON via
49 //! the serialization API.
50 //! To be able to encode a piece of data, it must implement the `serialize::RustcEncodable` trait.
51 //! To be able to decode a piece of data, it must implement the `serialize::RustcDecodable` trait.
52 //! The Rust compiler provides an annotation to automatically generate the code for these traits:
53 //! `#[derive(RustcDecodable, RustcEncodable)]`
55 //! The JSON API provides an enum `json::Json` and a trait `ToJson` to encode objects.
56 //! The `ToJson` trait provides a `to_json` method to convert an object into a `json::Json` value.
57 //! A `json::Json` value can be encoded as a string or buffer using the functions described above.
58 //! You can also use the `json::Encoder` object, which implements the `Encoder` trait.
60 //! When using `ToJson` the `RustcEncodable` trait implementation is not mandatory.
64 //! ## Using Autoserialization
66 //! Create a struct called `TestStruct` and serialize and deserialize it to and from JSON using the
67 //! serialization API, using the derived serialization code.
70 //! # #![feature(rustc_private)]
71 //! extern crate serialize as rustc_serialize; // for the deriving below
72 //! use rustc_serialize::json;
74 //! // Automatically generate `Decodable` and `Encodable` trait implementations
75 //! #[derive(RustcDecodable, RustcEncodable)]
76 //! pub struct TestStruct {
79 //! data_vector: Vec<u8>,
83 //! let object = TestStruct {
85 //! data_str: "homura".to_string(),
86 //! data_vector: vec![2,3,4,5],
89 //! // Serialize using `json::encode`
90 //! let encoded = json::encode(&object).unwrap();
92 //! // Deserialize using `json::decode`
93 //! let decoded: TestStruct = json::decode(&encoded[..]).unwrap();
97 //! ## Using the `ToJson` trait
99 //! The examples above use the `ToJson` trait to generate the JSON string, which is required
100 //! for custom mappings.
102 //! ### Simple example of `ToJson` usage
105 //! # #![feature(rustc_private)]
106 //! # #![allow(deprecated)]
107 //! extern crate serialize;
108 //! use serialize::json::{self, ToJson, Json};
110 //! // A custom data structure
111 //! struct ComplexNum {
116 //! // JSON value representation
117 //! impl ToJson for ComplexNum {
118 //! fn to_json(&self) -> Json {
119 //! Json::String(format!("{}+{}i", self.a, self.b))
123 //! // Only generate `RustcEncodable` trait implementation
124 //! #[derive(Encodable)]
125 //! pub struct ComplexNumRecord {
132 //! let num = ComplexNum { a: 0.0001, b: 12.539 };
133 //! let data: String = json::encode(&ComplexNumRecord{
135 //! dsc: "test".to_string(),
136 //! val: num.to_json(),
138 //! println!("data: {}", data);
139 //! // data: {"uid":1,"dsc":"test","val":"0.0001+12.539i"};
143 //! ### Verbose example of `ToJson` usage
146 //! # #![feature(rustc_private)]
147 //! # #![allow(deprecated)]
148 //! extern crate serialize;
149 //! use std::collections::BTreeMap;
150 //! use serialize::json::{self, Json, ToJson};
152 //! // Only generate `Decodable` trait implementation
153 //! #[derive(Decodable)]
154 //! pub struct TestStruct {
156 //! data_str: String,
157 //! data_vector: Vec<u8>,
160 //! // Specify encoding method manually
161 //! impl ToJson for TestStruct {
162 //! fn to_json(&self) -> Json {
163 //! let mut d = BTreeMap::new();
164 //! // All standard types implement `to_json()`, so use it
165 //! d.insert("data_int".to_string(), self.data_int.to_json());
166 //! d.insert("data_str".to_string(), self.data_str.to_json());
167 //! d.insert("data_vector".to_string(), self.data_vector.to_json());
173 //! // Serialize using `ToJson`
174 //! let input_data = TestStruct {
176 //! data_str: "madoka".to_string(),
177 //! data_vector: vec![2,3,4,5],
179 //! let json_obj: Json = input_data.to_json();
180 //! let json_str: String = json_obj.to_string();
182 //! // Deserialize like before
183 //! let decoded: TestStruct = json::decode(&json_str).unwrap();
187 use self::JsonEvent::*;
188 use self::ErrorCode::*;
189 use self::ParserError::*;
190 use self::DecoderError::*;
191 use self::ParserState::*;
192 use self::InternalStackElement::*;
194 use std::borrow::Cow;
195 use std::collections::{HashMap, BTreeMap};
196 use std::io::prelude::*;
199 use std::num::FpCategory as Fp;
201 use std::str::FromStr;
203 use std::{char, f64, fmt, str};
205 use crate::Encodable;
207 /// Represents a json value
208 #[derive(Clone, PartialEq, PartialOrd, Debug)]
213 String(string::String),
216 Object(self::Object),
220 pub type Array = Vec<Json>;
221 pub type Object = BTreeMap<string::String, Json>;
223 pub struct PrettyJson<'a> { inner: &'a Json }
225 pub struct AsJson<'a, T> { inner: &'a T }
226 pub struct AsPrettyJson<'a, T> { inner: &'a T, indent: Option<usize> }
228 /// The errors that can arise while parsing a JSON stream.
229 #[derive(Clone, Copy, PartialEq, Debug)]
233 EOFWhileParsingObject,
234 EOFWhileParsingArray,
235 EOFWhileParsingValue,
236 EOFWhileParsingString,
242 InvalidUnicodeCodePoint,
243 LoneLeadingSurrogateInHexEscape,
244 UnexpectedEndOfHexEscape,
250 #[derive(Clone, PartialEq, Debug)]
251 pub enum ParserError {
253 SyntaxError(ErrorCode, usize, usize),
254 IoError(io::ErrorKind, String),
257 // Builder and Parser have the same errors.
258 pub type BuilderError = ParserError;
260 #[derive(Clone, PartialEq, Debug)]
261 pub enum DecoderError {
262 ParseError(ParserError),
263 ExpectedError(string::String, string::String),
264 MissingFieldError(string::String),
265 UnknownVariantError(string::String),
266 ApplicationError(string::String)
269 #[derive(Copy, Clone, Debug)]
270 pub enum EncoderError {
271 FmtError(fmt::Error),
275 /// Returns a readable error string for a given error code.
276 pub fn error_str(error: ErrorCode) -> &'static str {
278 InvalidSyntax => "invalid syntax",
279 InvalidNumber => "invalid number",
280 EOFWhileParsingObject => "EOF While parsing object",
281 EOFWhileParsingArray => "EOF While parsing array",
282 EOFWhileParsingValue => "EOF While parsing value",
283 EOFWhileParsingString => "EOF While parsing string",
284 KeyMustBeAString => "key must be a string",
285 ExpectedColon => "expected `:`",
286 TrailingCharacters => "trailing characters",
287 TrailingComma => "trailing comma",
288 InvalidEscape => "invalid escape",
289 UnrecognizedHex => "invalid \\u{ esc}ape (unrecognized hex)",
290 NotFourDigit => "invalid \\u{ esc}ape (not four digits)",
291 NotUtf8 => "contents not utf-8",
292 InvalidUnicodeCodePoint => "invalid Unicode code point",
293 LoneLeadingSurrogateInHexEscape => "lone leading surrogate in hex escape",
294 UnexpectedEndOfHexEscape => "unexpected end of hex escape",
298 /// Shortcut function to decode a JSON `&str` into an object
299 pub fn decode<T: crate::Decodable>(s: &str) -> DecodeResult<T> {
300 let json = match from_str(s) {
302 Err(e) => return Err(ParseError(e))
305 let mut decoder = Decoder::new(json);
306 crate::Decodable::decode(&mut decoder)
309 /// Shortcut function to encode a `T` into a JSON `String`
310 pub fn encode<T: crate::Encodable>(object: &T) -> Result<string::String, EncoderError> {
311 let mut s = String::new();
313 let mut encoder = Encoder::new(&mut s);
314 object.encode(&mut encoder)?;
319 impl fmt::Display for ErrorCode {
320 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
321 error_str(*self).fmt(f)
325 fn io_error_to_error(io: io::Error) -> ParserError {
326 IoError(io.kind(), io.to_string())
329 impl fmt::Display for ParserError {
330 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
331 // FIXME this should be a nicer error
332 fmt::Debug::fmt(self, f)
336 impl fmt::Display for DecoderError {
337 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
338 // FIXME this should be a nicer error
339 fmt::Debug::fmt(self, f)
343 impl std::error::Error for DecoderError {
344 fn description(&self) -> &str { "decoder error" }
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 {
355 fn description(&self) -> &str { "encoder error" }
358 impl From<fmt::Error> for EncoderError {
359 /// Converts a [`fmt::Error`] into `EncoderError`
361 /// This conversion does not allocate memory.
362 fn from(err: fmt::Error) -> EncoderError { 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",
414 wr.write_str(&v[start..i])?;
417 wr.write_str(escaped)?;
422 if start != v.len() {
423 wr.write_str(&v[start..])?;
430 fn escape_char(writer: &mut dyn fmt::Write, v: char) -> EncodeResult {
431 escape_str(writer, v.encode_utf8(&mut [0; 4]))
434 fn spaces(wr: &mut dyn fmt::Write, mut n: usize) -> EncodeResult {
435 const BUF: &str = " ";
437 while n >= BUF.len() {
443 wr.write_str(&BUF[..n])?;
448 fn fmt_number_or_null(v: f64) -> string::String {
450 Fp::Nan | Fp::Infinite => string::String::from("null"),
451 _ if v.fract() != 0f64 => v.to_string(),
452 _ => v.to_string() + ".0",
456 /// A structure for implementing serialization to JSON.
457 pub struct Encoder<'a> {
458 writer: &'a mut (dyn fmt::Write+'a),
459 is_emitting_map_key: bool,
462 impl<'a> Encoder<'a> {
463 /// Creates a new JSON encoder whose output will be written to the writer
465 pub fn new(writer: &'a mut dyn fmt::Write) -> Encoder<'a> {
466 Encoder { writer, is_emitting_map_key: false, }
470 macro_rules! emit_enquoted_if_mapkey {
471 ($enc:ident,$e:expr) => ({
472 if $enc.is_emitting_map_key {
473 write!($enc.writer, "\"{}\"", $e)?;
475 write!($enc.writer, "{}", $e)?;
481 impl<'a> crate::Encoder for Encoder<'a> {
482 type Error = EncoderError;
484 fn emit_unit(&mut self) -> EncodeResult {
485 if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
486 write!(self.writer, "null")?;
490 fn emit_usize(&mut self, v: usize) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
491 fn emit_u128(&mut self, v: u128) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
492 fn emit_u64(&mut self, v: u64) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
493 fn emit_u32(&mut self, v: u32) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
494 fn emit_u16(&mut self, v: u16) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
495 fn emit_u8(&mut self, v: u8) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
497 fn emit_isize(&mut self, v: isize) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
498 fn emit_i128(&mut self, v: i128) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
499 fn emit_i64(&mut self, v: i64) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
500 fn emit_i32(&mut self, v: i32) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
501 fn emit_i16(&mut self, v: i16) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
502 fn emit_i8(&mut self, v: i8) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
504 fn emit_bool(&mut self, v: bool) -> EncodeResult {
505 if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
507 write!(self.writer, "true")?;
509 write!(self.writer, "false")?;
514 fn emit_f64(&mut self, v: f64) -> EncodeResult {
515 emit_enquoted_if_mapkey!(self, fmt_number_or_null(v))
517 fn emit_f32(&mut self, v: f32) -> EncodeResult {
518 self.emit_f64(f64::from(v))
521 fn emit_char(&mut self, v: char) -> EncodeResult {
522 escape_char(self.writer, v)
524 fn emit_str(&mut self, v: &str) -> EncodeResult {
525 escape_str(self.writer, v)
528 fn emit_enum<F>(&mut self, _name: &str, f: F) -> EncodeResult where
529 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
534 fn emit_enum_variant<F>(&mut self,
538 f: F) -> EncodeResult where
539 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
541 // enums are encoded as strings or objects
543 // Kangaroo(34,"William") => {"variant": "Kangaroo", "fields": [34,"William"]}
545 escape_str(self.writer, name)
547 if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
548 write!(self.writer, "{{\"variant\":")?;
549 escape_str(self.writer, name)?;
550 write!(self.writer, ",\"fields\":[")?;
552 write!(self.writer, "]}}")?;
557 fn emit_enum_variant_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult where
558 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
560 if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
562 write!(self.writer, ",")?;
567 fn emit_enum_struct_variant<F>(&mut self,
571 f: F) -> EncodeResult where
572 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
574 if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
575 self.emit_enum_variant(name, id, cnt, f)
578 fn emit_enum_struct_variant_field<F>(&mut self,
581 f: F) -> EncodeResult where
582 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
584 if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
585 self.emit_enum_variant_arg(idx, f)
588 fn emit_struct<F>(&mut self, _: &str, _: usize, f: F) -> EncodeResult where
589 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
591 if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
592 write!(self.writer, "{{")?;
594 write!(self.writer, "}}")?;
598 fn emit_struct_field<F>(&mut self, name: &str, idx: usize, f: F) -> EncodeResult where
599 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
601 if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
602 if idx != 0 { write!(self.writer, ",")?; }
603 escape_str(self.writer, name)?;
604 write!(self.writer, ":")?;
608 fn emit_tuple<F>(&mut self, len: usize, f: F) -> EncodeResult where
609 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
611 if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
612 self.emit_seq(len, f)
614 fn emit_tuple_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult where
615 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
617 if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
618 self.emit_seq_elt(idx, f)
621 fn emit_tuple_struct<F>(&mut self, _name: &str, len: usize, f: F) -> EncodeResult where
622 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
624 if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
625 self.emit_seq(len, f)
627 fn emit_tuple_struct_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult where
628 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
630 if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
631 self.emit_seq_elt(idx, f)
634 fn emit_option<F>(&mut self, f: F) -> EncodeResult where
635 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
637 if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
640 fn emit_option_none(&mut self) -> EncodeResult {
641 if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
644 fn emit_option_some<F>(&mut self, f: F) -> EncodeResult where
645 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
647 if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
651 fn emit_seq<F>(&mut self, _len: usize, f: F) -> EncodeResult where
652 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
654 if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
655 write!(self.writer, "[")?;
657 write!(self.writer, "]")?;
661 fn emit_seq_elt<F>(&mut self, idx: usize, f: F) -> EncodeResult where
662 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
664 if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
666 write!(self.writer, ",")?;
671 fn emit_map<F>(&mut self, _len: usize, f: F) -> EncodeResult where
672 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
674 if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
675 write!(self.writer, "{{")?;
677 write!(self.writer, "}}")?;
681 fn emit_map_elt_key<F>(&mut self, idx: usize, f: F) -> EncodeResult where
682 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
684 if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
685 if idx != 0 { write!(self.writer, ",")? }
686 self.is_emitting_map_key = true;
688 self.is_emitting_map_key = false;
692 fn emit_map_elt_val<F>(&mut self, _idx: usize, f: F) -> EncodeResult where
693 F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
695 if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
696 write!(self.writer, ":")?;
701 /// Another encoder for JSON, but prints out human-readable JSON instead of
703 pub struct PrettyEncoder<'a> {
704 writer: &'a mut (dyn fmt::Write+'a),
707 is_emitting_map_key: bool,
710 impl<'a> PrettyEncoder<'a> {
711 /// Creates a new encoder whose output will be written to the specified writer
712 pub fn new(writer: &'a mut dyn fmt::Write) -> PrettyEncoder<'a> {
717 is_emitting_map_key: false,
721 /// Sets the number of spaces to indent for each level.
722 /// This is safe to set during encoding.
723 pub fn set_indent(&mut self, indent: usize) {
724 // self.indent very well could be 0 so we need to use checked division.
725 let level = self.curr_indent.checked_div(self.indent).unwrap_or(0);
726 self.indent = indent;
727 self.curr_indent = level * self.indent;
731 impl<'a> crate::Encoder for PrettyEncoder<'a> {
732 type Error = EncoderError;
734 fn emit_unit(&mut self) -> EncodeResult {
735 if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
736 write!(self.writer, "null")?;
740 fn emit_usize(&mut self, v: usize) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
741 fn emit_u128(&mut self, v: u128) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
742 fn emit_u64(&mut self, v: u64) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
743 fn emit_u32(&mut self, v: u32) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
744 fn emit_u16(&mut self, v: u16) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
745 fn emit_u8(&mut self, v: u8) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
747 fn emit_isize(&mut self, v: isize) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
748 fn emit_i128(&mut self, v: i128) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
749 fn emit_i64(&mut self, v: i64) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
750 fn emit_i32(&mut self, v: i32) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
751 fn emit_i16(&mut self, v: i16) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
752 fn emit_i8(&mut self, v: i8) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
754 fn emit_bool(&mut self, v: bool) -> EncodeResult {
755 if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
757 write!(self.writer, "true")?;
759 write!(self.writer, "false")?;
764 fn emit_f64(&mut self, v: f64) -> EncodeResult {
765 emit_enquoted_if_mapkey!(self, fmt_number_or_null(v))
767 fn emit_f32(&mut self, v: f32) -> EncodeResult {
768 self.emit_f64(f64::from(v))
771 fn emit_char(&mut self, v: char) -> EncodeResult {
772 escape_char(self.writer, v)
774 fn emit_str(&mut self, v: &str) -> EncodeResult {
775 escape_str(self.writer, v)
778 fn emit_enum<F>(&mut self, _name: &str, f: F) -> EncodeResult where
779 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
784 fn emit_enum_variant<F>(&mut self,
789 -> EncodeResult where
790 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
793 escape_str(self.writer, name)
795 if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
796 writeln!(self.writer, "{{")?;
797 self.curr_indent += self.indent;
798 spaces(self.writer, self.curr_indent)?;
799 write!(self.writer, "\"variant\": ")?;
800 escape_str(self.writer, name)?;
801 writeln!(self.writer, ",")?;
802 spaces(self.writer, self.curr_indent)?;
803 writeln!(self.writer, "\"fields\": [")?;
804 self.curr_indent += self.indent;
806 self.curr_indent -= self.indent;
807 writeln!(self.writer)?;
808 spaces(self.writer, self.curr_indent)?;
809 self.curr_indent -= self.indent;
810 writeln!(self.writer, "]")?;
811 spaces(self.writer, self.curr_indent)?;
812 write!(self.writer, "}}")?;
817 fn emit_enum_variant_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult where
818 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
820 if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
822 writeln!(self.writer, ",")?;
824 spaces(self.writer, self.curr_indent)?;
828 fn emit_enum_struct_variant<F>(&mut self,
832 f: F) -> EncodeResult where
833 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
835 if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
836 self.emit_enum_variant(name, id, cnt, f)
839 fn emit_enum_struct_variant_field<F>(&mut self,
842 f: F) -> EncodeResult where
843 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
845 if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
846 self.emit_enum_variant_arg(idx, f)
850 fn emit_struct<F>(&mut self, _: &str, len: usize, f: F) -> EncodeResult where
851 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
853 if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
855 write!(self.writer, "{{}}")?;
857 write!(self.writer, "{{")?;
858 self.curr_indent += self.indent;
860 self.curr_indent -= self.indent;
861 writeln!(self.writer)?;
862 spaces(self.writer, self.curr_indent)?;
863 write!(self.writer, "}}")?;
868 fn emit_struct_field<F>(&mut self, name: &str, idx: usize, f: F) -> EncodeResult where
869 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
871 if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
873 writeln!(self.writer)?;
875 writeln!(self.writer, ",")?;
877 spaces(self.writer, self.curr_indent)?;
878 escape_str(self.writer, name)?;
879 write!(self.writer, ": ")?;
883 fn emit_tuple<F>(&mut self, len: usize, f: F) -> EncodeResult where
884 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
886 if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
887 self.emit_seq(len, f)
889 fn emit_tuple_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult where
890 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
892 if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
893 self.emit_seq_elt(idx, f)
896 fn emit_tuple_struct<F>(&mut self, _: &str, len: usize, f: F) -> EncodeResult where
897 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
899 if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
900 self.emit_seq(len, f)
902 fn emit_tuple_struct_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult where
903 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
905 if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
906 self.emit_seq_elt(idx, f)
909 fn emit_option<F>(&mut self, f: F) -> EncodeResult where
910 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
912 if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
915 fn emit_option_none(&mut self) -> EncodeResult {
916 if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
919 fn emit_option_some<F>(&mut self, f: F) -> EncodeResult where
920 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
922 if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
926 fn emit_seq<F>(&mut self, len: usize, f: F) -> EncodeResult where
927 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
929 if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
931 write!(self.writer, "[]")?;
933 write!(self.writer, "[")?;
934 self.curr_indent += self.indent;
936 self.curr_indent -= self.indent;
937 writeln!(self.writer)?;
938 spaces(self.writer, self.curr_indent)?;
939 write!(self.writer, "]")?;
944 fn emit_seq_elt<F>(&mut self, idx: usize, f: F) -> EncodeResult where
945 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
947 if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
949 writeln!(self.writer)?;
951 writeln!(self.writer, ",")?;
953 spaces(self.writer, self.curr_indent)?;
957 fn emit_map<F>(&mut self, len: usize, f: F) -> EncodeResult where
958 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
960 if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
962 write!(self.writer, "{{}}")?;
964 write!(self.writer, "{{")?;
965 self.curr_indent += self.indent;
967 self.curr_indent -= self.indent;
968 writeln!(self.writer)?;
969 spaces(self.writer, self.curr_indent)?;
970 write!(self.writer, "}}")?;
975 fn emit_map_elt_key<F>(&mut self, idx: usize, f: F) -> EncodeResult where
976 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
978 if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
980 writeln!(self.writer)?;
982 writeln!(self.writer, ",")?;
984 spaces(self.writer, self.curr_indent)?;
985 self.is_emitting_map_key = true;
987 self.is_emitting_map_key = false;
991 fn emit_map_elt_val<F>(&mut self, _idx: usize, f: F) -> EncodeResult where
992 F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
994 if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
995 write!(self.writer, ": ")?;
1000 impl Encodable for Json {
1001 fn encode<E: crate::Encoder>(&self, e: &mut E) -> Result<(), E::Error> {
1003 Json::I64(v) => v.encode(e),
1004 Json::U64(v) => v.encode(e),
1005 Json::F64(v) => v.encode(e),
1006 Json::String(ref v) => v.encode(e),
1007 Json::Boolean(v) => v.encode(e),
1008 Json::Array(ref v) => v.encode(e),
1009 Json::Object(ref v) => v.encode(e),
1010 Json::Null => e.emit_unit(),
1015 /// Creates an `AsJson` wrapper which can be used to print a value as JSON
1016 /// on-the-fly via `write!`
1017 pub fn as_json<T>(t: &T) -> AsJson<'_, T> {
1021 /// Creates an `AsPrettyJson` wrapper which can be used to print a value as JSON
1022 /// on-the-fly via `write!`
1023 pub fn as_pretty_json<T>(t: &T) -> AsPrettyJson<'_, T> {
1024 AsPrettyJson { inner: t, indent: None }
1028 /// Borrow this json object as a pretty object to generate a pretty
1029 /// representation for it via `Display`.
1030 pub fn pretty(&self) -> PrettyJson<'_> {
1031 PrettyJson { inner: self }
1034 /// If the Json value is an Object, returns the value associated with the provided key.
1035 /// Otherwise, returns None.
1036 pub fn find(&self, key: &str) -> Option<&Json> {
1038 Json::Object(ref map) => map.get(key),
1043 /// Attempts to get a nested Json Object for each key in `keys`.
1044 /// If any key is found not to exist, `find_path` will return `None`.
1045 /// Otherwise, it will return the Json value associated with the final key.
1046 pub fn find_path<'a>(&'a self, keys: &[&str]) -> Option<&'a Json>{
1047 let mut target = self;
1049 target = target.find(*key)?;
1054 /// If the Json value is an Object, performs a depth-first search until
1055 /// a value associated with the provided key is found. If no value is found
1056 /// or the Json value is not an Object, returns `None`.
1057 pub fn search(&self, key: &str) -> Option<&Json> {
1059 &Json::Object(ref map) => {
1060 match map.get(key) {
1061 Some(json_value) => Some(json_value),
1064 match v.search(key) {
1065 x if x.is_some() => return x,
1077 /// Returns `true` if the Json value is an `Object`.
1078 pub fn is_object(&self) -> bool {
1079 self.as_object().is_some()
1082 /// If the Json value is an `Object`, returns the associated `BTreeMap`;
1083 /// returns `None` otherwise.
1084 pub fn as_object(&self) -> Option<&Object> {
1086 Json::Object(ref map) => Some(map),
1091 /// Returns `true` if the Json value is an `Array`.
1092 pub fn is_array(&self) -> bool {
1093 self.as_array().is_some()
1096 /// If the Json value is an `Array`, returns the associated vector;
1097 /// returns `None` otherwise.
1098 pub fn as_array(&self) -> Option<&Array> {
1100 Json::Array(ref array) => Some(&*array),
1105 /// Returns `true` if the Json value is a `String`.
1106 pub fn is_string(&self) -> bool {
1107 self.as_string().is_some()
1110 /// If the Json value is a `String`, returns the associated `str`;
1111 /// returns `None` otherwise.
1112 pub fn as_string(&self) -> Option<&str> {
1114 Json::String(ref s) => Some(&s[..]),
1119 /// Returns `true` if the Json value is a `Number`.
1120 pub fn is_number(&self) -> bool {
1122 Json::I64(_) | Json::U64(_) | Json::F64(_) => true,
1127 /// Returns `true` if the Json value is a `i64`.
1128 pub fn is_i64(&self) -> bool {
1130 Json::I64(_) => true,
1135 /// Returns `true` if the Json value is a `u64`.
1136 pub fn is_u64(&self) -> bool {
1138 Json::U64(_) => true,
1143 /// Returns `true` if the Json value is a `f64`.
1144 pub fn is_f64(&self) -> bool {
1146 Json::F64(_) => true,
1151 /// If the Json value is a number, returns or cast it to a `i64`;
1152 /// returns `None` otherwise.
1153 pub fn as_i64(&self) -> Option<i64> {
1155 Json::I64(n) => Some(n),
1156 Json::U64(n) => Some(n as i64),
1161 /// If the Json value is a number, returns or cast it to a `u64`;
1162 /// returns `None` otherwise.
1163 pub fn as_u64(&self) -> Option<u64> {
1165 Json::I64(n) => Some(n as u64),
1166 Json::U64(n) => Some(n),
1171 /// If the Json value is a number, returns or cast it to a `f64`;
1172 /// returns `None` otherwise.
1173 pub fn as_f64(&self) -> Option<f64> {
1175 Json::I64(n) => Some(n as f64),
1176 Json::U64(n) => Some(n as f64),
1177 Json::F64(n) => Some(n),
1182 /// Returns `true` if the Json value is a `Boolean`.
1183 pub fn is_boolean(&self) -> bool {
1184 self.as_boolean().is_some()
1187 /// If the Json value is a `Boolean`, returns the associated `bool`;
1188 /// returns `None` otherwise.
1189 pub fn as_boolean(&self) -> Option<bool> {
1191 Json::Boolean(b) => Some(b),
1196 /// Returns `true` if the Json value is a `Null`.
1197 pub fn is_null(&self) -> bool {
1198 self.as_null().is_some()
1201 /// If the Json value is a `Null`, returns `()`;
1202 /// returns `None` otherwise.
1203 pub fn as_null(&self) -> Option<()> {
1205 Json::Null => Some(()),
1211 impl<'a> Index<&'a str> for Json {
1214 fn index(&self, idx: &'a str) -> &Json {
1215 self.find(idx).unwrap()
1219 impl Index<usize> for Json {
1222 fn index(&self, idx: usize) -> &Json {
1224 Json::Array(ref v) => &v[idx],
1225 _ => panic!("can only index Json with usize if it is an array")
1230 /// The output of the streaming parser.
1231 #[derive(PartialEq, Clone, Debug)]
1232 pub enum JsonEvent {
1241 StringValue(string::String),
1246 #[derive(PartialEq, Debug)]
1248 // Parse a value in an array, true means first element.
1250 // Parse ',' or ']' after an element in an array.
1252 // Parse a key:value in an object, true means first element.
1254 // Parse ',' or ']' after an element in an object.
1258 // Expecting the stream to end.
1260 // Parsing can't continue.
1264 /// A Stack represents the current position of the parser in the logical
1265 /// structure of the JSON stream.
1266 /// For example foo.bar[3].x
1268 stack: Vec<InternalStackElement>,
1269 str_buffer: Vec<u8>,
1272 /// StackElements compose a Stack.
1273 /// For example, StackElement::Key("foo"), StackElement::Key("bar"),
1274 /// StackElement::Index(3) and StackElement::Key("x") are the
1275 /// StackElements compositing the stack that represents foo.bar[3].x
1276 #[derive(PartialEq, Clone, Debug)]
1277 pub enum StackElement<'l> {
1282 // Internally, Key elements are stored as indices in a buffer to avoid
1283 // allocating a string for every member of an object.
1284 #[derive(PartialEq, Clone, Debug)]
1285 enum InternalStackElement {
1287 InternalKey(u16, u16), // start, size
1291 pub fn new() -> Stack {
1292 Stack { stack: Vec::new(), str_buffer: Vec::new() }
1295 /// Returns The number of elements in the Stack.
1296 pub fn len(&self) -> usize { self.stack.len() }
1298 /// Returns `true` if the stack is empty.
1299 pub fn is_empty(&self) -> bool { self.stack.is_empty() }
1301 /// Provides access to the StackElement at a given index.
1302 /// lower indices are at the bottom of the stack while higher indices are
1304 pub fn get(&self, idx: usize) -> StackElement<'_> {
1305 match self.stack[idx] {
1306 InternalIndex(i) => StackElement::Index(i),
1307 InternalKey(start, size) => {
1308 StackElement::Key(str::from_utf8(
1309 &self.str_buffer[start as usize .. start as usize + size as usize])
1315 /// Compares this stack with an array of StackElement<'_>s.
1316 pub fn is_equal_to(&self, rhs: &[StackElement<'_>]) -> bool {
1317 if self.stack.len() != rhs.len() { return false; }
1318 for (i, r) in rhs.iter().enumerate() {
1319 if self.get(i) != *r { return false; }
1324 /// Returns `true` if the bottom-most elements of this stack are the same as
1325 /// the ones passed as parameter.
1326 pub fn starts_with(&self, rhs: &[StackElement<'_>]) -> bool {
1327 if self.stack.len() < rhs.len() { return false; }
1328 for (i, r) in rhs.iter().enumerate() {
1329 if self.get(i) != *r { return false; }
1334 /// Returns `true` if the top-most elements of this stack are the same as
1335 /// the ones passed as parameter.
1336 pub fn ends_with(&self, rhs: &[StackElement<'_>]) -> bool {
1337 if self.stack.len() < rhs.len() { return false; }
1338 let offset = self.stack.len() - rhs.len();
1339 for (i, r) in rhs.iter().enumerate() {
1340 if self.get(i + offset) != *r { return false; }
1345 /// Returns the top-most element (if any).
1346 pub fn top(&self) -> Option<StackElement<'_>> {
1347 match self.stack.last() {
1349 Some(&InternalIndex(i)) => Some(StackElement::Index(i)),
1350 Some(&InternalKey(start, size)) => {
1351 Some(StackElement::Key(str::from_utf8(
1352 &self.str_buffer[start as usize .. (start+size) as usize]
1358 // Used by Parser to insert StackElement::Key elements at the top of the stack.
1359 fn push_key(&mut self, key: string::String) {
1360 self.stack.push(InternalKey(self.str_buffer.len() as u16, key.len() as u16));
1361 self.str_buffer.extend(key.as_bytes());
1364 // Used by Parser to insert StackElement::Index elements at the top of the stack.
1365 fn push_index(&mut self, index: u32) {
1366 self.stack.push(InternalIndex(index));
1369 // Used by Parser to remove the top-most element of the stack.
1371 assert!(!self.is_empty());
1372 match *self.stack.last().unwrap() {
1373 InternalKey(_, sz) => {
1374 let new_size = self.str_buffer.len() - sz as usize;
1375 self.str_buffer.truncate(new_size);
1377 InternalIndex(_) => {}
1382 // Used by Parser to test whether the top-most element is an index.
1383 fn last_is_index(&self) -> bool {
1384 match self.stack.last() {
1385 Some(InternalIndex(_)) => true,
1390 // Used by Parser to increment the index of the top-most element.
1391 fn bump_index(&mut self) {
1392 let len = self.stack.len();
1393 let idx = match *self.stack.last().unwrap() {
1394 InternalIndex(i) => { i + 1 }
1397 self.stack[len - 1] = InternalIndex(idx);
1401 /// A streaming JSON parser implemented as an iterator of JsonEvent, consuming
1402 /// an iterator of char.
1403 pub struct Parser<T> {
1408 // We maintain a stack representing where we are in the logical structure
1409 // of the JSON stream.
1411 // A state machine is kept to make it possible to interrupt and resume parsing.
1415 impl<T: Iterator<Item=char>> Iterator for Parser<T> {
1416 type Item = JsonEvent;
1418 fn next(&mut self) -> Option<JsonEvent> {
1419 if self.state == ParseFinished {
1423 if self.state == ParseBeforeFinish {
1424 self.parse_whitespace();
1425 // Make sure there is no trailing characters.
1427 self.state = ParseFinished;
1430 return Some(self.error_event(TrailingCharacters));
1438 impl<T: Iterator<Item=char>> Parser<T> {
1439 /// Creates the JSON parser.
1440 pub fn new(rdr: T) -> Parser<T> {
1441 let mut p = Parser {
1446 stack: Stack::new(),
1453 /// Provides access to the current position in the logical structure of the
1455 pub fn stack(&self) -> &Stack {
1459 fn eof(&self) -> bool { self.ch.is_none() }
1460 fn ch_or_null(&self) -> char { self.ch.unwrap_or('\x00') }
1461 fn bump(&mut self) {
1462 self.ch = self.rdr.next();
1464 if self.ch_is('\n') {
1472 fn next_char(&mut self) -> Option<char> {
1476 fn ch_is(&self, c: char) -> bool {
1480 fn error<U>(&self, reason: ErrorCode) -> Result<U, ParserError> {
1481 Err(SyntaxError(reason, self.line, self.col))
1484 fn parse_whitespace(&mut self) {
1485 while self.ch_is(' ') ||
1488 self.ch_is('\r') { self.bump(); }
1491 fn parse_number(&mut self) -> JsonEvent {
1492 let mut neg = false;
1494 if self.ch_is('-') {
1499 let res = match self.parse_u64() {
1501 Err(e) => { return Error(e); }
1504 if self.ch_is('.') || self.ch_is('e') || self.ch_is('E') {
1505 let mut res = res as f64;
1507 if self.ch_is('.') {
1508 res = match self.parse_decimal(res) {
1510 Err(e) => { return Error(e); }
1514 if self.ch_is('e') || self.ch_is('E') {
1515 res = match self.parse_exponent(res) {
1517 Err(e) => { return Error(e); }
1527 let res = (res as i64).wrapping_neg();
1529 // Make sure we didn't underflow.
1531 Error(SyntaxError(InvalidNumber, self.line, self.col))
1540 fn parse_u64(&mut self) -> Result<u64, ParserError> {
1541 let mut accum = 0u64;
1542 let last_accum = 0; // necessary to detect overflow.
1544 match self.ch_or_null() {
1548 // A leading '0' must be the only digit before the decimal point.
1549 if let '0' ..= '9' = self.ch_or_null() {
1550 return self.error(InvalidNumber)
1555 match self.ch_or_null() {
1556 c @ '0' ..= '9' => {
1557 accum = accum.wrapping_mul(10);
1558 accum = accum.wrapping_add((c as u64) - ('0' as u64));
1560 // Detect overflow by comparing to the last value.
1561 if accum <= last_accum { return self.error(InvalidNumber); }
1569 _ => return self.error(InvalidNumber),
1575 fn parse_decimal(&mut self, mut res: f64) -> Result<f64, ParserError> {
1578 // Make sure a digit follows the decimal place.
1579 match self.ch_or_null() {
1581 _ => return self.error(InvalidNumber)
1586 match self.ch_or_null() {
1587 c @ '0' ..= '9' => {
1589 res += (((c as isize) - ('0' as isize)) as f64) * dec;
1599 fn parse_exponent(&mut self, mut res: f64) -> Result<f64, ParserError> {
1603 let mut neg_exp = false;
1605 if self.ch_is('+') {
1607 } else if self.ch_is('-') {
1612 // Make sure a digit follows the exponent place.
1613 match self.ch_or_null() {
1615 _ => return self.error(InvalidNumber)
1618 match self.ch_or_null() {
1619 c @ '0' ..= '9' => {
1621 exp += (c as usize) - ('0' as usize);
1629 let exp = 10_f64.powi(exp as i32);
1639 fn decode_hex_escape(&mut self) -> Result<u16, ParserError> {
1642 while i < 4 && !self.eof() {
1644 n = match self.ch_or_null() {
1645 c @ '0' ..= '9' => n * 16 + ((c as u16) - ('0' as u16)),
1646 'a' | 'A' => n * 16 + 10,
1647 'b' | 'B' => n * 16 + 11,
1648 'c' | 'C' => n * 16 + 12,
1649 'd' | 'D' => n * 16 + 13,
1650 'e' | 'E' => n * 16 + 14,
1651 'f' | 'F' => n * 16 + 15,
1652 _ => return self.error(InvalidEscape)
1658 // Error out if we didn't parse 4 digits.
1660 return self.error(InvalidEscape);
1666 fn parse_str(&mut self) -> Result<string::String, ParserError> {
1667 let mut escape = false;
1668 let mut res = string::String::new();
1673 return self.error(EOFWhileParsingString);
1677 match self.ch_or_null() {
1678 '"' => res.push('"'),
1679 '\\' => res.push('\\'),
1680 '/' => res.push('/'),
1681 'b' => res.push('\x08'),
1682 'f' => res.push('\x0c'),
1683 'n' => res.push('\n'),
1684 'r' => res.push('\r'),
1685 't' => res.push('\t'),
1686 'u' => match self.decode_hex_escape()? {
1687 0xDC00 ..= 0xDFFF => {
1688 return self.error(LoneLeadingSurrogateInHexEscape)
1691 // Non-BMP characters are encoded as a sequence of
1692 // two hex escapes, representing UTF-16 surrogates.
1693 n1 @ 0xD800 ..= 0xDBFF => {
1694 match (self.next_char(), self.next_char()) {
1695 (Some('\\'), Some('u')) => (),
1696 _ => return self.error(UnexpectedEndOfHexEscape),
1699 let n2 = self.decode_hex_escape()?;
1700 if n2 < 0xDC00 || n2 > 0xDFFF {
1701 return self.error(LoneLeadingSurrogateInHexEscape)
1703 let c = (u32::from(n1 - 0xD800) << 10 |
1704 u32::from(n2 - 0xDC00)) + 0x1_0000;
1705 res.push(char::from_u32(c).unwrap());
1708 n => match char::from_u32(u32::from(n)) {
1709 Some(c) => res.push(c),
1710 None => return self.error(InvalidUnicodeCodePoint),
1713 _ => return self.error(InvalidEscape),
1716 } else if self.ch_is('\\') {
1724 Some(c) => res.push(c),
1725 None => unreachable!()
1731 // Invoked at each iteration, consumes the stream until it has enough
1732 // information to return a JsonEvent.
1733 // Manages an internal state so that parsing can be interrupted and resumed.
1734 // Also keeps track of the position in the logical structure of the json
1735 // stream isize the form of a stack that can be queried by the user using the
1737 fn parse(&mut self) -> JsonEvent {
1739 // The only paths where the loop can spin a new iteration
1740 // are in the cases ParseArrayComma and ParseObjectComma if ','
1741 // is parsed. In these cases the state is set to (respectively)
1742 // ParseArray(false) and ParseObject(false), which always return,
1743 // so there is no risk of getting stuck in an infinite loop.
1744 // All other paths return before the end of the loop's iteration.
1745 self.parse_whitespace();
1749 return self.parse_start();
1751 ParseArray(first) => {
1752 return self.parse_array(first);
1754 ParseArrayComma => {
1755 if let Some(evt) = self.parse_array_comma_or_end() {
1759 ParseObject(first) => {
1760 return self.parse_object(first);
1762 ParseObjectComma => {
1764 if self.ch_is(',') {
1765 self.state = ParseObject(false);
1768 return self.parse_object_end();
1772 return self.error_event(InvalidSyntax);
1778 fn parse_start(&mut self) -> JsonEvent {
1779 let val = self.parse_value();
1780 self.state = match val {
1781 Error(_) => ParseFinished,
1782 ArrayStart => ParseArray(true),
1783 ObjectStart => ParseObject(true),
1784 _ => ParseBeforeFinish,
1789 fn parse_array(&mut self, first: bool) -> JsonEvent {
1790 if self.ch_is(']') {
1792 self.error_event(InvalidSyntax)
1794 self.state = if self.stack.is_empty() {
1796 } else if self.stack.last_is_index() {
1806 self.stack.push_index(0);
1808 let val = self.parse_value();
1809 self.state = match val {
1810 Error(_) => ParseFinished,
1811 ArrayStart => ParseArray(true),
1812 ObjectStart => ParseObject(true),
1813 _ => ParseArrayComma,
1819 fn parse_array_comma_or_end(&mut self) -> Option<JsonEvent> {
1820 if self.ch_is(',') {
1821 self.stack.bump_index();
1822 self.state = ParseArray(false);
1825 } else if self.ch_is(']') {
1827 self.state = if self.stack.is_empty() {
1829 } else if self.stack.last_is_index() {
1836 } else if self.eof() {
1837 Some(self.error_event(EOFWhileParsingArray))
1839 Some(self.error_event(InvalidSyntax))
1843 fn parse_object(&mut self, first: bool) -> JsonEvent {
1844 if self.ch_is('}') {
1846 if self.stack.is_empty() {
1847 return self.error_event(TrailingComma);
1852 self.state = if self.stack.is_empty() {
1854 } else if self.stack.last_is_index() {
1863 return self.error_event(EOFWhileParsingObject);
1865 if !self.ch_is('"') {
1866 return self.error_event(KeyMustBeAString);
1868 let s = match self.parse_str() {
1871 self.state = ParseFinished;
1875 self.parse_whitespace();
1877 return self.error_event(EOFWhileParsingObject);
1878 } else if self.ch_or_null() != ':' {
1879 return self.error_event(ExpectedColon);
1881 self.stack.push_key(s);
1883 self.parse_whitespace();
1885 let val = self.parse_value();
1887 self.state = match val {
1888 Error(_) => ParseFinished,
1889 ArrayStart => ParseArray(true),
1890 ObjectStart => ParseObject(true),
1891 _ => ParseObjectComma,
1896 fn parse_object_end(&mut self) -> JsonEvent {
1897 if self.ch_is('}') {
1898 self.state = if self.stack.is_empty() {
1900 } else if self.stack.last_is_index() {
1907 } else if self.eof() {
1908 self.error_event(EOFWhileParsingObject)
1910 self.error_event(InvalidSyntax)
1914 fn parse_value(&mut self) -> JsonEvent {
1915 if self.eof() { return self.error_event(EOFWhileParsingValue); }
1916 match self.ch_or_null() {
1917 'n' => { self.parse_ident("ull", NullValue) }
1918 't' => { self.parse_ident("rue", BooleanValue(true)) }
1919 'f' => { self.parse_ident("alse", BooleanValue(false)) }
1920 '0' ..= '9' | '-' => self.parse_number(),
1921 '"' => match self.parse_str() {
1922 Ok(s) => StringValue(s),
1933 _ => { self.error_event(InvalidSyntax) }
1937 fn parse_ident(&mut self, ident: &str, value: JsonEvent) -> JsonEvent {
1938 if ident.chars().all(|c| Some(c) == self.next_char()) {
1942 Error(SyntaxError(InvalidSyntax, self.line, self.col))
1946 fn error_event(&mut self, reason: ErrorCode) -> JsonEvent {
1947 self.state = ParseFinished;
1948 Error(SyntaxError(reason, self.line, self.col))
1952 /// A Builder consumes a json::Parser to create a generic Json structure.
1953 pub struct Builder<T> {
1955 token: Option<JsonEvent>,
1958 impl<T: Iterator<Item=char>> Builder<T> {
1959 /// Creates a JSON Builder.
1960 pub fn new(src: T) -> Builder<T> {
1961 Builder { parser: Parser::new(src), token: None, }
1964 // Decode a Json value from a Parser.
1965 pub fn build(&mut self) -> Result<Json, BuilderError> {
1967 let result = self.build_value();
1971 Some(Error(ref e)) => { return Err(e.clone()); }
1972 ref tok => { panic!("unexpected token {:?}", tok.clone()); }
1977 fn bump(&mut self) {
1978 self.token = self.parser.next();
1981 fn build_value(&mut self) -> Result<Json, BuilderError> {
1983 Some(NullValue) => Ok(Json::Null),
1984 Some(I64Value(n)) => Ok(Json::I64(n)),
1985 Some(U64Value(n)) => Ok(Json::U64(n)),
1986 Some(F64Value(n)) => Ok(Json::F64(n)),
1987 Some(BooleanValue(b)) => Ok(Json::Boolean(b)),
1988 Some(StringValue(ref mut s)) => {
1989 let mut temp = string::String::new();
1991 Ok(Json::String(temp))
1993 Some(Error(ref e)) => Err(e.clone()),
1994 Some(ArrayStart) => self.build_array(),
1995 Some(ObjectStart) => self.build_object(),
1996 Some(ObjectEnd) => self.parser.error(InvalidSyntax),
1997 Some(ArrayEnd) => self.parser.error(InvalidSyntax),
1998 None => self.parser.error(EOFWhileParsingValue),
2002 fn build_array(&mut self) -> Result<Json, BuilderError> {
2004 let mut values = Vec::new();
2007 if self.token == Some(ArrayEnd) {
2008 return Ok(Json::Array(values.into_iter().collect()));
2010 match self.build_value() {
2011 Ok(v) => values.push(v),
2012 Err(e) => { return Err(e) }
2018 fn build_object(&mut self) -> Result<Json, BuilderError> {
2021 let mut values = BTreeMap::new();
2025 Some(ObjectEnd) => { return Ok(Json::Object(values)); }
2026 Some(Error(ref e)) => { return Err(e.clone()); }
2030 let key = match self.parser.stack().top() {
2031 Some(StackElement::Key(k)) => { k.to_owned() }
2032 _ => { panic!("invalid state"); }
2034 match self.build_value() {
2035 Ok(value) => { values.insert(key, value); }
2036 Err(e) => { return Err(e); }
2040 self.parser.error(EOFWhileParsingObject)
2044 /// Decodes a json value from an `&mut io::Read`
2045 pub fn from_reader(rdr: &mut dyn Read) -> Result<Json, BuilderError> {
2046 let mut contents = Vec::new();
2047 match rdr.read_to_end(&mut contents) {
2049 Err(e) => return Err(io_error_to_error(e))
2051 let s = match str::from_utf8(&contents).ok() {
2053 _ => return Err(SyntaxError(NotUtf8, 0, 0))
2055 let mut builder = Builder::new(s.chars());
2059 /// Decodes a json value from a string
2060 pub fn from_str(s: &str) -> Result<Json, BuilderError> {
2061 let mut builder = Builder::new(s.chars());
2065 /// A structure to decode JSON to values in rust.
2066 pub struct Decoder {
2071 /// Creates a new decoder instance for decoding the specified JSON value.
2072 pub fn new(json: Json) -> Decoder {
2073 Decoder { stack: vec![json] }
2076 fn pop(&mut self) -> Json {
2077 self.stack.pop().unwrap()
2081 macro_rules! expect {
2082 ($e:expr, Null) => ({
2084 Json::Null => Ok(()),
2085 other => Err(ExpectedError("Null".to_owned(),
2089 ($e:expr, $t:ident) => ({
2091 Json::$t(v) => Ok(v),
2093 Err(ExpectedError(stringify!($t).to_owned(),
2100 macro_rules! read_primitive {
2101 ($name:ident, $ty:ty) => {
2102 fn $name(&mut self) -> DecodeResult<$ty> {
2104 Json::I64(f) => Ok(f as $ty),
2105 Json::U64(f) => Ok(f as $ty),
2106 Json::F64(f) => Err(ExpectedError("Integer".to_owned(), f.to_string())),
2107 // re: #12967.. a type w/ numeric keys (ie HashMap<usize, V> etc)
2108 // is going to have a string here, as per JSON spec.
2109 Json::String(s) => match s.parse().ok() {
2111 None => Err(ExpectedError("Number".to_owned(), s)),
2113 value => Err(ExpectedError("Number".to_owned(), value.to_string())),
2119 impl crate::Decoder for Decoder {
2120 type Error = DecoderError;
2122 fn read_nil(&mut self) -> DecodeResult<()> {
2123 expect!(self.pop(), Null)
2126 read_primitive! { read_usize, usize }
2127 read_primitive! { read_u8, u8 }
2128 read_primitive! { read_u16, u16 }
2129 read_primitive! { read_u32, u32 }
2130 read_primitive! { read_u64, u64 }
2131 read_primitive! { read_u128, u128 }
2132 read_primitive! { read_isize, isize }
2133 read_primitive! { read_i8, i8 }
2134 read_primitive! { read_i16, i16 }
2135 read_primitive! { read_i32, i32 }
2136 read_primitive! { read_i64, i64 }
2137 read_primitive! { read_i128, i128 }
2139 fn read_f32(&mut self) -> DecodeResult<f32> { self.read_f64().map(|x| x as f32) }
2141 fn read_f64(&mut self) -> DecodeResult<f64> {
2143 Json::I64(f) => Ok(f as f64),
2144 Json::U64(f) => Ok(f as f64),
2145 Json::F64(f) => Ok(f),
2146 Json::String(s) => {
2147 // re: #12967.. a type w/ numeric keys (ie HashMap<usize, V> etc)
2148 // is going to have a string here, as per JSON spec.
2149 match s.parse().ok() {
2151 None => Err(ExpectedError("Number".to_owned(), s)),
2154 Json::Null => Ok(f64::NAN),
2155 value => Err(ExpectedError("Number".to_owned(), value.to_string()))
2159 fn read_bool(&mut self) -> DecodeResult<bool> {
2160 expect!(self.pop(), Boolean)
2163 fn read_char(&mut self) -> DecodeResult<char> {
2164 let s = self.read_str()?;
2166 let mut it = s.chars();
2167 match (it.next(), it.next()) {
2168 // exactly one character
2169 (Some(c), None) => return Ok(c),
2173 Err(ExpectedError("single character string".to_owned(), s.to_string()))
2176 fn read_str(&mut self) -> DecodeResult<Cow<'_, str>> {
2177 expect!(self.pop(), String).map(Cow::Owned)
2180 fn read_enum<T, F>(&mut self, _name: &str, f: F) -> DecodeResult<T> where
2181 F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2186 fn read_enum_variant<T, F>(&mut self, names: &[&str],
2187 mut f: F) -> DecodeResult<T>
2188 where F: FnMut(&mut Decoder, usize) -> DecodeResult<T>,
2190 let name = match self.pop() {
2191 Json::String(s) => s,
2192 Json::Object(mut o) => {
2193 let n = match o.remove(&"variant".to_owned()) {
2194 Some(Json::String(s)) => s,
2196 return Err(ExpectedError("String".to_owned(), val.to_string()))
2199 return Err(MissingFieldError("variant".to_owned()))
2202 match o.remove(&"fields".to_string()) {
2203 Some(Json::Array(l)) => {
2204 self.stack.extend(l.into_iter().rev());
2207 return Err(ExpectedError("Array".to_owned(), val.to_string()))
2210 return Err(MissingFieldError("fields".to_owned()))
2216 return Err(ExpectedError("String or Object".to_owned(), json.to_string()))
2219 let idx = match names.iter().position(|n| *n == &name[..]) {
2221 None => return Err(UnknownVariantError(name))
2226 fn read_enum_variant_arg<T, F>(&mut self, _idx: usize, f: F) -> DecodeResult<T> where
2227 F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2232 fn read_enum_struct_variant<T, F>(&mut self, names: &[&str], f: F) -> DecodeResult<T> where
2233 F: FnMut(&mut Decoder, usize) -> DecodeResult<T>,
2235 self.read_enum_variant(names, f)
2239 fn read_enum_struct_variant_field<T, F>(&mut self,
2243 -> DecodeResult<T> where
2244 F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2246 self.read_enum_variant_arg(idx, f)
2249 fn read_struct<T, F>(&mut self, _name: &str, _len: usize, f: F) -> DecodeResult<T> where
2250 F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2252 let value = f(self)?;
2257 fn read_struct_field<T, F>(&mut self,
2261 -> DecodeResult<T> where
2262 F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2264 let mut obj = expect!(self.pop(), Object)?;
2266 let value = match obj.remove(&name.to_string()) {
2268 // Add a Null and try to parse it as an Option<_>
2269 // to get None as a default value.
2270 self.stack.push(Json::Null);
2273 Err(_) => return Err(MissingFieldError(name.to_string())),
2277 self.stack.push(json);
2281 self.stack.push(Json::Object(obj));
2285 fn read_tuple<T, F>(&mut self, tuple_len: usize, f: F) -> DecodeResult<T> where
2286 F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2288 self.read_seq(move |d, len| {
2289 if len == tuple_len {
2292 Err(ExpectedError(format!("Tuple{}", tuple_len), format!("Tuple{}", len)))
2297 fn read_tuple_arg<T, F>(&mut self, idx: usize, f: F) -> DecodeResult<T> where
2298 F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2300 self.read_seq_elt(idx, f)
2303 fn read_tuple_struct<T, F>(&mut self,
2307 -> DecodeResult<T> where
2308 F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2310 self.read_tuple(len, f)
2313 fn read_tuple_struct_arg<T, F>(&mut self,
2316 -> DecodeResult<T> where
2317 F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2319 self.read_tuple_arg(idx, f)
2322 fn read_option<T, F>(&mut self, mut f: F) -> DecodeResult<T> where
2323 F: FnMut(&mut Decoder, bool) -> DecodeResult<T>,
2326 Json::Null => f(self, false),
2327 value => { self.stack.push(value); f(self, true) }
2331 fn read_seq<T, F>(&mut self, f: F) -> DecodeResult<T> where
2332 F: FnOnce(&mut Decoder, usize) -> DecodeResult<T>,
2334 let array = expect!(self.pop(), Array)?;
2335 let len = array.len();
2336 self.stack.extend(array.into_iter().rev());
2340 fn read_seq_elt<T, F>(&mut self, _idx: usize, f: F) -> DecodeResult<T> where
2341 F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2346 fn read_map<T, F>(&mut self, f: F) -> DecodeResult<T> where
2347 F: FnOnce(&mut Decoder, usize) -> DecodeResult<T>,
2349 let obj = expect!(self.pop(), Object)?;
2350 let len = obj.len();
2351 for (key, value) in obj {
2352 self.stack.push(value);
2353 self.stack.push(Json::String(key));
2358 fn read_map_elt_key<T, F>(&mut self, _idx: usize, f: F) -> DecodeResult<T> where
2359 F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2364 fn read_map_elt_val<T, F>(&mut self, _idx: usize, f: F) -> DecodeResult<T> where
2365 F: FnOnce(&mut Decoder) -> DecodeResult<T>,
2370 fn error(&mut self, err: &str) -> DecoderError {
2371 ApplicationError(err.to_string())
2375 /// A trait for converting values to JSON
2377 /// Converts the value of `self` to an instance of JSON
2378 fn to_json(&self) -> Json;
2381 macro_rules! to_json_impl_i64 {
2383 $(impl ToJson for $t {
2384 fn to_json(&self) -> Json {
2385 Json::I64(*self as i64)
2391 to_json_impl_i64! { isize, i8, i16, i32, i64 }
2393 macro_rules! to_json_impl_u64 {
2395 $(impl ToJson for $t {
2396 fn to_json(&self) -> Json {
2397 Json::U64(*self as u64)
2403 to_json_impl_u64! { usize, u8, u16, u32, u64 }
2405 impl ToJson for Json {
2406 fn to_json(&self) -> Json { self.clone() }
2409 impl ToJson for f32 {
2410 fn to_json(&self) -> Json { f64::from(*self).to_json() }
2413 impl ToJson for f64 {
2414 fn to_json(&self) -> Json {
2415 match self.classify() {
2416 Fp::Nan | Fp::Infinite => Json::Null,
2417 _ => Json::F64(*self)
2422 impl ToJson for () {
2423 fn to_json(&self) -> Json { Json::Null }
2426 impl ToJson for bool {
2427 fn to_json(&self) -> Json { Json::Boolean(*self) }
2430 impl ToJson for str {
2431 fn to_json(&self) -> Json { Json::String(self.to_string()) }
2434 impl ToJson for string::String {
2435 fn to_json(&self) -> Json { Json::String((*self).clone()) }
2438 macro_rules! tuple_impl {
2439 // use variables to indicate the arity of the tuple
2440 ($($tyvar:ident),* ) => {
2441 // the trailing commas are for the 1 tuple
2443 $( $tyvar : ToJson ),*
2444 > ToJson for ( $( $tyvar ),* , ) {
2447 #[allow(non_snake_case)]
2448 fn to_json(&self) -> Json {
2450 ($(ref $tyvar),*,) => Json::Array(vec![$($tyvar.to_json()),*])
2459 tuple_impl!{A, B, C}
2460 tuple_impl!{A, B, C, D}
2461 tuple_impl!{A, B, C, D, E}
2462 tuple_impl!{A, B, C, D, E, F}
2463 tuple_impl!{A, B, C, D, E, F, G}
2464 tuple_impl!{A, B, C, D, E, F, G, H}
2465 tuple_impl!{A, B, C, D, E, F, G, H, I}
2466 tuple_impl!{A, B, C, D, E, F, G, H, I, J}
2467 tuple_impl!{A, B, C, D, E, F, G, H, I, J, K}
2468 tuple_impl!{A, B, C, D, E, F, G, H, I, J, K, L}
2470 impl<A: ToJson> ToJson for [A] {
2471 fn to_json(&self) -> Json { Json::Array(self.iter().map(|elt| elt.to_json()).collect()) }
2474 impl<A: ToJson> ToJson for Vec<A> {
2475 fn to_json(&self) -> Json { Json::Array(self.iter().map(|elt| elt.to_json()).collect()) }
2478 impl<A: ToJson> ToJson for BTreeMap<string::String, A> {
2479 fn to_json(&self) -> Json {
2480 let mut d = BTreeMap::new();
2481 for (key, value) in self {
2482 d.insert((*key).clone(), value.to_json());
2488 impl<A: ToJson> ToJson for HashMap<string::String, A> {
2489 fn to_json(&self) -> Json {
2490 let mut d = BTreeMap::new();
2491 for (key, value) in self {
2492 d.insert((*key).clone(), value.to_json());
2498 impl<A:ToJson> ToJson for Option<A> {
2499 fn to_json(&self) -> Json {
2502 Some(ref value) => value.to_json()
2507 struct FormatShim<'a, 'b> {
2508 inner: &'a mut fmt::Formatter<'b>,
2511 impl<'a, 'b> fmt::Write for FormatShim<'a, 'b> {
2512 fn write_str(&mut self, s: &str) -> fmt::Result {
2513 match self.inner.write_str(s) {
2515 Err(_) => Err(fmt::Error)
2520 impl fmt::Display for Json {
2521 /// Encodes a json value into a string
2522 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2523 let mut shim = FormatShim { inner: f };
2524 let mut encoder = Encoder::new(&mut shim);
2525 match self.encode(&mut encoder) {
2527 Err(_) => Err(fmt::Error)
2532 impl<'a> fmt::Display for PrettyJson<'a> {
2533 /// Encodes a json value into a string
2534 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2535 let mut shim = FormatShim { inner: f };
2536 let mut encoder = PrettyEncoder::new(&mut shim);
2537 match self.inner.encode(&mut encoder) {
2539 Err(_) => Err(fmt::Error)
2544 impl<'a, T: Encodable> fmt::Display for AsJson<'a, T> {
2545 /// Encodes a json value into a string
2546 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2547 let mut shim = FormatShim { inner: f };
2548 let mut encoder = Encoder::new(&mut shim);
2549 match self.inner.encode(&mut encoder) {
2551 Err(_) => Err(fmt::Error)
2556 impl<'a, T> AsPrettyJson<'a, T> {
2557 /// Sets the indentation level for the emitted JSON
2558 pub fn indent(mut self, indent: usize) -> AsPrettyJson<'a, T> {
2559 self.indent = Some(indent);
2564 impl<'a, T: Encodable> fmt::Display for AsPrettyJson<'a, T> {
2565 /// Encodes a json value into a string
2566 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2567 let mut shim = FormatShim { inner: f };
2568 let mut encoder = PrettyEncoder::new(&mut shim);
2569 if let Some(n) = self.indent {
2570 encoder.set_indent(n);
2572 match self.inner.encode(&mut encoder) {
2574 Err(_) => Err(fmt::Error)
2579 impl FromStr for Json {
2580 type Err = BuilderError;
2581 fn from_str(s: &str) -> Result<Json, BuilderError> {