2012-12-03 18:48:01 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// 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.
|
2012-09-13 00:09:55 -05:00
|
|
|
#[forbid(non_camel_case_types)];
|
2011-11-07 13:01:28 -06:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
//! json serialization
|
2012-03-07 20:17:30 -06:00
|
|
|
|
2013-03-13 13:29:05 -05:00
|
|
|
use core::prelude::*;
|
|
|
|
use core::io::{WriterUtil, ReaderUtil};
|
|
|
|
use core::hashmap::linear::LinearMap;
|
|
|
|
|
2013-01-25 18:57:39 -06:00
|
|
|
use serialize::Encodable;
|
2012-12-23 16:41:37 -06:00
|
|
|
use serialize;
|
2012-09-07 20:53:14 -05:00
|
|
|
use sort::Sort;
|
2011-11-07 13:01:28 -06:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Represents a json value
|
2012-09-24 11:55:42 -05:00
|
|
|
pub enum Json {
|
|
|
|
Number(float),
|
|
|
|
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-01-23 16:06:32 -06:00
|
|
|
pub type Object = LinearMap<~str, Json>;
|
2012-09-24 11:55:42 -05:00
|
|
|
|
2013-03-22 15:09:20 -05:00
|
|
|
#[deriving(Eq)]
|
2012-09-24 11:55:42 -05:00
|
|
|
pub struct Error {
|
2012-02-25 18:39:32 -06:00
|
|
|
line: uint,
|
|
|
|
col: uint,
|
2012-07-14 00:57:48 -05:00
|
|
|
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-03-13 13:29:05 -05:00
|
|
|
for str::each_char(s) |c| {
|
2012-09-24 11:55:42 -05:00
|
|
|
match c {
|
|
|
|
'"' => escaped += ~"\\\"",
|
|
|
|
'\\' => escaped += ~"\\\\",
|
|
|
|
'\x08' => escaped += ~"\\b",
|
|
|
|
'\x0c' => escaped += ~"\\f",
|
|
|
|
'\n' => escaped += ~"\\n",
|
|
|
|
'\r' => escaped += ~"\\r",
|
|
|
|
'\t' => escaped += ~"\\t",
|
|
|
|
_ => escaped += str::from_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
|
|
|
|
2012-09-24 11:55:42 -05:00
|
|
|
escaped += ~"\"";
|
|
|
|
|
|
|
|
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 = ~"";
|
2012-10-11 16:12:50 -05:00
|
|
|
for n.times { str::push_str(&mut ss, " "); }
|
2012-09-24 11:55:42 -05:00
|
|
|
return ss;
|
|
|
|
}
|
|
|
|
|
2012-12-17 21:31:04 -06:00
|
|
|
pub struct Encoder {
|
2013-03-12 15:00:50 -05:00
|
|
|
priv wr: @io::Writer,
|
2012-09-24 11:55:42 -05:00
|
|
|
}
|
|
|
|
|
2013-03-12 15:00:50 -05:00
|
|
|
pub fn Encoder(wr: @io::Writer) -> Encoder {
|
2012-12-17 21:31:04 -06:00
|
|
|
Encoder { wr: wr }
|
2012-09-24 11:55:42 -05:00
|
|
|
}
|
|
|
|
|
2013-02-26 19:12:00 -06:00
|
|
|
impl serialize::Encoder for Encoder {
|
2012-09-24 11:55:42 -05:00
|
|
|
fn emit_nil(&self) { self.wr.write_str("null") }
|
|
|
|
|
|
|
|
fn emit_uint(&self, v: uint) { self.emit_float(v as float); }
|
|
|
|
fn emit_u64(&self, v: u64) { self.emit_float(v as float); }
|
|
|
|
fn emit_u32(&self, v: u32) { self.emit_float(v as float); }
|
|
|
|
fn emit_u16(&self, v: u16) { self.emit_float(v as float); }
|
|
|
|
fn emit_u8(&self, v: u8) { self.emit_float(v as float); }
|
|
|
|
|
|
|
|
fn emit_int(&self, v: int) { self.emit_float(v as float); }
|
|
|
|
fn emit_i64(&self, v: i64) { self.emit_float(v as float); }
|
|
|
|
fn emit_i32(&self, v: i32) { self.emit_float(v as float); }
|
|
|
|
fn emit_i16(&self, v: i16) { self.emit_float(v as float); }
|
|
|
|
fn emit_i8(&self, v: i8) { self.emit_float(v as float); }
|
|
|
|
|
|
|
|
fn emit_bool(&self, v: bool) {
|
|
|
|
if v {
|
|
|
|
self.wr.write_str("true");
|
|
|
|
} else {
|
|
|
|
self.wr.write_str("false");
|
2012-08-30 23:03:19 -05:00
|
|
|
}
|
2012-09-24 11:55:42 -05:00
|
|
|
}
|
2012-08-30 23:03:19 -05:00
|
|
|
|
2012-09-24 11:55:42 -05:00
|
|
|
fn emit_f64(&self, v: f64) { self.emit_float(v as float); }
|
|
|
|
fn emit_f32(&self, v: f32) { self.emit_float(v as float); }
|
|
|
|
fn emit_float(&self, v: float) {
|
2013-01-26 20:28:39 -06:00
|
|
|
self.wr.write_str(float::to_str_digits(v, 6u));
|
2012-09-24 11:55:42 -05:00
|
|
|
}
|
2012-08-30 23:03:19 -05:00
|
|
|
|
2013-03-28 22:51:05 -05:00
|
|
|
fn emit_char(&self, v: char) { self.emit_str(str::from_char(v)) }
|
|
|
|
fn emit_str(&self, v: &str) { self.wr.write_str(escape_str(v)) }
|
2012-09-26 23:35:13 -05:00
|
|
|
|
2013-03-28 22:51:05 -05:00
|
|
|
fn emit_enum(&self, _name: &str, f: &fn()) { f() }
|
2013-03-27 02:14:52 -05:00
|
|
|
fn emit_enum_variant(&self, name: &str, _id: uint, cnt: uint, f: &fn()) {
|
2013-03-26 17:26:05 -05:00
|
|
|
// enums are encoded as strings or vectors:
|
2013-03-27 02:14:52 -05:00
|
|
|
// Bunny => "Bunny"
|
2013-02-08 17:36:40 -06:00
|
|
|
// Kangaroo(34,"William") => ["Kangaroo",[34,"William"]]
|
2013-02-08 20:58:33 -06:00
|
|
|
|
2013-03-26 17:26:05 -05:00
|
|
|
if cnt == 0 {
|
2013-03-27 02:14:52 -05:00
|
|
|
self.wr.write_str(escape_str(name));
|
2013-02-08 17:36:40 -06:00
|
|
|
} else {
|
|
|
|
self.wr.write_char('[');
|
|
|
|
self.wr.write_str(escape_str(name));
|
|
|
|
self.wr.write_char(',');
|
|
|
|
f();
|
|
|
|
self.wr.write_char(']');
|
|
|
|
}
|
2012-09-24 11:55:42 -05:00
|
|
|
}
|
2013-02-07 19:06:26 -06:00
|
|
|
|
2013-03-07 16:38:38 -06:00
|
|
|
fn emit_enum_variant_arg(&self, idx: uint, f: &fn()) {
|
2013-02-08 17:36:40 -06:00
|
|
|
if (idx != 0) {self.wr.write_char(',');}
|
2013-02-06 19:19:11 -06:00
|
|
|
f();
|
2012-09-24 11:55:42 -05:00
|
|
|
}
|
|
|
|
|
2013-03-28 23:34:47 -05:00
|
|
|
fn emit_seq(&self, _len: uint, f: &fn()) {
|
2012-09-24 11:55:42 -05:00
|
|
|
self.wr.write_char('[');
|
|
|
|
f();
|
|
|
|
self.wr.write_char(']');
|
|
|
|
}
|
2013-02-08 20:58:33 -06:00
|
|
|
|
2013-03-28 23:34:47 -05:00
|
|
|
fn emit_seq_elt(&self, idx: uint, f: &fn()) {
|
2012-09-24 11:55:42 -05:00
|
|
|
if idx != 0 { self.wr.write_char(','); }
|
|
|
|
f()
|
|
|
|
}
|
|
|
|
|
2013-03-07 16:38:38 -06:00
|
|
|
fn emit_struct(&self, _name: &str, _len: uint, f: &fn()) {
|
2012-09-26 23:35:13 -05:00
|
|
|
self.wr.write_char('{');
|
|
|
|
f();
|
|
|
|
self.wr.write_char('}');
|
|
|
|
}
|
2013-03-07 16:38:38 -06:00
|
|
|
fn emit_field(&self, name: &str, idx: uint, f: &fn()) {
|
2012-09-24 11:55:42 -05:00
|
|
|
if idx != 0 { self.wr.write_char(','); }
|
|
|
|
self.wr.write_str(escape_str(name));
|
|
|
|
self.wr.write_char(':');
|
|
|
|
f();
|
|
|
|
}
|
2012-09-26 23:35:13 -05:00
|
|
|
|
2013-03-26 17:26:05 -05:00
|
|
|
fn emit_option(&self, f: &fn()) { f(); }
|
|
|
|
fn emit_option_none(&self) { self.emit_nil(); }
|
|
|
|
fn emit_option_some(&self, f: &fn()) { f(); }
|
2013-03-29 11:04:35 -05:00
|
|
|
|
|
|
|
fn emit_map(&self, _len: uint, f: &fn()) {
|
|
|
|
self.wr.write_char('{');
|
|
|
|
f();
|
|
|
|
self.wr.write_char('}');
|
|
|
|
}
|
|
|
|
|
|
|
|
fn emit_map_elt_key(&self, idx: uint, f: &fn()) {
|
|
|
|
if idx != 0 { self.wr.write_char(','); }
|
|
|
|
f()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn emit_map_elt_val(&self, _idx: uint, f: &fn()) {
|
|
|
|
self.wr.write_char(':');
|
|
|
|
f()
|
|
|
|
}
|
2012-09-24 11:55:42 -05:00
|
|
|
}
|
|
|
|
|
2012-12-17 21:31:04 -06:00
|
|
|
pub struct PrettyEncoder {
|
2013-03-12 15:00:50 -05:00
|
|
|
priv wr: @io::Writer,
|
2012-09-24 11:55:42 -05:00
|
|
|
priv mut indent: uint,
|
|
|
|
}
|
|
|
|
|
2013-03-12 15:00:50 -05:00
|
|
|
pub fn PrettyEncoder(wr: @io::Writer) -> PrettyEncoder {
|
2012-12-17 21:31:04 -06:00
|
|
|
PrettyEncoder { wr: wr, indent: 0 }
|
2012-09-24 11:55:42 -05:00
|
|
|
}
|
2012-08-30 18:39:56 -05:00
|
|
|
|
2013-02-26 19:12:00 -06:00
|
|
|
impl serialize::Encoder for PrettyEncoder {
|
2012-09-24 11:55:42 -05:00
|
|
|
fn emit_nil(&self) { self.wr.write_str("null") }
|
|
|
|
|
|
|
|
fn emit_uint(&self, v: uint) { self.emit_float(v as float); }
|
|
|
|
fn emit_u64(&self, v: u64) { self.emit_float(v as float); }
|
|
|
|
fn emit_u32(&self, v: u32) { self.emit_float(v as float); }
|
|
|
|
fn emit_u16(&self, v: u16) { self.emit_float(v as float); }
|
|
|
|
fn emit_u8(&self, v: u8) { self.emit_float(v as float); }
|
|
|
|
|
|
|
|
fn emit_int(&self, v: int) { self.emit_float(v as float); }
|
|
|
|
fn emit_i64(&self, v: i64) { self.emit_float(v as float); }
|
|
|
|
fn emit_i32(&self, v: i32) { self.emit_float(v as float); }
|
|
|
|
fn emit_i16(&self, v: i16) { self.emit_float(v as float); }
|
|
|
|
fn emit_i8(&self, v: i8) { self.emit_float(v as float); }
|
|
|
|
|
|
|
|
fn emit_bool(&self, v: bool) {
|
|
|
|
if v {
|
|
|
|
self.wr.write_str("true");
|
|
|
|
} else {
|
|
|
|
self.wr.write_str("false");
|
2012-08-30 23:03:19 -05:00
|
|
|
}
|
2012-09-24 11:55:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn emit_f64(&self, v: f64) { self.emit_float(v as float); }
|
|
|
|
fn emit_f32(&self, v: f32) { self.emit_float(v as float); }
|
|
|
|
fn emit_float(&self, v: float) {
|
2013-01-26 20:28:39 -06:00
|
|
|
self.wr.write_str(float::to_str_digits(v, 6u));
|
2012-09-24 11:55:42 -05:00
|
|
|
}
|
2012-08-30 23:03:19 -05:00
|
|
|
|
2013-03-28 22:51:05 -05:00
|
|
|
fn emit_char(&self, v: char) { self.emit_str(str::from_char(v)) }
|
|
|
|
fn emit_str(&self, v: &str) { self.wr.write_str(escape_str(v)); }
|
2012-08-30 23:03:19 -05:00
|
|
|
|
2013-03-26 19:34:49 -05:00
|
|
|
fn emit_enum(&self, _name: &str, f: &fn()) { f() }
|
|
|
|
fn emit_enum_variant(&self, name: &str, _id: uint, cnt: uint, f: &fn()) {
|
2013-03-26 17:26:05 -05:00
|
|
|
if cnt == 0 {
|
2013-03-27 02:14:52 -05:00
|
|
|
self.wr.write_str(escape_str(name));
|
2012-09-24 11:55:42 -05:00
|
|
|
} else {
|
2013-03-26 19:34:49 -05:00
|
|
|
self.wr.write_char('[');
|
|
|
|
self.indent += 2;
|
|
|
|
self.wr.write_char('\n');
|
|
|
|
self.wr.write_str(spaces(self.indent));
|
|
|
|
self.wr.write_str(escape_str(name));
|
2013-03-27 02:14:52 -05:00
|
|
|
self.wr.write_str(",\n");
|
|
|
|
f();
|
|
|
|
self.wr.write_char('\n');
|
2013-03-26 19:34:49 -05:00
|
|
|
self.indent -= 2;
|
2013-03-27 02:14:52 -05:00
|
|
|
self.wr.write_str(spaces(self.indent));
|
2013-03-26 19:34:49 -05:00
|
|
|
self.wr.write_char(']');
|
2012-08-30 19:38:07 -05:00
|
|
|
}
|
2012-09-24 11:55:42 -05:00
|
|
|
}
|
2013-03-26 19:34:49 -05:00
|
|
|
fn emit_enum_variant_arg(&self, idx: uint, f: &fn()) {
|
|
|
|
if idx != 0 {
|
|
|
|
self.wr.write_str(",\n");
|
|
|
|
}
|
|
|
|
self.wr.write_str(spaces(self.indent));
|
2012-09-24 11:55:42 -05:00
|
|
|
f()
|
|
|
|
}
|
2012-08-30 19:38:07 -05:00
|
|
|
|
2013-03-28 23:34:47 -05:00
|
|
|
fn emit_seq(&self, len: uint, f: &fn()) {
|
2013-03-26 19:34:49 -05:00
|
|
|
if len == 0 {
|
|
|
|
self.wr.write_str("[]");
|
|
|
|
} else {
|
|
|
|
self.wr.write_char('[');
|
|
|
|
self.indent += 2;
|
|
|
|
f();
|
|
|
|
self.wr.write_char('\n');
|
|
|
|
self.indent -= 2;
|
|
|
|
self.wr.write_str(spaces(self.indent));
|
|
|
|
self.wr.write_char(']');
|
|
|
|
}
|
2012-09-24 11:55:42 -05:00
|
|
|
}
|
2013-03-28 23:34:47 -05:00
|
|
|
fn emit_seq_elt(&self, idx: uint, f: &fn()) {
|
2012-09-24 11:55:42 -05:00
|
|
|
if idx == 0 {
|
|
|
|
self.wr.write_char('\n');
|
|
|
|
} else {
|
|
|
|
self.wr.write_str(",\n");
|
|
|
|
}
|
|
|
|
self.wr.write_str(spaces(self.indent));
|
|
|
|
f()
|
|
|
|
}
|
2012-08-30 18:39:56 -05:00
|
|
|
|
2013-03-26 19:34:49 -05:00
|
|
|
fn emit_struct(&self, _name: &str, len: uint, f: &fn()) {
|
|
|
|
if len == 0 {
|
|
|
|
self.wr.write_str("{}");
|
|
|
|
} else {
|
2013-03-29 00:19:43 -05:00
|
|
|
self.wr.write_char('{');
|
|
|
|
self.indent += 2;
|
|
|
|
f();
|
|
|
|
self.wr.write_char('\n');
|
|
|
|
self.indent -= 2;
|
|
|
|
self.wr.write_str(spaces(self.indent));
|
|
|
|
self.wr.write_char('}');
|
2013-03-26 19:34:49 -05:00
|
|
|
}
|
2012-09-26 23:35:13 -05:00
|
|
|
}
|
2013-03-07 16:38:38 -06:00
|
|
|
fn emit_field(&self, name: &str, idx: uint, f: &fn()) {
|
2012-09-24 11:55:42 -05:00
|
|
|
if idx == 0 {
|
|
|
|
self.wr.write_char('\n');
|
|
|
|
} else {
|
|
|
|
self.wr.write_str(",\n");
|
|
|
|
}
|
|
|
|
self.wr.write_str(spaces(self.indent));
|
|
|
|
self.wr.write_str(escape_str(name));
|
|
|
|
self.wr.write_str(": ");
|
|
|
|
f();
|
|
|
|
}
|
2013-03-26 17:26:05 -05:00
|
|
|
|
|
|
|
fn emit_option(&self, f: &fn()) { f(); }
|
|
|
|
fn emit_option_none(&self) { self.emit_nil(); }
|
|
|
|
fn emit_option_some(&self, f: &fn()) { f(); }
|
2013-03-29 11:04:35 -05:00
|
|
|
|
|
|
|
fn emit_map(&self, len: uint, f: &fn()) {
|
|
|
|
if len == 0 {
|
|
|
|
self.wr.write_str("{}");
|
|
|
|
} else {
|
|
|
|
self.wr.write_char('{');
|
|
|
|
self.indent += 2;
|
|
|
|
f();
|
|
|
|
self.wr.write_char('\n');
|
|
|
|
self.indent -= 2;
|
|
|
|
self.wr.write_str(spaces(self.indent));
|
|
|
|
self.wr.write_char('}');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn emit_map_elt_key(&self, idx: uint, f: &fn()) {
|
|
|
|
if idx == 0 {
|
|
|
|
self.wr.write_char('\n');
|
|
|
|
} else {
|
|
|
|
self.wr.write_str(",\n");
|
|
|
|
}
|
|
|
|
self.wr.write_str(spaces(self.indent));
|
|
|
|
f();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn emit_map_elt_val(&self, _idx: uint, f: &fn()) {
|
|
|
|
self.wr.write_str(": ");
|
|
|
|
f();
|
|
|
|
}
|
2012-08-30 18:39:56 -05:00
|
|
|
}
|
|
|
|
|
2013-03-29 00:19:43 -05:00
|
|
|
impl<E: serialize::Encoder> serialize::Encodable<E> for Json {
|
|
|
|
fn encode(&self, e: &E) {
|
2012-10-13 11:11:33 -05:00
|
|
|
match *self {
|
2013-03-29 00:19:43 -05:00
|
|
|
Number(v) => v.encode(e),
|
|
|
|
String(ref v) => v.encode(e),
|
|
|
|
Boolean(v) => v.encode(e),
|
|
|
|
List(ref v) => v.encode(e),
|
2013-03-29 11:04:35 -05:00
|
|
|
Object(ref v) => v.encode(e),
|
2013-03-29 00:19:43 -05:00
|
|
|
Null => e.emit_nil(),
|
2012-09-24 11:55:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-06-12 19:20:51 -05:00
|
|
|
|
2012-12-17 21:31:04 -06:00
|
|
|
/// Encodes a json value into a io::writer
|
2013-03-12 15:00:50 -05:00
|
|
|
pub fn to_writer(wr: @io::Writer, json: &Json) {
|
2012-12-17 21:31:04 -06:00
|
|
|
json.encode(&Encoder(wr))
|
2012-09-24 11:55:42 -05:00
|
|
|
}
|
2012-06-12 19:20:51 -05:00
|
|
|
|
2012-12-17 21:31:04 -06:00
|
|
|
/// Encodes a json value into a string
|
2013-03-21 23:34:30 -05:00
|
|
|
pub fn to_str(json: &Json) -> ~str {
|
2013-01-23 13:43:58 -06:00
|
|
|
unsafe {
|
|
|
|
// ugh, should be safe
|
|
|
|
io::with_str_writer(|wr| to_writer(wr, json))
|
|
|
|
}
|
2012-09-24 11:55:42 -05:00
|
|
|
}
|
|
|
|
|
2012-12-17 21:31:04 -06:00
|
|
|
/// Encodes a json value into a io::writer
|
2013-03-12 15:00:50 -05:00
|
|
|
pub fn to_pretty_writer(wr: @io::Writer, json: &Json) {
|
2012-12-17 21:31:04 -06:00
|
|
|
json.encode(&PrettyEncoder(wr))
|
2012-06-12 19:20:51 -05:00
|
|
|
}
|
|
|
|
|
2012-12-17 21:31:04 -06:00
|
|
|
/// Encodes a json value into a string
|
2012-09-24 11:55:42 -05:00
|
|
|
pub fn to_pretty_str(json: &Json) -> ~str {
|
|
|
|
io::with_str_writer(|wr| to_pretty_writer(wr, json))
|
2011-11-07 13:01:28 -06:00
|
|
|
}
|
|
|
|
|
2012-09-24 11:55:42 -05:00
|
|
|
pub struct Parser {
|
2013-03-12 15:00:50 -05:00
|
|
|
priv rdr: @io::Reader,
|
2012-09-24 11:55:42 -05:00
|
|
|
priv mut ch: char,
|
|
|
|
priv mut line: uint,
|
|
|
|
priv mut col: uint,
|
2012-08-30 18:39:56 -05:00
|
|
|
}
|
|
|
|
|
2012-12-17 21:31:04 -06:00
|
|
|
/// Decode a json value from an io::reader
|
2013-03-12 15:00:50 -05:00
|
|
|
pub fn Parser(rdr: @io::Reader) -> Parser {
|
2012-09-24 11:55:42 -05:00
|
|
|
Parser {
|
|
|
|
rdr: rdr,
|
|
|
|
ch: rdr.read_char(),
|
2012-09-19 00:35:42 -05:00
|
|
|
line: 1,
|
|
|
|
col: 1,
|
2012-09-24 11:55:42 -05:00
|
|
|
}
|
|
|
|
}
|
2011-11-07 13:01:28 -06:00
|
|
|
|
2012-09-24 11:55:42 -05:00
|
|
|
pub impl Parser {
|
2013-03-07 20:11:09 -06:00
|
|
|
fn parse(&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
|
|
|
}
|
|
|
|
|
2012-09-24 11:55:42 -05:00
|
|
|
priv impl Parser {
|
2013-03-07 20:11:09 -06:00
|
|
|
fn eof(&self) -> bool { self.ch == -1 as char }
|
2012-02-25 18:39:32 -06:00
|
|
|
|
2013-03-07 20:11:09 -06:00
|
|
|
fn bump(&self) {
|
2012-02-25 18:39:32 -06:00
|
|
|
self.ch = self.rdr.read_char();
|
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;
|
|
|
|
} else {
|
|
|
|
self.col += 1u;
|
2011-11-07 13:01:28 -06:00
|
|
|
}
|
2012-02-25 18:39:32 -06:00
|
|
|
}
|
|
|
|
|
2013-03-07 20:11:09 -06:00
|
|
|
fn next_char(&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> {
|
2012-09-24 11:55:42 -05:00
|
|
|
Err(Error { line: self.line, col: self.col, msg: @msg })
|
2011-11-07 13:01:28 -06:00
|
|
|
}
|
|
|
|
|
2013-03-07 20:11:09 -06:00
|
|
|
fn parse_value(&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 {
|
2012-08-11 09:08:42 -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-07 20:11:09 -06:00
|
|
|
fn parse_whitespace(&self) {
|
2012-02-25 18:39:32 -06:00
|
|
|
while char::is_whitespace(self.ch) { self.bump(); }
|
|
|
|
}
|
|
|
|
|
2013-03-07 20:11:09 -06:00
|
|
|
fn parse_ident(&self, ident: &str, value: Json) -> Result<Json, Error> {
|
2012-06-30 18:19:07 -05:00
|
|
|
if str::all(ident, |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-07 20:11:09 -06:00
|
|
|
fn parse_number(&self) -> Result<Json, Error> {
|
2012-03-14 13:03:56 -05:00
|
|
|
let mut neg = 1f;
|
2012-02-25 18:39:32 -06:00
|
|
|
|
|
|
|
if self.ch == '-' {
|
|
|
|
self.bump();
|
|
|
|
neg = -1f;
|
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-03-07 20:11:09 -06:00
|
|
|
fn parse_integer(&self) -> Result<float, Error> {
|
2012-03-14 13:03:56 -05:00
|
|
|
let mut res = 0f;
|
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' => {
|
2012-02-25 18:39:32 -06:00
|
|
|
res *= 10f;
|
|
|
|
res += ((self.ch as int) - ('0' as int)) as float;
|
|
|
|
|
|
|
|
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-03-07 20:11:09 -06:00
|
|
|
fn parse_decimal(&self, res: float) -> Result<float, 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;
|
|
|
|
let mut dec = 1f;
|
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
|
|
|
dec /= 10f;
|
|
|
|
res += (((self.ch as int) - ('0' as int)) as float) * dec;
|
|
|
|
|
|
|
|
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-03-07 20:11:09 -06:00
|
|
|
fn parse_exponent(&self, res: float) -> Result<float, Error> {
|
2012-02-25 18:39:32 -06:00
|
|
|
self.bump();
|
|
|
|
|
2012-03-14 13:03:56 -05:00
|
|
|
let mut res = res;
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let exp = float::pow_with_uint(10u, exp);
|
|
|
|
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-07 20:11:09 -06:00
|
|
|
fn parse_str(&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
|
|
|
|
|
|
|
while !self.eof() {
|
|
|
|
self.bump();
|
|
|
|
|
|
|
|
if (escape) {
|
2012-08-06 14:34:08 -05:00
|
|
|
match self.ch {
|
2012-10-11 16:12:50 -05:00
|
|
|
'"' => str::push_char(&mut res, '"'),
|
|
|
|
'\\' => str::push_char(&mut res, '\\'),
|
|
|
|
'/' => str::push_char(&mut res, '/'),
|
|
|
|
'b' => str::push_char(&mut res, '\x08'),
|
|
|
|
'f' => str::push_char(&mut res, '\x0c'),
|
|
|
|
'n' => str::push_char(&mut res, '\n'),
|
|
|
|
'r' => str::push_char(&mut res, '\r'),
|
|
|
|
't' => str::push_char(&mut res, '\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
|
|
|
}
|
|
|
|
|
2012-10-11 16:12:50 -05:00
|
|
|
str::push_char(&mut res, n as char);
|
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
|
|
|
}
|
2012-10-11 16:12:50 -05:00
|
|
|
str::push_char(&mut res, self.ch);
|
2011-11-07 13:01:28 -06:00
|
|
|
}
|
|
|
|
}
|
2012-02-25 18:39:32 -06:00
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
self.error(~"EOF while parsing string")
|
2011-11-07 13:01:28 -06:00
|
|
|
}
|
|
|
|
|
2013-03-07 20:11:09 -06:00
|
|
|
fn parse_list(&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-07 20:11:09 -06:00
|
|
|
fn parse_object(&self) -> Result<Json, Error> {
|
2012-02-25 18:39:32 -06:00
|
|
|
self.bump();
|
|
|
|
self.parse_whitespace();
|
|
|
|
|
2013-01-23 16:06:32 -06:00
|
|
|
let mut values = ~LinearMap::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();
|
|
|
|
|
|
|
|
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-03-12 15:00:50 -05:00
|
|
|
/// Decodes a json value from an @io::Reader
|
|
|
|
pub fn from_reader(rdr: @io::Reader) -> Result<Json, Error> {
|
2012-09-24 11:55:42 -05:00
|
|
|
Parser(rdr).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> {
|
|
|
|
do io::with_str_reader(s) |rdr| {
|
|
|
|
from_reader(rdr)
|
|
|
|
}
|
2012-02-25 18:39:32 -06:00
|
|
|
}
|
|
|
|
|
2013-03-29 11:04:35 -05:00
|
|
|
pub struct Decoder {
|
|
|
|
priv mut stack: ~[Json],
|
2012-09-24 11:55:42 -05:00
|
|
|
}
|
|
|
|
|
2012-12-17 21:31:04 -06:00
|
|
|
pub fn Decoder(json: Json) -> Decoder {
|
2013-03-29 11:04:35 -05:00
|
|
|
Decoder { stack: ~[json] }
|
2012-09-24 11:55:42 -05:00
|
|
|
}
|
|
|
|
|
2013-03-29 11:04:35 -05:00
|
|
|
impl serialize::Decoder for Decoder {
|
2012-09-24 11:55:42 -05:00
|
|
|
fn read_nil(&self) -> () {
|
|
|
|
debug!("read_nil");
|
2013-03-29 11:04:35 -05:00
|
|
|
match self.stack.pop() {
|
2012-09-24 11:55:42 -05:00
|
|
|
Null => (),
|
2013-03-29 11:04:35 -05:00
|
|
|
value => fail!(fmt!("not a null: %?", value))
|
2012-09-07 20:53:14 -05:00
|
|
|
}
|
2012-09-24 11:55:42 -05:00
|
|
|
}
|
2012-09-07 20:53:14 -05:00
|
|
|
|
2012-09-24 11:55:42 -05:00
|
|
|
fn read_u64(&self) -> u64 { self.read_float() as u64 }
|
|
|
|
fn read_u32(&self) -> u32 { self.read_float() as u32 }
|
|
|
|
fn read_u16(&self) -> u16 { self.read_float() as u16 }
|
|
|
|
fn read_u8 (&self) -> u8 { self.read_float() as u8 }
|
|
|
|
fn read_uint(&self) -> uint { self.read_float() as uint }
|
|
|
|
|
|
|
|
fn read_i64(&self) -> i64 { self.read_float() as i64 }
|
|
|
|
fn read_i32(&self) -> i32 { self.read_float() as i32 }
|
|
|
|
fn read_i16(&self) -> i16 { self.read_float() as i16 }
|
|
|
|
fn read_i8 (&self) -> i8 { self.read_float() as i8 }
|
|
|
|
fn read_int(&self) -> int { self.read_float() as int }
|
|
|
|
|
|
|
|
fn read_bool(&self) -> bool {
|
|
|
|
debug!("read_bool");
|
2013-03-29 11:04:35 -05:00
|
|
|
match self.stack.pop() {
|
2012-09-24 11:55:42 -05:00
|
|
|
Boolean(b) => b,
|
2013-03-29 11:04:35 -05:00
|
|
|
value => fail!(fmt!("not a boolean: %?", value))
|
2012-09-07 20:53:14 -05:00
|
|
|
}
|
2012-09-24 11:55:42 -05:00
|
|
|
}
|
2012-09-07 20:53:14 -05:00
|
|
|
|
2012-09-24 11:55:42 -05:00
|
|
|
fn read_f64(&self) -> f64 { self.read_float() as f64 }
|
|
|
|
fn read_f32(&self) -> f32 { self.read_float() as f32 }
|
|
|
|
fn read_float(&self) -> float {
|
|
|
|
debug!("read_float");
|
2013-03-29 11:04:35 -05:00
|
|
|
match self.stack.pop() {
|
2012-09-24 11:55:42 -05:00
|
|
|
Number(f) => f,
|
2013-03-29 11:04:35 -05:00
|
|
|
value => fail!(fmt!("not a number: %?", value))
|
2012-09-24 11:55:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-26 23:35:13 -05:00
|
|
|
fn read_char(&self) -> char {
|
2013-03-24 01:51:18 -05:00
|
|
|
let mut v = ~[];
|
2013-03-28 22:51:05 -05:00
|
|
|
for str::each_char(self.read_str()) |c| { v.push(c) }
|
2013-02-11 21:26:38 -06:00
|
|
|
if v.len() != 1 { fail!(~"string must have one character") }
|
2012-09-26 23:35:13 -05:00
|
|
|
v[0]
|
|
|
|
}
|
|
|
|
|
2013-03-28 22:51:05 -05:00
|
|
|
fn read_str(&self) -> ~str {
|
|
|
|
debug!("read_str");
|
2013-03-29 11:04:35 -05:00
|
|
|
match self.stack.pop() {
|
|
|
|
String(s) => s,
|
|
|
|
json => fail!(fmt!("not a string: %?", json))
|
2012-09-07 20:53:14 -05:00
|
|
|
}
|
2012-09-24 11:55:42 -05:00
|
|
|
}
|
|
|
|
|
2013-03-07 16:38:38 -06:00
|
|
|
fn read_enum<T>(&self, name: &str, f: &fn() -> T) -> T {
|
2012-09-24 11:55:42 -05:00
|
|
|
debug!("read_enum(%s)", name);
|
|
|
|
f()
|
|
|
|
}
|
|
|
|
|
2013-03-26 20:46:48 -05:00
|
|
|
fn read_enum_variant<T>(&self, names: &[&str], f: &fn(uint) -> T) -> T {
|
|
|
|
debug!("read_enum_variant(names=%?)", names);
|
2013-03-29 11:04:35 -05:00
|
|
|
let name = match self.stack.pop() {
|
|
|
|
String(s) => s,
|
|
|
|
List(list) => {
|
|
|
|
do vec::consume_reverse(list) |_i, v| {
|
|
|
|
self.stack.push(v);
|
|
|
|
}
|
|
|
|
match self.stack.pop() {
|
|
|
|
String(s) => s,
|
|
|
|
value => fail!(fmt!("invalid variant name: %?", value)),
|
|
|
|
}
|
|
|
|
}
|
2013-03-27 01:15:14 -05:00
|
|
|
ref json => fail!(fmt!("invalid variant: %?", *json)),
|
2013-03-26 20:46:48 -05:00
|
|
|
};
|
2013-03-29 11:04:35 -05:00
|
|
|
let idx = match vec::position(names, |n| str::eq_slice(*n, name)) {
|
2013-03-26 20:46:48 -05:00
|
|
|
Some(idx) => idx,
|
|
|
|
None => fail!(fmt!("Unknown variant name: %?", name)),
|
|
|
|
};
|
|
|
|
f(idx)
|
|
|
|
}
|
|
|
|
|
2013-03-07 16:38:38 -06:00
|
|
|
fn read_enum_variant_arg<T>(&self, idx: uint, f: &fn() -> T) -> T {
|
2012-09-24 11:55:42 -05:00
|
|
|
debug!("read_enum_variant_arg(idx=%u)", idx);
|
2013-03-29 11:04:35 -05:00
|
|
|
f()
|
2012-09-24 11:55:42 -05:00
|
|
|
}
|
2012-09-07 20:53:14 -05:00
|
|
|
|
2013-03-28 23:34:47 -05:00
|
|
|
fn read_seq<T>(&self, f: &fn(uint) -> T) -> T {
|
|
|
|
debug!("read_seq()");
|
2013-03-29 11:04:35 -05:00
|
|
|
let len = match self.stack.pop() {
|
|
|
|
List(list) => {
|
|
|
|
let len = list.len();
|
|
|
|
do vec::consume_reverse(list) |_i, v| {
|
|
|
|
self.stack.push(v);
|
|
|
|
}
|
|
|
|
len
|
|
|
|
}
|
2013-02-11 21:26:38 -06:00
|
|
|
_ => fail!(~"not a list"),
|
2012-09-24 11:55:42 -05:00
|
|
|
};
|
2013-03-29 11:10:31 -05:00
|
|
|
f(len)
|
2012-09-24 11:55:42 -05:00
|
|
|
}
|
|
|
|
|
2013-03-28 23:34:47 -05:00
|
|
|
fn read_seq_elt<T>(&self, idx: uint, f: &fn() -> T) -> T {
|
|
|
|
debug!("read_seq_elt(idx=%u)", idx);
|
2013-03-29 11:04:35 -05:00
|
|
|
f()
|
2012-09-24 11:55:42 -05:00
|
|
|
}
|
2012-09-07 20:53:14 -05:00
|
|
|
|
2013-03-29 11:04:35 -05:00
|
|
|
fn read_struct<T>(&self, name: &str, len: uint, f: &fn() -> T) -> T {
|
|
|
|
debug!("read_struct(name=%s, len=%u)", name, len);
|
2012-09-26 23:35:13 -05:00
|
|
|
let value = f();
|
2013-03-29 11:04:35 -05:00
|
|
|
self.stack.pop();
|
2013-02-15 01:30:30 -06:00
|
|
|
value
|
2012-09-26 23:35:13 -05:00
|
|
|
}
|
|
|
|
|
2013-03-07 16:38:38 -06:00
|
|
|
fn read_field<T>(&self, name: &str, idx: uint, f: &fn() -> T) -> T {
|
2013-03-29 00:19:43 -05:00
|
|
|
debug!("read_field(%s, idx=%u)", name, idx);
|
2013-03-29 11:04:35 -05:00
|
|
|
match self.stack.pop() {
|
|
|
|
Object(obj) => {
|
|
|
|
let mut obj = obj;
|
|
|
|
let value = match obj.pop(&name.to_owned()) {
|
2013-02-11 21:26:38 -06:00
|
|
|
None => fail!(fmt!("no such field: %s", name)),
|
2012-09-24 11:55:42 -05:00
|
|
|
Some(json) => {
|
2012-09-26 19:33:34 -05:00
|
|
|
self.stack.push(json);
|
2012-09-24 11:55:42 -05:00
|
|
|
f()
|
2012-09-07 20:53:14 -05:00
|
|
|
}
|
2013-03-29 11:04:35 -05:00
|
|
|
};
|
|
|
|
self.stack.push(Object(obj));
|
|
|
|
value
|
2012-09-07 20:53:14 -05:00
|
|
|
}
|
2013-03-29 11:04:35 -05:00
|
|
|
value => fail!(fmt!("not an object: %?", value))
|
2012-09-07 20:53:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-28 15:11:14 -05:00
|
|
|
fn read_option<T>(&self, f: &fn(bool) -> T) -> T {
|
2013-03-29 11:04:35 -05:00
|
|
|
match self.stack.pop() {
|
|
|
|
Null => f(false),
|
|
|
|
value => { self.stack.push(value); f(true) }
|
2013-03-26 17:26:05 -05:00
|
|
|
}
|
|
|
|
}
|
2013-03-29 11:04:35 -05:00
|
|
|
|
|
|
|
fn read_map<T>(&self, f: &fn(uint) -> T) -> T {
|
|
|
|
debug!("read_map()");
|
|
|
|
let len = match self.stack.pop() {
|
|
|
|
Object(obj) => {
|
|
|
|
let mut obj = obj;
|
|
|
|
let len = obj.len();
|
|
|
|
do obj.consume |key, value| {
|
|
|
|
self.stack.push(value);
|
|
|
|
self.stack.push(String(key));
|
|
|
|
}
|
|
|
|
len
|
|
|
|
}
|
|
|
|
json => fail!(fmt!("not an object: %?", json)),
|
|
|
|
};
|
|
|
|
f(len)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read_map_elt_key<T>(&self, idx: uint, f: &fn() -> T) -> T {
|
|
|
|
debug!("read_map_elt_key(idx=%u)", idx);
|
|
|
|
f()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read_map_elt_val<T>(&self, idx: uint, f: &fn() -> T) -> T {
|
|
|
|
debug!("read_map_elt_val(idx=%u)", idx);
|
|
|
|
f()
|
|
|
|
}
|
2012-09-19 20:00:26 -05:00
|
|
|
}
|
2012-08-27 18:26:35 -05:00
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl Eq for Json {
|
2013-03-21 23:34:30 -05:00
|
|
|
fn eq(&self, other: &Json) -> bool {
|
2013-01-10 22:09:16 -06:00
|
|
|
match (self) {
|
|
|
|
&Number(f0) =>
|
|
|
|
match other { &Number(f1) => f0 == f1, _ => false },
|
|
|
|
&String(ref s0) =>
|
|
|
|
match other { &String(ref s1) => s0 == s1, _ => false },
|
|
|
|
&Boolean(b0) =>
|
|
|
|
match other { &Boolean(b1) => b0 == b1, _ => false },
|
|
|
|
&Null =>
|
|
|
|
match other { &Null => true, _ => false },
|
|
|
|
&List(ref v0) =>
|
|
|
|
match other { &List(ref v1) => v0 == v1, _ => false },
|
|
|
|
&Object(ref d0) => {
|
|
|
|
match other {
|
|
|
|
&Object(ref d1) => {
|
2012-11-14 20:59:30 -06:00
|
|
|
if d0.len() == d1.len() {
|
|
|
|
let mut equal = true;
|
2013-02-07 20:03:13 -06:00
|
|
|
for d0.each |&(k, v0)| {
|
2013-01-23 10:47:43 -06:00
|
|
|
match d1.find(k) {
|
2012-11-14 20:59:30 -06:00
|
|
|
Some(v1) if v0 == v1 => { },
|
|
|
|
_ => { equal = false; break }
|
|
|
|
}
|
|
|
|
};
|
|
|
|
equal
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-03-21 23:34:30 -05:00
|
|
|
fn ne(&self, other: &Json) -> bool { !self.eq(other) }
|
2012-09-19 20:00:26 -05:00
|
|
|
}
|
2012-08-27 18:26:35 -05:00
|
|
|
|
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,
|
|
|
|
Object(ref d1) => {
|
|
|
|
unsafe {
|
|
|
|
let mut d0_flat = ~[];
|
|
|
|
let mut d1_flat = ~[];
|
|
|
|
|
2013-01-10 22:09:16 -06:00
|
|
|
// FIXME #4430: this is horribly inefficient...
|
2013-02-07 20:03:13 -06:00
|
|
|
for d0.each |&(k, v)| {
|
2012-11-14 20:59:30 -06:00
|
|
|
d0_flat.push((@copy *k, @copy *v));
|
|
|
|
}
|
|
|
|
d0_flat.qsort();
|
|
|
|
|
2013-02-07 20:03:13 -06:00
|
|
|
for d1.each |&(k, v)| {
|
2012-11-14 20:59:30 -06:00
|
|
|
d1_flat.push((@copy *k, @copy *v));
|
|
|
|
}
|
|
|
|
d1_flat.qsort();
|
|
|
|
|
|
|
|
d0_flat < d1_flat
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Null => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Null => {
|
|
|
|
match *other {
|
|
|
|
Number(_) | String(_) | Boolean(_) | List(_) |
|
|
|
|
Object(_) =>
|
|
|
|
false,
|
|
|
|
Null => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-03-21 23:34:30 -05:00
|
|
|
fn le(&self, other: &Json) -> bool { !(*other).lt(&(*self)) }
|
|
|
|
fn ge(&self, other: &Json) -> bool { !(*self).lt(other) }
|
|
|
|
fn gt(&self, other: &Json) -> bool { (*other).lt(&(*self)) }
|
2012-09-19 20:00:26 -05:00
|
|
|
}
|
2012-09-07 20:53:14 -05:00
|
|
|
|
2013-03-07 20:11:09 -06:00
|
|
|
trait ToJson { 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-03-07 20:11:09 -06:00
|
|
|
fn to_json(&self) -> Json { copy *self }
|
2012-05-28 14:10:32 -05:00
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl ToJson for @Json {
|
2013-03-07 20:11:09 -06:00
|
|
|
fn to_json(&self) -> Json { (**self).to_json() }
|
2012-06-13 10:30:54 -05:00
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl ToJson for int {
|
2013-03-07 20:11:09 -06:00
|
|
|
fn to_json(&self) -> Json { Number(*self as float) }
|
2012-06-13 10:30:54 -05:00
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl ToJson for i8 {
|
2013-03-07 20:11:09 -06:00
|
|
|
fn to_json(&self) -> Json { Number(*self as float) }
|
2012-05-28 14:10:32 -05:00
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl ToJson for i16 {
|
2013-03-07 20:11:09 -06:00
|
|
|
fn to_json(&self) -> Json { Number(*self as float) }
|
2012-05-28 14:10:32 -05:00
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl ToJson for i32 {
|
2013-03-07 20:11:09 -06:00
|
|
|
fn to_json(&self) -> Json { Number(*self as float) }
|
2012-05-28 14:10:32 -05:00
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl ToJson for i64 {
|
2013-03-07 20:11:09 -06:00
|
|
|
fn to_json(&self) -> Json { Number(*self as float) }
|
2012-05-28 14:10:32 -05:00
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl ToJson for uint {
|
2013-03-07 20:11:09 -06:00
|
|
|
fn to_json(&self) -> Json { Number(*self as float) }
|
2012-06-13 10:30:54 -05:00
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl ToJson for u8 {
|
2013-03-07 20:11:09 -06:00
|
|
|
fn to_json(&self) -> Json { Number(*self as float) }
|
2012-05-28 14:10:32 -05:00
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl ToJson for u16 {
|
2013-03-07 20:11:09 -06:00
|
|
|
fn to_json(&self) -> Json { Number(*self as float) }
|
2012-05-28 14:10:32 -05:00
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl ToJson for u32 {
|
2013-03-07 20:11:09 -06:00
|
|
|
fn to_json(&self) -> Json { Number(*self as float) }
|
2012-05-28 14:10:32 -05:00
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl ToJson for u64 {
|
2013-03-07 20:11:09 -06:00
|
|
|
fn to_json(&self) -> Json { Number(*self as float) }
|
2012-05-28 14:10:32 -05:00
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl ToJson for float {
|
2013-03-07 20:11:09 -06: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 f32 {
|
2013-03-07 20:11:09 -06:00
|
|
|
fn to_json(&self) -> Json { Number(*self as float) }
|
2012-05-28 14:10:32 -05:00
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl ToJson for f64 {
|
2013-03-07 20:11:09 -06:00
|
|
|
fn to_json(&self) -> Json { Number(*self as float) }
|
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-03-07 20:11:09 -06:00
|
|
|
fn to_json(&self) -> Json { String(copy *self) }
|
2012-06-12 19:20:51 -05:00
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl ToJson for @~str {
|
2013-03-07 20:11:09 -06:00
|
|
|
fn to_json(&self) -> Json { String(copy **self) }
|
2012-05-28 14:10:32 -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-02-20 19:07:17 -06:00
|
|
|
impl<A:ToJson + Copy> ToJson for LinearMap<~str, A> {
|
2013-03-07 20:11:09 -06:00
|
|
|
fn to_json(&self) -> Json {
|
2013-01-23 16:06:32 -06:00
|
|
|
let mut d = LinearMap::new();
|
2013-02-07 20:03:13 -06:00
|
|
|
for self.each |&(key, value)| {
|
2012-09-24 11:55:42 -05:00
|
|
|
d.insert(copy *key, value.to_json());
|
|
|
|
}
|
|
|
|
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-03-21 23:34:30 -05:00
|
|
|
fn to_str(&self) -> ~str { to_str(self) }
|
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 {
|
2012-08-22 19:24:52 -05:00
|
|
|
fmt!("%u:%u: %s", 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-03-26 19:46:29 -05:00
|
|
|
use super::*;
|
2013-01-08 21:37:25 -06:00
|
|
|
|
2013-03-26 19:46:29 -05:00
|
|
|
use core::prelude::*;
|
2013-01-23 16:06:32 -06:00
|
|
|
use core::hashmap::linear::LinearMap;
|
2013-02-07 19:06:26 -06:00
|
|
|
|
2013-03-27 02:13:01 -05:00
|
|
|
use std::serialize::Decodable;
|
|
|
|
|
2013-03-30 13:08:57 -05:00
|
|
|
#[auto_encode]
|
|
|
|
#[auto_decode]
|
|
|
|
#[deriving(Eq)]
|
|
|
|
enum Animal {
|
|
|
|
Dog,
|
|
|
|
Frog(~str, int)
|
|
|
|
}
|
|
|
|
|
2013-03-30 15:31:03 -05:00
|
|
|
#[auto_encode]
|
|
|
|
#[auto_decode]
|
|
|
|
#[deriving(Eq)]
|
|
|
|
struct Inner {
|
|
|
|
a: (),
|
|
|
|
b: uint,
|
|
|
|
c: ~[~str],
|
|
|
|
}
|
|
|
|
|
|
|
|
#[auto_encode]
|
|
|
|
#[auto_decode]
|
|
|
|
#[deriving(Eq)]
|
|
|
|
struct Outer {
|
|
|
|
inner: ~[Inner],
|
|
|
|
}
|
|
|
|
|
2012-09-24 11:55:42 -05:00
|
|
|
fn mk_object(items: &[(~str, Json)]) -> Json {
|
2013-01-23 20:09:50 -06:00
|
|
|
let mut d = ~LinearMap::new();
|
2012-02-25 18:39:32 -06:00
|
|
|
|
2012-09-24 11:55:42 -05:00
|
|
|
for items.each |item| {
|
|
|
|
match *item {
|
2013-02-15 01:30:30 -06:00
|
|
|
(copy key, copy value) => { d.insert(key, value); },
|
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-03-26 19:23:00 -05:00
|
|
|
assert_eq!(to_str(&Null), ~"null");
|
2013-03-30 13:08:57 -05:00
|
|
|
assert_eq!(to_pretty_str(&Null), ~"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-03-26 19:23:00 -05:00
|
|
|
assert_eq!(to_str(&Number(3f)), ~"3");
|
2013-03-30 13:08:57 -05:00
|
|
|
assert_eq!(to_pretty_str(&Number(3f)), ~"3");
|
|
|
|
|
2013-03-26 19:23:00 -05:00
|
|
|
assert_eq!(to_str(&Number(3.1f)), ~"3.1");
|
2013-03-30 13:08:57 -05:00
|
|
|
assert_eq!(to_pretty_str(&Number(3.1f)), ~"3.1");
|
|
|
|
|
2013-03-26 19:23:00 -05:00
|
|
|
assert_eq!(to_str(&Number(-1.5f)), ~"-1.5");
|
2013-03-30 13:08:57 -05:00
|
|
|
assert_eq!(to_pretty_str(&Number(-1.5f)), ~"-1.5");
|
|
|
|
|
2013-03-26 19:23:00 -05:00
|
|
|
assert_eq!(to_str(&Number(0.5f)), ~"0.5");
|
2013-03-30 13:08:57 -05:00
|
|
|
assert_eq!(to_pretty_str(&Number(0.5f)), ~"0.5");
|
2012-02-25 18:39:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_write_str() {
|
2013-03-26 19:23:00 -05:00
|
|
|
assert_eq!(to_str(&String(~"")), ~"\"\"");
|
2013-03-30 13:08:57 -05:00
|
|
|
assert_eq!(to_pretty_str(&String(~"")), ~"\"\"");
|
|
|
|
|
2013-03-26 19:23:00 -05:00
|
|
|
assert_eq!(to_str(&String(~"foo")), ~"\"foo\"");
|
2013-03-30 13:08:57 -05:00
|
|
|
assert_eq!(to_pretty_str(&String(~"foo")), ~"\"foo\"");
|
2012-02-25 18:39:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_write_bool() {
|
2013-03-26 19:23:00 -05:00
|
|
|
assert_eq!(to_str(&Boolean(true)), ~"true");
|
2013-03-30 13:08:57 -05:00
|
|
|
assert_eq!(to_pretty_str(&Boolean(true)), ~"true");
|
|
|
|
|
2013-03-26 19:23:00 -05:00
|
|
|
assert_eq!(to_str(&Boolean(false)), ~"false");
|
2013-03-30 13:08:57 -05:00
|
|
|
assert_eq!(to_pretty_str(&Boolean(false)), ~"false");
|
2012-02-25 18:39:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_write_list() {
|
2013-03-26 19:23:00 -05:00
|
|
|
assert_eq!(to_str(&List(~[])), ~"[]");
|
2013-03-26 19:34:49 -05:00
|
|
|
assert_eq!(to_pretty_str(&List(~[])), ~"[]");
|
2013-03-30 13:08:57 -05:00
|
|
|
|
|
|
|
assert_eq!(to_str(&List(~[Boolean(true)])), ~"[true]");
|
2013-03-26 19:34:49 -05:00
|
|
|
assert_eq!(
|
|
|
|
to_pretty_str(&List(~[Boolean(true)])),
|
|
|
|
~"\
|
|
|
|
[\n \
|
|
|
|
true\n\
|
|
|
|
]"
|
|
|
|
);
|
2013-03-30 13:08:57 -05:00
|
|
|
|
|
|
|
assert_eq!(to_str(&List(~[
|
|
|
|
Boolean(false),
|
|
|
|
Null,
|
|
|
|
List(~[String(~"foo\nbar"), Number(3.5f)])
|
|
|
|
])), ~"[false,null,[\"foo\\nbar\",3.5]]");
|
2013-03-26 19:34:49 -05:00
|
|
|
assert_eq!(
|
|
|
|
to_pretty_str(&List(~[
|
|
|
|
Boolean(false),
|
|
|
|
Null,
|
|
|
|
List(~[String(~"foo\nbar"), Number(3.5f)])
|
|
|
|
])),
|
|
|
|
~"\
|
|
|
|
[\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-03-26 19:23:00 -05:00
|
|
|
assert_eq!(to_str(&mk_object(~[])), ~"{}");
|
2013-03-30 13:08:57 -05:00
|
|
|
assert_eq!(to_pretty_str(&mk_object(~[])), ~"{}");
|
|
|
|
|
2013-03-26 19:23:00 -05:00
|
|
|
assert_eq!(
|
|
|
|
to_str(&mk_object(~[(~"a", Boolean(true))])),
|
|
|
|
~"{\"a\":true}"
|
|
|
|
);
|
2013-03-30 13:08:57 -05:00
|
|
|
assert_eq!(
|
|
|
|
to_pretty_str(&mk_object(~[(~"a", Boolean(true))])),
|
|
|
|
~"\
|
|
|
|
{\n \
|
|
|
|
\"a\": true\n\
|
|
|
|
}"
|
|
|
|
);
|
|
|
|
|
2013-03-26 19:34:49 -05:00
|
|
|
assert_eq!(
|
|
|
|
to_str(&mk_object(~[
|
|
|
|
(~"b", List(~[
|
|
|
|
mk_object(~[(~"c", String(~"\x0c\r"))]),
|
|
|
|
mk_object(~[(~"d", String(~""))])
|
|
|
|
]))
|
|
|
|
])),
|
|
|
|
~"{\
|
|
|
|
\"b\":[\
|
|
|
|
{\"c\":\"\\f\\r\"},\
|
|
|
|
{\"d\":\"\"}\
|
|
|
|
]\
|
|
|
|
}"
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
to_pretty_str(&mk_object(~[
|
|
|
|
(~"b", List(~[
|
|
|
|
mk_object(~[(~"c", String(~"\x0c\r"))]),
|
|
|
|
mk_object(~[(~"d", String(~""))])
|
|
|
|
]))
|
|
|
|
])),
|
|
|
|
~"\
|
|
|
|
{\n \
|
|
|
|
\"b\": [\n \
|
|
|
|
{\n \
|
|
|
|
\"c\": \"\\f\\r\"\n \
|
|
|
|
},\n \
|
|
|
|
{\n \
|
|
|
|
\"d\": \"\"\n \
|
|
|
|
}\n \
|
|
|
|
]\n\
|
|
|
|
}"
|
|
|
|
);
|
2013-03-30 13:08:57 -05:00
|
|
|
|
2012-09-24 11:55:42 -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(~[
|
|
|
|
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-03-30 13:08:57 -05:00
|
|
|
assert_eq!(copy a, from_str(to_str(&a)).unwrap());
|
|
|
|
assert_eq!(copy a, from_str(to_pretty_str(&a)).unwrap());
|
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!(
|
|
|
|
do io::with_str_writer |wr| {
|
|
|
|
let encoder = Encoder(wr);
|
|
|
|
animal.encode(&encoder);
|
|
|
|
},
|
|
|
|
~"\"Dog\""
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
do io::with_str_writer |wr| {
|
|
|
|
let encoder = PrettyEncoder(wr);
|
|
|
|
animal.encode(&encoder);
|
|
|
|
},
|
|
|
|
~"\"Dog\""
|
|
|
|
);
|
2013-03-26 19:34:49 -05:00
|
|
|
|
|
|
|
let animal = Frog(~"Henry", 349);
|
|
|
|
assert_eq!(
|
2013-03-30 13:08:57 -05:00
|
|
|
do io::with_str_writer |wr| {
|
|
|
|
let encoder = Encoder(wr);
|
|
|
|
animal.encode(&encoder);
|
|
|
|
},
|
|
|
|
~"[\"Frog\",\"Henry\",349]"
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
do io::with_str_writer |wr| {
|
|
|
|
let encoder = PrettyEncoder(wr);
|
|
|
|
animal.encode(&encoder);
|
|
|
|
},
|
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");
|
|
|
|
let s = do io::with_str_writer |wr| {
|
|
|
|
let encoder = Encoder(wr);
|
|
|
|
value.encode(&encoder);
|
|
|
|
};
|
|
|
|
assert_eq!(s, ~"\"jodhpurs\"");
|
2013-02-08 17:36:40 -06:00
|
|
|
|
2013-03-26 19:34:49 -05:00
|
|
|
let value = Some(~"jodhpurs");
|
|
|
|
let s = do io::with_str_writer |wr| {
|
|
|
|
let encoder = PrettyEncoder(wr);
|
|
|
|
value.encode(&encoder);
|
|
|
|
};
|
|
|
|
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;
|
|
|
|
let s = do io::with_str_writer |wr| {
|
|
|
|
let encoder = Encoder(wr);
|
|
|
|
value.encode(&encoder);
|
|
|
|
};
|
|
|
|
assert_eq!(s, ~"null");
|
2013-02-07 19:06:26 -06:00
|
|
|
|
2013-03-26 19:34:49 -05:00
|
|
|
let s = do io::with_str_writer |wr| {
|
|
|
|
let encoder = Encoder(wr);
|
|
|
|
value.encode(&encoder);
|
|
|
|
};
|
|
|
|
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-03-26 19:23:00 -05:00
|
|
|
assert_eq!(from_str(~"nulla"),
|
2013-03-06 15:58:02 -06:00
|
|
|
Err(Error {line: 1u, col: 5u, msg: @~"trailing characters"}));
|
2013-03-26 19:23:00 -05:00
|
|
|
assert_eq!(from_str(~"truea"),
|
2013-03-06 15:58:02 -06:00
|
|
|
Err(Error {line: 1u, col: 5u, msg: @~"trailing characters"}));
|
2013-03-26 19:23:00 -05:00
|
|
|
assert_eq!(from_str(~"falsea"),
|
2013-03-06 15:58:02 -06:00
|
|
|
Err(Error {line: 1u, col: 6u, msg: @~"trailing characters"}));
|
2013-03-26 19:23:00 -05:00
|
|
|
assert_eq!(from_str(~"1a"),
|
2013-03-06 15:58:02 -06:00
|
|
|
Err(Error {line: 1u, col: 2u, msg: @~"trailing characters"}));
|
2013-03-26 19:23:00 -05:00
|
|
|
assert_eq!(from_str(~"[]a"),
|
2013-03-06 15:58:02 -06:00
|
|
|
Err(Error {line: 1u, col: 3u, msg: @~"trailing characters"}));
|
2013-03-26 19:23:00 -05:00
|
|
|
assert_eq!(from_str(~"{}a"),
|
2013-03-06 15:58:02 -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-03-26 19:23:00 -05:00
|
|
|
assert_eq!(from_str(~"n"),
|
2013-03-06 15:58:02 -06:00
|
|
|
Err(Error {line: 1u, col: 2u, msg: @~"invalid syntax"}));
|
2013-03-26 19:23:00 -05:00
|
|
|
assert_eq!(from_str(~"nul"),
|
2013-03-06 15:58:02 -06:00
|
|
|
Err(Error {line: 1u, col: 4u, msg: @~"invalid syntax"}));
|
2012-02-25 18:39:32 -06:00
|
|
|
|
2013-03-26 19:23:00 -05:00
|
|
|
assert_eq!(from_str(~"t"),
|
2013-03-06 15:58:02 -06:00
|
|
|
Err(Error {line: 1u, col: 2u, msg: @~"invalid syntax"}));
|
2013-03-26 19:23:00 -05:00
|
|
|
assert_eq!(from_str(~"truz"),
|
2013-03-06 15:58:02 -06:00
|
|
|
Err(Error {line: 1u, col: 4u, msg: @~"invalid syntax"}));
|
2012-02-25 18:39:32 -06:00
|
|
|
|
2013-03-26 19:23:00 -05:00
|
|
|
assert_eq!(from_str(~"f"),
|
2013-03-06 15:58:02 -06:00
|
|
|
Err(Error {line: 1u, col: 2u, msg: @~"invalid syntax"}));
|
2013-03-26 19:23:00 -05:00
|
|
|
assert_eq!(from_str(~"faz"),
|
2013-03-06 15:58:02 -06:00
|
|
|
Err(Error {line: 1u, col: 3u, msg: @~"invalid syntax"}));
|
2012-02-25 18:39:32 -06:00
|
|
|
|
2013-03-26 19:23: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() {
|
|
|
|
let v: () = Decodable::decode(&Decoder(from_str(~"null").unwrap()));
|
|
|
|
assert_eq!(v, ());
|
|
|
|
|
|
|
|
let v: bool = Decodable::decode(&Decoder(from_str(~"true").unwrap()));
|
|
|
|
assert_eq!(v, true);
|
|
|
|
|
|
|
|
let v: bool = Decodable::decode(&Decoder(from_str(~"false").unwrap()));
|
|
|
|
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-03-26 19:23:00 -05:00
|
|
|
assert_eq!(from_str(~"+"),
|
2013-03-06 15:58:02 -06:00
|
|
|
Err(Error {line: 1u, col: 1u, msg: @~"invalid syntax"}));
|
2013-03-26 19:23:00 -05:00
|
|
|
assert_eq!(from_str(~"."),
|
2013-03-06 15:58:02 -06:00
|
|
|
Err(Error {line: 1u, col: 1u, msg: @~"invalid syntax"}));
|
|
|
|
|
2013-03-26 19:23:00 -05:00
|
|
|
assert_eq!(from_str(~"-"),
|
2013-03-06 15:58:02 -06:00
|
|
|
Err(Error {line: 1u, col: 2u, msg: @~"invalid number"}));
|
2013-03-26 19:23:00 -05:00
|
|
|
assert_eq!(from_str(~"00"),
|
2013-03-06 15:58:02 -06:00
|
|
|
Err(Error {line: 1u, col: 2u, msg: @~"invalid number"}));
|
2013-03-26 19:23:00 -05:00
|
|
|
assert_eq!(from_str(~"1."),
|
2013-03-06 15:58:02 -06:00
|
|
|
Err(Error {line: 1u, col: 3u, msg: @~"invalid number"}));
|
2013-03-26 19:23:00 -05:00
|
|
|
assert_eq!(from_str(~"1e"),
|
2013-03-06 15:58:02 -06:00
|
|
|
Err(Error {line: 1u, col: 3u, msg: @~"invalid number"}));
|
2013-03-26 19:23:00 -05:00
|
|
|
assert_eq!(from_str(~"1e+"),
|
2013-03-06 15:58:02 -06:00
|
|
|
Err(Error {line: 1u, col: 4u, msg: @~"invalid number"}));
|
|
|
|
|
2013-03-26 19:23:00 -05:00
|
|
|
assert_eq!(from_str(~"3"), Ok(Number(3f)));
|
|
|
|
assert_eq!(from_str(~"3.1"), Ok(Number(3.1f)));
|
|
|
|
assert_eq!(from_str(~"-1.2"), Ok(Number(-1.2f)));
|
|
|
|
assert_eq!(from_str(~"0.4"), Ok(Number(0.4f)));
|
|
|
|
assert_eq!(from_str(~"0.4e5"), Ok(Number(0.4e5f)));
|
|
|
|
assert_eq!(from_str(~"0.4e+15"), Ok(Number(0.4e15f)));
|
|
|
|
assert_eq!(from_str(~"0.4e-01"), Ok(Number(0.4e-01f)));
|
|
|
|
assert_eq!(from_str(~" 3 "), Ok(Number(3f)));
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
|
2013-03-30 15:31:03 -05:00
|
|
|
#[test]
|
|
|
|
fn test_decode_numbers() {
|
|
|
|
let v: float = Decodable::decode(&Decoder(from_str(~"3").unwrap()));
|
|
|
|
assert_eq!(v, 3f);
|
|
|
|
|
|
|
|
let v: float = Decodable::decode(&Decoder(from_str(~"3.1").unwrap()));
|
|
|
|
assert_eq!(v, 3.1f);
|
|
|
|
|
|
|
|
let v: float = Decodable::decode(&Decoder(from_str(~"-1.2").unwrap()));
|
|
|
|
assert_eq!(v, -1.2f);
|
|
|
|
|
|
|
|
let v: float = Decodable::decode(&Decoder(from_str(~"0.4").unwrap()));
|
|
|
|
assert_eq!(v, 0.4f);
|
|
|
|
|
|
|
|
let v: float = Decodable::decode(&Decoder(from_str(~"0.4e5").unwrap()));
|
|
|
|
assert_eq!(v, 0.4e5f);
|
|
|
|
|
|
|
|
let v: float = Decodable::decode(&Decoder(from_str(~"0.4e15").unwrap()));
|
|
|
|
assert_eq!(v, 0.4e15f);
|
|
|
|
|
|
|
|
let v: float = Decodable::decode(&Decoder(from_str(~"0.4e-01").unwrap()));
|
|
|
|
assert_eq!(v, 0.4e-01f);
|
|
|
|
}
|
|
|
|
|
2012-01-17 21:05:07 -06:00
|
|
|
#[test]
|
2012-02-25 18:39:32 -06:00
|
|
|
fn test_read_str() {
|
2013-03-26 19:23:00 -05:00
|
|
|
assert_eq!(from_str(~"\""),
|
2013-03-06 15:58:02 -06:00
|
|
|
Err(Error {line: 1u, col: 2u, msg: @~"EOF while parsing string"
|
|
|
|
}));
|
2013-03-26 19:23:00 -05:00
|
|
|
assert_eq!(from_str(~"\"lol"),
|
2013-03-06 15:58:02 -06:00
|
|
|
Err(Error {line: 1u, col: 5u, msg: @~"EOF while parsing string"
|
|
|
|
}));
|
|
|
|
|
2013-03-26 19:23: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")));
|
2013-03-30 15:31:03 -05:00
|
|
|
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() {
|
|
|
|
let v: ~str = Decodable::decode(&Decoder(from_str(~"\"\"").unwrap()));
|
|
|
|
assert_eq!(v, ~"");
|
|
|
|
|
|
|
|
let v: ~str = Decodable::decode(&Decoder(from_str(~"\"foo\"").unwrap()));
|
|
|
|
assert_eq!(v, ~"foo");
|
|
|
|
|
|
|
|
let v: ~str = Decodable::decode(&Decoder(from_str(~"\"\\\"\"").unwrap()));
|
|
|
|
assert_eq!(v, ~"\"");
|
|
|
|
|
|
|
|
let v: ~str = Decodable::decode(&Decoder(from_str(~"\"\\b\"").unwrap()));
|
|
|
|
assert_eq!(v, ~"\x08");
|
|
|
|
|
|
|
|
let v: ~str = Decodable::decode(&Decoder(from_str(~"\"\\n\"").unwrap()));
|
|
|
|
assert_eq!(v, ~"\n");
|
|
|
|
|
|
|
|
let v: ~str = Decodable::decode(&Decoder(from_str(~"\"\\r\"").unwrap()));
|
|
|
|
assert_eq!(v, ~"\r");
|
|
|
|
|
|
|
|
let v: ~str = Decodable::decode(&Decoder(from_str(~"\"\\t\"").unwrap()));
|
|
|
|
assert_eq!(v, ~"\t");
|
|
|
|
|
|
|
|
let v: ~str = Decodable::decode(&Decoder(from_str(~"\"\\u12ab\"").unwrap()));
|
|
|
|
assert_eq!(v, ~"\u12ab");
|
|
|
|
|
|
|
|
let v: ~str = Decodable::decode(&Decoder(from_str(~"\"\\uAB12\"").unwrap()));
|
|
|
|
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-03-26 19:23:00 -05:00
|
|
|
assert_eq!(from_str(~"["),
|
2013-03-06 15:58:02 -06:00
|
|
|
Err(Error {line: 1u, col: 2u, msg: @~"EOF while parsing value"}));
|
2013-03-26 19:23:00 -05:00
|
|
|
assert_eq!(from_str(~"[1"),
|
2013-03-06 15:58:02 -06:00
|
|
|
Err(Error {line: 1u, col: 3u, msg: @~"EOF while parsing list"}));
|
2013-03-26 19:23:00 -05:00
|
|
|
assert_eq!(from_str(~"[1,"),
|
2013-03-06 15:58:02 -06:00
|
|
|
Err(Error {line: 1u, col: 4u, msg: @~"EOF while parsing value"}));
|
2013-03-26 19:23:00 -05:00
|
|
|
assert_eq!(from_str(~"[1,]"),
|
2013-03-06 15:58:02 -06:00
|
|
|
Err(Error {line: 1u, col: 4u, msg: @~"invalid syntax"}));
|
2013-03-26 19:23:00 -05:00
|
|
|
assert_eq!(from_str(~"[6 7]"),
|
2013-03-06 15:58:02 -06:00
|
|
|
Err(Error {line: 1u, col: 4u, msg: @~"expected `,` or `]`"}));
|
|
|
|
|
2013-03-26 19:23: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-03-06 15:58:02 -06:00
|
|
|
Ok(List(~[Number(3f), Number(1f)])));
|
2013-03-26 19:23:00 -05:00
|
|
|
assert_eq!(from_str(~"\n[3, 2]\n"),
|
2013-03-06 15:58:02 -06:00
|
|
|
Ok(List(~[Number(3f), Number(2f)])));
|
2013-03-26 19:23:00 -05:00
|
|
|
assert_eq!(from_str(~"[2, [4, 1]]"),
|
2013-03-06 15:58:02 -06:00
|
|
|
Ok(List(~[Number(2f), List(~[Number(4f), Number(1f)])])));
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
|
2013-03-30 15:31:03 -05:00
|
|
|
#[test]
|
|
|
|
fn test_decode_list() {
|
|
|
|
let v: ~[()] = Decodable::decode(&Decoder(from_str(~"[]").unwrap()));
|
|
|
|
assert_eq!(v, ~[]);
|
|
|
|
|
|
|
|
let v: ~[()] = Decodable::decode(&Decoder(from_str(~"[null]").unwrap()));
|
|
|
|
assert_eq!(v, ~[()]);
|
|
|
|
|
|
|
|
|
|
|
|
let v: ~[bool] = Decodable::decode(&Decoder(from_str(~"[true]").unwrap()));
|
|
|
|
assert_eq!(v, ~[true]);
|
|
|
|
|
|
|
|
let v: ~[bool] = Decodable::decode(&Decoder(from_str(~"[true]").unwrap()));
|
|
|
|
assert_eq!(v, ~[true]);
|
|
|
|
|
|
|
|
let v: ~[int] = Decodable::decode(&Decoder(from_str(~"[3, 1]").unwrap()));
|
|
|
|
assert_eq!(v, ~[3, 1]);
|
|
|
|
|
|
|
|
let v: ~[~[uint]] = Decodable::decode(&Decoder(from_str(~"[[3], [1, 2]]").unwrap()));
|
|
|
|
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-03-26 19:23:00 -05:00
|
|
|
assert_eq!(from_str(~"{"),
|
2013-03-06 15:58:02 -06:00
|
|
|
Err(Error {
|
|
|
|
line: 1u,
|
|
|
|
col: 2u,
|
|
|
|
msg: @~"EOF while parsing object"}));
|
2013-03-26 19:23:00 -05:00
|
|
|
assert_eq!(from_str(~"{ "),
|
2013-03-06 15:58:02 -06:00
|
|
|
Err(Error {
|
|
|
|
line: 1u,
|
|
|
|
col: 3u,
|
|
|
|
msg: @~"EOF while parsing object"}));
|
2013-03-26 19:23:00 -05:00
|
|
|
assert_eq!(from_str(~"{1"),
|
2013-03-06 15:58:02 -06:00
|
|
|
Err(Error {
|
|
|
|
line: 1u,
|
|
|
|
col: 2u,
|
|
|
|
msg: @~"key must be a string"}));
|
2013-03-26 19:23:00 -05:00
|
|
|
assert_eq!(from_str(~"{ \"a\""),
|
2013-03-06 15:58:02 -06:00
|
|
|
Err(Error {
|
|
|
|
line: 1u,
|
|
|
|
col: 6u,
|
|
|
|
msg: @~"EOF while parsing object"}));
|
2013-03-26 19:23:00 -05:00
|
|
|
assert_eq!(from_str(~"{\"a\""),
|
2013-03-06 15:58:02 -06:00
|
|
|
Err(Error {
|
|
|
|
line: 1u,
|
|
|
|
col: 5u,
|
|
|
|
msg: @~"EOF while parsing object"}));
|
2013-03-26 19:23:00 -05:00
|
|
|
assert_eq!(from_str(~"{\"a\" "),
|
2013-03-06 15:58:02 -06:00
|
|
|
Err(Error {
|
|
|
|
line: 1u,
|
|
|
|
col: 6u,
|
|
|
|
msg: @~"EOF while parsing object"}));
|
|
|
|
|
2013-03-26 19:23:00 -05:00
|
|
|
assert_eq!(from_str(~"{\"a\" 1"),
|
2013-03-06 15:58:02 -06:00
|
|
|
Err(Error {line: 1u, col: 6u, msg: @~"expected `:`"}));
|
2013-03-26 19:23:00 -05:00
|
|
|
assert_eq!(from_str(~"{\"a\":"),
|
2013-03-06 15:58:02 -06:00
|
|
|
Err(Error {line: 1u, col: 6u, msg: @~"EOF while parsing value"}));
|
2013-03-26 19:23:00 -05:00
|
|
|
assert_eq!(from_str(~"{\"a\":1"),
|
2013-03-06 15:58:02 -06:00
|
|
|
Err(Error {
|
|
|
|
line: 1u,
|
|
|
|
col: 7u,
|
|
|
|
msg: @~"EOF while parsing object"}));
|
2013-03-26 19:23:00 -05:00
|
|
|
assert_eq!(from_str(~"{\"a\":1 1"),
|
2013-03-06 15:58:02 -06:00
|
|
|
Err(Error {line: 1u, col: 8u, msg: @~"expected `,` or `}`"}));
|
2013-03-26 19:23:00 -05:00
|
|
|
assert_eq!(from_str(~"{\"a\":1,"),
|
2013-03-06 15:58:02 -06:00
|
|
|
Err(Error {
|
|
|
|
line: 1u,
|
|
|
|
col: 8u,
|
|
|
|
msg: @~"EOF while parsing object"}));
|
|
|
|
|
2013-03-26 19:23:00 -05:00
|
|
|
assert_eq!(result::unwrap(from_str(~"{}")), mk_object(~[]));
|
|
|
|
assert_eq!(result::unwrap(from_str(~"{\"a\": 3}")),
|
2013-03-06 15:58:02 -06:00
|
|
|
mk_object(~[(~"a", Number(3.0f))]));
|
|
|
|
|
2013-03-26 19:23:00 -05:00
|
|
|
assert_eq!(result::unwrap(from_str(
|
|
|
|
~"{ \"a\": null, \"b\" : true }")),
|
2012-09-24 11:55:42 -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-03-26 19:23:00 -05:00
|
|
|
assert_eq!(result::unwrap(
|
|
|
|
from_str(~"\n{ \"a\": null, \"b\" : true }\n")),
|
2012-09-24 11:55:42 -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-03-26 19:23:00 -05:00
|
|
|
assert_eq!(result::unwrap(from_str(
|
|
|
|
~"{\"a\" : 1.0 ,\"b\": [ true ]}")),
|
2012-09-24 11:55:42 -05:00
|
|
|
mk_object(~[
|
|
|
|
(~"a", Number(1.0)),
|
|
|
|
(~"b", List(~[Boolean(true)]))
|
2013-03-06 15:58:02 -06:00
|
|
|
]));
|
2013-03-26 19:23:00 -05:00
|
|
|
assert_eq!(result::unwrap(from_str(
|
2012-07-14 00:57:48 -05:00
|
|
|
~"{" +
|
|
|
|
~"\"a\": 1.0, " +
|
|
|
|
~"\"b\": [" +
|
|
|
|
~"true," +
|
|
|
|
~"\"foo\\nbar\", " +
|
|
|
|
~"{ \"c\": {\"d\": null} } " +
|
|
|
|
~"]" +
|
2013-03-26 19:23:00 -05:00
|
|
|
~"}")),
|
2012-09-24 11:55:42 -05:00
|
|
|
mk_object(~[
|
|
|
|
(~"a", Number(1.0f)),
|
|
|
|
(~"b", List(~[
|
2012-08-11 09:08:42 -05:00
|
|
|
Boolean(true),
|
2012-09-24 11:55:42 -05:00
|
|
|
String(~"foo\nbar"),
|
|
|
|
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\"] }
|
|
|
|
]
|
|
|
|
}";
|
|
|
|
let v: Outer = Decodable::decode(&Decoder(from_str(s).unwrap()));
|
|
|
|
assert_eq!(
|
|
|
|
v,
|
|
|
|
Outer {
|
|
|
|
inner: ~[
|
|
|
|
Inner { a: (), b: 2, c: ~[~"abc", ~"xyz"] }
|
|
|
|
]
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_decode_option() {
|
2013-03-27 02:13:01 -05:00
|
|
|
let decoder = Decoder(from_str(~"null").unwrap());
|
|
|
|
let value: Option<~str> = Decodable::decode(&decoder);
|
|
|
|
assert_eq!(value, None);
|
|
|
|
|
|
|
|
let decoder = Decoder(from_str(~"\"jodhpurs\"").unwrap());
|
|
|
|
let value: Option<~str> = Decodable::decode(&decoder);
|
|
|
|
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-03-26 19:42:01 -05:00
|
|
|
let decoder = Decoder(from_str(~"\"Dog\"").unwrap());
|
|
|
|
let value: Animal = Decodable::decode(&decoder);
|
|
|
|
assert_eq!(value, Dog);
|
|
|
|
|
|
|
|
let decoder = Decoder(from_str(~"[\"Frog\",\"Henry\",349]").unwrap());
|
|
|
|
let value: Animal = Decodable::decode(&decoder);
|
|
|
|
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-03-29 11:04:35 -05:00
|
|
|
let s = ~"{\"a\": \"Dog\", \"b\": [\"Frog\", \"Henry\", 349]}";
|
|
|
|
let decoder = Decoder(from_str(s).unwrap());
|
2013-03-29 11:10:31 -05:00
|
|
|
let mut map: LinearMap<~str, Animal> = Decodable::decode(&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-03-26 19:23: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,
|
|
|
|
msg: @~"EOF while parsing object"}));
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
}
|