PageRenderTime 57ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/src/libserialize/json.rs

http://github.com/eholk/rust
Rust | 4017 lines | 3528 code | 190 blank | 299 comment | 176 complexity | 8d467e4d40ef13e044e61abb847be881 MD5 | raw file
Possible License(s): AGPL-1.0, BSD-2-Clause, 0BSD, Apache-2.0, MIT, LGPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
  2. // file at the top-level directory of this distribution and at
  3. // http://rust-lang.org/COPYRIGHT.
  4. //
  5. // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
  6. // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
  7. // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
  8. // option. This file may not be copied, modified, or distributed
  9. // except according to those terms.
  10. // Rust JSON serialization library
  11. // Copyright (c) 2011 Google Inc.
  12. #![forbid(non_camel_case_types)]
  13. #![allow(missing_docs)]
  14. //! JSON parsing and serialization
  15. //!
  16. //! # What is JSON?
  17. //!
  18. //! JSON (JavaScript Object Notation) is a way to write data in Javascript.
  19. //! Like XML, it allows to encode structured data in a text format that can be easily read by humans
  20. //! Its simple syntax and native compatibility with JavaScript have made it a widely used format.
  21. //!
  22. //! Data types that can be encoded are JavaScript types (see the `Json` enum for more details):
  23. //!
  24. //! * `Boolean`: equivalent to rust's `bool`
  25. //! * `Number`: equivalent to rust's `f64`
  26. //! * `String`: equivalent to rust's `String`
  27. //! * `Array`: equivalent to rust's `Vec<T>`, but also allowing objects of different types in the
  28. //! same array
  29. //! * `Object`: equivalent to rust's `BTreeMap<String, json::Json>`
  30. //! * `Null`
  31. //!
  32. //! An object is a series of string keys mapping to values, in `"key": value` format.
  33. //! Arrays are enclosed in square brackets ([ ... ]) and objects in curly brackets ({ ... }).
  34. //! A simple JSON document encoding a person, their age, address and phone numbers could look like
  35. //!
  36. //! ```ignore
  37. //! {
  38. //! "FirstName": "John",
  39. //! "LastName": "Doe",
  40. //! "Age": 43,
  41. //! "Address": {
  42. //! "Street": "Downing Street 10",
  43. //! "City": "London",
  44. //! "Country": "Great Britain"
  45. //! },
  46. //! "PhoneNumbers": [
  47. //! "+44 1234567",
  48. //! "+44 2345678"
  49. //! ]
  50. //! }
  51. //! ```
  52. //!
  53. //! # Rust Type-based Encoding and Decoding
  54. //!
  55. //! Rust provides a mechanism for low boilerplate encoding & decoding of values to and from JSON via
  56. //! the serialization API.
  57. //! To be able to encode a piece of data, it must implement the `serialize::RustcEncodable` trait.
  58. //! To be able to decode a piece of data, it must implement the `serialize::RustcDecodable` trait.
  59. //! The Rust compiler provides an annotation to automatically generate the code for these traits:
  60. //! `#[derive(RustcDecodable, RustcEncodable)]`
  61. //!
  62. //! The JSON API provides an enum `json::Json` and a trait `ToJson` to encode objects.
  63. //! The `ToJson` trait provides a `to_json` method to convert an object into a `json::Json` value.
  64. //! A `json::Json` value can be encoded as a string or buffer using the functions described above.
  65. //! You can also use the `json::Encoder` object, which implements the `Encoder` trait.
  66. //!
  67. //! When using `ToJson` the `RustcEncodable` trait implementation is not mandatory.
  68. //!
  69. //! # Examples of use
  70. //!
  71. //! ## Using Autoserialization
  72. //!
  73. //! Create a struct called `TestStruct` and serialize and deserialize it to and from JSON using the
  74. //! serialization API, using the derived serialization code.
  75. //!
  76. //! ```rust
  77. //! # #![feature(rustc_private)]
  78. //! extern crate serialize as rustc_serialize; // for the deriving below
  79. //! use rustc_serialize::json;
  80. //!
  81. //! // Automatically generate `Decodable` and `Encodable` trait implementations
  82. //! #[derive(RustcDecodable, RustcEncodable)]
  83. //! pub struct TestStruct {
  84. //! data_int: u8,
  85. //! data_str: String,
  86. //! data_vector: Vec<u8>,
  87. //! }
  88. //!
  89. //! fn main() {
  90. //! let object = TestStruct {
  91. //! data_int: 1,
  92. //! data_str: "homura".to_string(),
  93. //! data_vector: vec![2,3,4,5],
  94. //! };
  95. //!
  96. //! // Serialize using `json::encode`
  97. //! let encoded = json::encode(&object).unwrap();
  98. //!
  99. //! // Deserialize using `json::decode`
  100. //! let decoded: TestStruct = json::decode(&encoded[..]).unwrap();
  101. //! }
  102. //! ```
  103. //!
  104. //! ## Using the `ToJson` trait
  105. //!
  106. //! The examples above use the `ToJson` trait to generate the JSON string, which is required
  107. //! for custom mappings.
  108. //!
  109. //! ### Simple example of `ToJson` usage
  110. //!
  111. //! ```rust
  112. //! # #![feature(rustc_private)]
  113. //! extern crate serialize;
  114. //! use serialize::json::{self, ToJson, Json};
  115. //!
  116. //! // A custom data structure
  117. //! struct ComplexNum {
  118. //! a: f64,
  119. //! b: f64,
  120. //! }
  121. //!
  122. //! // JSON value representation
  123. //! impl ToJson for ComplexNum {
  124. //! fn to_json(&self) -> Json {
  125. //! Json::String(format!("{}+{}i", self.a, self.b))
  126. //! }
  127. //! }
  128. //!
  129. //! // Only generate `RustcEncodable` trait implementation
  130. //! #[derive(Encodable)]
  131. //! pub struct ComplexNumRecord {
  132. //! uid: u8,
  133. //! dsc: String,
  134. //! val: Json,
  135. //! }
  136. //!
  137. //! fn main() {
  138. //! let num = ComplexNum { a: 0.0001, b: 12.539 };
  139. //! let data: String = json::encode(&ComplexNumRecord{
  140. //! uid: 1,
  141. //! dsc: "test".to_string(),
  142. //! val: num.to_json(),
  143. //! }).unwrap();
  144. //! println!("data: {}", data);
  145. //! // data: {"uid":1,"dsc":"test","val":"0.0001+12.539i"};
  146. //! }
  147. //! ```
  148. //!
  149. //! ### Verbose example of `ToJson` usage
  150. //!
  151. //! ```rust
  152. //! # #![feature(rustc_private)]
  153. //! extern crate serialize;
  154. //! use std::collections::BTreeMap;
  155. //! use serialize::json::{self, Json, ToJson};
  156. //!
  157. //! // Only generate `Decodable` trait implementation
  158. //! #[derive(Decodable)]
  159. //! pub struct TestStruct {
  160. //! data_int: u8,
  161. //! data_str: String,
  162. //! data_vector: Vec<u8>,
  163. //! }
  164. //!
  165. //! // Specify encoding method manually
  166. //! impl ToJson for TestStruct {
  167. //! fn to_json(&self) -> Json {
  168. //! let mut d = BTreeMap::new();
  169. //! // All standard types implement `to_json()`, so use it
  170. //! d.insert("data_int".to_string(), self.data_int.to_json());
  171. //! d.insert("data_str".to_string(), self.data_str.to_json());
  172. //! d.insert("data_vector".to_string(), self.data_vector.to_json());
  173. //! Json::Object(d)
  174. //! }
  175. //! }
  176. //!
  177. //! fn main() {
  178. //! // Serialize using `ToJson`
  179. //! let input_data = TestStruct {
  180. //! data_int: 1,
  181. //! data_str: "madoka".to_string(),
  182. //! data_vector: vec![2,3,4,5],
  183. //! };
  184. //! let json_obj: Json = input_data.to_json();
  185. //! let json_str: String = json_obj.to_string();
  186. //!
  187. //! // Deserialize like before
  188. //! let decoded: TestStruct = json::decode(&json_str).unwrap();
  189. //! }
  190. //! ```
  191. use self::JsonEvent::*;
  192. use self::ErrorCode::*;
  193. use self::ParserError::*;
  194. use self::DecoderError::*;
  195. use self::ParserState::*;
  196. use self::InternalStackElement::*;
  197. use std::borrow::Cow;
  198. use std::collections::{HashMap, BTreeMap};
  199. use std::io::prelude::*;
  200. use std::io;
  201. use std::mem::swap;
  202. use std::num::FpCategory as Fp;
  203. use std::ops::Index;
  204. use std::str::FromStr;
  205. use std::string;
  206. use std::{char, f64, fmt, str};
  207. use std;
  208. use Encodable;
  209. /// Represents a json value
  210. #[derive(Clone, PartialEq, PartialOrd, Debug)]
  211. pub enum Json {
  212. I64(i64),
  213. U64(u64),
  214. F64(f64),
  215. String(string::String),
  216. Boolean(bool),
  217. Array(self::Array),
  218. Object(self::Object),
  219. Null,
  220. }
  221. pub type Array = Vec<Json>;
  222. pub type Object = BTreeMap<string::String, Json>;
  223. pub struct PrettyJson<'a> { inner: &'a Json }
  224. pub struct AsJson<'a, T: 'a> { inner: &'a T }
  225. pub struct AsPrettyJson<'a, T: 'a> { inner: &'a T, indent: Option<usize> }
  226. /// The errors that can arise while parsing a JSON stream.
  227. #[derive(Clone, Copy, PartialEq, Debug)]
  228. pub enum ErrorCode {
  229. InvalidSyntax,
  230. InvalidNumber,
  231. EOFWhileParsingObject,
  232. EOFWhileParsingArray,
  233. EOFWhileParsingValue,
  234. EOFWhileParsingString,
  235. KeyMustBeAString,
  236. ExpectedColon,
  237. TrailingCharacters,
  238. TrailingComma,
  239. InvalidEscape,
  240. InvalidUnicodeCodePoint,
  241. LoneLeadingSurrogateInHexEscape,
  242. UnexpectedEndOfHexEscape,
  243. UnrecognizedHex,
  244. NotFourDigit,
  245. NotUtf8,
  246. }
  247. #[derive(Clone, PartialEq, Debug)]
  248. pub enum ParserError {
  249. /// msg, line, col
  250. SyntaxError(ErrorCode, usize, usize),
  251. IoError(io::ErrorKind, String),
  252. }
  253. // Builder and Parser have the same errors.
  254. pub type BuilderError = ParserError;
  255. #[derive(Clone, PartialEq, Debug)]
  256. pub enum DecoderError {
  257. ParseError(ParserError),
  258. ExpectedError(string::String, string::String),
  259. MissingFieldError(string::String),
  260. UnknownVariantError(string::String),
  261. ApplicationError(string::String)
  262. }
  263. #[derive(Copy, Clone, Debug)]
  264. pub enum EncoderError {
  265. FmtError(fmt::Error),
  266. BadHashmapKey,
  267. }
  268. /// Returns a readable error string for a given error code.
  269. pub fn error_str(error: ErrorCode) -> &'static str {
  270. match error {
  271. InvalidSyntax => "invalid syntax",
  272. InvalidNumber => "invalid number",
  273. EOFWhileParsingObject => "EOF While parsing object",
  274. EOFWhileParsingArray => "EOF While parsing array",
  275. EOFWhileParsingValue => "EOF While parsing value",
  276. EOFWhileParsingString => "EOF While parsing string",
  277. KeyMustBeAString => "key must be a string",
  278. ExpectedColon => "expected `:`",
  279. TrailingCharacters => "trailing characters",
  280. TrailingComma => "trailing comma",
  281. InvalidEscape => "invalid escape",
  282. UnrecognizedHex => "invalid \\u{ esc}ape (unrecognized hex)",
  283. NotFourDigit => "invalid \\u{ esc}ape (not four digits)",
  284. NotUtf8 => "contents not utf-8",
  285. InvalidUnicodeCodePoint => "invalid Unicode code point",
  286. LoneLeadingSurrogateInHexEscape => "lone leading surrogate in hex escape",
  287. UnexpectedEndOfHexEscape => "unexpected end of hex escape",
  288. }
  289. }
  290. /// Shortcut function to decode a JSON `&str` into an object
  291. pub fn decode<T: ::Decodable>(s: &str) -> DecodeResult<T> {
  292. let json = match from_str(s) {
  293. Ok(x) => x,
  294. Err(e) => return Err(ParseError(e))
  295. };
  296. let mut decoder = Decoder::new(json);
  297. ::Decodable::decode(&mut decoder)
  298. }
  299. /// Shortcut function to encode a `T` into a JSON `String`
  300. pub fn encode<T: ::Encodable>(object: &T) -> Result<string::String, EncoderError> {
  301. let mut s = String::new();
  302. {
  303. let mut encoder = Encoder::new(&mut s);
  304. object.encode(&mut encoder)?;
  305. }
  306. Ok(s)
  307. }
  308. impl fmt::Display for ErrorCode {
  309. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  310. error_str(*self).fmt(f)
  311. }
  312. }
  313. fn io_error_to_error(io: io::Error) -> ParserError {
  314. IoError(io.kind(), io.to_string())
  315. }
  316. impl fmt::Display for ParserError {
  317. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  318. // FIXME this should be a nicer error
  319. fmt::Debug::fmt(self, f)
  320. }
  321. }
  322. impl fmt::Display for DecoderError {
  323. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  324. // FIXME this should be a nicer error
  325. fmt::Debug::fmt(self, f)
  326. }
  327. }
  328. impl std::error::Error for DecoderError {
  329. fn description(&self) -> &str { "decoder error" }
  330. }
  331. impl fmt::Display for EncoderError {
  332. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  333. // FIXME this should be a nicer error
  334. fmt::Debug::fmt(self, f)
  335. }
  336. }
  337. impl std::error::Error for EncoderError {
  338. fn description(&self) -> &str { "encoder error" }
  339. }
  340. impl From<fmt::Error> for EncoderError {
  341. fn from(err: fmt::Error) -> EncoderError { EncoderError::FmtError(err) }
  342. }
  343. pub type EncodeResult = Result<(), EncoderError>;
  344. pub type DecodeResult<T> = Result<T, DecoderError>;
  345. fn escape_str(wr: &mut fmt::Write, v: &str) -> EncodeResult {
  346. wr.write_str("\"")?;
  347. let mut start = 0;
  348. for (i, byte) in v.bytes().enumerate() {
  349. let escaped = match byte {
  350. b'"' => "\\\"",
  351. b'\\' => "\\\\",
  352. b'\x00' => "\\u0000",
  353. b'\x01' => "\\u0001",
  354. b'\x02' => "\\u0002",
  355. b'\x03' => "\\u0003",
  356. b'\x04' => "\\u0004",
  357. b'\x05' => "\\u0005",
  358. b'\x06' => "\\u0006",
  359. b'\x07' => "\\u0007",
  360. b'\x08' => "\\b",
  361. b'\t' => "\\t",
  362. b'\n' => "\\n",
  363. b'\x0b' => "\\u000b",
  364. b'\x0c' => "\\f",
  365. b'\r' => "\\r",
  366. b'\x0e' => "\\u000e",
  367. b'\x0f' => "\\u000f",
  368. b'\x10' => "\\u0010",
  369. b'\x11' => "\\u0011",
  370. b'\x12' => "\\u0012",
  371. b'\x13' => "\\u0013",
  372. b'\x14' => "\\u0014",
  373. b'\x15' => "\\u0015",
  374. b'\x16' => "\\u0016",
  375. b'\x17' => "\\u0017",
  376. b'\x18' => "\\u0018",
  377. b'\x19' => "\\u0019",
  378. b'\x1a' => "\\u001a",
  379. b'\x1b' => "\\u001b",
  380. b'\x1c' => "\\u001c",
  381. b'\x1d' => "\\u001d",
  382. b'\x1e' => "\\u001e",
  383. b'\x1f' => "\\u001f",
  384. b'\x7f' => "\\u007f",
  385. _ => { continue; }
  386. };
  387. if start < i {
  388. wr.write_str(&v[start..i])?;
  389. }
  390. wr.write_str(escaped)?;
  391. start = i + 1;
  392. }
  393. if start != v.len() {
  394. wr.write_str(&v[start..])?;
  395. }
  396. wr.write_str("\"")?;
  397. Ok(())
  398. }
  399. fn escape_char(writer: &mut fmt::Write, v: char) -> EncodeResult {
  400. escape_str(writer, v.encode_utf8(&mut [0; 4]))
  401. }
  402. fn spaces(wr: &mut fmt::Write, mut n: usize) -> EncodeResult {
  403. const BUF: &'static str = " ";
  404. while n >= BUF.len() {
  405. wr.write_str(BUF)?;
  406. n -= BUF.len();
  407. }
  408. if n > 0 {
  409. wr.write_str(&BUF[..n])?;
  410. }
  411. Ok(())
  412. }
  413. fn fmt_number_or_null(v: f64) -> string::String {
  414. match v.classify() {
  415. Fp::Nan | Fp::Infinite => string::String::from("null"),
  416. _ if v.fract() != 0f64 => v.to_string(),
  417. _ => v.to_string() + ".0",
  418. }
  419. }
  420. /// A structure for implementing serialization to JSON.
  421. pub struct Encoder<'a> {
  422. writer: &'a mut (fmt::Write+'a),
  423. is_emitting_map_key: bool,
  424. }
  425. impl<'a> Encoder<'a> {
  426. /// Creates a new JSON encoder whose output will be written to the writer
  427. /// specified.
  428. pub fn new(writer: &'a mut fmt::Write) -> Encoder<'a> {
  429. Encoder { writer: writer, is_emitting_map_key: false, }
  430. }
  431. }
  432. macro_rules! emit_enquoted_if_mapkey {
  433. ($enc:ident,$e:expr) => ({
  434. if $enc.is_emitting_map_key {
  435. write!($enc.writer, "\"{}\"", $e)?;
  436. } else {
  437. write!($enc.writer, "{}", $e)?;
  438. }
  439. Ok(())
  440. })
  441. }
  442. impl<'a> ::Encoder for Encoder<'a> {
  443. type Error = EncoderError;
  444. fn emit_nil(&mut self) -> EncodeResult {
  445. if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
  446. write!(self.writer, "null")?;
  447. Ok(())
  448. }
  449. fn emit_usize(&mut self, v: usize) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
  450. fn emit_u128(&mut self, v: u128) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
  451. fn emit_u64(&mut self, v: u64) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
  452. fn emit_u32(&mut self, v: u32) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
  453. fn emit_u16(&mut self, v: u16) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
  454. fn emit_u8(&mut self, v: u8) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
  455. fn emit_isize(&mut self, v: isize) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
  456. fn emit_i128(&mut self, v: i128) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
  457. fn emit_i64(&mut self, v: i64) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
  458. fn emit_i32(&mut self, v: i32) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
  459. fn emit_i16(&mut self, v: i16) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
  460. fn emit_i8(&mut self, v: i8) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
  461. fn emit_bool(&mut self, v: bool) -> EncodeResult {
  462. if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
  463. if v {
  464. write!(self.writer, "true")?;
  465. } else {
  466. write!(self.writer, "false")?;
  467. }
  468. Ok(())
  469. }
  470. fn emit_f64(&mut self, v: f64) -> EncodeResult {
  471. emit_enquoted_if_mapkey!(self, fmt_number_or_null(v))
  472. }
  473. fn emit_f32(&mut self, v: f32) -> EncodeResult {
  474. self.emit_f64(v as f64)
  475. }
  476. fn emit_char(&mut self, v: char) -> EncodeResult {
  477. escape_char(self.writer, v)
  478. }
  479. fn emit_str(&mut self, v: &str) -> EncodeResult {
  480. escape_str(self.writer, v)
  481. }
  482. fn emit_enum<F>(&mut self, _name: &str, f: F) -> EncodeResult where
  483. F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
  484. {
  485. f(self)
  486. }
  487. fn emit_enum_variant<F>(&mut self,
  488. name: &str,
  489. _id: usize,
  490. cnt: usize,
  491. f: F) -> EncodeResult where
  492. F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
  493. {
  494. // enums are encoded as strings or objects
  495. // Bunny => "Bunny"
  496. // Kangaroo(34,"William") => {"variant": "Kangaroo", "fields": [34,"William"]}
  497. if cnt == 0 {
  498. escape_str(self.writer, name)
  499. } else {
  500. if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
  501. write!(self.writer, "{{\"variant\":")?;
  502. escape_str(self.writer, name)?;
  503. write!(self.writer, ",\"fields\":[")?;
  504. f(self)?;
  505. write!(self.writer, "]}}")?;
  506. Ok(())
  507. }
  508. }
  509. fn emit_enum_variant_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult where
  510. F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
  511. {
  512. if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
  513. if idx != 0 {
  514. write!(self.writer, ",")?;
  515. }
  516. f(self)
  517. }
  518. fn emit_enum_struct_variant<F>(&mut self,
  519. name: &str,
  520. id: usize,
  521. cnt: usize,
  522. f: F) -> EncodeResult where
  523. F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
  524. {
  525. if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
  526. self.emit_enum_variant(name, id, cnt, f)
  527. }
  528. fn emit_enum_struct_variant_field<F>(&mut self,
  529. _: &str,
  530. idx: usize,
  531. f: F) -> EncodeResult where
  532. F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
  533. {
  534. if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
  535. self.emit_enum_variant_arg(idx, f)
  536. }
  537. fn emit_struct<F>(&mut self, _: &str, _: usize, f: F) -> EncodeResult where
  538. F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
  539. {
  540. if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
  541. write!(self.writer, "{{")?;
  542. f(self)?;
  543. write!(self.writer, "}}")?;
  544. Ok(())
  545. }
  546. fn emit_struct_field<F>(&mut self, name: &str, idx: usize, f: F) -> EncodeResult where
  547. F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
  548. {
  549. if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
  550. if idx != 0 { write!(self.writer, ",")?; }
  551. escape_str(self.writer, name)?;
  552. write!(self.writer, ":")?;
  553. f(self)
  554. }
  555. fn emit_tuple<F>(&mut self, len: usize, f: F) -> EncodeResult where
  556. F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
  557. {
  558. if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
  559. self.emit_seq(len, f)
  560. }
  561. fn emit_tuple_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult where
  562. F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
  563. {
  564. if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
  565. self.emit_seq_elt(idx, f)
  566. }
  567. fn emit_tuple_struct<F>(&mut self, _name: &str, len: usize, f: F) -> EncodeResult where
  568. F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
  569. {
  570. if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
  571. self.emit_seq(len, f)
  572. }
  573. fn emit_tuple_struct_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult where
  574. F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
  575. {
  576. if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
  577. self.emit_seq_elt(idx, f)
  578. }
  579. fn emit_option<F>(&mut self, f: F) -> EncodeResult where
  580. F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
  581. {
  582. if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
  583. f(self)
  584. }
  585. fn emit_option_none(&mut self) -> EncodeResult {
  586. if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
  587. self.emit_nil()
  588. }
  589. fn emit_option_some<F>(&mut self, f: F) -> EncodeResult where
  590. F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
  591. {
  592. if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
  593. f(self)
  594. }
  595. fn emit_seq<F>(&mut self, _len: usize, f: F) -> EncodeResult where
  596. F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
  597. {
  598. if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
  599. write!(self.writer, "[")?;
  600. f(self)?;
  601. write!(self.writer, "]")?;
  602. Ok(())
  603. }
  604. fn emit_seq_elt<F>(&mut self, idx: usize, f: F) -> EncodeResult where
  605. F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
  606. {
  607. if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
  608. if idx != 0 {
  609. write!(self.writer, ",")?;
  610. }
  611. f(self)
  612. }
  613. fn emit_map<F>(&mut self, _len: usize, f: F) -> EncodeResult where
  614. F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
  615. {
  616. if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
  617. write!(self.writer, "{{")?;
  618. f(self)?;
  619. write!(self.writer, "}}")?;
  620. Ok(())
  621. }
  622. fn emit_map_elt_key<F>(&mut self, idx: usize, f: F) -> EncodeResult where
  623. F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
  624. {
  625. if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
  626. if idx != 0 { write!(self.writer, ",")? }
  627. self.is_emitting_map_key = true;
  628. f(self)?;
  629. self.is_emitting_map_key = false;
  630. Ok(())
  631. }
  632. fn emit_map_elt_val<F>(&mut self, _idx: usize, f: F) -> EncodeResult where
  633. F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
  634. {
  635. if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
  636. write!(self.writer, ":")?;
  637. f(self)
  638. }
  639. }
  640. /// Another encoder for JSON, but prints out human-readable JSON instead of
  641. /// compact data
  642. pub struct PrettyEncoder<'a> {
  643. writer: &'a mut (fmt::Write+'a),
  644. curr_indent: usize,
  645. indent: usize,
  646. is_emitting_map_key: bool,
  647. }
  648. impl<'a> PrettyEncoder<'a> {
  649. /// Creates a new encoder whose output will be written to the specified writer
  650. pub fn new(writer: &'a mut fmt::Write) -> PrettyEncoder<'a> {
  651. PrettyEncoder {
  652. writer: writer,
  653. curr_indent: 0,
  654. indent: 2,
  655. is_emitting_map_key: false,
  656. }
  657. }
  658. /// Set the number of spaces to indent for each level.
  659. /// This is safe to set during encoding.
  660. pub fn set_indent(&mut self, indent: usize) {
  661. // self.indent very well could be 0 so we need to use checked division.
  662. let level = self.curr_indent.checked_div(self.indent).unwrap_or(0);
  663. self.indent = indent;
  664. self.curr_indent = level * self.indent;
  665. }
  666. }
  667. impl<'a> ::Encoder for PrettyEncoder<'a> {
  668. type Error = EncoderError;
  669. fn emit_nil(&mut self) -> EncodeResult {
  670. if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
  671. write!(self.writer, "null")?;
  672. Ok(())
  673. }
  674. fn emit_usize(&mut self, v: usize) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
  675. fn emit_u128(&mut self, v: u128) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
  676. fn emit_u64(&mut self, v: u64) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
  677. fn emit_u32(&mut self, v: u32) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
  678. fn emit_u16(&mut self, v: u16) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
  679. fn emit_u8(&mut self, v: u8) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
  680. fn emit_isize(&mut self, v: isize) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
  681. fn emit_i128(&mut self, v: i128) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
  682. fn emit_i64(&mut self, v: i64) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
  683. fn emit_i32(&mut self, v: i32) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
  684. fn emit_i16(&mut self, v: i16) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
  685. fn emit_i8(&mut self, v: i8) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
  686. fn emit_bool(&mut self, v: bool) -> EncodeResult {
  687. if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
  688. if v {
  689. write!(self.writer, "true")?;
  690. } else {
  691. write!(self.writer, "false")?;
  692. }
  693. Ok(())
  694. }
  695. fn emit_f64(&mut self, v: f64) -> EncodeResult {
  696. emit_enquoted_if_mapkey!(self, fmt_number_or_null(v))
  697. }
  698. fn emit_f32(&mut self, v: f32) -> EncodeResult {
  699. self.emit_f64(v as f64)
  700. }
  701. fn emit_char(&mut self, v: char) -> EncodeResult {
  702. escape_char(self.writer, v)
  703. }
  704. fn emit_str(&mut self, v: &str) -> EncodeResult {
  705. escape_str(self.writer, v)
  706. }
  707. fn emit_enum<F>(&mut self, _name: &str, f: F) -> EncodeResult where
  708. F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
  709. {
  710. f(self)
  711. }
  712. fn emit_enum_variant<F>(&mut self,
  713. name: &str,
  714. _id: usize,
  715. cnt: usize,
  716. f: F)
  717. -> EncodeResult where
  718. F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
  719. {
  720. if cnt == 0 {
  721. escape_str(self.writer, name)
  722. } else {
  723. if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
  724. write!(self.writer, "{{\n")?;
  725. self.curr_indent += self.indent;
  726. spaces(self.writer, self.curr_indent)?;
  727. write!(self.writer, "\"variant\": ")?;
  728. escape_str(self.writer, name)?;
  729. write!(self.writer, ",\n")?;
  730. spaces(self.writer, self.curr_indent)?;
  731. write!(self.writer, "\"fields\": [\n")?;
  732. self.curr_indent += self.indent;
  733. f(self)?;
  734. self.curr_indent -= self.indent;
  735. write!(self.writer, "\n")?;
  736. spaces(self.writer, self.curr_indent)?;
  737. self.curr_indent -= self.indent;
  738. write!(self.writer, "]\n")?;
  739. spaces(self.writer, self.curr_indent)?;
  740. write!(self.writer, "}}")?;
  741. Ok(())
  742. }
  743. }
  744. fn emit_enum_variant_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult where
  745. F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
  746. {
  747. if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
  748. if idx != 0 {
  749. write!(self.writer, ",\n")?;
  750. }
  751. spaces(self.writer, self.curr_indent)?;
  752. f(self)
  753. }
  754. fn emit_enum_struct_variant<F>(&mut self,
  755. name: &str,
  756. id: usize,
  757. cnt: usize,
  758. f: F) -> EncodeResult where
  759. F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
  760. {
  761. if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
  762. self.emit_enum_variant(name, id, cnt, f)
  763. }
  764. fn emit_enum_struct_variant_field<F>(&mut self,
  765. _: &str,
  766. idx: usize,
  767. f: F) -> EncodeResult where
  768. F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
  769. {
  770. if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
  771. self.emit_enum_variant_arg(idx, f)
  772. }
  773. fn emit_struct<F>(&mut self, _: &str, len: usize, f: F) -> EncodeResult where
  774. F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
  775. {
  776. if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
  777. if len == 0 {
  778. write!(self.writer, "{{}}")?;
  779. } else {
  780. write!(self.writer, "{{")?;
  781. self.curr_indent += self.indent;
  782. f(self)?;
  783. self.curr_indent -= self.indent;
  784. write!(self.writer, "\n")?;
  785. spaces(self.writer, self.curr_indent)?;
  786. write!(self.writer, "}}")?;
  787. }
  788. Ok(())
  789. }
  790. fn emit_struct_field<F>(&mut self, name: &str, idx: usize, f: F) -> EncodeResult where
  791. F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
  792. {
  793. if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
  794. if idx == 0 {
  795. write!(self.writer, "\n")?;
  796. } else {
  797. write!(self.writer, ",\n")?;
  798. }
  799. spaces(self.writer, self.curr_indent)?;
  800. escape_str(self.writer, name)?;
  801. write!(self.writer, ": ")?;
  802. f(self)
  803. }
  804. fn emit_tuple<F>(&mut self, len: usize, f: F) -> EncodeResult where
  805. F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
  806. {
  807. if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
  808. self.emit_seq(len, f)
  809. }
  810. fn emit_tuple_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult where
  811. F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
  812. {
  813. if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
  814. self.emit_seq_elt(idx, f)
  815. }
  816. fn emit_tuple_struct<F>(&mut self, _: &str, len: usize, f: F) -> EncodeResult where
  817. F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
  818. {
  819. if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
  820. self.emit_seq(len, f)
  821. }
  822. fn emit_tuple_struct_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult where
  823. F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
  824. {
  825. if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
  826. self.emit_seq_elt(idx, f)
  827. }
  828. fn emit_option<F>(&mut self, f: F) -> EncodeResult where
  829. F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
  830. {
  831. if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
  832. f(self)
  833. }
  834. fn emit_option_none(&mut self) -> EncodeResult {
  835. if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
  836. self.emit_nil()
  837. }
  838. fn emit_option_some<F>(&mut self, f: F) -> EncodeResult where
  839. F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
  840. {
  841. if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
  842. f(self)
  843. }
  844. fn emit_seq<F>(&mut self, len: usize, f: F) -> EncodeResult where
  845. F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
  846. {
  847. if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
  848. if len == 0 {
  849. write!(self.writer, "[]")?;
  850. } else {
  851. write!(self.writer, "[")?;
  852. self.curr_indent += self.indent;
  853. f(self)?;
  854. self.curr_indent -= self.indent;
  855. write!(self.writer, "\n")?;
  856. spaces(self.writer, self.curr_indent)?;
  857. write!(self.writer, "]")?;
  858. }
  859. Ok(())
  860. }
  861. fn emit_seq_elt<F>(&mut self, idx: usize, f: F) -> EncodeResult where
  862. F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
  863. {
  864. if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
  865. if idx == 0 {
  866. write!(self.writer, "\n")?;
  867. } else {
  868. write!(self.writer, ",\n")?;
  869. }
  870. spaces(self.writer, self.curr_indent)?;
  871. f(self)
  872. }
  873. fn emit_map<F>(&mut self, len: usize, f: F) -> EncodeResult where
  874. F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
  875. {
  876. if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
  877. if len == 0 {
  878. write!(self.writer, "{{}}")?;
  879. } else {
  880. write!(self.writer, "{{")?;
  881. self.curr_indent += self.indent;
  882. f(self)?;
  883. self.curr_indent -= self.indent;
  884. write!(self.writer, "\n")?;
  885. spaces(self.writer, self.curr_indent)?;
  886. write!(self.writer, "}}")?;
  887. }
  888. Ok(())
  889. }
  890. fn emit_map_elt_key<F>(&mut self, idx: usize, f: F) -> EncodeResult where
  891. F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
  892. {
  893. if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
  894. if idx == 0 {
  895. write!(self.writer, "\n")?;
  896. } else {
  897. write!(self.writer, ",\n")?;
  898. }
  899. spaces(self.writer, self.curr_indent)?;
  900. self.is_emitting_map_key = true;
  901. f(self)?;
  902. self.is_emitting_map_key = false;
  903. Ok(())
  904. }
  905. fn emit_map_elt_val<F>(&mut self, _idx: usize, f: F) -> EncodeResult where
  906. F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
  907. {
  908. if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
  909. write!(self.writer, ": ")?;
  910. f(self)
  911. }
  912. }
  913. impl Encodable for Json {
  914. fn encode<E: ::Encoder>(&self, e: &mut E) -> Result<(), E::Error> {
  915. match *self {
  916. Json::I64(v) => v.encode(e),
  917. Json::U64(v) => v.encode(e),
  918. Json::F64(v) => v.encode(e),
  919. Json::String(ref v) => v.encode(e),
  920. Json::Boolean(v) => v.encode(e),
  921. Json::Array(ref v) => v.encode(e),
  922. Json::Object(ref v) => v.encode(e),
  923. Json::Null => e.emit_nil(),
  924. }
  925. }
  926. }
  927. /// Create an `AsJson` wrapper which can be used to print a value as JSON
  928. /// on-the-fly via `write!`
  929. pub fn as_json<T>(t: &T) -> AsJson<T> {
  930. AsJson { inner: t }
  931. }
  932. /// Create an `AsPrettyJson` wrapper which can be used to print a value as JSON
  933. /// on-the-fly via `write!`
  934. pub fn as_pretty_json<T>(t: &T) -> AsPrettyJson<T> {
  935. AsPrettyJson { inner: t, indent: None }
  936. }
  937. impl Json {
  938. /// Borrow this json object as a pretty object to generate a pretty
  939. /// representation for it via `Display`.
  940. pub fn pretty(&self) -> PrettyJson {
  941. PrettyJson { inner: self }
  942. }
  943. /// If the Json value is an Object, returns the value associated with the provided key.
  944. /// Otherwise, returns None.
  945. pub fn find<'a>(&'a self, key: &str) -> Option<&'a Json>{
  946. match *self {
  947. Json::Object(ref map) => map.get(key),
  948. _ => None
  949. }
  950. }
  951. /// Attempts to get a nested Json Object for each key in `keys`.
  952. /// If any key is found not to exist, find_path will return None.
  953. /// Otherwise, it will return the Json value associated with the final key.
  954. pub fn find_path<'a>(&'a self, keys: &[&str]) -> Option<&'a Json>{
  955. let mut target = self;
  956. for key in keys {
  957. match target.find(*key) {
  958. Some(t) => { target = t; },
  959. None => return None
  960. }
  961. }
  962. Some(target)
  963. }
  964. /// If the Json value is an Object, performs a depth-first search until
  965. /// a value associated with the provided key is found. If no value is found
  966. /// or the Json value is not an Object, returns None.
  967. pub fn search<'a>(&'a self, key: &str) -> Option<&'a Json> {
  968. match self {
  969. &Json::Object(ref map) => {
  970. match map.get(key) {
  971. Some(json_value) => Some(json_value),
  972. None => {
  973. for (_, v) in map {
  974. match v.search(key) {
  975. x if x.is_some() => return x,
  976. _ => ()
  977. }
  978. }
  979. None
  980. }
  981. }
  982. },
  983. _ => None
  984. }
  985. }
  986. /// Returns true if the Json value is an Object. Returns false otherwise.
  987. pub fn is_object(&self) -> bool {
  988. self.as_object().is_some()
  989. }
  990. /// If the Json value is an Object, returns the associated BTreeMap.
  991. /// Returns None otherwise.
  992. pub fn as_object(&self) -> Option<&Object> {
  993. match *self {
  994. Json::Object(ref map) => Some(map),
  995. _ => None
  996. }
  997. }
  998. /// Returns true if the Json value is an Array. Returns false otherwise.
  999. pub fn is_array(&self) -> bool {
  1000. self.as_array().is_some()
  1001. }
  1002. /// If the Json value is an Array, returns the associated vector.
  1003. /// Returns None otherwise.
  1004. pub fn as_array(&self) -> Option<&Array> {
  1005. match *self {
  1006. Json::Array(ref array) => Some(&*array),
  1007. _ => None
  1008. }
  1009. }
  1010. /// Returns true if the Json value is a String. Returns false otherwise.
  1011. pub fn is_string(&self) -> bool {
  1012. self.as_string().is_some()
  1013. }
  1014. /// If the Json value is a String, returns the associated str.
  1015. /// Returns None otherwise.
  1016. pub fn as_string(&self) -> Option<&str> {
  1017. match *self {
  1018. Json::String(ref s) => Some(&s[..]),
  1019. _ => None
  1020. }
  1021. }
  1022. /// Returns true if the Json value is a Number. Returns false otherwise.
  1023. pub fn is_number(&self) -> bool {
  1024. match *self {
  1025. Json::I64(_) | Json::U64(_) | Json::F64(_) => true,
  1026. _ => false,
  1027. }
  1028. }
  1029. /// Returns true if the Json value is a i64. Returns false otherwise.
  1030. pub fn is_i64(&self) -> bool {
  1031. match *self {
  1032. Json::I64(_) => true,
  1033. _ => false,
  1034. }
  1035. }
  1036. /// Returns true if the Json value is a u64. Returns false otherwise.
  1037. pub fn is_u64(&self) -> bool {
  1038. match *self {
  1039. Json::U64(_) => true,
  1040. _ => false,
  1041. }
  1042. }
  1043. /// Returns true if the Json value is a f64. Returns false otherwise.
  1044. pub fn is_f64(&self) -> bool {
  1045. match *self {
  1046. Json::F64(_) => true,
  1047. _ => false,
  1048. }
  1049. }
  1050. /// If the Json value is a number, return or cast it to a i64.
  1051. /// Returns None otherwise.
  1052. pub fn as_i64(&self) -> Option<i64> {
  1053. match *self {
  1054. Json::I64(n) => Some(n),
  1055. Json::U64(n) => Some(n as i64),
  1056. _ => None
  1057. }
  1058. }
  1059. /// If the Json value is a number, return or cast it to a u64.
  1060. /// Returns None otherwise.
  1061. pub fn as_u64(&self) -> Option<u64> {
  1062. match *self {
  1063. Json::I64(n) => Some(n as u64),
  1064. Json::U64(n) => Some(n),
  1065. _ => None
  1066. }
  1067. }
  1068. /// If the Json value is a number, return or cast it to a f64.
  1069. /// Returns None otherwise.
  1070. pub fn as_f64(&self) -> Option<f64> {
  1071. match *self {
  1072. Json::I64(n) => Some(n as f64),
  1073. Json::U64(n) => Some(n as f64),
  1074. Json::F64(n) => Some(n),
  1075. _ => None
  1076. }
  1077. }
  1078. /// Returns true if the Json value is a Boolean. Returns false otherwise.
  1079. pub fn is_boolean(&self) -> bool {
  1080. self.as_boolean().is_some()
  1081. }
  1082. /// If the Json value is a Boolean, returns the associated bool.
  1083. /// Returns None otherwise.
  1084. pub fn as_boolean(&self) -> Option<bool> {
  1085. match *self {
  1086. Json::Boolean(b) => Some(b),
  1087. _ => None
  1088. }
  1089. }
  1090. /// Returns true if the Json value is a Null. Returns false otherwise.
  1091. pub fn is_null(&self) -> bool {
  1092. self.as_null().is_some()
  1093. }
  1094. /// If the Json value is a Null, returns ().
  1095. /// Returns None otherwise.
  1096. pub fn as_null(&self) -> Option<()> {
  1097. match *self {
  1098. Json::Null => Some(()),
  1099. _ => None
  1100. }
  1101. }
  1102. }
  1103. impl<'a> Index<&'a str> for Json {
  1104. type Output = Json;
  1105. fn index(&self, idx: &'a str) -> &Json {
  1106. self.find(idx).unwrap()
  1107. }
  1108. }
  1109. impl Index<usize> for Json {
  1110. type Output = Json;
  1111. fn index(&self, idx: usize) -> &Json {
  1112. match *self {
  1113. Json::Array(ref v) => &v[idx],
  1114. _ => panic!("can only index Json with usize if it is an array")
  1115. }
  1116. }
  1117. }
  1118. /// The output of the streaming parser.
  1119. #[derive(PartialEq, Clone, Debug)]
  1120. pub enum JsonEvent {
  1121. ObjectStart,
  1122. ObjectEnd,
  1123. ArrayStart,
  1124. ArrayEnd,
  1125. BooleanValue(bool),
  1126. I64Value(i64),
  1127. U64Value(u64),
  1128. F64Value(f64),
  1129. StringValue(string::String),
  1130. NullValue,
  1131. Error(ParserError),
  1132. }
  1133. #[derive(PartialEq, Debug)]
  1134. enum ParserState {
  1135. // Parse a value in an array, true means first element.
  1136. ParseArray(bool),
  1137. // Parse ',' or ']' after an element in an array.
  1138. ParseArrayComma,
  1139. // Parse a key:value in an object, true means first element.
  1140. ParseObject(bool),
  1141. // Parse ',' or ']' after an element in an object.
  1142. ParseObjectComma,
  1143. // Initial state.
  1144. ParseStart,
  1145. // Expecting the stream to end.
  1146. ParseBeforeFinish,
  1147. // Parsing can't continue.
  1148. ParseFinished,
  1149. }
  1150. /// A Stack represents the current position of the parser in the logical
  1151. /// structure of the JSON stream.
  1152. /// For example foo.bar[3].x
  1153. pub struct Stack {
  1154. stack: Vec<InternalStackElement>,
  1155. str_buffer: Vec<u8>,
  1156. }
  1157. /// StackElements compose a Stack.
  1158. /// For example, StackElement::Key("foo"), StackElement::Key("bar"),
  1159. /// StackElement::Index(3) and StackElement::Key("x") are the
  1160. /// StackElements compositing the stack that represents foo.bar[3].x
  1161. #[derive(PartialEq, Clone, Debug)]
  1162. pub enum StackElement<'l> {
  1163. Index(u32),
  1164. Key(&'l str),
  1165. }
  1166. // Internally, Key elements are stored as indices in a buffer to avoid
  1167. // allocating a string for every member of an object.
  1168. #[derive(PartialEq, Clone, Debug)]
  1169. enum InternalStackElement {
  1170. InternalIndex(u32),
  1171. InternalKey(u16, u16), // start, size
  1172. }
  1173. impl Stack {
  1174. pub fn new() -> Stack {
  1175. Stack { stack: Vec::new(), str_buffer: Vec::new() }
  1176. }
  1177. /// Returns The number of elements in the Stack.
  1178. pub fn len(&self) -> usize { self.stack.len() }
  1179. /// Returns true if the stack is empty.
  1180. pub fn is_empty(&self) -> bool { self.stack.is_empty() }
  1181. /// Provides access to the StackElement at a given index.
  1182. /// lower indices are at the bottom of the stack while higher indices are
  1183. /// at the top.
  1184. pub fn get(&self, idx: usize) -> StackElement {
  1185. match self.stack[idx] {
  1186. InternalIndex(i) => StackElement::Index(i),
  1187. InternalKey(start, size) => {
  1188. StackElement::Key(str::from_utf8(
  1189. &self.str_buffer[start as usize .. start as usize + size as usize])
  1190. .unwrap())
  1191. }
  1192. }
  1193. }
  1194. /// Compares this stack with an array of StackElements.
  1195. pub fn is_equal_to(&self, rhs: &[StackElement]) -> bool {
  1196. if self.stack.len() != rhs.len() { return false; }
  1197. for (i, r) in rhs.iter().enumerate() {
  1198. if self.get(i) != *r { return false; }
  1199. }
  1200. true
  1201. }
  1202. /// Returns true if the bottom-most elements of this stack are the same as
  1203. /// the ones passed as parameter.
  1204. pub fn starts_with(&self, rhs: &[StackElement]) -> bool {
  1205. if self.stack.len() < rhs.len() { return false; }
  1206. for (i, r) in rhs.iter().enumerate() {
  1207. if self.get(i) != *r { return false; }
  1208. }
  1209. true
  1210. }
  1211. /// Returns true if the top-most elements of this stack are the same as
  1212. /// the ones passed as parameter.
  1213. pub fn ends_with(&self, rhs: &[StackElement]) -> bool {
  1214. if self.stack.len() < rhs.len() { return false; }
  1215. let offset = self.stack.len() - rhs.len();
  1216. for (i, r) in rhs.iter().enumerate() {
  1217. if self.get(i + offset) != *r { return false; }
  1218. }
  1219. true
  1220. }
  1221. /// Returns the top-most element (if any).
  1222. pub fn top(&self) -> Option<StackElement> {
  1223. match self.stack.last() {
  1224. None => None,
  1225. Some(&InternalIndex(i)) => Some(StackElement::Index(i)),
  1226. Some(&InternalKey(start, size)) => {
  1227. Some(StackElement::Key(str::from_utf8(
  1228. &self.str_buffer[start as usize .. (start+size) as usize]
  1229. ).unwrap()))
  1230. }
  1231. }
  1232. }
  1233. // Used by Parser to insert StackElement::Key elements at the top of the stack.
  1234. fn push_key(&mut self, key: string::String) {
  1235. self.stack.push(InternalKey(self.str_buffer.len() as u16, key.len() as u16));
  1236. for c in key.as_bytes() {
  1237. self.str_buffer.push(*c);
  1238. }
  1239. }
  1240. // Used by Parser to insert StackElement::Index elements at the top of the stack.
  1241. fn push_index(&mut self, index: u32) {
  1242. self.stack.push(InternalIndex(index));
  1243. }
  1244. // Used by Parser to remove the top-most element of the stack.
  1245. fn pop(&mut self) {
  1246. assert!(!self.is_empty());
  1247. match *self.stack.last().unwrap() {
  1248. InternalKey(_, sz) => {
  1249. let new_size = self.str_buffer.len() - sz as usize;
  1250. self.str_buffer.truncate(new_size);
  1251. }
  1252. InternalIndex(_) => {}
  1253. }
  1254. self.stack.pop();
  1255. }
  1256. // Used by Parser to test whether the top-most element is an index.
  1257. fn last_is_index(&self) -> bool {
  1258. if self.is_empty() { return false; }
  1259. return match *self.stack.last().unwrap() {
  1260. InternalIndex(_) => true,
  1261. _ => false,
  1262. }
  1263. }
  1264. // Used by Parser to increment the index of the top-most element.
  1265. fn bump_index(&mut self) {
  1266. let len = self.stack.len();
  1267. let idx = match *self.stack.last().unwrap() {
  1268. InternalIndex(i) => { i + 1 }
  1269. _ => { panic!(); }
  1270. };
  1271. self.stack[len - 1] = InternalIndex(idx);
  1272. }
  1273. }
  1274. /// A streaming JSON parser implemented as an iterator of JsonEvent, consuming
  1275. /// an iterator of char.
  1276. pub struct Parser<T> {
  1277. rdr: T,
  1278. ch: Option<char>,
  1279. line: usize,
  1280. col: usize,
  1281. // We maintain a stack representing where we are in the logical structure
  1282. // of the JSON stream.
  1283. stack: Stack,
  1284. // A state machine is kept to make it possible to interrupt and resume parsing.
  1285. state: ParserState,
  1286. }
  1287. impl<T: Iterator<Item=char>> Iterator for Parser<T> {
  1288. type Item = JsonEvent;
  1289. fn next(&mut self) -> Option<JsonEvent> {
  1290. if self.state == ParseFinished {
  1291. return None;
  1292. }
  1293. if self.state == ParseBeforeFinish {
  1294. self.parse_whitespace();
  1295. // Make sure there is no trailing characters.
  1296. if self.eof() {
  1297. self.state = ParseFinished;
  1298. return None;
  1299. } else {
  1300. return Some(self.error_event(TrailingCharacters));
  1301. }
  1302. }
  1303. Some(self.parse())
  1304. }
  1305. }
  1306. impl<T: Iterator<Item=char>> Parser<T> {
  1307. /// Creates the JSON parser.
  1308. pub fn new(rdr: T) -> Parser<T> {
  1309. let mut p = Parser {
  1310. rdr: rdr,
  1311. ch: Some('\x00'),
  1312. line: 1,
  1313. col: 0,
  1314. stack: Stack::new(),
  1315. state: ParseStart,
  1316. };
  1317. p.bump();
  1318. p
  1319. }
  1320. /// Provides access to the current position in the logical structure of the
  1321. /// JSON stream.
  1322. pub fn stack(&self) -> &Stack {
  1323. &self.stack
  1324. }
  1325. fn eof(&self) -> bool { self.ch.is_none() }
  1326. fn ch_or_null(&self) -> char { self.ch.unwrap_or('\x00') }
  1327. fn bump(&mut self) {
  1328. self.ch = self.rdr.next();
  1329. if self.ch_is('\n') {
  1330. self.line += 1;
  1331. self.col = 1;
  1332. } else {
  1333. self.col += 1;
  1334. }
  1335. }
  1336. fn next_char(&mut self) -> Option<char> {
  1337. self.bump();
  1338. self.ch
  1339. }
  1340. fn ch_is(&self, c: char) -> bool {
  1341. self.ch == Some(c)
  1342. }
  1343. fn error<U>(&self, reason: ErrorCode) -> Result<U, ParserError> {
  1344. Err(SyntaxError(reason, self.line, self.col))
  1345. }
  1346. fn parse_whitespace(&mut self) {
  1347. while self.ch_is(' ') ||
  1348. self.ch_is('\n') ||
  1349. self.ch_is('\t') ||
  1350. self.ch_is('\r') { self.bump(); }
  1351. }
  1352. fn parse_number(&mut self) -> JsonEvent {
  1353. let mut neg = false;
  1354. if self.ch_is('-') {
  1355. self.bump();
  1356. neg = true;
  1357. }
  1358. let res = match self.parse_u64() {
  1359. Ok(res) => res,
  1360. Err(e) => { return Error(e); }
  1361. };
  1362. if self.ch_is('.') || self.ch_is('e') || self.ch_is('E') {
  1363. let mut res = res as f64;
  1364. if self.ch_is('.') {
  1365. res = match self.parse_decimal(res) {
  1366. Ok(res) => res,
  1367. Err(e) => { return Error(e); }
  1368. };
  1369. }
  1370. if self.ch_is('e') || self.ch_is('E') {
  1371. res = match self.parse_exponent(res) {
  1372. Ok(res) => res,
  1373. Err(e) => { return Error(e); }
  1374. };
  1375. }
  1376. if neg {
  1377. res *= -1.0;
  1378. }
  1379. F64Value(res)
  1380. } else {
  1381. if neg {
  1382. let res = (res as i64).wrapping_neg();
  1383. // Make sure we didn't underflow.
  1384. if res > 0 {
  1385. Error(SyntaxError(InvalidNumber, self.line, self.col))
  1386. } else {
  1387. I64Value(res)
  1388. }
  1389. } else {
  1390. U64Value(res)
  1391. }
  1392. }
  1393. }
  1394. fn parse_u64(&mut self) -> Result<u64, ParserError> {
  1395. let mut accum = 0u64;
  1396. let last_accum = 0; // necessary to detect overflow.
  1397. match self.ch_or_null() {
  1398. '0' => {
  1399. self.bump();
  1400. // A leading '0' must be the only digit before the decimal …

Large files files are truncated, but you can click here to view the full file