2013-08-29 13:22:04 -05:00
|
|
|
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 18:48:01 -06:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2011-11-07 17:24:44 -06:00
|
|
|
// Rust JSON serialization library
|
|
|
|
// Copyright (c) 2011 Google Inc.
|
2013-05-31 17:17:22 -05:00
|
|
|
|
2012-09-13 00:09:55 -05:00
|
|
|
#[forbid(non_camel_case_types)];
|
2013-05-31 17:17:22 -05:00
|
|
|
#[allow(missing_doc)];
|
2011-11-07 13:01:28 -06:00
|
|
|
|
2013-07-14 20:26:45 -05:00
|
|
|
//! json parsing and serialization
|
2012-03-07 20:17:30 -06:00
|
|
|
|
2013-09-03 18:24:12 -05:00
|
|
|
use std::char;
|
|
|
|
use std::cast::transmute;
|
2013-09-26 01:26:09 -05:00
|
|
|
use std::f64;
|
2013-06-28 17:32:26 -05:00
|
|
|
use std::hashmap::HashMap;
|
2013-11-11 00:46:32 -06:00
|
|
|
use std::io;
|
|
|
|
use std::io::Decorator;
|
|
|
|
use std::io::mem::MemWriter;
|
2013-09-26 01:26:09 -05:00
|
|
|
use std::num;
|
2013-06-28 17:32:26 -05:00
|
|
|
use std::str;
|
|
|
|
use std::to_str;
|
2013-03-13 13:29:05 -05:00
|
|
|
|
2013-01-25 18:57:39 -06:00
|
|
|
use serialize::Encodable;
|
2012-12-23 16:41:37 -06:00
|
|
|
use serialize;
|
2013-07-08 18:21:09 -05:00
|
|
|
use treemap::TreeMap;
|
2011-11-07 13:01:28 -06:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Represents a json value
|
2013-07-02 14:47:32 -05:00
|
|
|
#[deriving(Clone, Eq)]
|
2012-09-24 11:55:42 -05:00
|
|
|
pub enum Json {
|
2013-09-26 01:26:09 -05:00
|
|
|
Number(f64),
|
2012-09-24 11:55:42 -05:00
|
|
|
String(~str),
|
2012-08-11 09:08:42 -05:00
|
|
|
Boolean(bool),
|
2012-09-24 11:55:42 -05:00
|
|
|
List(List),
|
|
|
|
Object(~Object),
|
2012-08-11 09:08:42 -05:00
|
|
|
Null,
|
2011-11-07 13:01:28 -06:00
|
|
|
}
|
|
|
|
|
2012-09-24 11:55:42 -05:00
|
|
|
pub type List = ~[Json];
|
2013-07-18 14:37:40 -05:00
|
|
|
pub type Object = TreeMap<~str, Json>;
|
2012-09-24 11:55:42 -05:00
|
|
|
|
2013-03-22 15:09:20 -05:00
|
|
|
#[deriving(Eq)]
|
2013-05-28 22:11:41 -05:00
|
|
|
/// If an error occurs while parsing some JSON, this is the structure which is
|
|
|
|
/// returned
|
2012-09-24 11:55:42 -05:00
|
|
|
pub struct Error {
|
2013-05-28 22:11:41 -05:00
|
|
|
/// The line number at which the error occurred
|
2013-10-19 19:33:09 -05:00
|
|
|
priv line: uint,
|
2013-05-28 22:11:41 -05:00
|
|
|
/// The column number at which the error occurred
|
2013-10-19 19:33:09 -05:00
|
|
|
priv col: uint,
|
2013-05-28 22:11:41 -05:00
|
|
|
/// A message describing the type of the error
|
2013-11-28 16:50:21 -06:00
|
|
|
priv msg: ~str,
|
2012-09-24 11:55:42 -05:00
|
|
|
}
|
2012-02-25 18:39:32 -06:00
|
|
|
|
2012-09-24 11:55:42 -05:00
|
|
|
fn escape_str(s: &str) -> ~str {
|
|
|
|
let mut escaped = ~"\"";
|
2013-11-23 04:18:51 -06:00
|
|
|
for c in s.chars() {
|
2012-09-24 11:55:42 -05:00
|
|
|
match c {
|
2013-06-11 21:13:42 -05:00
|
|
|
'"' => escaped.push_str("\\\""),
|
|
|
|
'\\' => escaped.push_str("\\\\"),
|
|
|
|
'\x08' => escaped.push_str("\\b"),
|
|
|
|
'\x0c' => escaped.push_str("\\f"),
|
|
|
|
'\n' => escaped.push_str("\\n"),
|
|
|
|
'\r' => escaped.push_str("\\r"),
|
|
|
|
'\t' => escaped.push_str("\\t"),
|
|
|
|
_ => escaped.push_char(c),
|
2011-11-07 13:01:28 -06:00
|
|
|
}
|
2012-09-24 11:55:42 -05:00
|
|
|
};
|
2012-02-25 18:39:32 -06:00
|
|
|
|
2013-06-11 21:13:42 -05:00
|
|
|
escaped.push_char('"');
|
2012-09-24 11:55:42 -05:00
|
|
|
|
|
|
|
escaped
|
2011-11-07 13:01:28 -06:00
|
|
|
}
|
|
|
|
|
2012-09-24 11:55:42 -05:00
|
|
|
fn spaces(n: uint) -> ~str {
|
|
|
|
let mut ss = ~"";
|
2013-11-20 17:46:49 -06:00
|
|
|
n.times(|| ss.push_str(" "));
|
2012-09-24 11:55:42 -05:00
|
|
|
return ss;
|
|
|
|
}
|
|
|
|
|
2013-05-28 22:11:41 -05:00
|
|
|
/// A structure for implementing serialization to JSON.
|
2013-11-29 12:12:08 -06:00
|
|
|
pub struct Encoder<'self> {
|
|
|
|
priv wr: &'self mut io::Writer,
|
2012-09-24 11:55:42 -05:00
|
|
|
}
|
|
|
|
|
2013-11-29 12:12:08 -06:00
|
|
|
impl<'self> Encoder<'self> {
|
2013-11-29 13:11:52 -06:00
|
|
|
/// Creates a new JSON encoder whose output will be written to the writer
|
|
|
|
/// specified.
|
2013-12-03 21:18:35 -06:00
|
|
|
pub fn new<'a>(wr: &'a mut io::Writer) -> Encoder<'a> {
|
2013-11-29 13:11:52 -06:00
|
|
|
Encoder { wr: wr }
|
|
|
|
}
|
2012-09-24 11:55:42 -05:00
|
|
|
}
|
|
|
|
|
2013-11-29 12:12:08 -06:00
|
|
|
impl<'self> serialize::Encoder for Encoder<'self> {
|
2013-10-13 20:48:47 -05:00
|
|
|
fn emit_nil(&mut self) { write!(self.wr, "null") }
|
2013-05-01 19:54:54 -05:00
|
|
|
|
2013-09-26 01:26:09 -05:00
|
|
|
fn emit_uint(&mut self, v: uint) { self.emit_f64(v as f64); }
|
|
|
|
fn emit_u64(&mut self, v: u64) { self.emit_f64(v as f64); }
|
|
|
|
fn emit_u32(&mut self, v: u32) { self.emit_f64(v as f64); }
|
|
|
|
fn emit_u16(&mut self, v: u16) { self.emit_f64(v as f64); }
|
|
|
|
fn emit_u8(&mut self, v: u8) { self.emit_f64(v as f64); }
|
2013-05-01 19:54:54 -05:00
|
|
|
|
2013-09-26 01:26:09 -05:00
|
|
|
fn emit_int(&mut self, v: int) { self.emit_f64(v as f64); }
|
|
|
|
fn emit_i64(&mut self, v: i64) { self.emit_f64(v as f64); }
|
|
|
|
fn emit_i32(&mut self, v: i32) { self.emit_f64(v as f64); }
|
|
|
|
fn emit_i16(&mut self, v: i16) { self.emit_f64(v as f64); }
|
|
|
|
fn emit_i8(&mut self, v: i8) { self.emit_f64(v as f64); }
|
2013-05-01 19:54:54 -05:00
|
|
|
|
|
|
|
fn emit_bool(&mut self, v: bool) {
|
|
|
|
if v {
|
2013-10-13 20:48:47 -05:00
|
|
|
write!(self.wr, "true");
|
2013-05-01 19:54:54 -05:00
|
|
|
} else {
|
2013-10-13 20:48:47 -05:00
|
|
|
write!(self.wr, "false");
|
2013-05-01 19:54:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-13 20:48:47 -05:00
|
|
|
fn emit_f64(&mut self, v: f64) {
|
|
|
|
write!(self.wr, "{}", f64::to_str_digits(v, 6u))
|
|
|
|
}
|
2013-09-26 01:26:09 -05:00
|
|
|
fn emit_f32(&mut self, v: f32) { self.emit_f64(v as f64); }
|
2013-05-01 19:54:54 -05:00
|
|
|
|
|
|
|
fn emit_char(&mut self, v: char) { self.emit_str(str::from_char(v)) }
|
2013-10-13 20:48:47 -05:00
|
|
|
fn emit_str(&mut self, v: &str) {
|
|
|
|
write!(self.wr, "{}", escape_str(v))
|
|
|
|
}
|
2013-05-01 19:54:54 -05:00
|
|
|
|
2013-11-29 12:12:08 -06:00
|
|
|
fn emit_enum(&mut self, _name: &str, f: |&mut Encoder<'self>|) { f(self) }
|
2013-05-01 19:54:54 -05:00
|
|
|
|
|
|
|
fn emit_enum_variant(&mut self,
|
|
|
|
name: &str,
|
|
|
|
_id: uint,
|
|
|
|
cnt: uint,
|
2013-11-29 12:12:08 -06:00
|
|
|
f: |&mut Encoder<'self>|) {
|
2013-08-09 19:05:24 -05:00
|
|
|
// enums are encoded as strings or objects
|
2013-05-01 19:54:54 -05:00
|
|
|
// Bunny => "Bunny"
|
2013-08-09 19:05:24 -05:00
|
|
|
// Kangaroo(34,"William") => {"variant": "Kangaroo", "fields": [34,"William"]}
|
2013-05-01 19:54:54 -05:00
|
|
|
if cnt == 0 {
|
2013-10-13 20:48:47 -05:00
|
|
|
write!(self.wr, "{}", escape_str(name));
|
2013-05-01 19:54:54 -05:00
|
|
|
} else {
|
2013-10-13 20:48:47 -05:00
|
|
|
write!(self.wr, "\\{\"variant\":");
|
|
|
|
write!(self.wr, "{}", escape_str(name));
|
|
|
|
write!(self.wr, ",\"fields\":[");
|
2013-05-01 19:54:54 -05:00
|
|
|
f(self);
|
2013-10-13 20:48:47 -05:00
|
|
|
write!(self.wr, "]\\}");
|
2013-05-01 19:54:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-29 12:12:08 -06:00
|
|
|
fn emit_enum_variant_arg(&mut self, idx: uint, f: |&mut Encoder<'self>|) {
|
2013-05-01 19:54:54 -05:00
|
|
|
if idx != 0 {
|
2013-10-13 20:48:47 -05:00
|
|
|
write!(self.wr, ",");
|
2013-05-01 19:54:54 -05:00
|
|
|
}
|
|
|
|
f(self);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn emit_enum_struct_variant(&mut self,
|
|
|
|
name: &str,
|
|
|
|
id: uint,
|
|
|
|
cnt: uint,
|
2013-11-29 12:12:08 -06:00
|
|
|
f: |&mut Encoder<'self>|) {
|
2013-05-01 19:54:54 -05:00
|
|
|
self.emit_enum_variant(name, id, cnt, f)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn emit_enum_struct_variant_field(&mut self,
|
|
|
|
_: &str,
|
|
|
|
idx: uint,
|
2013-11-29 12:12:08 -06:00
|
|
|
f: |&mut Encoder<'self>|) {
|
2013-05-01 19:54:54 -05:00
|
|
|
self.emit_enum_variant_arg(idx, f)
|
|
|
|
}
|
|
|
|
|
2013-11-29 12:12:08 -06:00
|
|
|
fn emit_struct(&mut self, _: &str, _: uint, f: |&mut Encoder<'self>|) {
|
2013-10-13 20:48:47 -05:00
|
|
|
write!(self.wr, r"\{");
|
2013-05-01 19:54:54 -05:00
|
|
|
f(self);
|
2013-10-13 20:48:47 -05:00
|
|
|
write!(self.wr, r"\}");
|
2013-05-01 19:54:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn emit_struct_field(&mut self,
|
|
|
|
name: &str,
|
|
|
|
idx: uint,
|
2013-11-29 12:12:08 -06:00
|
|
|
f: |&mut Encoder<'self>|) {
|
2013-10-13 20:48:47 -05:00
|
|
|
if idx != 0 { write!(self.wr, ",") }
|
|
|
|
write!(self.wr, "{}:", escape_str(name));
|
2013-05-01 19:54:54 -05:00
|
|
|
f(self);
|
|
|
|
}
|
|
|
|
|
2013-11-29 12:12:08 -06:00
|
|
|
fn emit_tuple(&mut self, len: uint, f: |&mut Encoder<'self>|) {
|
2013-05-01 19:54:54 -05:00
|
|
|
self.emit_seq(len, f)
|
|
|
|
}
|
2013-11-29 12:12:08 -06:00
|
|
|
fn emit_tuple_arg(&mut self, idx: uint, f: |&mut Encoder<'self>|) {
|
2013-05-01 19:54:54 -05:00
|
|
|
self.emit_seq_elt(idx, f)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn emit_tuple_struct(&mut self,
|
|
|
|
_name: &str,
|
|
|
|
len: uint,
|
2013-11-29 12:12:08 -06:00
|
|
|
f: |&mut Encoder<'self>|) {
|
2013-05-01 19:54:54 -05:00
|
|
|
self.emit_seq(len, f)
|
|
|
|
}
|
2013-11-29 12:12:08 -06:00
|
|
|
fn emit_tuple_struct_arg(&mut self, idx: uint, f: |&mut Encoder<'self>|) {
|
2013-05-01 19:54:54 -05:00
|
|
|
self.emit_seq_elt(idx, f)
|
|
|
|
}
|
|
|
|
|
2013-11-29 12:12:08 -06:00
|
|
|
fn emit_option(&mut self, f: |&mut Encoder<'self>|) { f(self); }
|
2013-05-01 19:54:54 -05:00
|
|
|
fn emit_option_none(&mut self) { self.emit_nil(); }
|
2013-11-29 12:12:08 -06:00
|
|
|
fn emit_option_some(&mut self, f: |&mut Encoder<'self>|) { f(self); }
|
2013-05-01 19:54:54 -05:00
|
|
|
|
2013-11-29 12:12:08 -06:00
|
|
|
fn emit_seq(&mut self, _len: uint, f: |&mut Encoder<'self>|) {
|
2013-10-13 20:48:47 -05:00
|
|
|
write!(self.wr, "[");
|
2013-05-01 19:54:54 -05:00
|
|
|
f(self);
|
2013-10-13 20:48:47 -05:00
|
|
|
write!(self.wr, "]");
|
2013-05-01 19:54:54 -05:00
|
|
|
}
|
|
|
|
|
2013-11-29 12:12:08 -06:00
|
|
|
fn emit_seq_elt(&mut self, idx: uint, f: |&mut Encoder<'self>|) {
|
2013-05-01 19:54:54 -05:00
|
|
|
if idx != 0 {
|
2013-10-13 20:48:47 -05:00
|
|
|
write!(self.wr, ",");
|
2013-05-01 19:54:54 -05:00
|
|
|
}
|
|
|
|
f(self)
|
|
|
|
}
|
|
|
|
|
2013-11-29 12:12:08 -06:00
|
|
|
fn emit_map(&mut self, _len: uint, f: |&mut Encoder<'self>|) {
|
2013-10-13 20:48:47 -05:00
|
|
|
write!(self.wr, r"\{");
|
2013-05-01 19:54:54 -05:00
|
|
|
f(self);
|
2013-10-13 20:48:47 -05:00
|
|
|
write!(self.wr, r"\}");
|
2013-05-01 19:54:54 -05:00
|
|
|
}
|
|
|
|
|
2013-11-29 12:12:08 -06:00
|
|
|
fn emit_map_elt_key(&mut self, idx: uint, f: |&mut Encoder<'self>|) {
|
2013-10-13 20:48:47 -05:00
|
|
|
if idx != 0 { write!(self.wr, ",") }
|
2013-05-01 19:54:54 -05:00
|
|
|
f(self)
|
|
|
|
}
|
|
|
|
|
2013-11-29 12:12:08 -06:00
|
|
|
fn emit_map_elt_val(&mut self, _idx: uint, f: |&mut Encoder<'self>|) {
|
2013-10-13 20:48:47 -05:00
|
|
|
write!(self.wr, ":");
|
2013-05-01 19:54:54 -05:00
|
|
|
f(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-28 22:11:41 -05:00
|
|
|
/// Another encoder for JSON, but prints out human-readable JSON instead of
|
|
|
|
/// compact data
|
2013-11-29 12:12:08 -06:00
|
|
|
pub struct PrettyEncoder<'self> {
|
|
|
|
priv wr: &'self mut io::Writer,
|
2013-05-03 19:55:53 -05:00
|
|
|
priv indent: uint,
|
|
|
|
}
|
|
|
|
|
2013-11-29 12:12:08 -06:00
|
|
|
impl<'self> PrettyEncoder<'self> {
|
2013-11-29 13:11:52 -06:00
|
|
|
/// Creates a new encoder whose output will be written to the specified writer
|
2013-12-03 21:18:35 -06:00
|
|
|
pub fn new<'a>(wr: &'a mut io::Writer) -> PrettyEncoder<'a> {
|
2013-11-29 13:11:52 -06:00
|
|
|
PrettyEncoder {
|
|
|
|
wr: wr,
|
|
|
|
indent: 0,
|
|
|
|
}
|
2013-05-01 19:54:54 -05:00
|
|
|
}
|
2012-09-24 11:55:42 -05:00
|
|
|
}
|
2012-08-30 18:39:56 -05:00
|
|
|
|
2013-11-29 12:12:08 -06:00
|
|
|
impl<'self> serialize::Encoder for PrettyEncoder<'self> {
|
2013-10-13 20:48:47 -05:00
|
|
|
fn emit_nil(&mut self) { write!(self.wr, "null") }
|
2013-05-01 19:54:54 -05:00
|
|
|
|
2013-09-26 01:26:09 -05:00
|
|
|
fn emit_uint(&mut self, v: uint) { self.emit_f64(v as f64); }
|
|
|
|
fn emit_u64(&mut self, v: u64) { self.emit_f64(v as f64); }
|
|
|
|
fn emit_u32(&mut self, v: u32) { self.emit_f64(v as f64); }
|
|
|
|
fn emit_u16(&mut self, v: u16) { self.emit_f64(v as f64); }
|
|
|
|
fn emit_u8(&mut self, v: u8) { self.emit_f64(v as f64); }
|
2013-05-01 19:54:54 -05:00
|
|
|
|
2013-09-26 01:26:09 -05:00
|
|
|
fn emit_int(&mut self, v: int) { self.emit_f64(v as f64); }
|
|
|
|
fn emit_i64(&mut self, v: i64) { self.emit_f64(v as f64); }
|
|
|
|
fn emit_i32(&mut self, v: i32) { self.emit_f64(v as f64); }
|
|
|
|
fn emit_i16(&mut self, v: i16) { self.emit_f64(v as f64); }
|
|
|
|
fn emit_i8(&mut self, v: i8) { self.emit_f64(v as f64); }
|
2013-05-01 19:54:54 -05:00
|
|
|
|
|
|
|
fn emit_bool(&mut self, v: bool) {
|
|
|
|
if v {
|
2013-10-13 20:48:47 -05:00
|
|
|
write!(self.wr, "true");
|
2013-05-01 19:54:54 -05:00
|
|
|
} else {
|
2013-10-13 20:48:47 -05:00
|
|
|
write!(self.wr, "false");
|
2013-05-01 19:54:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-13 20:48:47 -05:00
|
|
|
fn emit_f64(&mut self, v: f64) {
|
|
|
|
write!(self.wr, "{}", f64::to_str_digits(v, 6u))
|
|
|
|
}
|
2013-09-26 01:26:09 -05:00
|
|
|
fn emit_f32(&mut self, v: f32) { self.emit_f64(v as f64); }
|
2013-05-01 19:54:54 -05:00
|
|
|
|
|
|
|
fn emit_char(&mut self, v: char) { self.emit_str(str::from_char(v)) }
|
2013-10-13 20:48:47 -05:00
|
|
|
fn emit_str(&mut self, v: &str) { write!(self.wr, "{}", escape_str(v)); }
|
2013-05-01 19:54:54 -05:00
|
|
|
|
2013-11-29 12:12:08 -06:00
|
|
|
fn emit_enum(&mut self, _name: &str, f: |&mut PrettyEncoder<'self>|) {
|
2013-05-01 19:54:54 -05:00
|
|
|
f(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn emit_enum_variant(&mut self,
|
|
|
|
name: &str,
|
|
|
|
_: uint,
|
|
|
|
cnt: uint,
|
2013-11-29 12:12:08 -06:00
|
|
|
f: |&mut PrettyEncoder<'self>|) {
|
2013-05-01 19:54:54 -05:00
|
|
|
if cnt == 0 {
|
2013-10-13 20:48:47 -05:00
|
|
|
write!(self.wr, "{}", escape_str(name));
|
2013-05-01 19:54:54 -05:00
|
|
|
} else {
|
|
|
|
self.indent += 2;
|
2013-10-13 20:48:47 -05:00
|
|
|
write!(self.wr, "[\n{}{},\n", spaces(self.indent), escape_str(name));
|
2013-05-01 19:54:54 -05:00
|
|
|
f(self);
|
|
|
|
self.indent -= 2;
|
2013-10-13 20:48:47 -05:00
|
|
|
write!(self.wr, "\n{}]", spaces(self.indent));
|
2013-05-01 19:54:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn emit_enum_variant_arg(&mut self,
|
|
|
|
idx: uint,
|
2013-11-29 12:12:08 -06:00
|
|
|
f: |&mut PrettyEncoder<'self>|) {
|
2013-05-01 19:54:54 -05:00
|
|
|
if idx != 0 {
|
2013-10-13 20:48:47 -05:00
|
|
|
write!(self.wr, ",\n");
|
2013-05-01 19:54:54 -05:00
|
|
|
}
|
2013-10-13 20:48:47 -05:00
|
|
|
write!(self.wr, "{}", spaces(self.indent));
|
2013-05-01 19:54:54 -05:00
|
|
|
f(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn emit_enum_struct_variant(&mut self,
|
|
|
|
name: &str,
|
|
|
|
id: uint,
|
|
|
|
cnt: uint,
|
2013-11-29 12:12:08 -06:00
|
|
|
f: |&mut PrettyEncoder<'self>|) {
|
2013-05-01 19:54:54 -05:00
|
|
|
self.emit_enum_variant(name, id, cnt, f)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn emit_enum_struct_variant_field(&mut self,
|
|
|
|
_: &str,
|
|
|
|
idx: uint,
|
2013-11-29 12:12:08 -06:00
|
|
|
f: |&mut PrettyEncoder<'self>|) {
|
2013-05-01 19:54:54 -05:00
|
|
|
self.emit_enum_variant_arg(idx, f)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn emit_struct(&mut self,
|
|
|
|
_: &str,
|
|
|
|
len: uint,
|
2013-11-29 12:12:08 -06:00
|
|
|
f: |&mut PrettyEncoder<'self>|) {
|
2013-05-01 19:54:54 -05:00
|
|
|
if len == 0 {
|
2013-10-13 20:48:47 -05:00
|
|
|
write!(self.wr, "\\{\\}");
|
2013-05-01 19:54:54 -05:00
|
|
|
} else {
|
2013-10-13 20:48:47 -05:00
|
|
|
write!(self.wr, "\\{");
|
2013-05-01 19:54:54 -05:00
|
|
|
self.indent += 2;
|
|
|
|
f(self);
|
|
|
|
self.indent -= 2;
|
2013-10-13 20:48:47 -05:00
|
|
|
write!(self.wr, "\n{}\\}", spaces(self.indent));
|
2013-05-01 19:54:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn emit_struct_field(&mut self,
|
|
|
|
name: &str,
|
|
|
|
idx: uint,
|
2013-11-29 12:12:08 -06:00
|
|
|
f: |&mut PrettyEncoder<'self>|) {
|
2013-05-01 19:54:54 -05:00
|
|
|
if idx == 0 {
|
2013-10-13 20:48:47 -05:00
|
|
|
write!(self.wr, "\n");
|
2013-05-01 19:54:54 -05:00
|
|
|
} else {
|
2013-10-13 20:48:47 -05:00
|
|
|
write!(self.wr, ",\n");
|
2013-05-01 19:54:54 -05:00
|
|
|
}
|
2013-10-13 20:48:47 -05:00
|
|
|
write!(self.wr, "{}{}: ", spaces(self.indent), escape_str(name));
|
2013-05-01 19:54:54 -05:00
|
|
|
f(self);
|
|
|
|
}
|
|
|
|
|
2013-11-29 12:12:08 -06:00
|
|
|
fn emit_tuple(&mut self, len: uint, f: |&mut PrettyEncoder<'self>|) {
|
2013-05-01 19:54:54 -05:00
|
|
|
self.emit_seq(len, f)
|
|
|
|
}
|
2013-11-29 12:12:08 -06:00
|
|
|
fn emit_tuple_arg(&mut self, idx: uint, f: |&mut PrettyEncoder<'self>|) {
|
2013-05-01 19:54:54 -05:00
|
|
|
self.emit_seq_elt(idx, f)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn emit_tuple_struct(&mut self,
|
|
|
|
_: &str,
|
|
|
|
len: uint,
|
2013-11-29 12:12:08 -06:00
|
|
|
f: |&mut PrettyEncoder<'self>|) {
|
2013-05-01 19:54:54 -05:00
|
|
|
self.emit_seq(len, f)
|
|
|
|
}
|
|
|
|
fn emit_tuple_struct_arg(&mut self,
|
|
|
|
idx: uint,
|
2013-11-29 12:12:08 -06:00
|
|
|
f: |&mut PrettyEncoder<'self>|) {
|
2013-05-01 19:54:54 -05:00
|
|
|
self.emit_seq_elt(idx, f)
|
|
|
|
}
|
|
|
|
|
2013-11-29 12:12:08 -06:00
|
|
|
fn emit_option(&mut self, f: |&mut PrettyEncoder<'self>|) { f(self); }
|
2013-05-01 19:54:54 -05:00
|
|
|
fn emit_option_none(&mut self) { self.emit_nil(); }
|
2013-11-29 12:12:08 -06:00
|
|
|
fn emit_option_some(&mut self, f: |&mut PrettyEncoder<'self>|) { f(self); }
|
2013-05-01 19:54:54 -05:00
|
|
|
|
2013-11-29 12:12:08 -06:00
|
|
|
fn emit_seq(&mut self, len: uint, f: |&mut PrettyEncoder<'self>|) {
|
2013-05-01 19:54:54 -05:00
|
|
|
if len == 0 {
|
2013-10-13 20:48:47 -05:00
|
|
|
write!(self.wr, "[]");
|
2013-05-01 19:54:54 -05:00
|
|
|
} else {
|
2013-10-13 20:48:47 -05:00
|
|
|
write!(self.wr, "[");
|
2013-05-01 19:54:54 -05:00
|
|
|
self.indent += 2;
|
|
|
|
f(self);
|
|
|
|
self.indent -= 2;
|
2013-10-13 20:48:47 -05:00
|
|
|
write!(self.wr, "\n{}]", spaces(self.indent));
|
2013-05-01 19:54:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-29 12:12:08 -06:00
|
|
|
fn emit_seq_elt(&mut self, idx: uint, f: |&mut PrettyEncoder<'self>|) {
|
2013-05-01 19:54:54 -05:00
|
|
|
if idx == 0 {
|
2013-10-13 20:48:47 -05:00
|
|
|
write!(self.wr, "\n");
|
2013-05-01 19:54:54 -05:00
|
|
|
} else {
|
2013-10-13 20:48:47 -05:00
|
|
|
write!(self.wr, ",\n");
|
2013-05-01 19:54:54 -05:00
|
|
|
}
|
2013-10-13 20:48:47 -05:00
|
|
|
write!(self.wr, "{}", spaces(self.indent));
|
2013-05-01 19:54:54 -05:00
|
|
|
f(self)
|
|
|
|
}
|
|
|
|
|
2013-11-29 12:12:08 -06:00
|
|
|
fn emit_map(&mut self, len: uint, f: |&mut PrettyEncoder<'self>|) {
|
2013-05-01 19:54:54 -05:00
|
|
|
if len == 0 {
|
2013-10-13 20:48:47 -05:00
|
|
|
write!(self.wr, "\\{\\}");
|
2013-05-01 19:54:54 -05:00
|
|
|
} else {
|
2013-10-13 20:48:47 -05:00
|
|
|
write!(self.wr, "\\{");
|
2013-05-01 19:54:54 -05:00
|
|
|
self.indent += 2;
|
|
|
|
f(self);
|
|
|
|
self.indent -= 2;
|
2013-10-13 20:48:47 -05:00
|
|
|
write!(self.wr, "\n{}\\}", spaces(self.indent));
|
2013-05-01 19:54:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-29 12:12:08 -06:00
|
|
|
fn emit_map_elt_key(&mut self, idx: uint, f: |&mut PrettyEncoder<'self>|) {
|
2013-05-01 19:54:54 -05:00
|
|
|
if idx == 0 {
|
2013-10-13 20:48:47 -05:00
|
|
|
write!(self.wr, "\n");
|
2013-05-01 19:54:54 -05:00
|
|
|
} else {
|
2013-10-13 20:48:47 -05:00
|
|
|
write!(self.wr, ",\n");
|
2013-05-01 19:54:54 -05:00
|
|
|
}
|
2013-10-13 20:48:47 -05:00
|
|
|
write!(self.wr, "{}", spaces(self.indent));
|
2013-05-01 19:54:54 -05:00
|
|
|
f(self);
|
|
|
|
}
|
|
|
|
|
2013-11-29 12:12:08 -06:00
|
|
|
fn emit_map_elt_val(&mut self, _idx: uint, f: |&mut PrettyEncoder<'self>|) {
|
2013-10-13 20:48:47 -05:00
|
|
|
write!(self.wr, ": ");
|
2013-05-01 19:54:54 -05:00
|
|
|
f(self);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<E: serialize::Encoder> serialize::Encodable<E> for Json {
|
|
|
|
fn encode(&self, e: &mut E) {
|
|
|
|
match *self {
|
|
|
|
Number(v) => v.encode(e),
|
|
|
|
String(ref v) => v.encode(e),
|
|
|
|
Boolean(v) => v.encode(e),
|
|
|
|
List(ref v) => v.encode(e),
|
|
|
|
Object(ref v) => v.encode(e),
|
|
|
|
Null => e.emit_nil(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-22 00:32:18 -05:00
|
|
|
impl Json{
|
|
|
|
/// Encodes a json value into a io::writer. Uses a single line.
|
2013-11-29 12:12:08 -06:00
|
|
|
pub fn to_writer(&self, wr: &mut io::Writer) {
|
2013-12-03 21:18:35 -06:00
|
|
|
let mut encoder = Encoder::new(wr);
|
2013-08-22 00:32:18 -05:00
|
|
|
self.encode(&mut encoder)
|
|
|
|
}
|
2012-09-24 11:55:42 -05:00
|
|
|
|
2013-08-22 00:32:18 -05:00
|
|
|
/// Encodes a json value into a io::writer.
|
|
|
|
/// Pretty-prints in a more readable format.
|
2013-11-29 12:12:08 -06:00
|
|
|
pub fn to_pretty_writer(&self, wr: &mut io::Writer) {
|
2013-12-03 21:18:35 -06:00
|
|
|
let mut encoder = PrettyEncoder::new(wr);
|
2013-08-22 00:32:18 -05:00
|
|
|
self.encode(&mut encoder)
|
|
|
|
}
|
2012-06-12 19:20:51 -05:00
|
|
|
|
2013-08-22 00:32:18 -05:00
|
|
|
/// Encodes a json value into a string
|
|
|
|
pub fn to_pretty_str(&self) -> ~str {
|
2013-11-29 12:12:08 -06:00
|
|
|
let mut s = MemWriter::new();
|
|
|
|
self.to_pretty_writer(&mut s as &mut io::Writer);
|
2013-11-29 23:26:03 -06:00
|
|
|
str::from_utf8_owned(s.inner())
|
2013-08-22 00:32:18 -05:00
|
|
|
}
|
2011-11-07 13:01:28 -06:00
|
|
|
}
|
|
|
|
|
2013-07-10 22:03:10 -05:00
|
|
|
pub struct Parser<T> {
|
2013-11-29 13:11:52 -06:00
|
|
|
priv rdr: T,
|
2013-03-24 11:41:19 -05:00
|
|
|
priv ch: char,
|
|
|
|
priv line: uint,
|
|
|
|
priv col: uint,
|
2012-08-30 18:39:56 -05:00
|
|
|
}
|
|
|
|
|
2013-11-29 13:11:52 -06:00
|
|
|
impl<T: Iterator<char>> Parser<T> {
|
|
|
|
/// Decode a json value from an Iterator<char>
|
2013-12-03 21:18:35 -06:00
|
|
|
pub fn new(rdr: T) -> Parser<T> {
|
2013-11-29 13:11:52 -06:00
|
|
|
let mut p = Parser {
|
|
|
|
rdr: rdr,
|
|
|
|
ch: '\x00',
|
|
|
|
line: 1,
|
|
|
|
col: 0,
|
|
|
|
};
|
|
|
|
p.bump();
|
|
|
|
p
|
|
|
|
}
|
2012-09-24 11:55:42 -05:00
|
|
|
}
|
2011-11-07 13:01:28 -06:00
|
|
|
|
2013-09-08 10:01:16 -05:00
|
|
|
impl<T: Iterator<char>> Parser<T> {
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn parse(&mut self) -> Result<Json, Error> {
|
2013-02-15 01:30:30 -06:00
|
|
|
match self.parse_value() {
|
|
|
|
Ok(value) => {
|
2012-09-24 11:55:42 -05:00
|
|
|
// Skip trailing whitespaces.
|
|
|
|
self.parse_whitespace();
|
|
|
|
// Make sure there is no trailing characters.
|
|
|
|
if self.eof() {
|
2013-02-15 01:30:30 -06:00
|
|
|
Ok(value)
|
2012-09-24 11:55:42 -05:00
|
|
|
} else {
|
|
|
|
self.error(~"trailing characters")
|
|
|
|
}
|
|
|
|
}
|
2013-02-15 01:30:30 -06:00
|
|
|
Err(e) => Err(e)
|
2012-09-24 11:55:42 -05:00
|
|
|
}
|
|
|
|
}
|
2012-07-11 17:00:40 -05:00
|
|
|
}
|
|
|
|
|
2013-09-08 10:01:16 -05:00
|
|
|
impl<T : Iterator<char>> Parser<T> {
|
2013-09-03 18:24:12 -05:00
|
|
|
// FIXME: #8971: unsound
|
|
|
|
fn eof(&self) -> bool { self.ch == unsafe { transmute(-1u32) } }
|
2012-02-25 18:39:32 -06:00
|
|
|
|
2013-03-24 11:41:19 -05:00
|
|
|
fn bump(&mut self) {
|
2013-07-10 22:03:10 -05:00
|
|
|
match self.rdr.next() {
|
|
|
|
Some(ch) => self.ch = ch,
|
2013-09-03 18:24:12 -05:00
|
|
|
None() => self.ch = unsafe { transmute(-1u32) }, // FIXME: #8971: unsound
|
2013-07-05 18:01:57 -05:00
|
|
|
}
|
2011-11-07 13:01:28 -06:00
|
|
|
|
2012-02-25 18:39:32 -06:00
|
|
|
if self.ch == '\n' {
|
|
|
|
self.line += 1u;
|
|
|
|
self.col = 1u;
|
2013-07-10 22:03:10 -05:00
|
|
|
} else {
|
|
|
|
self.col += 1u;
|
2011-11-07 13:01:28 -06:00
|
|
|
}
|
2012-02-25 18:39:32 -06:00
|
|
|
}
|
|
|
|
|
2013-03-24 11:41:19 -05:00
|
|
|
fn next_char(&mut self) -> char {
|
2012-02-25 18:39:32 -06:00
|
|
|
self.bump();
|
|
|
|
self.ch
|
|
|
|
}
|
|
|
|
|
2013-03-07 20:11:09 -06:00
|
|
|
fn error<T>(&self, msg: ~str) -> Result<T, Error> {
|
2013-11-28 16:50:21 -06:00
|
|
|
Err(Error { line: self.line, col: self.col, msg: msg })
|
2011-11-07 13:01:28 -06:00
|
|
|
}
|
|
|
|
|
2013-03-24 11:41:19 -05:00
|
|
|
fn parse_value(&mut self) -> Result<Json, Error> {
|
2012-02-25 18:39:32 -06:00
|
|
|
self.parse_whitespace();
|
|
|
|
|
2012-08-01 19:30:05 -05:00
|
|
|
if self.eof() { return self.error(~"EOF while parsing value"); }
|
2011-11-07 13:01:28 -06:00
|
|
|
|
2012-08-06 14:34:08 -05:00
|
|
|
match self.ch {
|
2013-05-19 00:07:44 -05:00
|
|
|
'n' => self.parse_ident("ull", Null),
|
|
|
|
't' => self.parse_ident("rue", Boolean(true)),
|
|
|
|
'f' => self.parse_ident("alse", Boolean(false)),
|
2012-09-01 20:38:05 -05:00
|
|
|
'0' .. '9' | '-' => self.parse_number(),
|
2012-09-24 11:55:42 -05:00
|
|
|
'"' =>
|
2013-02-15 01:30:30 -06:00
|
|
|
match self.parse_str() {
|
|
|
|
Ok(s) => Ok(String(s)),
|
|
|
|
Err(e) => Err(e),
|
2012-09-24 11:55:42 -05:00
|
|
|
},
|
2012-08-03 21:59:04 -05:00
|
|
|
'[' => self.parse_list(),
|
|
|
|
'{' => self.parse_object(),
|
|
|
|
_ => self.error(~"invalid syntax")
|
2011-11-07 13:01:28 -06:00
|
|
|
}
|
2012-02-25 18:39:32 -06:00
|
|
|
}
|
|
|
|
|
2013-03-24 11:41:19 -05:00
|
|
|
fn parse_whitespace(&mut self) {
|
2013-07-10 22:03:10 -05:00
|
|
|
while self.ch == ' ' ||
|
|
|
|
self.ch == '\n' ||
|
|
|
|
self.ch == '\t' ||
|
|
|
|
self.ch == '\r' { self.bump(); }
|
2012-02-25 18:39:32 -06:00
|
|
|
}
|
|
|
|
|
2013-03-24 11:41:19 -05:00
|
|
|
fn parse_ident(&mut self, ident: &str, value: Json) -> Result<Json, Error> {
|
2013-11-23 04:18:51 -06:00
|
|
|
if ident.chars().all(|c| c == self.next_char()) {
|
2012-02-25 18:39:32 -06:00
|
|
|
self.bump();
|
2013-02-15 01:30:30 -06:00
|
|
|
Ok(value)
|
2012-02-25 18:39:32 -06:00
|
|
|
} else {
|
2012-07-14 00:57:48 -05:00
|
|
|
self.error(~"invalid syntax")
|
2011-11-07 13:01:28 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-24 11:41:19 -05:00
|
|
|
fn parse_number(&mut self) -> Result<Json, Error> {
|
2013-09-26 01:26:09 -05:00
|
|
|
let mut neg = 1.0;
|
2012-02-25 18:39:32 -06:00
|
|
|
|
|
|
|
if self.ch == '-' {
|
|
|
|
self.bump();
|
2013-09-26 01:26:09 -05:00
|
|
|
neg = -1.0;
|
2011-11-07 13:01:28 -06:00
|
|
|
}
|
2012-02-25 18:39:32 -06:00
|
|
|
|
2012-08-06 14:34:08 -05:00
|
|
|
let mut res = match self.parse_integer() {
|
2012-08-26 18:54:31 -05:00
|
|
|
Ok(res) => res,
|
|
|
|
Err(e) => return Err(e)
|
2012-02-25 18:39:32 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
if self.ch == '.' {
|
2012-08-06 14:34:08 -05:00
|
|
|
match self.parse_decimal(res) {
|
2012-08-26 18:54:31 -05:00
|
|
|
Ok(r) => res = r,
|
|
|
|
Err(e) => return Err(e)
|
2012-02-25 18:39:32 -06:00
|
|
|
}
|
2011-11-07 13:01:28 -06:00
|
|
|
}
|
2012-02-25 18:39:32 -06:00
|
|
|
|
|
|
|
if self.ch == 'e' || self.ch == 'E' {
|
2012-08-06 14:34:08 -05:00
|
|
|
match self.parse_exponent(res) {
|
2012-08-26 18:54:31 -05:00
|
|
|
Ok(r) => res = r,
|
|
|
|
Err(e) => return Err(e)
|
2012-02-25 18:39:32 -06:00
|
|
|
}
|
2011-11-07 13:01:28 -06:00
|
|
|
}
|
2012-02-25 18:39:32 -06:00
|
|
|
|
2012-09-24 11:55:42 -05:00
|
|
|
Ok(Number(neg * res))
|
2011-11-07 13:01:28 -06:00
|
|
|
}
|
|
|
|
|
2013-09-26 01:26:09 -05:00
|
|
|
fn parse_integer(&mut self) -> Result<f64, Error> {
|
|
|
|
let mut res = 0.0;
|
2011-11-07 13:01:28 -06:00
|
|
|
|
2012-08-06 14:34:08 -05:00
|
|
|
match self.ch {
|
2012-08-03 21:59:04 -05:00
|
|
|
'0' => {
|
2012-02-25 18:39:32 -06:00
|
|
|
self.bump();
|
|
|
|
|
|
|
|
// There can be only one leading '0'.
|
2012-08-06 14:34:08 -05:00
|
|
|
match self.ch {
|
2012-09-01 20:38:05 -05:00
|
|
|
'0' .. '9' => return self.error(~"invalid number"),
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => ()
|
2012-02-25 18:39:32 -06:00
|
|
|
}
|
|
|
|
}
|
2012-09-01 20:38:05 -05:00
|
|
|
'1' .. '9' => {
|
2012-02-25 18:39:32 -06:00
|
|
|
while !self.eof() {
|
2012-08-06 14:34:08 -05:00
|
|
|
match self.ch {
|
2012-09-01 20:38:05 -05:00
|
|
|
'0' .. '9' => {
|
2013-09-26 01:26:09 -05:00
|
|
|
res *= 10.0;
|
|
|
|
res += ((self.ch as int) - ('0' as int)) as f64;
|
2012-02-25 18:39:32 -06:00
|
|
|
|
|
|
|
self.bump();
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => break
|
2012-02-25 18:39:32 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => return self.error(~"invalid number")
|
2011-11-07 13:01:28 -06:00
|
|
|
}
|
2012-02-25 18:39:32 -06:00
|
|
|
|
2012-08-26 18:54:31 -05:00
|
|
|
Ok(res)
|
2011-11-07 13:01:28 -06:00
|
|
|
}
|
|
|
|
|
2013-09-26 01:26:09 -05:00
|
|
|
fn parse_decimal(&mut self, res: f64) -> Result<f64, Error> {
|
2012-02-25 18:39:32 -06:00
|
|
|
self.bump();
|
|
|
|
|
|
|
|
// Make sure a digit follows the decimal place.
|
2012-08-06 14:34:08 -05:00
|
|
|
match self.ch {
|
2012-09-01 20:38:05 -05:00
|
|
|
'0' .. '9' => (),
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => return self.error(~"invalid number")
|
2012-02-25 18:39:32 -06:00
|
|
|
}
|
|
|
|
|
2012-03-14 13:03:56 -05:00
|
|
|
let mut res = res;
|
2013-09-26 01:26:09 -05:00
|
|
|
let mut dec = 1.0;
|
2012-02-25 18:39:32 -06:00
|
|
|
while !self.eof() {
|
2012-08-06 14:34:08 -05:00
|
|
|
match self.ch {
|
2012-09-01 20:38:05 -05:00
|
|
|
'0' .. '9' => {
|
2013-09-26 01:26:09 -05:00
|
|
|
dec /= 10.0;
|
|
|
|
res += (((self.ch as int) - ('0' as int)) as f64) * dec;
|
2012-02-25 18:39:32 -06:00
|
|
|
|
|
|
|
self.bump();
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => break
|
2011-11-07 13:01:28 -06:00
|
|
|
}
|
|
|
|
}
|
2012-02-25 18:39:32 -06:00
|
|
|
|
2012-08-26 18:54:31 -05:00
|
|
|
Ok(res)
|
2011-11-07 13:01:28 -06:00
|
|
|
}
|
|
|
|
|
2013-09-26 01:26:09 -05:00
|
|
|
fn parse_exponent(&mut self, mut res: f64) -> Result<f64, Error> {
|
2012-02-25 18:39:32 -06:00
|
|
|
self.bump();
|
|
|
|
|
2012-03-14 13:03:56 -05:00
|
|
|
let mut exp = 0u;
|
|
|
|
let mut neg_exp = false;
|
2012-02-25 18:39:32 -06:00
|
|
|
|
2012-08-06 14:34:08 -05:00
|
|
|
match self.ch {
|
2012-08-03 21:59:04 -05:00
|
|
|
'+' => self.bump(),
|
|
|
|
'-' => { self.bump(); neg_exp = true; }
|
|
|
|
_ => ()
|
2012-02-25 18:39:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure a digit follows the exponent place.
|
2012-08-06 14:34:08 -05:00
|
|
|
match self.ch {
|
2012-09-01 20:38:05 -05:00
|
|
|
'0' .. '9' => (),
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => return self.error(~"invalid number")
|
2012-02-25 18:39:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
while !self.eof() {
|
2012-08-06 14:34:08 -05:00
|
|
|
match self.ch {
|
2012-09-01 20:38:05 -05:00
|
|
|
'0' .. '9' => {
|
2012-02-25 18:39:32 -06:00
|
|
|
exp *= 10u;
|
|
|
|
exp += (self.ch as uint) - ('0' as uint);
|
|
|
|
|
|
|
|
self.bump();
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => break
|
2012-02-25 18:39:32 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-26 01:26:09 -05:00
|
|
|
let exp: f64 = num::pow_with_uint(10u, exp);
|
2012-02-25 18:39:32 -06:00
|
|
|
if neg_exp {
|
|
|
|
res /= exp;
|
|
|
|
} else {
|
|
|
|
res *= exp;
|
|
|
|
}
|
|
|
|
|
2012-08-26 18:54:31 -05:00
|
|
|
Ok(res)
|
2011-11-07 13:01:28 -06:00
|
|
|
}
|
|
|
|
|
2013-03-24 11:41:19 -05:00
|
|
|
fn parse_str(&mut self) -> Result<~str, Error> {
|
2012-03-14 13:03:56 -05:00
|
|
|
let mut escape = false;
|
2012-07-14 00:57:48 -05:00
|
|
|
let mut res = ~"";
|
2012-02-25 18:39:32 -06:00
|
|
|
|
2013-07-10 22:03:10 -05:00
|
|
|
loop {
|
2012-02-25 18:39:32 -06:00
|
|
|
self.bump();
|
2013-07-10 22:03:10 -05:00
|
|
|
if self.eof() {
|
|
|
|
return self.error(~"EOF while parsing string");
|
|
|
|
}
|
2012-02-25 18:39:32 -06:00
|
|
|
|
|
|
|
if (escape) {
|
2012-08-06 14:34:08 -05:00
|
|
|
match self.ch {
|
2013-06-10 02:42:24 -05:00
|
|
|
'"' => res.push_char('"'),
|
|
|
|
'\\' => res.push_char('\\'),
|
|
|
|
'/' => res.push_char('/'),
|
|
|
|
'b' => res.push_char('\x08'),
|
|
|
|
'f' => res.push_char('\x0c'),
|
|
|
|
'n' => res.push_char('\n'),
|
|
|
|
'r' => res.push_char('\r'),
|
|
|
|
't' => res.push_char('\t'),
|
2012-08-03 21:59:04 -05:00
|
|
|
'u' => {
|
2012-02-25 18:39:32 -06:00
|
|
|
// Parse \u1234.
|
2012-03-14 13:03:56 -05:00
|
|
|
let mut i = 0u;
|
|
|
|
let mut n = 0u;
|
2012-02-25 18:39:32 -06:00
|
|
|
while i < 4u {
|
2012-08-06 14:34:08 -05:00
|
|
|
match self.next_char() {
|
2012-09-01 20:38:05 -05:00
|
|
|
'0' .. '9' => {
|
2012-08-30 22:12:10 -05:00
|
|
|
n = n * 16u + (self.ch as uint)
|
|
|
|
- ('0' as uint);
|
|
|
|
},
|
|
|
|
'a' | 'A' => n = n * 16u + 10u,
|
|
|
|
'b' | 'B' => n = n * 16u + 11u,
|
|
|
|
'c' | 'C' => n = n * 16u + 12u,
|
|
|
|
'd' | 'D' => n = n * 16u + 13u,
|
|
|
|
'e' | 'E' => n = n * 16u + 14u,
|
|
|
|
'f' | 'F' => n = n * 16u + 15u,
|
|
|
|
_ => return self.error(
|
|
|
|
~"invalid \\u escape (unrecognized hex)")
|
2012-02-25 18:39:32 -06:00
|
|
|
}
|
2012-03-14 13:03:56 -05:00
|
|
|
i += 1u;
|
2012-02-25 18:39:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Error out if we didn't parse 4 digits.
|
|
|
|
if i != 4u {
|
2012-08-30 22:12:10 -05:00
|
|
|
return self.error(
|
|
|
|
~"invalid \\u escape (not four digits)");
|
2012-02-25 18:39:32 -06:00
|
|
|
}
|
|
|
|
|
2013-09-03 18:24:12 -05:00
|
|
|
res.push_char(char::from_u32(n as u32).unwrap());
|
2012-02-25 18:39:32 -06:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => return self.error(~"invalid escape")
|
2012-02-25 18:39:32 -06:00
|
|
|
}
|
|
|
|
escape = false;
|
|
|
|
} else if self.ch == '\\' {
|
|
|
|
escape = true;
|
|
|
|
} else {
|
|
|
|
if self.ch == '"' {
|
|
|
|
self.bump();
|
2012-09-24 11:55:42 -05:00
|
|
|
return Ok(res);
|
2012-02-25 18:39:32 -06:00
|
|
|
}
|
2013-06-10 02:42:24 -05:00
|
|
|
res.push_char(self.ch);
|
2011-11-07 13:01:28 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-24 11:41:19 -05:00
|
|
|
fn parse_list(&mut self) -> Result<Json, Error> {
|
2012-02-25 18:39:32 -06:00
|
|
|
self.bump();
|
|
|
|
self.parse_whitespace();
|
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
let mut values = ~[];
|
2012-02-25 18:39:32 -06:00
|
|
|
|
|
|
|
if self.ch == ']' {
|
|
|
|
self.bump();
|
2013-02-15 01:30:30 -06:00
|
|
|
return Ok(List(values));
|
2012-02-25 18:39:32 -06:00
|
|
|
}
|
|
|
|
|
2012-03-10 22:34:17 -06:00
|
|
|
loop {
|
2013-02-15 01:30:30 -06:00
|
|
|
match self.parse_value() {
|
|
|
|
Ok(v) => values.push(v),
|
|
|
|
Err(e) => return Err(e)
|
2012-02-25 18:39:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
self.parse_whitespace();
|
2012-03-10 22:34:17 -06:00
|
|
|
if self.eof() {
|
2012-08-01 19:30:05 -05:00
|
|
|
return self.error(~"EOF while parsing list");
|
2012-03-10 22:34:17 -06:00
|
|
|
}
|
2012-02-25 18:39:32 -06:00
|
|
|
|
2012-08-06 14:34:08 -05:00
|
|
|
match self.ch {
|
2012-08-03 21:59:04 -05:00
|
|
|
',' => self.bump(),
|
2013-02-15 01:30:30 -06:00
|
|
|
']' => { self.bump(); return Ok(List(values)); }
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => return self.error(~"expected `,` or `]`")
|
2012-02-25 18:39:32 -06:00
|
|
|
}
|
2012-03-10 22:34:17 -06:00
|
|
|
};
|
2011-11-07 13:01:28 -06:00
|
|
|
}
|
|
|
|
|
2013-03-24 11:41:19 -05:00
|
|
|
fn parse_object(&mut self) -> Result<Json, Error> {
|
2012-02-25 18:39:32 -06:00
|
|
|
self.bump();
|
|
|
|
self.parse_whitespace();
|
|
|
|
|
2013-07-18 14:37:40 -05:00
|
|
|
let mut values = ~TreeMap::new();
|
2012-02-25 18:39:32 -06:00
|
|
|
|
|
|
|
if self.ch == '}' {
|
|
|
|
self.bump();
|
2013-02-15 01:30:30 -06:00
|
|
|
return Ok(Object(values));
|
2012-02-25 18:39:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
while !self.eof() {
|
|
|
|
self.parse_whitespace();
|
|
|
|
|
2013-11-23 17:17:34 -06:00
|
|
|
if self.ch != '\"' {
|
2012-08-01 19:30:05 -05:00
|
|
|
return self.error(~"key must be a string");
|
2012-02-25 18:39:32 -06:00
|
|
|
}
|
|
|
|
|
2013-02-15 01:30:30 -06:00
|
|
|
let key = match self.parse_str() {
|
|
|
|
Ok(key) => key,
|
|
|
|
Err(e) => return Err(e)
|
2012-02-25 18:39:32 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
self.parse_whitespace();
|
|
|
|
|
|
|
|
if self.ch != ':' {
|
|
|
|
if self.eof() { break; }
|
2012-08-01 19:30:05 -05:00
|
|
|
return self.error(~"expected `:`");
|
2012-02-25 18:39:32 -06:00
|
|
|
}
|
|
|
|
self.bump();
|
|
|
|
|
2013-02-15 01:30:30 -06:00
|
|
|
match self.parse_value() {
|
|
|
|
Ok(value) => { values.insert(key, value); }
|
|
|
|
Err(e) => return Err(e)
|
2012-02-25 18:39:32 -06:00
|
|
|
}
|
|
|
|
self.parse_whitespace();
|
|
|
|
|
2012-08-06 14:34:08 -05:00
|
|
|
match self.ch {
|
2012-08-03 21:59:04 -05:00
|
|
|
',' => self.bump(),
|
2013-02-15 01:30:30 -06:00
|
|
|
'}' => { self.bump(); return Ok(Object(values)); }
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => {
|
2012-02-25 18:39:32 -06:00
|
|
|
if self.eof() { break; }
|
2012-08-01 19:30:05 -05:00
|
|
|
return self.error(~"expected `,` or `}`");
|
2012-02-25 18:39:32 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-01 19:30:05 -05:00
|
|
|
return self.error(~"EOF while parsing object");
|
2011-12-21 14:36:43 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-13 20:48:47 -05:00
|
|
|
/// Decodes a json value from an `&mut io::Reader`
|
2013-10-25 20:08:45 -05:00
|
|
|
pub fn from_reader(rdr: &mut io::Reader) -> Result<Json, Error> {
|
2013-11-29 23:26:03 -06:00
|
|
|
let s = str::from_utf8_owned(rdr.read_to_end());
|
2013-12-03 21:18:35 -06:00
|
|
|
let mut parser = Parser::new(s.chars());
|
2013-03-24 11:41:19 -05:00
|
|
|
parser.parse()
|
2011-11-07 13:01:28 -06:00
|
|
|
}
|
|
|
|
|
2012-12-17 21:31:04 -06:00
|
|
|
/// Decodes a json value from a string
|
2012-09-24 11:55:42 -05:00
|
|
|
pub fn from_str(s: &str) -> Result<Json, Error> {
|
2013-12-03 21:18:35 -06:00
|
|
|
let mut parser = Parser::new(s.chars());
|
2013-07-10 22:03:10 -05:00
|
|
|
parser.parse()
|
2012-02-25 18:39:32 -06:00
|
|
|
}
|
|
|
|
|
2013-05-28 22:11:41 -05:00
|
|
|
/// A structure to decode JSON to values in rust.
|
2013-05-03 19:55:53 -05:00
|
|
|
pub struct Decoder {
|
|
|
|
priv stack: ~[Json],
|
|
|
|
}
|
|
|
|
|
2013-11-29 13:11:52 -06:00
|
|
|
impl Decoder {
|
|
|
|
/// Creates a new decoder instance for decoding the specified JSON value.
|
2013-12-03 21:18:35 -06:00
|
|
|
pub fn new(json: Json) -> Decoder {
|
2013-11-29 13:11:52 -06:00
|
|
|
Decoder {
|
|
|
|
stack: ~[json]
|
|
|
|
}
|
2013-05-01 19:54:54 -05:00
|
|
|
}
|
2012-09-24 11:55:42 -05:00
|
|
|
}
|
|
|
|
|
2013-11-23 17:17:34 -06:00
|
|
|
impl Decoder {
|
|
|
|
fn err(&self, msg: &str) -> ! {
|
|
|
|
fail!("JSON decode error: {}", msg);
|
|
|
|
}
|
|
|
|
fn missing_field(&self, field: &str, object: ~Object) -> ! {
|
|
|
|
self.err(format!("missing required '{}' field in object: {}",
|
|
|
|
field, Object(object).to_str()))
|
|
|
|
}
|
|
|
|
fn expected(&self, expected: &str, found: &Json) -> ! {
|
|
|
|
let found_s = match *found {
|
|
|
|
Null => "null",
|
2013-11-28 14:22:53 -06:00
|
|
|
List(..) => "list",
|
|
|
|
Object(..) => "object",
|
|
|
|
Number(..) => "number",
|
|
|
|
String(..) => "string",
|
|
|
|
Boolean(..) => "boolean"
|
2013-11-23 17:17:34 -06:00
|
|
|
};
|
|
|
|
self.err(format!("expected {expct} but found {fnd}: {val}",
|
|
|
|
expct=expected, fnd=found_s, val=found.to_str()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-01 19:54:54 -05:00
|
|
|
impl serialize::Decoder for Decoder {
|
|
|
|
fn read_nil(&mut self) -> () {
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("read_nil");
|
2013-05-01 19:54:54 -05:00
|
|
|
match self.stack.pop() {
|
|
|
|
Null => (),
|
2013-11-23 17:17:34 -06:00
|
|
|
value => self.expected("null", &value)
|
2013-05-01 19:54:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-26 01:26:09 -05:00
|
|
|
fn read_u64(&mut self) -> u64 { self.read_f64() as u64 }
|
|
|
|
fn read_u32(&mut self) -> u32 { self.read_f64() as u32 }
|
|
|
|
fn read_u16(&mut self) -> u16 { self.read_f64() as u16 }
|
|
|
|
fn read_u8 (&mut self) -> u8 { self.read_f64() as u8 }
|
|
|
|
fn read_uint(&mut self) -> uint { self.read_f64() as uint }
|
2013-05-01 19:54:54 -05:00
|
|
|
|
2013-09-26 01:26:09 -05:00
|
|
|
fn read_i64(&mut self) -> i64 { self.read_f64() as i64 }
|
|
|
|
fn read_i32(&mut self) -> i32 { self.read_f64() as i32 }
|
|
|
|
fn read_i16(&mut self) -> i16 { self.read_f64() as i16 }
|
|
|
|
fn read_i8 (&mut self) -> i8 { self.read_f64() as i8 }
|
|
|
|
fn read_int(&mut self) -> int { self.read_f64() as int }
|
2013-05-01 19:54:54 -05:00
|
|
|
|
|
|
|
fn read_bool(&mut self) -> bool {
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("read_bool");
|
2013-05-01 19:54:54 -05:00
|
|
|
match self.stack.pop() {
|
|
|
|
Boolean(b) => b,
|
2013-11-23 17:17:34 -06:00
|
|
|
value => self.expected("boolean", &value)
|
2013-05-01 19:54:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-26 01:26:09 -05:00
|
|
|
fn read_f64(&mut self) -> f64 {
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("read_f64");
|
2013-05-01 19:54:54 -05:00
|
|
|
match self.stack.pop() {
|
|
|
|
Number(f) => f,
|
2013-11-23 17:17:34 -06:00
|
|
|
value => self.expected("number", &value)
|
2013-05-01 19:54:54 -05:00
|
|
|
}
|
|
|
|
}
|
2013-09-26 01:26:09 -05:00
|
|
|
fn read_f32(&mut self) -> f32 { self.read_f64() as f32 }
|
|
|
|
fn read_f32(&mut self) -> f32 { self.read_f64() as f32 }
|
2013-05-01 19:54:54 -05:00
|
|
|
|
|
|
|
fn read_char(&mut self) -> char {
|
2013-06-08 07:04:46 -05:00
|
|
|
let s = self.read_str();
|
2013-11-23 17:17:34 -06:00
|
|
|
{
|
2013-11-23 04:18:51 -06:00
|
|
|
let mut it = s.chars();
|
2013-11-23 17:17:34 -06:00
|
|
|
match (it.next(), it.next()) {
|
|
|
|
// exactly one character
|
|
|
|
(Some(c), None) => return c,
|
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.expected("single character string", &String(s))
|
2013-05-01 19:54:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn read_str(&mut self) -> ~str {
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("read_str");
|
2013-05-01 19:54:54 -05:00
|
|
|
match self.stack.pop() {
|
|
|
|
String(s) => s,
|
2013-11-23 17:17:34 -06:00
|
|
|
value => self.expected("string", &value)
|
2013-05-01 19:54:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-18 23:54:13 -06:00
|
|
|
fn read_enum<T>(&mut self, name: &str, f: |&mut Decoder| -> T) -> T {
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("read_enum({})", name);
|
2013-05-01 19:54:54 -05:00
|
|
|
f(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read_enum_variant<T>(&mut self,
|
|
|
|
names: &[&str],
|
2013-11-18 23:54:13 -06:00
|
|
|
f: |&mut Decoder, uint| -> T)
|
2013-05-01 19:54:54 -05:00
|
|
|
-> T {
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("read_enum_variant(names={:?})", names);
|
2013-05-01 19:54:54 -05:00
|
|
|
let name = match self.stack.pop() {
|
|
|
|
String(s) => s,
|
2013-11-23 17:17:34 -06:00
|
|
|
Object(mut o) => {
|
|
|
|
let n = match o.pop(&~"variant") {
|
|
|
|
Some(String(s)) => s,
|
|
|
|
Some(val) => self.expected("string", &val),
|
|
|
|
None => self.missing_field("variant", o)
|
2013-08-09 19:05:24 -05:00
|
|
|
};
|
2013-11-23 17:17:34 -06:00
|
|
|
match o.pop(&~"fields") {
|
|
|
|
Some(List(l)) => {
|
|
|
|
for field in l.move_rev_iter() {
|
2013-08-09 19:05:24 -05:00
|
|
|
self.stack.push(field.clone());
|
|
|
|
}
|
|
|
|
},
|
2013-11-23 17:17:34 -06:00
|
|
|
Some(val) => self.expected("list", &val),
|
|
|
|
None => {
|
|
|
|
// re-insert the variant field so we're
|
|
|
|
// printing the "whole" struct in the error
|
|
|
|
// message... ick.
|
|
|
|
o.insert(~"variant", String(n));
|
|
|
|
self.missing_field("fields", o);
|
|
|
|
}
|
2013-05-01 19:54:54 -05:00
|
|
|
}
|
2013-08-09 19:05:24 -05:00
|
|
|
n
|
2013-05-01 19:54:54 -05:00
|
|
|
}
|
2013-11-23 17:17:34 -06:00
|
|
|
json => self.expected("string or object", &json)
|
2013-05-01 19:54:54 -05:00
|
|
|
};
|
2013-07-04 21:13:26 -05:00
|
|
|
let idx = match names.iter().position(|n| str::eq_slice(*n, name)) {
|
2013-05-01 19:54:54 -05:00
|
|
|
Some(idx) => idx,
|
2013-11-23 17:17:34 -06:00
|
|
|
None => self.err(format!("unknown variant name: {}", name))
|
2013-05-01 19:54:54 -05:00
|
|
|
};
|
|
|
|
f(self, idx)
|
|
|
|
}
|
|
|
|
|
2013-11-18 23:54:13 -06:00
|
|
|
fn read_enum_variant_arg<T>(&mut self, idx: uint, f: |&mut Decoder| -> T)
|
2013-05-01 19:54:54 -05:00
|
|
|
-> T {
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("read_enum_variant_arg(idx={})", idx);
|
2013-05-01 19:54:54 -05:00
|
|
|
f(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read_enum_struct_variant<T>(&mut self,
|
|
|
|
names: &[&str],
|
2013-11-18 23:54:13 -06:00
|
|
|
f: |&mut Decoder, uint| -> T)
|
2013-05-01 19:54:54 -05:00
|
|
|
-> T {
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("read_enum_struct_variant(names={:?})", names);
|
2013-05-01 19:54:54 -05:00
|
|
|
self.read_enum_variant(names, f)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn read_enum_struct_variant_field<T>(&mut self,
|
|
|
|
name: &str,
|
|
|
|
idx: uint,
|
2013-11-18 23:54:13 -06:00
|
|
|
f: |&mut Decoder| -> T)
|
2013-05-01 19:54:54 -05:00
|
|
|
-> T {
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("read_enum_struct_variant_field(name={}, idx={})", name, idx);
|
2013-05-01 19:54:54 -05:00
|
|
|
self.read_enum_variant_arg(idx, f)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read_struct<T>(&mut self,
|
|
|
|
name: &str,
|
|
|
|
len: uint,
|
2013-11-18 23:54:13 -06:00
|
|
|
f: |&mut Decoder| -> T)
|
2013-05-01 19:54:54 -05:00
|
|
|
-> T {
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("read_struct(name={}, len={})", name, len);
|
2013-05-01 19:54:54 -05:00
|
|
|
let value = f(self);
|
|
|
|
self.stack.pop();
|
|
|
|
value
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read_struct_field<T>(&mut self,
|
|
|
|
name: &str,
|
|
|
|
idx: uint,
|
2013-11-18 23:54:13 -06:00
|
|
|
f: |&mut Decoder| -> T)
|
2013-05-01 19:54:54 -05:00
|
|
|
-> T {
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("read_struct_field(name={}, idx={})", name, idx);
|
2013-05-01 19:54:54 -05:00
|
|
|
match self.stack.pop() {
|
2013-11-23 17:17:34 -06:00
|
|
|
Object(mut obj) => {
|
2013-05-01 19:54:54 -05:00
|
|
|
let value = match obj.pop(&name.to_owned()) {
|
2013-11-23 17:17:34 -06:00
|
|
|
None => self.missing_field(name, obj),
|
2013-05-01 19:54:54 -05:00
|
|
|
Some(json) => {
|
|
|
|
self.stack.push(json);
|
|
|
|
f(self)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
self.stack.push(Object(obj));
|
|
|
|
value
|
|
|
|
}
|
2013-11-23 17:17:34 -06:00
|
|
|
value => self.expected("object", &value)
|
2013-05-01 19:54:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-18 23:54:13 -06:00
|
|
|
fn read_tuple<T>(&mut self, f: |&mut Decoder, uint| -> T) -> T {
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("read_tuple()");
|
2013-05-01 19:54:54 -05:00
|
|
|
self.read_seq(f)
|
|
|
|
}
|
|
|
|
|
2013-11-18 23:54:13 -06:00
|
|
|
fn read_tuple_arg<T>(&mut self, idx: uint, f: |&mut Decoder| -> T) -> T {
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("read_tuple_arg(idx={})", idx);
|
2013-05-01 19:54:54 -05:00
|
|
|
self.read_seq_elt(idx, f)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read_tuple_struct<T>(&mut self,
|
|
|
|
name: &str,
|
2013-11-18 23:54:13 -06:00
|
|
|
f: |&mut Decoder, uint| -> T)
|
2013-05-01 19:54:54 -05:00
|
|
|
-> T {
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("read_tuple_struct(name={})", name);
|
2013-05-01 19:54:54 -05:00
|
|
|
self.read_tuple(f)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read_tuple_struct_arg<T>(&mut self,
|
|
|
|
idx: uint,
|
2013-11-18 23:54:13 -06:00
|
|
|
f: |&mut Decoder| -> T)
|
2013-05-01 19:54:54 -05:00
|
|
|
-> T {
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("read_tuple_struct_arg(idx={})", idx);
|
2013-05-01 19:54:54 -05:00
|
|
|
self.read_tuple_arg(idx, f)
|
|
|
|
}
|
|
|
|
|
2013-11-18 23:54:13 -06:00
|
|
|
fn read_option<T>(&mut self, f: |&mut Decoder, bool| -> T) -> T {
|
2013-05-01 19:54:54 -05:00
|
|
|
match self.stack.pop() {
|
|
|
|
Null => f(self, false),
|
|
|
|
value => { self.stack.push(value); f(self, true) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-18 23:54:13 -06:00
|
|
|
fn read_seq<T>(&mut self, f: |&mut Decoder, uint| -> T) -> T {
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("read_seq()");
|
2013-05-01 19:54:54 -05:00
|
|
|
let len = match self.stack.pop() {
|
|
|
|
List(list) => {
|
|
|
|
let len = list.len();
|
2013-08-07 21:21:36 -05:00
|
|
|
for v in list.move_rev_iter() {
|
2013-05-01 19:54:54 -05:00
|
|
|
self.stack.push(v);
|
|
|
|
}
|
|
|
|
len
|
|
|
|
}
|
2013-11-23 17:17:34 -06:00
|
|
|
value => self.expected("list", &value)
|
2013-05-01 19:54:54 -05:00
|
|
|
};
|
|
|
|
f(self, len)
|
|
|
|
}
|
|
|
|
|
2013-11-18 23:54:13 -06:00
|
|
|
fn read_seq_elt<T>(&mut self, idx: uint, f: |&mut Decoder| -> T) -> T {
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("read_seq_elt(idx={})", idx);
|
2013-05-01 19:54:54 -05:00
|
|
|
f(self)
|
|
|
|
}
|
|
|
|
|
2013-11-18 23:54:13 -06:00
|
|
|
fn read_map<T>(&mut self, f: |&mut Decoder, uint| -> T) -> T {
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("read_map()");
|
2013-05-01 19:54:54 -05:00
|
|
|
let len = match self.stack.pop() {
|
|
|
|
Object(obj) => {
|
|
|
|
let len = obj.len();
|
2013-08-07 21:21:36 -05:00
|
|
|
for (key, value) in obj.move_iter() {
|
2013-05-01 19:54:54 -05:00
|
|
|
self.stack.push(value);
|
|
|
|
self.stack.push(String(key));
|
|
|
|
}
|
|
|
|
len
|
|
|
|
}
|
2013-11-23 17:17:34 -06:00
|
|
|
value => self.expected("object", &value)
|
2013-05-01 19:54:54 -05:00
|
|
|
};
|
|
|
|
f(self, len)
|
|
|
|
}
|
|
|
|
|
2013-11-18 23:54:13 -06:00
|
|
|
fn read_map_elt_key<T>(&mut self, idx: uint, f: |&mut Decoder| -> T)
|
2013-05-01 19:54:54 -05:00
|
|
|
-> T {
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("read_map_elt_key(idx={})", idx);
|
2013-05-01 19:54:54 -05:00
|
|
|
f(self)
|
|
|
|
}
|
|
|
|
|
2013-11-18 23:54:13 -06:00
|
|
|
fn read_map_elt_val<T>(&mut self, idx: uint, f: |&mut Decoder| -> T)
|
2013-05-01 19:54:54 -05:00
|
|
|
-> T {
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("read_map_elt_val(idx={})", idx);
|
2013-05-01 19:54:54 -05:00
|
|
|
f(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-24 11:55:42 -05:00
|
|
|
/// Test if two json values are less than one another
|
2013-02-14 13:47:00 -06:00
|
|
|
impl Ord for Json {
|
2013-03-21 23:34:30 -05:00
|
|
|
fn lt(&self, other: &Json) -> bool {
|
2012-11-14 20:59:30 -06:00
|
|
|
match (*self) {
|
|
|
|
Number(f0) => {
|
|
|
|
match *other {
|
|
|
|
Number(f1) => f0 < f1,
|
|
|
|
String(_) | Boolean(_) | List(_) | Object(_) |
|
|
|
|
Null => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
String(ref s0) => {
|
|
|
|
match *other {
|
|
|
|
Number(_) => false,
|
|
|
|
String(ref s1) => s0 < s1,
|
|
|
|
Boolean(_) | List(_) | Object(_) | Null => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Boolean(b0) => {
|
|
|
|
match *other {
|
|
|
|
Number(_) | String(_) => false,
|
|
|
|
Boolean(b1) => b0 < b1,
|
|
|
|
List(_) | Object(_) | Null => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-07 21:34:57 -06:00
|
|
|
List(ref l0) => {
|
2012-11-14 20:59:30 -06:00
|
|
|
match *other {
|
|
|
|
Number(_) | String(_) | Boolean(_) => false,
|
2012-12-07 21:34:57 -06:00
|
|
|
List(ref l1) => (*l0) < (*l1),
|
2012-11-14 20:59:30 -06:00
|
|
|
Object(_) | Null => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Object(ref d0) => {
|
|
|
|
match *other {
|
|
|
|
Number(_) | String(_) | Boolean(_) | List(_) => false,
|
2013-08-05 12:37:53 -05:00
|
|
|
Object(ref d1) => d0 < d1,
|
2012-11-14 20:59:30 -06:00
|
|
|
Null => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Null => {
|
|
|
|
match *other {
|
|
|
|
Number(_) | String(_) | Boolean(_) | List(_) |
|
|
|
|
Object(_) =>
|
|
|
|
false,
|
|
|
|
Null => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-09-19 20:00:26 -05:00
|
|
|
}
|
2012-09-07 20:53:14 -05:00
|
|
|
|
2013-05-28 22:11:41 -05:00
|
|
|
/// A trait for converting values to JSON
|
2013-07-10 18:17:41 -05:00
|
|
|
pub trait ToJson {
|
2013-05-28 22:11:41 -05:00
|
|
|
/// Converts the value of `self` to an instance of JSON
|
|
|
|
fn to_json(&self) -> Json;
|
|
|
|
}
|
2012-05-28 14:10:32 -05:00
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl ToJson for Json {
|
2013-07-02 14:47:32 -05:00
|
|
|
fn to_json(&self) -> Json { (*self).clone() }
|
2012-05-28 14:10:32 -05:00
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl ToJson for int {
|
2013-09-26 01:26:09 -05:00
|
|
|
fn to_json(&self) -> Json { Number(*self as f64) }
|
2012-06-13 10:30:54 -05:00
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl ToJson for i8 {
|
2013-09-26 01:26:09 -05:00
|
|
|
fn to_json(&self) -> Json { Number(*self as f64) }
|
2012-05-28 14:10:32 -05:00
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl ToJson for i16 {
|
2013-09-26 01:26:09 -05:00
|
|
|
fn to_json(&self) -> Json { Number(*self as f64) }
|
2012-05-28 14:10:32 -05:00
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl ToJson for i32 {
|
2013-09-26 01:26:09 -05:00
|
|
|
fn to_json(&self) -> Json { Number(*self as f64) }
|
2012-05-28 14:10:32 -05:00
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl ToJson for i64 {
|
2013-09-26 01:26:09 -05:00
|
|
|
fn to_json(&self) -> Json { Number(*self as f64) }
|
2012-05-28 14:10:32 -05:00
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl ToJson for uint {
|
2013-09-26 01:26:09 -05:00
|
|
|
fn to_json(&self) -> Json { Number(*self as f64) }
|
2012-06-13 10:30:54 -05:00
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl ToJson for u8 {
|
2013-09-26 01:26:09 -05:00
|
|
|
fn to_json(&self) -> Json { Number(*self as f64) }
|
2012-05-28 14:10:32 -05:00
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl ToJson for u16 {
|
2013-09-26 01:26:09 -05:00
|
|
|
fn to_json(&self) -> Json { Number(*self as f64) }
|
2012-05-28 14:10:32 -05:00
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl ToJson for u32 {
|
2013-09-26 01:26:09 -05:00
|
|
|
fn to_json(&self) -> Json { Number(*self as f64) }
|
2012-05-28 14:10:32 -05:00
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl ToJson for u64 {
|
2013-09-26 01:26:09 -05:00
|
|
|
fn to_json(&self) -> Json { Number(*self as f64) }
|
2012-05-28 14:10:32 -05:00
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl ToJson for f32 {
|
2013-09-26 01:26:09 -05:00
|
|
|
fn to_json(&self) -> Json { Number(*self as f64) }
|
2012-05-28 14:10:32 -05:00
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl ToJson for f64 {
|
2013-09-26 01:26:09 -05:00
|
|
|
fn to_json(&self) -> Json { Number(*self) }
|
2012-05-28 14:10:32 -05:00
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl ToJson for () {
|
2013-03-07 20:11:09 -06:00
|
|
|
fn to_json(&self) -> Json { Null }
|
2012-05-28 14:10:32 -05:00
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl ToJson for bool {
|
2013-03-07 20:11:09 -06:00
|
|
|
fn to_json(&self) -> Json { Boolean(*self) }
|
2012-05-28 14:10:32 -05:00
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl ToJson for ~str {
|
2013-07-02 14:47:32 -05:00
|
|
|
fn to_json(&self) -> Json { String((*self).clone()) }
|
2012-06-12 19:20:51 -05:00
|
|
|
}
|
|
|
|
|
2013-02-20 19:07:17 -06:00
|
|
|
impl<A:ToJson,B:ToJson> ToJson for (A, B) {
|
2013-03-07 20:11:09 -06:00
|
|
|
fn to_json(&self) -> Json {
|
|
|
|
match *self {
|
2012-09-28 02:22:18 -05:00
|
|
|
(ref a, ref b) => {
|
2012-09-24 11:55:42 -05:00
|
|
|
List(~[a.to_json(), b.to_json()])
|
2012-08-02 17:42:56 -05:00
|
|
|
}
|
|
|
|
}
|
2012-05-28 14:10:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-20 19:07:17 -06:00
|
|
|
impl<A:ToJson,B:ToJson,C:ToJson> ToJson for (A, B, C) {
|
2013-03-07 20:11:09 -06:00
|
|
|
fn to_json(&self) -> Json {
|
|
|
|
match *self {
|
2012-09-28 02:22:18 -05:00
|
|
|
(ref a, ref b, ref c) => {
|
2012-09-24 11:55:42 -05:00
|
|
|
List(~[a.to_json(), b.to_json(), c.to_json()])
|
2012-08-02 17:42:56 -05:00
|
|
|
}
|
|
|
|
}
|
2012-05-28 14:10:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-20 19:07:17 -06:00
|
|
|
impl<A:ToJson> ToJson for ~[A] {
|
2013-03-07 20:11:09 -06:00
|
|
|
fn to_json(&self) -> Json { List(self.map(|elt| elt.to_json())) }
|
2012-05-28 14:10:32 -05:00
|
|
|
}
|
|
|
|
|
2013-07-18 14:37:40 -05:00
|
|
|
impl<A:ToJson> ToJson for TreeMap<~str, A> {
|
2013-03-07 20:11:09 -06:00
|
|
|
fn to_json(&self) -> Json {
|
2013-07-18 14:37:40 -05:00
|
|
|
let mut d = TreeMap::new();
|
2013-08-03 11:45:23 -05:00
|
|
|
for (key, value) in self.iter() {
|
2013-07-02 14:47:32 -05:00
|
|
|
d.insert((*key).clone(), value.to_json());
|
2012-09-24 11:55:42 -05:00
|
|
|
}
|
|
|
|
Object(~d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-18 14:37:40 -05:00
|
|
|
impl<A:ToJson> ToJson for HashMap<~str, A> {
|
2013-07-08 18:21:09 -05:00
|
|
|
fn to_json(&self) -> Json {
|
2013-07-18 14:37:40 -05:00
|
|
|
let mut d = TreeMap::new();
|
2013-08-03 11:45:23 -05:00
|
|
|
for (key, value) in self.iter() {
|
2013-07-02 14:47:32 -05:00
|
|
|
d.insert((*key).clone(), value.to_json());
|
2013-07-08 18:21:09 -05:00
|
|
|
}
|
|
|
|
Object(~d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-20 19:07:17 -06:00
|
|
|
impl<A:ToJson> ToJson for Option<A> {
|
2013-03-07 20:11:09 -06:00
|
|
|
fn to_json(&self) -> Json {
|
|
|
|
match *self {
|
2012-08-11 09:08:42 -05:00
|
|
|
None => Null,
|
2012-09-28 02:22:18 -05:00
|
|
|
Some(ref value) => value.to_json()
|
2012-05-28 14:10:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl to_str::ToStr for Json {
|
2013-08-22 00:32:18 -05:00
|
|
|
/// Encodes a json value into a string
|
|
|
|
fn to_str(&self) -> ~str {
|
2013-11-29 23:26:03 -06:00
|
|
|
let mut s = MemWriter::new();
|
|
|
|
self.to_writer(&mut s as &mut io::Writer);
|
|
|
|
str::from_utf8_owned(s.inner())
|
2013-08-22 00:32:18 -05:00
|
|
|
}
|
2012-05-28 14:10:32 -05:00
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl to_str::ToStr for Error {
|
2013-03-21 23:34:30 -05:00
|
|
|
fn to_str(&self) -> ~str {
|
2013-11-28 16:50:21 -06:00
|
|
|
format!("{}:{}: {}", self.line, self.col, self.msg)
|
2012-06-11 10:32:38 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-17 21:05:07 -06:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2013-05-21 19:24:31 -05:00
|
|
|
|
2013-03-26 19:46:29 -05:00
|
|
|
use super::*;
|
2013-01-08 21:37:25 -06:00
|
|
|
|
2013-11-11 00:46:32 -06:00
|
|
|
use std::io;
|
2013-07-18 14:37:40 -05:00
|
|
|
use serialize::Decodable;
|
|
|
|
use treemap::TreeMap;
|
2013-03-27 02:13:01 -05:00
|
|
|
|
2013-05-15 17:55:57 -05:00
|
|
|
#[deriving(Eq, Encodable, Decodable)]
|
2013-03-30 13:08:57 -05:00
|
|
|
enum Animal {
|
|
|
|
Dog,
|
|
|
|
Frog(~str, int)
|
|
|
|
}
|
|
|
|
|
2013-05-15 17:55:57 -05:00
|
|
|
#[deriving(Eq, Encodable, Decodable)]
|
2013-03-30 15:31:03 -05:00
|
|
|
struct Inner {
|
|
|
|
a: (),
|
|
|
|
b: uint,
|
|
|
|
c: ~[~str],
|
|
|
|
}
|
|
|
|
|
2013-05-15 17:55:57 -05:00
|
|
|
#[deriving(Eq, Encodable, Decodable)]
|
2013-03-30 15:31:03 -05:00
|
|
|
struct Outer {
|
|
|
|
inner: ~[Inner],
|
|
|
|
}
|
|
|
|
|
2012-09-24 11:55:42 -05:00
|
|
|
fn mk_object(items: &[(~str, Json)]) -> Json {
|
2013-07-18 14:37:40 -05:00
|
|
|
let mut d = ~TreeMap::new();
|
2012-02-25 18:39:32 -06:00
|
|
|
|
2013-08-03 11:45:23 -05:00
|
|
|
for item in items.iter() {
|
2012-09-24 11:55:42 -05:00
|
|
|
match *item {
|
2013-07-02 14:47:32 -05:00
|
|
|
(ref key, ref value) => { d.insert((*key).clone(), (*value).clone()); },
|
2012-09-24 11:55:42 -05:00
|
|
|
}
|
2012-02-25 18:39:32 -06:00
|
|
|
};
|
|
|
|
|
2013-02-15 01:30:30 -06:00
|
|
|
Object(d)
|
2012-02-25 18:39:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_write_null() {
|
2013-08-22 00:32:18 -05:00
|
|
|
assert_eq!(Null.to_str(), ~"null");
|
|
|
|
assert_eq!(Null.to_pretty_str(), ~"null");
|
2012-02-25 18:39:32 -06:00
|
|
|
}
|
|
|
|
|
2013-03-30 13:08:57 -05:00
|
|
|
|
2012-02-25 18:39:32 -06:00
|
|
|
#[test]
|
2012-09-24 11:55:42 -05:00
|
|
|
fn test_write_number() {
|
2013-09-26 01:26:09 -05:00
|
|
|
assert_eq!(Number(3.0).to_str(), ~"3");
|
|
|
|
assert_eq!(Number(3.0).to_pretty_str(), ~"3");
|
2013-03-30 13:08:57 -05:00
|
|
|
|
2013-09-26 01:26:09 -05:00
|
|
|
assert_eq!(Number(3.1).to_str(), ~"3.1");
|
|
|
|
assert_eq!(Number(3.1).to_pretty_str(), ~"3.1");
|
2013-03-30 13:08:57 -05:00
|
|
|
|
2013-09-26 01:26:09 -05:00
|
|
|
assert_eq!(Number(-1.5).to_str(), ~"-1.5");
|
|
|
|
assert_eq!(Number(-1.5).to_pretty_str(), ~"-1.5");
|
2013-03-30 13:08:57 -05:00
|
|
|
|
2013-09-26 01:26:09 -05:00
|
|
|
assert_eq!(Number(0.5).to_str(), ~"0.5");
|
|
|
|
assert_eq!(Number(0.5).to_pretty_str(), ~"0.5");
|
2012-02-25 18:39:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_write_str() {
|
2013-08-22 00:32:18 -05:00
|
|
|
assert_eq!(String(~"").to_str(), ~"\"\"");
|
|
|
|
assert_eq!(String(~"").to_pretty_str(), ~"\"\"");
|
2013-03-30 13:08:57 -05:00
|
|
|
|
2013-08-22 00:32:18 -05:00
|
|
|
assert_eq!(String(~"foo").to_str(), ~"\"foo\"");
|
|
|
|
assert_eq!(String(~"foo").to_pretty_str(), ~"\"foo\"");
|
2012-02-25 18:39:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_write_bool() {
|
2013-08-22 00:32:18 -05:00
|
|
|
assert_eq!(Boolean(true).to_str(), ~"true");
|
|
|
|
assert_eq!(Boolean(true).to_pretty_str(), ~"true");
|
2013-03-30 13:08:57 -05:00
|
|
|
|
2013-08-22 00:32:18 -05:00
|
|
|
assert_eq!(Boolean(false).to_str(), ~"false");
|
|
|
|
assert_eq!(Boolean(false).to_pretty_str(), ~"false");
|
2012-02-25 18:39:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_write_list() {
|
2013-08-22 00:32:18 -05:00
|
|
|
assert_eq!(List(~[]).to_str(), ~"[]");
|
|
|
|
assert_eq!(List(~[]).to_pretty_str(), ~"[]");
|
2013-03-30 13:08:57 -05:00
|
|
|
|
2013-08-22 00:32:18 -05:00
|
|
|
assert_eq!(List(~[Boolean(true)]).to_str(), ~"[true]");
|
2013-03-26 19:34:49 -05:00
|
|
|
assert_eq!(
|
2013-08-22 00:32:18 -05:00
|
|
|
List(~[Boolean(true)]).to_pretty_str(),
|
2013-03-26 19:34:49 -05:00
|
|
|
~"\
|
|
|
|
[\n \
|
|
|
|
true\n\
|
|
|
|
]"
|
|
|
|
);
|
2013-03-30 13:08:57 -05:00
|
|
|
|
2013-08-22 00:32:18 -05:00
|
|
|
let longTestList = List(~[
|
2013-03-30 13:08:57 -05:00
|
|
|
Boolean(false),
|
|
|
|
Null,
|
2013-09-26 01:26:09 -05:00
|
|
|
List(~[String(~"foo\nbar"), Number(3.5)])]);
|
2013-08-22 00:32:18 -05:00
|
|
|
|
|
|
|
assert_eq!(longTestList.to_str(),
|
|
|
|
~"[false,null,[\"foo\\nbar\",3.5]]");
|
2013-03-26 19:34:49 -05:00
|
|
|
assert_eq!(
|
2013-08-22 00:32:18 -05:00
|
|
|
longTestList.to_pretty_str(),
|
2013-03-26 19:34:49 -05:00
|
|
|
~"\
|
|
|
|
[\n \
|
|
|
|
false,\n \
|
|
|
|
null,\n \
|
|
|
|
[\n \
|
|
|
|
\"foo\\nbar\",\n \
|
|
|
|
3.5\n \
|
|
|
|
]\n\
|
|
|
|
]"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-02-25 18:39:32 -06:00
|
|
|
#[test]
|
2012-09-24 11:55:42 -05:00
|
|
|
fn test_write_object() {
|
2013-08-22 00:32:18 -05:00
|
|
|
assert_eq!(mk_object([]).to_str(), ~"{}");
|
|
|
|
assert_eq!(mk_object([]).to_pretty_str(), ~"{}");
|
2013-03-30 13:08:57 -05:00
|
|
|
|
2013-03-26 19:23:00 -05:00
|
|
|
assert_eq!(
|
2013-08-22 00:32:18 -05:00
|
|
|
mk_object([(~"a", Boolean(true))]).to_str(),
|
2013-03-26 19:23:00 -05:00
|
|
|
~"{\"a\":true}"
|
|
|
|
);
|
2013-03-30 13:08:57 -05:00
|
|
|
assert_eq!(
|
2013-08-22 00:32:18 -05:00
|
|
|
mk_object([(~"a", Boolean(true))]).to_pretty_str(),
|
2013-03-30 13:08:57 -05:00
|
|
|
~"\
|
|
|
|
{\n \
|
|
|
|
\"a\": true\n\
|
|
|
|
}"
|
|
|
|
);
|
|
|
|
|
2013-08-22 00:32:18 -05:00
|
|
|
let complexObj = mk_object([
|
2013-03-26 19:34:49 -05:00
|
|
|
(~"b", List(~[
|
2013-05-23 11:39:00 -05:00
|
|
|
mk_object([(~"c", String(~"\x0c\r"))]),
|
|
|
|
mk_object([(~"d", String(~""))])
|
2013-03-26 19:34:49 -05:00
|
|
|
]))
|
2013-08-22 00:32:18 -05:00
|
|
|
]);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
complexObj.to_str(),
|
2013-03-26 19:34:49 -05:00
|
|
|
~"{\
|
|
|
|
\"b\":[\
|
|
|
|
{\"c\":\"\\f\\r\"},\
|
|
|
|
{\"d\":\"\"}\
|
|
|
|
]\
|
|
|
|
}"
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2013-08-22 00:32:18 -05:00
|
|
|
complexObj.to_pretty_str(),
|
2013-03-26 19:34:49 -05:00
|
|
|
~"\
|
|
|
|
{\n \
|
|
|
|
\"b\": [\n \
|
|
|
|
{\n \
|
|
|
|
\"c\": \"\\f\\r\"\n \
|
|
|
|
},\n \
|
|
|
|
{\n \
|
|
|
|
\"d\": \"\"\n \
|
|
|
|
}\n \
|
|
|
|
]\n\
|
|
|
|
}"
|
|
|
|
);
|
2013-03-30 13:08:57 -05:00
|
|
|
|
2013-05-23 11:39:00 -05:00
|
|
|
let a = mk_object([
|
2012-08-11 09:08:42 -05:00
|
|
|
(~"a", Boolean(true)),
|
2012-09-24 11:55:42 -05:00
|
|
|
(~"b", List(~[
|
2013-05-23 11:39:00 -05:00
|
|
|
mk_object([(~"c", String(~"\x0c\r"))]),
|
|
|
|
mk_object([(~"d", String(~""))])
|
2012-06-29 18:26:56 -05:00
|
|
|
]))
|
2012-08-30 18:27:15 -05:00
|
|
|
]);
|
2013-03-30 13:08:57 -05:00
|
|
|
|
2012-09-24 11:55:42 -05:00
|
|
|
// We can't compare the strings directly because the object fields be
|
|
|
|
// printed in a different order.
|
2013-08-22 00:32:18 -05:00
|
|
|
assert_eq!(a.clone(), from_str(a.to_str()).unwrap());
|
|
|
|
assert_eq!(a.clone(), from_str(a.to_pretty_str()).unwrap());
|
2013-03-26 19:34:49 -05:00
|
|
|
}
|
|
|
|
|
2013-11-29 23:26:03 -06:00
|
|
|
fn with_str_writer(f: |&mut io::Writer|) -> ~str {
|
2013-11-11 00:46:32 -06:00
|
|
|
use std::io::mem::MemWriter;
|
|
|
|
use std::io::Decorator;
|
2013-10-13 20:48:47 -05:00
|
|
|
use std::str;
|
|
|
|
|
2013-11-29 23:26:03 -06:00
|
|
|
let mut m = MemWriter::new();
|
|
|
|
f(&mut m as &mut io::Writer);
|
|
|
|
str::from_utf8_owned(m.inner())
|
2013-10-13 20:48:47 -05:00
|
|
|
}
|
|
|
|
|
2013-03-26 19:34:49 -05:00
|
|
|
#[test]
|
2013-03-30 13:08:57 -05:00
|
|
|
fn test_write_enum() {
|
2013-03-26 19:34:49 -05:00
|
|
|
let animal = Dog;
|
2013-03-30 13:08:57 -05:00
|
|
|
assert_eq!(
|
2013-11-21 21:20:48 -06:00
|
|
|
with_str_writer(|wr| {
|
2013-12-03 21:18:35 -06:00
|
|
|
let mut encoder = Encoder::new(wr);
|
2013-05-01 19:54:54 -05:00
|
|
|
animal.encode(&mut encoder);
|
2013-11-21 21:20:48 -06:00
|
|
|
}),
|
2013-03-30 13:08:57 -05:00
|
|
|
~"\"Dog\""
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2013-11-21 21:20:48 -06:00
|
|
|
with_str_writer(|wr| {
|
2013-12-03 21:18:35 -06:00
|
|
|
let mut encoder = PrettyEncoder::new(wr);
|
2013-05-01 19:54:54 -05:00
|
|
|
animal.encode(&mut encoder);
|
2013-11-21 21:20:48 -06:00
|
|
|
}),
|
2013-03-30 13:08:57 -05:00
|
|
|
~"\"Dog\""
|
|
|
|
);
|
2013-03-26 19:34:49 -05:00
|
|
|
|
|
|
|
let animal = Frog(~"Henry", 349);
|
|
|
|
assert_eq!(
|
2013-11-21 21:20:48 -06:00
|
|
|
with_str_writer(|wr| {
|
2013-12-03 21:18:35 -06:00
|
|
|
let mut encoder = Encoder::new(wr);
|
2013-05-01 19:54:54 -05:00
|
|
|
animal.encode(&mut encoder);
|
2013-11-21 21:20:48 -06:00
|
|
|
}),
|
2013-08-09 19:05:24 -05:00
|
|
|
~"{\"variant\":\"Frog\",\"fields\":[\"Henry\",349]}"
|
2013-03-30 13:08:57 -05:00
|
|
|
);
|
|
|
|
assert_eq!(
|
2013-11-21 21:20:48 -06:00
|
|
|
with_str_writer(|wr| {
|
2013-12-03 21:18:35 -06:00
|
|
|
let mut encoder = PrettyEncoder::new(wr);
|
2013-05-01 19:54:54 -05:00
|
|
|
animal.encode(&mut encoder);
|
2013-11-21 21:20:48 -06:00
|
|
|
}),
|
2013-03-26 19:34:49 -05:00
|
|
|
~"\
|
|
|
|
[\n \
|
|
|
|
\"Frog\",\n \
|
2013-03-27 02:14:52 -05:00
|
|
|
\"Henry\",\n \
|
|
|
|
349\n\
|
2013-03-26 19:34:49 -05:00
|
|
|
]"
|
|
|
|
);
|
2013-02-08 17:36:40 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-03-26 19:23:00 -05:00
|
|
|
fn test_write_some() {
|
|
|
|
let value = Some(~"jodhpurs");
|
2013-11-21 21:20:48 -06:00
|
|
|
let s = with_str_writer(|wr| {
|
2013-12-03 21:18:35 -06:00
|
|
|
let mut encoder = Encoder::new(wr);
|
2013-05-01 19:54:54 -05:00
|
|
|
value.encode(&mut encoder);
|
2013-11-21 21:20:48 -06:00
|
|
|
});
|
2013-03-26 19:23:00 -05:00
|
|
|
assert_eq!(s, ~"\"jodhpurs\"");
|
2013-02-08 17:36:40 -06:00
|
|
|
|
2013-03-26 19:34:49 -05:00
|
|
|
let value = Some(~"jodhpurs");
|
2013-11-21 21:20:48 -06:00
|
|
|
let s = with_str_writer(|wr| {
|
2013-12-03 21:18:35 -06:00
|
|
|
let mut encoder = PrettyEncoder::new(wr);
|
2013-05-01 19:54:54 -05:00
|
|
|
value.encode(&mut encoder);
|
2013-11-21 21:20:48 -06:00
|
|
|
});
|
2013-03-26 19:34:49 -05:00
|
|
|
assert_eq!(s, ~"\"jodhpurs\"");
|
|
|
|
}
|
|
|
|
|
2013-02-08 17:36:40 -06:00
|
|
|
#[test]
|
2013-03-26 19:23:00 -05:00
|
|
|
fn test_write_none() {
|
|
|
|
let value: Option<~str> = None;
|
2013-11-21 21:20:48 -06:00
|
|
|
let s = with_str_writer(|wr| {
|
2013-12-03 21:18:35 -06:00
|
|
|
let mut encoder = Encoder::new(wr);
|
2013-05-01 19:54:54 -05:00
|
|
|
value.encode(&mut encoder);
|
2013-11-21 21:20:48 -06:00
|
|
|
});
|
2013-03-26 19:23:00 -05:00
|
|
|
assert_eq!(s, ~"null");
|
2013-02-07 19:06:26 -06:00
|
|
|
|
2013-11-21 21:20:48 -06:00
|
|
|
let s = with_str_writer(|wr| {
|
2013-12-03 21:18:35 -06:00
|
|
|
let mut encoder = Encoder::new(wr);
|
2013-05-01 19:54:54 -05:00
|
|
|
value.encode(&mut encoder);
|
2013-11-21 21:20:48 -06:00
|
|
|
});
|
2013-03-26 19:34:49 -05:00
|
|
|
assert_eq!(s, ~"null");
|
|
|
|
}
|
|
|
|
|
2012-01-17 21:05:07 -06:00
|
|
|
#[test]
|
2012-02-25 18:39:32 -06:00
|
|
|
fn test_trailing_characters() {
|
2013-05-23 11:39:00 -05:00
|
|
|
assert_eq!(from_str("nulla"),
|
2013-11-28 16:50:21 -06:00
|
|
|
Err(Error {line: 1u, col: 5u, msg: ~"trailing characters"}));
|
2013-05-23 11:39:00 -05:00
|
|
|
assert_eq!(from_str("truea"),
|
2013-11-28 16:50:21 -06:00
|
|
|
Err(Error {line: 1u, col: 5u, msg: ~"trailing characters"}));
|
2013-05-23 11:39:00 -05:00
|
|
|
assert_eq!(from_str("falsea"),
|
2013-11-28 16:50:21 -06:00
|
|
|
Err(Error {line: 1u, col: 6u, msg: ~"trailing characters"}));
|
2013-05-23 11:39:00 -05:00
|
|
|
assert_eq!(from_str("1a"),
|
2013-11-28 16:50:21 -06:00
|
|
|
Err(Error {line: 1u, col: 2u, msg: ~"trailing characters"}));
|
2013-05-23 11:39:00 -05:00
|
|
|
assert_eq!(from_str("[]a"),
|
2013-11-28 16:50:21 -06:00
|
|
|
Err(Error {line: 1u, col: 3u, msg: ~"trailing characters"}));
|
2013-05-23 11:39:00 -05:00
|
|
|
assert_eq!(from_str("{}a"),
|
2013-11-28 16:50:21 -06:00
|
|
|
Err(Error {line: 1u, col: 3u, msg: ~"trailing characters"}));
|
2012-02-25 18:39:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_read_identifiers() {
|
2013-05-23 11:39:00 -05:00
|
|
|
assert_eq!(from_str("n"),
|
2013-11-28 16:50:21 -06:00
|
|
|
Err(Error {line: 1u, col: 2u, msg: ~"invalid syntax"}));
|
2013-05-23 11:39:00 -05:00
|
|
|
assert_eq!(from_str("nul"),
|
2013-11-28 16:50:21 -06:00
|
|
|
Err(Error {line: 1u, col: 4u, msg: ~"invalid syntax"}));
|
2012-02-25 18:39:32 -06:00
|
|
|
|
2013-05-23 11:39:00 -05:00
|
|
|
assert_eq!(from_str("t"),
|
2013-11-28 16:50:21 -06:00
|
|
|
Err(Error {line: 1u, col: 2u, msg: ~"invalid syntax"}));
|
2013-05-23 11:39:00 -05:00
|
|
|
assert_eq!(from_str("truz"),
|
2013-11-28 16:50:21 -06:00
|
|
|
Err(Error {line: 1u, col: 4u, msg: ~"invalid syntax"}));
|
2012-02-25 18:39:32 -06:00
|
|
|
|
2013-05-23 11:39:00 -05:00
|
|
|
assert_eq!(from_str("f"),
|
2013-11-28 16:50:21 -06:00
|
|
|
Err(Error {line: 1u, col: 2u, msg: ~"invalid syntax"}));
|
2013-05-23 11:39:00 -05:00
|
|
|
assert_eq!(from_str("faz"),
|
2013-11-28 16:50:21 -06:00
|
|
|
Err(Error {line: 1u, col: 3u, msg: ~"invalid syntax"}));
|
2012-02-25 18:39:32 -06:00
|
|
|
|
2013-05-23 11:39:00 -05:00
|
|
|
assert_eq!(from_str("null"), Ok(Null));
|
|
|
|
assert_eq!(from_str("true"), Ok(Boolean(true)));
|
|
|
|
assert_eq!(from_str("false"), Ok(Boolean(false)));
|
|
|
|
assert_eq!(from_str(" null "), Ok(Null));
|
|
|
|
assert_eq!(from_str(" true "), Ok(Boolean(true)));
|
|
|
|
assert_eq!(from_str(" false "), Ok(Boolean(false)));
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
|
2013-03-30 15:31:03 -05:00
|
|
|
#[test]
|
|
|
|
fn test_decode_identifiers() {
|
2013-12-03 21:18:35 -06:00
|
|
|
let mut decoder = Decoder::new(from_str("null").unwrap());
|
2013-05-01 19:54:54 -05:00
|
|
|
let v: () = Decodable::decode(&mut decoder);
|
2013-03-30 15:31:03 -05:00
|
|
|
assert_eq!(v, ());
|
|
|
|
|
2013-12-03 21:18:35 -06:00
|
|
|
let mut decoder = Decoder::new(from_str("true").unwrap());
|
2013-05-01 19:54:54 -05:00
|
|
|
let v: bool = Decodable::decode(&mut decoder);
|
2013-03-30 15:31:03 -05:00
|
|
|
assert_eq!(v, true);
|
|
|
|
|
2013-12-03 21:18:35 -06:00
|
|
|
let mut decoder = Decoder::new(from_str("false").unwrap());
|
2013-05-01 19:54:54 -05:00
|
|
|
let v: bool = Decodable::decode(&mut decoder);
|
2013-03-30 15:31:03 -05:00
|
|
|
assert_eq!(v, false);
|
|
|
|
}
|
|
|
|
|
2012-01-17 21:05:07 -06:00
|
|
|
#[test]
|
2012-09-24 11:55:42 -05:00
|
|
|
fn test_read_number() {
|
2013-05-23 11:39:00 -05:00
|
|
|
assert_eq!(from_str("+"),
|
2013-11-28 16:50:21 -06:00
|
|
|
Err(Error {line: 1u, col: 1u, msg: ~"invalid syntax"}));
|
2013-05-23 11:39:00 -05:00
|
|
|
assert_eq!(from_str("."),
|
2013-11-28 16:50:21 -06:00
|
|
|
Err(Error {line: 1u, col: 1u, msg: ~"invalid syntax"}));
|
2013-03-06 15:58:02 -06:00
|
|
|
|
2013-05-23 11:39:00 -05:00
|
|
|
assert_eq!(from_str("-"),
|
2013-11-28 16:50:21 -06:00
|
|
|
Err(Error {line: 1u, col: 2u, msg: ~"invalid number"}));
|
2013-05-23 11:39:00 -05:00
|
|
|
assert_eq!(from_str("00"),
|
2013-11-28 16:50:21 -06:00
|
|
|
Err(Error {line: 1u, col: 2u, msg: ~"invalid number"}));
|
2013-05-23 11:39:00 -05:00
|
|
|
assert_eq!(from_str("1."),
|
2013-11-28 16:50:21 -06:00
|
|
|
Err(Error {line: 1u, col: 3u, msg: ~"invalid number"}));
|
2013-05-23 11:39:00 -05:00
|
|
|
assert_eq!(from_str("1e"),
|
2013-11-28 16:50:21 -06:00
|
|
|
Err(Error {line: 1u, col: 3u, msg: ~"invalid number"}));
|
2013-05-23 11:39:00 -05:00
|
|
|
assert_eq!(from_str("1e+"),
|
2013-11-28 16:50:21 -06:00
|
|
|
Err(Error {line: 1u, col: 4u, msg: ~"invalid number"}));
|
2013-03-06 15:58:02 -06:00
|
|
|
|
2013-09-26 01:26:09 -05:00
|
|
|
assert_eq!(from_str("3"), Ok(Number(3.0)));
|
|
|
|
assert_eq!(from_str("3.1"), Ok(Number(3.1)));
|
|
|
|
assert_eq!(from_str("-1.2"), Ok(Number(-1.2)));
|
|
|
|
assert_eq!(from_str("0.4"), Ok(Number(0.4)));
|
|
|
|
assert_eq!(from_str("0.4e5"), Ok(Number(0.4e5)));
|
|
|
|
assert_eq!(from_str("0.4e+15"), Ok(Number(0.4e15)));
|
|
|
|
assert_eq!(from_str("0.4e-01"), Ok(Number(0.4e-01)));
|
|
|
|
assert_eq!(from_str(" 3 "), Ok(Number(3.0)));
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
|
2013-03-30 15:31:03 -05:00
|
|
|
#[test]
|
|
|
|
fn test_decode_numbers() {
|
2013-12-03 21:18:35 -06:00
|
|
|
let mut decoder = Decoder::new(from_str("3").unwrap());
|
2013-09-26 01:26:09 -05:00
|
|
|
let v: f64 = Decodable::decode(&mut decoder);
|
|
|
|
assert_eq!(v, 3.0);
|
2013-03-30 15:31:03 -05:00
|
|
|
|
2013-12-03 21:18:35 -06:00
|
|
|
let mut decoder = Decoder::new(from_str("3.1").unwrap());
|
2013-09-26 01:26:09 -05:00
|
|
|
let v: f64 = Decodable::decode(&mut decoder);
|
|
|
|
assert_eq!(v, 3.1);
|
2013-03-30 15:31:03 -05:00
|
|
|
|
2013-12-03 21:18:35 -06:00
|
|
|
let mut decoder = Decoder::new(from_str("-1.2").unwrap());
|
2013-09-26 01:26:09 -05:00
|
|
|
let v: f64 = Decodable::decode(&mut decoder);
|
|
|
|
assert_eq!(v, -1.2);
|
2013-03-30 15:31:03 -05:00
|
|
|
|
2013-12-03 21:18:35 -06:00
|
|
|
let mut decoder = Decoder::new(from_str("0.4").unwrap());
|
2013-09-26 01:26:09 -05:00
|
|
|
let v: f64 = Decodable::decode(&mut decoder);
|
|
|
|
assert_eq!(v, 0.4);
|
2013-03-30 15:31:03 -05:00
|
|
|
|
2013-12-03 21:18:35 -06:00
|
|
|
let mut decoder = Decoder::new(from_str("0.4e5").unwrap());
|
2013-09-26 01:26:09 -05:00
|
|
|
let v: f64 = Decodable::decode(&mut decoder);
|
|
|
|
assert_eq!(v, 0.4e5);
|
2013-03-30 15:31:03 -05:00
|
|
|
|
2013-12-03 21:18:35 -06:00
|
|
|
let mut decoder = Decoder::new(from_str("0.4e15").unwrap());
|
2013-09-26 01:26:09 -05:00
|
|
|
let v: f64 = Decodable::decode(&mut decoder);
|
|
|
|
assert_eq!(v, 0.4e15);
|
2013-03-30 15:31:03 -05:00
|
|
|
|
2013-12-03 21:18:35 -06:00
|
|
|
let mut decoder = Decoder::new(from_str("0.4e-01").unwrap());
|
2013-09-26 01:26:09 -05:00
|
|
|
let v: f64 = Decodable::decode(&mut decoder);
|
|
|
|
assert_eq!(v, 0.4e-01);
|
2013-03-30 15:31:03 -05:00
|
|
|
}
|
|
|
|
|
2013-07-10 22:03:10 -05:00
|
|
|
#[test]
|
2012-02-25 18:39:32 -06:00
|
|
|
fn test_read_str() {
|
2013-05-23 11:39:00 -05:00
|
|
|
assert_eq!(from_str("\""),
|
2013-11-28 16:50:21 -06:00
|
|
|
Err(Error {line: 1u, col: 2u, msg: ~"EOF while parsing string"
|
2013-03-06 15:58:02 -06:00
|
|
|
}));
|
2013-05-23 11:39:00 -05:00
|
|
|
assert_eq!(from_str("\"lol"),
|
2013-11-28 16:50:21 -06:00
|
|
|
Err(Error {line: 1u, col: 5u, msg: ~"EOF while parsing string"
|
2013-03-06 15:58:02 -06:00
|
|
|
}));
|
|
|
|
|
2013-05-23 11:39:00 -05:00
|
|
|
assert_eq!(from_str("\"\""), Ok(String(~"")));
|
|
|
|
assert_eq!(from_str("\"foo\""), Ok(String(~"foo")));
|
|
|
|
assert_eq!(from_str("\"\\\"\""), Ok(String(~"\"")));
|
|
|
|
assert_eq!(from_str("\"\\b\""), Ok(String(~"\x08")));
|
|
|
|
assert_eq!(from_str("\"\\n\""), Ok(String(~"\n")));
|
|
|
|
assert_eq!(from_str("\"\\r\""), Ok(String(~"\r")));
|
|
|
|
assert_eq!(from_str("\"\\t\""), Ok(String(~"\t")));
|
|
|
|
assert_eq!(from_str(" \"foo\" "), Ok(String(~"foo")));
|
|
|
|
assert_eq!(from_str("\"\\u12ab\""), Ok(String(~"\u12ab")));
|
|
|
|
assert_eq!(from_str("\"\\uAB12\""), Ok(String(~"\uAB12")));
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
|
2012-08-30 23:03:19 -05:00
|
|
|
#[test]
|
2013-03-30 15:31:03 -05:00
|
|
|
fn test_decode_str() {
|
2013-12-03 21:18:35 -06:00
|
|
|
let mut decoder = Decoder::new(from_str("\"\"").unwrap());
|
2013-05-01 19:54:54 -05:00
|
|
|
let v: ~str = Decodable::decode(&mut decoder);
|
2013-03-30 15:31:03 -05:00
|
|
|
assert_eq!(v, ~"");
|
|
|
|
|
2013-12-03 21:18:35 -06:00
|
|
|
let mut decoder = Decoder::new(from_str("\"foo\"").unwrap());
|
2013-05-01 19:54:54 -05:00
|
|
|
let v: ~str = Decodable::decode(&mut decoder);
|
2013-03-30 15:31:03 -05:00
|
|
|
assert_eq!(v, ~"foo");
|
|
|
|
|
2013-12-03 21:18:35 -06:00
|
|
|
let mut decoder = Decoder::new(from_str("\"\\\"\"").unwrap());
|
2013-05-01 19:54:54 -05:00
|
|
|
let v: ~str = Decodable::decode(&mut decoder);
|
2013-03-30 15:31:03 -05:00
|
|
|
assert_eq!(v, ~"\"");
|
|
|
|
|
2013-12-03 21:18:35 -06:00
|
|
|
let mut decoder = Decoder::new(from_str("\"\\b\"").unwrap());
|
2013-05-01 19:54:54 -05:00
|
|
|
let v: ~str = Decodable::decode(&mut decoder);
|
2013-03-30 15:31:03 -05:00
|
|
|
assert_eq!(v, ~"\x08");
|
|
|
|
|
2013-12-03 21:18:35 -06:00
|
|
|
let mut decoder = Decoder::new(from_str("\"\\n\"").unwrap());
|
2013-05-01 19:54:54 -05:00
|
|
|
let v: ~str = Decodable::decode(&mut decoder);
|
2013-03-30 15:31:03 -05:00
|
|
|
assert_eq!(v, ~"\n");
|
|
|
|
|
2013-12-03 21:18:35 -06:00
|
|
|
let mut decoder = Decoder::new(from_str("\"\\r\"").unwrap());
|
2013-05-01 19:54:54 -05:00
|
|
|
let v: ~str = Decodable::decode(&mut decoder);
|
2013-03-30 15:31:03 -05:00
|
|
|
assert_eq!(v, ~"\r");
|
|
|
|
|
2013-12-03 21:18:35 -06:00
|
|
|
let mut decoder = Decoder::new(from_str("\"\\t\"").unwrap());
|
2013-05-01 19:54:54 -05:00
|
|
|
let v: ~str = Decodable::decode(&mut decoder);
|
2013-03-30 15:31:03 -05:00
|
|
|
assert_eq!(v, ~"\t");
|
|
|
|
|
2013-12-03 21:18:35 -06:00
|
|
|
let mut decoder = Decoder::new(from_str("\"\\u12ab\"").unwrap());
|
2013-05-01 19:54:54 -05:00
|
|
|
let v: ~str = Decodable::decode(&mut decoder);
|
2013-03-30 15:31:03 -05:00
|
|
|
assert_eq!(v, ~"\u12ab");
|
|
|
|
|
2013-12-03 21:18:35 -06:00
|
|
|
let mut decoder = Decoder::new(from_str("\"\\uAB12\"").unwrap());
|
2013-05-01 19:54:54 -05:00
|
|
|
let v: ~str = Decodable::decode(&mut decoder);
|
2013-03-30 15:31:03 -05:00
|
|
|
assert_eq!(v, ~"\uAB12");
|
2012-08-30 23:03:19 -05:00
|
|
|
}
|
|
|
|
|
2012-01-17 21:05:07 -06:00
|
|
|
#[test]
|
2012-02-25 18:39:32 -06:00
|
|
|
fn test_read_list() {
|
2013-05-23 11:39:00 -05:00
|
|
|
assert_eq!(from_str("["),
|
2013-11-28 16:50:21 -06:00
|
|
|
Err(Error {line: 1u, col: 2u, msg: ~"EOF while parsing value"}));
|
2013-05-23 11:39:00 -05:00
|
|
|
assert_eq!(from_str("[1"),
|
2013-11-28 16:50:21 -06:00
|
|
|
Err(Error {line: 1u, col: 3u, msg: ~"EOF while parsing list"}));
|
2013-05-23 11:39:00 -05:00
|
|
|
assert_eq!(from_str("[1,"),
|
2013-11-28 16:50:21 -06:00
|
|
|
Err(Error {line: 1u, col: 4u, msg: ~"EOF while parsing value"}));
|
2013-05-23 11:39:00 -05:00
|
|
|
assert_eq!(from_str("[1,]"),
|
2013-11-28 16:50:21 -06:00
|
|
|
Err(Error {line: 1u, col: 4u, msg: ~"invalid syntax"}));
|
2013-05-23 11:39:00 -05:00
|
|
|
assert_eq!(from_str("[6 7]"),
|
2013-11-28 16:50:21 -06:00
|
|
|
Err(Error {line: 1u, col: 4u, msg: ~"expected `,` or `]`"}));
|
2013-03-06 15:58:02 -06:00
|
|
|
|
2013-05-23 11:39:00 -05:00
|
|
|
assert_eq!(from_str("[]"), Ok(List(~[])));
|
|
|
|
assert_eq!(from_str("[ ]"), Ok(List(~[])));
|
|
|
|
assert_eq!(from_str("[true]"), Ok(List(~[Boolean(true)])));
|
|
|
|
assert_eq!(from_str("[ false ]"), Ok(List(~[Boolean(false)])));
|
|
|
|
assert_eq!(from_str("[null]"), Ok(List(~[Null])));
|
|
|
|
assert_eq!(from_str("[3, 1]"),
|
2013-09-26 01:26:09 -05:00
|
|
|
Ok(List(~[Number(3.0), Number(1.0)])));
|
2013-05-23 11:39:00 -05:00
|
|
|
assert_eq!(from_str("\n[3, 2]\n"),
|
2013-09-26 01:26:09 -05:00
|
|
|
Ok(List(~[Number(3.0), Number(2.0)])));
|
2013-05-23 11:39:00 -05:00
|
|
|
assert_eq!(from_str("[2, [4, 1]]"),
|
2013-09-26 01:26:09 -05:00
|
|
|
Ok(List(~[Number(2.0), List(~[Number(4.0), Number(1.0)])])));
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
|
2013-03-30 15:31:03 -05:00
|
|
|
#[test]
|
|
|
|
fn test_decode_list() {
|
2013-12-03 21:18:35 -06:00
|
|
|
let mut decoder = Decoder::new(from_str("[]").unwrap());
|
2013-05-01 19:54:54 -05:00
|
|
|
let v: ~[()] = Decodable::decode(&mut decoder);
|
2013-03-30 15:31:03 -05:00
|
|
|
assert_eq!(v, ~[]);
|
|
|
|
|
2013-12-03 21:18:35 -06:00
|
|
|
let mut decoder = Decoder::new(from_str("[null]").unwrap());
|
2013-05-01 19:54:54 -05:00
|
|
|
let v: ~[()] = Decodable::decode(&mut decoder);
|
2013-03-30 15:31:03 -05:00
|
|
|
assert_eq!(v, ~[()]);
|
|
|
|
|
2013-12-03 21:18:35 -06:00
|
|
|
let mut decoder = Decoder::new(from_str("[true]").unwrap());
|
2013-05-01 19:54:54 -05:00
|
|
|
let v: ~[bool] = Decodable::decode(&mut decoder);
|
2013-03-30 15:31:03 -05:00
|
|
|
assert_eq!(v, ~[true]);
|
|
|
|
|
2013-12-03 21:18:35 -06:00
|
|
|
let mut decoder = Decoder::new(from_str("[true]").unwrap());
|
2013-05-01 19:54:54 -05:00
|
|
|
let v: ~[bool] = Decodable::decode(&mut decoder);
|
2013-03-30 15:31:03 -05:00
|
|
|
assert_eq!(v, ~[true]);
|
|
|
|
|
2013-12-03 21:18:35 -06:00
|
|
|
let mut decoder = Decoder::new(from_str("[3, 1]").unwrap());
|
2013-05-01 19:54:54 -05:00
|
|
|
let v: ~[int] = Decodable::decode(&mut decoder);
|
2013-03-30 15:31:03 -05:00
|
|
|
assert_eq!(v, ~[3, 1]);
|
|
|
|
|
2013-12-03 21:18:35 -06:00
|
|
|
let mut decoder = Decoder::new(from_str("[[3], [1, 2]]").unwrap());
|
2013-05-01 19:54:54 -05:00
|
|
|
let v: ~[~[uint]] = Decodable::decode(&mut decoder);
|
2013-03-30 15:31:03 -05:00
|
|
|
assert_eq!(v, ~[~[3], ~[1, 2]]);
|
|
|
|
}
|
|
|
|
|
2012-01-17 21:05:07 -06:00
|
|
|
#[test]
|
2012-09-24 11:55:42 -05:00
|
|
|
fn test_read_object() {
|
2013-05-23 11:39:00 -05:00
|
|
|
assert_eq!(from_str("{"),
|
2013-03-06 15:58:02 -06:00
|
|
|
Err(Error {
|
|
|
|
line: 1u,
|
|
|
|
col: 2u,
|
2013-11-28 16:50:21 -06:00
|
|
|
msg: ~"EOF while parsing object"}));
|
2013-05-23 11:39:00 -05:00
|
|
|
assert_eq!(from_str("{ "),
|
2013-03-06 15:58:02 -06:00
|
|
|
Err(Error {
|
|
|
|
line: 1u,
|
|
|
|
col: 3u,
|
2013-11-28 16:50:21 -06:00
|
|
|
msg: ~"EOF while parsing object"}));
|
2013-05-23 11:39:00 -05:00
|
|
|
assert_eq!(from_str("{1"),
|
2013-03-06 15:58:02 -06:00
|
|
|
Err(Error {
|
|
|
|
line: 1u,
|
|
|
|
col: 2u,
|
2013-11-28 16:50:21 -06:00
|
|
|
msg: ~"key must be a string"}));
|
2013-05-23 11:39:00 -05:00
|
|
|
assert_eq!(from_str("{ \"a\""),
|
2013-03-06 15:58:02 -06:00
|
|
|
Err(Error {
|
|
|
|
line: 1u,
|
|
|
|
col: 6u,
|
2013-11-28 16:50:21 -06:00
|
|
|
msg: ~"EOF while parsing object"}));
|
2013-05-23 11:39:00 -05:00
|
|
|
assert_eq!(from_str("{\"a\""),
|
2013-03-06 15:58:02 -06:00
|
|
|
Err(Error {
|
|
|
|
line: 1u,
|
|
|
|
col: 5u,
|
2013-11-28 16:50:21 -06:00
|
|
|
msg: ~"EOF while parsing object"}));
|
2013-05-23 11:39:00 -05:00
|
|
|
assert_eq!(from_str("{\"a\" "),
|
2013-03-06 15:58:02 -06:00
|
|
|
Err(Error {
|
|
|
|
line: 1u,
|
|
|
|
col: 6u,
|
2013-11-28 16:50:21 -06:00
|
|
|
msg: ~"EOF while parsing object"}));
|
2013-03-06 15:58:02 -06:00
|
|
|
|
2013-05-23 11:39:00 -05:00
|
|
|
assert_eq!(from_str("{\"a\" 1"),
|
2013-11-28 16:50:21 -06:00
|
|
|
Err(Error {line: 1u, col: 6u, msg: ~"expected `:`"}));
|
2013-05-23 11:39:00 -05:00
|
|
|
assert_eq!(from_str("{\"a\":"),
|
2013-11-28 16:50:21 -06:00
|
|
|
Err(Error {line: 1u, col: 6u, msg: ~"EOF while parsing value"}));
|
2013-05-23 11:39:00 -05:00
|
|
|
assert_eq!(from_str("{\"a\":1"),
|
2013-03-06 15:58:02 -06:00
|
|
|
Err(Error {
|
|
|
|
line: 1u,
|
|
|
|
col: 7u,
|
2013-11-28 16:50:21 -06:00
|
|
|
msg: ~"EOF while parsing object"}));
|
2013-05-23 11:39:00 -05:00
|
|
|
assert_eq!(from_str("{\"a\":1 1"),
|
2013-11-28 16:50:21 -06:00
|
|
|
Err(Error {line: 1u, col: 8u, msg: ~"expected `,` or `}`"}));
|
2013-05-23 11:39:00 -05:00
|
|
|
assert_eq!(from_str("{\"a\":1,"),
|
2013-03-06 15:58:02 -06:00
|
|
|
Err(Error {
|
|
|
|
line: 1u,
|
|
|
|
col: 8u,
|
2013-11-28 16:50:21 -06:00
|
|
|
msg: ~"EOF while parsing object"}));
|
2013-03-06 15:58:02 -06:00
|
|
|
|
2013-07-26 20:36:51 -05:00
|
|
|
assert_eq!(from_str("{}").unwrap(), mk_object([]));
|
|
|
|
assert_eq!(from_str("{\"a\": 3}").unwrap(),
|
2013-09-26 01:26:09 -05:00
|
|
|
mk_object([(~"a", Number(3.0))]));
|
2013-03-06 15:58:02 -06:00
|
|
|
|
2013-07-26 20:36:51 -05:00
|
|
|
assert_eq!(from_str(
|
|
|
|
"{ \"a\": null, \"b\" : true }").unwrap(),
|
2013-05-23 11:39:00 -05:00
|
|
|
mk_object([
|
2012-08-11 09:08:42 -05:00
|
|
|
(~"a", Null),
|
2013-03-06 15:58:02 -06:00
|
|
|
(~"b", Boolean(true))]));
|
2013-07-26 20:36:51 -05:00
|
|
|
assert_eq!(from_str("\n{ \"a\": null, \"b\" : true }\n").unwrap(),
|
2013-05-23 11:39:00 -05:00
|
|
|
mk_object([
|
2012-08-11 09:08:42 -05:00
|
|
|
(~"a", Null),
|
2013-03-06 15:58:02 -06:00
|
|
|
(~"b", Boolean(true))]));
|
2013-07-26 20:36:51 -05:00
|
|
|
assert_eq!(from_str(
|
|
|
|
"{\"a\" : 1.0 ,\"b\": [ true ]}").unwrap(),
|
2013-05-23 11:39:00 -05:00
|
|
|
mk_object([
|
2012-09-24 11:55:42 -05:00
|
|
|
(~"a", Number(1.0)),
|
|
|
|
(~"b", List(~[Boolean(true)]))
|
2013-03-06 15:58:02 -06:00
|
|
|
]));
|
2013-07-26 20:36:51 -05:00
|
|
|
assert_eq!(from_str(
|
2012-07-14 00:57:48 -05:00
|
|
|
~"{" +
|
2013-05-27 18:04:00 -05:00
|
|
|
"\"a\": 1.0, " +
|
|
|
|
"\"b\": [" +
|
|
|
|
"true," +
|
|
|
|
"\"foo\\nbar\", " +
|
|
|
|
"{ \"c\": {\"d\": null} } " +
|
|
|
|
"]" +
|
2013-07-26 20:36:51 -05:00
|
|
|
"}").unwrap(),
|
2013-05-23 11:39:00 -05:00
|
|
|
mk_object([
|
2013-09-26 01:26:09 -05:00
|
|
|
(~"a", Number(1.0)),
|
2012-09-24 11:55:42 -05:00
|
|
|
(~"b", List(~[
|
2012-08-11 09:08:42 -05:00
|
|
|
Boolean(true),
|
2012-09-24 11:55:42 -05:00
|
|
|
String(~"foo\nbar"),
|
2013-05-23 11:39:00 -05:00
|
|
|
mk_object([
|
|
|
|
(~"c", mk_object([(~"d", Null)]))
|
2012-06-29 18:26:56 -05:00
|
|
|
])
|
|
|
|
]))
|
2013-03-06 15:58:02 -06:00
|
|
|
]));
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
|
2013-03-27 02:13:01 -05:00
|
|
|
#[test]
|
2013-03-30 15:31:03 -05:00
|
|
|
fn test_decode_struct() {
|
|
|
|
let s = ~"{
|
|
|
|
\"inner\": [
|
|
|
|
{ \"a\": null, \"b\": 2, \"c\": [\"abc\", \"xyz\"] }
|
|
|
|
]
|
|
|
|
}";
|
2013-12-03 21:18:35 -06:00
|
|
|
let mut decoder = Decoder::new(from_str(s).unwrap());
|
2013-05-01 19:54:54 -05:00
|
|
|
let v: Outer = Decodable::decode(&mut decoder);
|
2013-03-30 15:31:03 -05:00
|
|
|
assert_eq!(
|
|
|
|
v,
|
|
|
|
Outer {
|
|
|
|
inner: ~[
|
|
|
|
Inner { a: (), b: 2, c: ~[~"abc", ~"xyz"] }
|
|
|
|
]
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_decode_option() {
|
2013-12-03 21:18:35 -06:00
|
|
|
let mut decoder = Decoder::new(from_str("null").unwrap());
|
2013-05-01 19:54:54 -05:00
|
|
|
let value: Option<~str> = Decodable::decode(&mut decoder);
|
2013-03-27 02:13:01 -05:00
|
|
|
assert_eq!(value, None);
|
|
|
|
|
2013-12-03 21:18:35 -06:00
|
|
|
let mut decoder = Decoder::new(from_str("\"jodhpurs\"").unwrap());
|
2013-05-01 19:54:54 -05:00
|
|
|
let value: Option<~str> = Decodable::decode(&mut decoder);
|
2013-03-27 02:13:01 -05:00
|
|
|
assert_eq!(value, Some(~"jodhpurs"));
|
|
|
|
}
|
|
|
|
|
2013-03-26 19:42:01 -05:00
|
|
|
#[test]
|
2013-03-30 15:31:03 -05:00
|
|
|
fn test_decode_enum() {
|
2013-12-03 21:18:35 -06:00
|
|
|
let mut decoder = Decoder::new(from_str("\"Dog\"").unwrap());
|
2013-05-01 19:54:54 -05:00
|
|
|
let value: Animal = Decodable::decode(&mut decoder);
|
2013-03-26 19:42:01 -05:00
|
|
|
assert_eq!(value, Dog);
|
|
|
|
|
2013-11-29 13:11:52 -06:00
|
|
|
let s = "{\"variant\":\"Frog\",\"fields\":[\"Henry\",349]}";
|
2013-12-03 21:18:35 -06:00
|
|
|
let mut decoder = Decoder::new(from_str(s).unwrap());
|
2013-05-01 19:54:54 -05:00
|
|
|
let value: Animal = Decodable::decode(&mut decoder);
|
2013-03-26 19:42:01 -05:00
|
|
|
assert_eq!(value, Frog(~"Henry", 349));
|
|
|
|
}
|
|
|
|
|
2013-03-29 11:04:35 -05:00
|
|
|
#[test]
|
2013-03-30 15:31:03 -05:00
|
|
|
fn test_decode_map() {
|
2013-08-09 19:05:24 -05:00
|
|
|
let s = ~"{\"a\": \"Dog\", \"b\": {\"variant\":\"Frog\",\"fields\":[\"Henry\", 349]}}";
|
2013-12-03 21:18:35 -06:00
|
|
|
let mut decoder = Decoder::new(from_str(s).unwrap());
|
2013-07-18 14:37:40 -05:00
|
|
|
let mut map: TreeMap<~str, Animal> = Decodable::decode(&mut decoder);
|
2013-03-29 11:04:35 -05:00
|
|
|
|
2013-03-29 11:10:31 -05:00
|
|
|
assert_eq!(map.pop(&~"a"), Some(Dog));
|
|
|
|
assert_eq!(map.pop(&~"b"), Some(Frog(~"Henry", 349)));
|
2013-03-29 11:04:35 -05:00
|
|
|
}
|
|
|
|
|
2012-01-17 21:05:07 -06:00
|
|
|
#[test]
|
2012-02-25 18:39:32 -06:00
|
|
|
fn test_multiline_errors() {
|
2013-05-23 11:39:00 -05:00
|
|
|
assert_eq!(from_str("{\n \"foo\":\n \"bar\""),
|
2013-03-06 15:58:02 -06:00
|
|
|
Err(Error {
|
|
|
|
line: 3u,
|
|
|
|
col: 8u,
|
2013-11-28 16:50:21 -06:00
|
|
|
msg: ~"EOF while parsing object"}));
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
2013-11-23 17:17:34 -06:00
|
|
|
|
|
|
|
#[deriving(Decodable)]
|
|
|
|
struct DecodeStruct {
|
|
|
|
x: f64,
|
|
|
|
y: bool,
|
|
|
|
z: ~str,
|
|
|
|
w: ~[DecodeStruct]
|
|
|
|
}
|
|
|
|
#[deriving(Decodable)]
|
|
|
|
enum DecodeEnum {
|
|
|
|
A(f64),
|
|
|
|
B(~str)
|
|
|
|
}
|
|
|
|
fn check_err<T: Decodable<Decoder>>(to_parse: &'static str, expected_error: &str) {
|
|
|
|
use std::task;
|
2013-11-24 13:44:28 -06:00
|
|
|
let res = do task::try {
|
2013-11-23 17:17:34 -06:00
|
|
|
// either fails in `decode` (which is what we want), or
|
|
|
|
// returns Some(error_message)/None if the string was
|
|
|
|
// invalid or valid JSON.
|
|
|
|
match from_str(to_parse) {
|
|
|
|
Err(e) => Some(e.to_str()),
|
|
|
|
Ok(json) => {
|
2013-12-03 21:18:35 -06:00
|
|
|
let _: T = Decodable::decode(&mut Decoder::new(json));
|
2013-11-23 17:17:34 -06:00
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2013-11-24 13:44:28 -06:00
|
|
|
};
|
2013-11-23 17:17:34 -06:00
|
|
|
match res {
|
|
|
|
Ok(Some(parse_error)) => fail!("`{}` is not valid json: {}",
|
|
|
|
to_parse, parse_error),
|
|
|
|
Ok(None) => fail!("`{}` parsed & decoded ok, expecting error `{}`",
|
|
|
|
to_parse, expected_error),
|
|
|
|
Err(e) => {
|
|
|
|
let err = e.as_ref::<~str>().unwrap();
|
|
|
|
assert!(err.contains(expected_error),
|
|
|
|
"`{}` errored incorrectly, found `{}` expecting `{}`",
|
|
|
|
to_parse, *err, expected_error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#[test]
|
|
|
|
fn test_decode_errors_struct() {
|
|
|
|
check_err::<DecodeStruct>("[]", "object but found list");
|
|
|
|
check_err::<DecodeStruct>("{\"x\": true, \"y\": true, \"z\": \"\", \"w\": []}",
|
|
|
|
"number but found boolean");
|
|
|
|
check_err::<DecodeStruct>("{\"x\": 1, \"y\": [], \"z\": \"\", \"w\": []}",
|
|
|
|
"boolean but found list");
|
|
|
|
check_err::<DecodeStruct>("{\"x\": 1, \"y\": true, \"z\": {}, \"w\": []}",
|
|
|
|
"string but found object");
|
|
|
|
check_err::<DecodeStruct>("{\"x\": 1, \"y\": true, \"z\": \"\", \"w\": null}",
|
|
|
|
"list but found null");
|
|
|
|
check_err::<DecodeStruct>("{\"x\": 1, \"y\": true, \"z\": \"\"}",
|
|
|
|
"'w' field in object");
|
|
|
|
}
|
|
|
|
#[test]
|
|
|
|
fn test_decode_errors_enum() {
|
|
|
|
check_err::<DecodeEnum>("{}",
|
|
|
|
"'variant' field in object");
|
|
|
|
check_err::<DecodeEnum>("{\"variant\": 1}",
|
|
|
|
"string but found number");
|
|
|
|
check_err::<DecodeEnum>("{\"variant\": \"A\"}",
|
|
|
|
"'fields' field in object");
|
|
|
|
check_err::<DecodeEnum>("{\"variant\": \"A\", \"fields\": null}",
|
|
|
|
"list but found null");
|
|
|
|
check_err::<DecodeEnum>("{\"variant\": \"C\", \"fields\": []}",
|
|
|
|
"unknown variant name");
|
|
|
|
}
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|