2011-11-07 17:24:44 -06:00
|
|
|
// Rust JSON serialization library
|
|
|
|
// Copyright (c) 2011 Google Inc.
|
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
|
|
|
|
2012-03-16 17:14:37 -05:00
|
|
|
import result::{result, ok, err};
|
2012-02-25 18:39:32 -06:00
|
|
|
import io;
|
|
|
|
import io::{reader_util, writer_util};
|
2011-11-07 13:01:28 -06:00
|
|
|
import map;
|
2012-03-07 18:48:57 -06:00
|
|
|
import map::hashmap;
|
2012-05-22 12:54:12 -05:00
|
|
|
import core::vec::extensions;
|
2011-11-07 13:01:28 -06:00
|
|
|
|
|
|
|
export json;
|
2012-03-05 10:47:21 -06:00
|
|
|
export error;
|
2012-02-25 18:39:32 -06:00
|
|
|
export to_writer;
|
2011-11-07 17:24:44 -06:00
|
|
|
export to_str;
|
2012-02-25 18:39:32 -06:00
|
|
|
export from_reader;
|
2011-11-07 17:24:44 -06:00
|
|
|
export from_str;
|
2012-02-25 18:39:32 -06:00
|
|
|
export eq;
|
2012-05-28 14:10:32 -05:00
|
|
|
export to_json;
|
2011-11-07 13:01:28 -06:00
|
|
|
|
2011-11-07 17:24:44 -06:00
|
|
|
export num;
|
|
|
|
export string;
|
|
|
|
export boolean;
|
|
|
|
export list;
|
|
|
|
export dict;
|
2012-02-25 18:39:32 -06:00
|
|
|
export null;
|
2011-11-07 17:24:44 -06:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Represents a json value
|
2012-01-19 17:20:57 -06:00
|
|
|
enum json {
|
2012-01-19 19:55:34 -06:00
|
|
|
num(float),
|
2012-07-14 00:57:48 -05:00
|
|
|
string(@~str),
|
2012-01-19 19:55:34 -06:00
|
|
|
boolean(bool),
|
2012-06-29 18:26:56 -05:00
|
|
|
list(@~[json]),
|
2012-07-14 00:57:48 -05:00
|
|
|
dict(map::hashmap<~str, json>),
|
2012-01-19 19:55:34 -06:00
|
|
|
null,
|
2011-11-07 13:01:28 -06:00
|
|
|
}
|
|
|
|
|
2012-02-25 18:39:32 -06:00
|
|
|
type error = {
|
|
|
|
line: uint,
|
|
|
|
col: uint,
|
2012-07-14 00:57:48 -05:00
|
|
|
msg: @~str,
|
2012-02-25 18:39:32 -06:00
|
|
|
};
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Serializes a json value into a io::writer
|
2012-02-25 18:39:32 -06:00
|
|
|
fn to_writer(wr: io::writer, j: json) {
|
2011-11-07 13:01:28 -06:00
|
|
|
alt j {
|
2012-02-25 18:39:32 -06:00
|
|
|
num(n) { wr.write_str(float::to_str(n, 6u)); }
|
|
|
|
string(s) {
|
2012-06-12 19:20:51 -05:00
|
|
|
wr.write_str(escape_str(*s));
|
2012-02-25 18:39:32 -06:00
|
|
|
}
|
|
|
|
boolean(b) {
|
2012-07-14 00:57:48 -05:00
|
|
|
wr.write_str(if b { ~"true" } else { ~"false" });
|
2012-02-25 18:39:32 -06:00
|
|
|
}
|
|
|
|
list(v) {
|
|
|
|
wr.write_char('[');
|
2012-03-14 13:03:56 -05:00
|
|
|
let mut first = true;
|
2012-06-30 18:19:07 -05:00
|
|
|
for (*v).each |item| {
|
2012-02-25 18:39:32 -06:00
|
|
|
if !first {
|
2012-07-14 00:57:48 -05:00
|
|
|
wr.write_str(~", ");
|
2012-02-25 18:39:32 -06:00
|
|
|
}
|
|
|
|
first = false;
|
|
|
|
to_writer(wr, item);
|
|
|
|
};
|
|
|
|
wr.write_char(']');
|
|
|
|
}
|
|
|
|
dict(d) {
|
|
|
|
if d.size() == 0u {
|
2012-07-14 00:57:48 -05:00
|
|
|
wr.write_str(~"{}");
|
2012-02-25 18:39:32 -06:00
|
|
|
ret;
|
2011-11-07 13:01:28 -06:00
|
|
|
}
|
2012-02-25 18:39:32 -06:00
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
wr.write_str(~"{ ");
|
2012-03-14 13:03:56 -05:00
|
|
|
let mut first = true;
|
2012-06-30 18:19:07 -05:00
|
|
|
for d.each |key, value| {
|
2012-02-25 18:39:32 -06:00
|
|
|
if !first {
|
2012-07-14 00:57:48 -05:00
|
|
|
wr.write_str(~", ");
|
2012-02-25 18:39:32 -06:00
|
|
|
}
|
|
|
|
first = false;
|
2012-06-12 19:20:51 -05:00
|
|
|
wr.write_str(escape_str(key));
|
2012-07-14 00:57:48 -05:00
|
|
|
wr.write_str(~": ");
|
2012-02-25 18:39:32 -06:00
|
|
|
to_writer(wr, value);
|
|
|
|
};
|
2012-07-14 00:57:48 -05:00
|
|
|
wr.write_str(~" }");
|
2012-02-25 18:39:32 -06:00
|
|
|
}
|
|
|
|
null {
|
2012-07-14 00:57:48 -05:00
|
|
|
wr.write_str(~"null");
|
2012-02-25 18:39:32 -06:00
|
|
|
}
|
2011-11-07 13:01:28 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
fn escape_str(s: ~str) -> ~str {
|
|
|
|
let mut escaped = ~"\"";
|
2012-06-30 18:19:07 -05:00
|
|
|
do str::chars_iter(s) |c| {
|
2012-06-12 19:20:51 -05:00
|
|
|
alt c {
|
2012-07-14 00:57:48 -05:00
|
|
|
'"' { escaped += ~"\\\""; }
|
|
|
|
'\\' { escaped += ~"\\\\"; }
|
|
|
|
'\x08' { escaped += ~"\\b"; }
|
|
|
|
'\x0c' { escaped += ~"\\f"; }
|
|
|
|
'\n' { escaped += ~"\\n"; }
|
|
|
|
'\r' { escaped += ~"\\r"; }
|
|
|
|
'\t' { escaped += ~"\\t"; }
|
2012-06-12 19:20:51 -05:00
|
|
|
_ { escaped += str::from_char(c); }
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
escaped += ~"\"";
|
2012-06-12 19:20:51 -05:00
|
|
|
|
|
|
|
escaped
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Serializes a json value into a string
|
2012-07-14 00:57:48 -05:00
|
|
|
fn to_str(j: json) -> ~str {
|
2012-06-30 18:19:07 -05:00
|
|
|
io::with_str_writer(|wr| to_writer(wr, j))
|
2011-11-07 13:01:28 -06:00
|
|
|
}
|
|
|
|
|
2012-02-25 18:39:32 -06:00
|
|
|
type parser = {
|
|
|
|
rdr: io::reader,
|
2012-03-26 20:35:18 -05:00
|
|
|
mut ch: char,
|
|
|
|
mut line: uint,
|
|
|
|
mut col: uint,
|
2012-02-25 18:39:32 -06:00
|
|
|
};
|
2011-11-07 13:01:28 -06:00
|
|
|
|
2012-02-25 18:39:32 -06:00
|
|
|
impl parser for parser {
|
|
|
|
fn eof() -> bool { self.ch == -1 as char }
|
|
|
|
|
|
|
|
fn bump() {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
fn next_char() -> char {
|
|
|
|
self.bump();
|
|
|
|
self.ch
|
|
|
|
}
|
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
fn error<T>(+msg: ~str) -> result<T, error> {
|
2012-06-12 19:20:51 -05:00
|
|
|
err({ line: self.line, col: self.col, msg: @msg })
|
2012-02-25 18:39:32 -06:00
|
|
|
}
|
|
|
|
|
2012-03-13 16:39:28 -05:00
|
|
|
fn parse() -> result<json, error> {
|
2012-02-25 18:39:32 -06:00
|
|
|
alt self.parse_value() {
|
|
|
|
ok(value) {
|
2012-02-29 02:47:17 -06:00
|
|
|
// Skip trailing whitespaces.
|
|
|
|
self.parse_whitespace();
|
2012-02-25 18:39:32 -06:00
|
|
|
// Make sure there is no trailing characters.
|
|
|
|
if self.eof() {
|
|
|
|
ok(value)
|
|
|
|
} else {
|
2012-07-14 00:57:48 -05:00
|
|
|
self.error(~"trailing characters")
|
2012-02-25 18:39:32 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
e { e }
|
2011-11-07 13:01:28 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-13 16:39:28 -05:00
|
|
|
fn parse_value() -> result<json, error> {
|
2012-02-25 18:39:32 -06:00
|
|
|
self.parse_whitespace();
|
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
if self.eof() { ret self.error(~"EOF while parsing value"); }
|
2011-11-07 13:01:28 -06:00
|
|
|
|
2012-02-25 18:39:32 -06:00
|
|
|
alt self.ch {
|
2012-07-14 00:57:48 -05:00
|
|
|
'n' { self.parse_ident(~"ull", null) }
|
|
|
|
't' { self.parse_ident(~"rue", boolean(true)) }
|
|
|
|
'f' { self.parse_ident(~"alse", boolean(false)) }
|
2012-02-25 18:39:32 -06:00
|
|
|
'0' to '9' | '-' { self.parse_number() }
|
|
|
|
'"' {
|
|
|
|
alt self.parse_str() {
|
|
|
|
ok(s) { ok(string(s)) }
|
|
|
|
err(e) { err(e) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
'[' { self.parse_list() }
|
|
|
|
'{' { self.parse_object() }
|
2012-07-14 00:57:48 -05:00
|
|
|
_ { self.error(~"invalid syntax") }
|
2011-11-07 13:01:28 -06:00
|
|
|
}
|
2012-02-25 18:39:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_whitespace() {
|
|
|
|
while char::is_whitespace(self.ch) { self.bump(); }
|
|
|
|
}
|
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
fn parse_ident(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();
|
|
|
|
ok(value)
|
|
|
|
} else {
|
2012-07-14 00:57:48 -05:00
|
|
|
self.error(~"invalid syntax")
|
2011-11-07 13:01:28 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-13 16:39:28 -05:00
|
|
|
fn parse_number() -> 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-03-14 13:03:56 -05:00
|
|
|
let mut res = alt self.parse_integer() {
|
2012-02-25 18:39:32 -06:00
|
|
|
ok(res) { res }
|
|
|
|
err(e) { ret err(e); }
|
|
|
|
};
|
|
|
|
|
|
|
|
if self.ch == '.' {
|
|
|
|
alt self.parse_decimal(res) {
|
|
|
|
ok(r) { res = r; }
|
|
|
|
err(e) { ret err(e); }
|
|
|
|
}
|
2011-11-07 13:01:28 -06:00
|
|
|
}
|
2012-02-25 18:39:32 -06:00
|
|
|
|
|
|
|
if self.ch == 'e' || self.ch == 'E' {
|
|
|
|
alt self.parse_exponent(res) {
|
|
|
|
ok(r) { res = r; }
|
|
|
|
err(e) { ret err(e); }
|
|
|
|
}
|
2011-11-07 13:01:28 -06:00
|
|
|
}
|
2012-02-25 18:39:32 -06:00
|
|
|
|
|
|
|
ok(num(neg * res))
|
2011-11-07 13:01:28 -06:00
|
|
|
}
|
|
|
|
|
2012-03-13 16:39:28 -05:00
|
|
|
fn parse_integer() -> result<float, error> {
|
2012-03-14 13:03:56 -05:00
|
|
|
let mut res = 0f;
|
2011-11-07 13:01:28 -06:00
|
|
|
|
2012-02-25 18:39:32 -06:00
|
|
|
alt self.ch {
|
|
|
|
'0' {
|
|
|
|
self.bump();
|
|
|
|
|
|
|
|
// There can be only one leading '0'.
|
|
|
|
alt self.ch {
|
2012-07-14 00:57:48 -05:00
|
|
|
'0' to '9' { ret self.error(~"invalid number"); }
|
2012-02-25 18:39:32 -06:00
|
|
|
_ {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
'1' to '9' {
|
|
|
|
while !self.eof() {
|
|
|
|
alt self.ch {
|
|
|
|
'0' to '9' {
|
|
|
|
res *= 10f;
|
|
|
|
res += ((self.ch as int) - ('0' as int)) as float;
|
|
|
|
|
|
|
|
self.bump();
|
|
|
|
}
|
|
|
|
_ { break; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-07-14 00:57:48 -05:00
|
|
|
_ { ret self.error(~"invalid number"); }
|
2011-11-07 13:01:28 -06:00
|
|
|
}
|
2012-02-25 18:39:32 -06:00
|
|
|
|
|
|
|
ok(res)
|
2011-11-07 13:01:28 -06:00
|
|
|
}
|
|
|
|
|
2012-03-13 16:39:28 -05:00
|
|
|
fn parse_decimal(res: float) -> result<float, error> {
|
2012-02-25 18:39:32 -06:00
|
|
|
self.bump();
|
|
|
|
|
|
|
|
// Make sure a digit follows the decimal place.
|
|
|
|
alt self.ch {
|
|
|
|
'0' to '9' {}
|
2012-07-14 00:57:48 -05:00
|
|
|
_ { ret 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() {
|
|
|
|
alt self.ch {
|
|
|
|
'0' to '9' {
|
|
|
|
dec /= 10f;
|
|
|
|
res += (((self.ch as int) - ('0' as int)) as float) * dec;
|
|
|
|
|
|
|
|
self.bump();
|
|
|
|
}
|
|
|
|
_ { break; }
|
2011-11-07 13:01:28 -06:00
|
|
|
}
|
|
|
|
}
|
2012-02-25 18:39:32 -06:00
|
|
|
|
|
|
|
ok(res)
|
2011-11-07 13:01:28 -06:00
|
|
|
}
|
|
|
|
|
2012-03-13 16:39:28 -05:00
|
|
|
fn parse_exponent(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
|
|
|
|
|
|
|
alt self.ch {
|
|
|
|
'+' { self.bump(); }
|
|
|
|
'-' { self.bump(); neg_exp = true; }
|
|
|
|
_ {}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure a digit follows the exponent place.
|
|
|
|
alt self.ch {
|
|
|
|
'0' to '9' {}
|
2012-07-14 00:57:48 -05:00
|
|
|
_ { ret self.error(~"invalid number"); }
|
2012-02-25 18:39:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
while !self.eof() {
|
|
|
|
alt self.ch {
|
|
|
|
'0' to '9' {
|
|
|
|
exp *= 10u;
|
|
|
|
exp += (self.ch as uint) - ('0' as uint);
|
|
|
|
|
|
|
|
self.bump();
|
|
|
|
}
|
|
|
|
_ { break; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let exp = float::pow_with_uint(10u, exp);
|
|
|
|
if neg_exp {
|
|
|
|
res /= exp;
|
|
|
|
} else {
|
|
|
|
res *= exp;
|
|
|
|
}
|
|
|
|
|
|
|
|
ok(res)
|
2011-11-07 13:01:28 -06:00
|
|
|
}
|
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
fn parse_str() -> 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) {
|
|
|
|
alt self.ch {
|
|
|
|
'"' { str::push_char(res, '"'); }
|
|
|
|
'\\' { str::push_char(res, '\\'); }
|
|
|
|
'/' { str::push_char(res, '/'); }
|
|
|
|
'b' { str::push_char(res, '\x08'); }
|
|
|
|
'f' { str::push_char(res, '\x0c'); }
|
|
|
|
'n' { str::push_char(res, '\n'); }
|
|
|
|
'r' { str::push_char(res, '\r'); }
|
|
|
|
't' { str::push_char(res, '\t'); }
|
|
|
|
'u' {
|
|
|
|
// 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 {
|
|
|
|
alt self.next_char() {
|
|
|
|
'0' to '9' {
|
|
|
|
n = n * 10u +
|
|
|
|
(self.ch as uint) - ('0' as uint);
|
|
|
|
}
|
2012-07-14 00:57:48 -05:00
|
|
|
_ { ret self.error(~"invalid \\u escape"); }
|
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-07-14 00:57:48 -05:00
|
|
|
ret self.error(~"invalid \\u escape");
|
2012-02-25 18:39:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
str::push_char(res, n as char);
|
|
|
|
}
|
2012-07-14 00:57:48 -05:00
|
|
|
_ { ret 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-06-12 19:20:51 -05:00
|
|
|
ret ok(@res);
|
2012-02-25 18:39:32 -06:00
|
|
|
}
|
|
|
|
str::push_char(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
|
|
|
}
|
|
|
|
|
2012-03-13 16:39:28 -05:00
|
|
|
fn parse_list() -> 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();
|
2012-06-12 19:20:51 -05:00
|
|
|
ret ok(list(@values));
|
2012-02-25 18:39:32 -06:00
|
|
|
}
|
|
|
|
|
2012-03-10 22:34:17 -06:00
|
|
|
loop {
|
2012-02-25 18:39:32 -06:00
|
|
|
alt self.parse_value() {
|
|
|
|
ok(v) { vec::push(values, v); }
|
|
|
|
e { ret e; }
|
|
|
|
}
|
|
|
|
|
|
|
|
self.parse_whitespace();
|
2012-03-10 22:34:17 -06:00
|
|
|
if self.eof() {
|
2012-07-14 00:57:48 -05:00
|
|
|
ret self.error(~"EOF while parsing list");
|
2012-03-10 22:34:17 -06:00
|
|
|
}
|
2012-02-25 18:39:32 -06:00
|
|
|
|
|
|
|
alt self.ch {
|
|
|
|
',' { self.bump(); }
|
2012-06-12 19:20:51 -05:00
|
|
|
']' { self.bump(); ret ok(list(@values)); }
|
2012-07-14 00:57:48 -05:00
|
|
|
_ { ret 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
|
|
|
}
|
|
|
|
|
2012-03-13 16:39:28 -05:00
|
|
|
fn parse_object() -> result<json, error> {
|
2012-02-25 18:39:32 -06:00
|
|
|
self.bump();
|
|
|
|
self.parse_whitespace();
|
|
|
|
|
2012-03-14 14:07:23 -05:00
|
|
|
let values = map::str_hash();
|
2012-02-25 18:39:32 -06:00
|
|
|
|
|
|
|
if self.ch == '}' {
|
|
|
|
self.bump();
|
|
|
|
ret ok(dict(values));
|
|
|
|
}
|
|
|
|
|
|
|
|
while !self.eof() {
|
|
|
|
self.parse_whitespace();
|
|
|
|
|
|
|
|
if self.ch != '"' {
|
2012-07-14 00:57:48 -05:00
|
|
|
ret self.error(~"key must be a string");
|
2012-02-25 18:39:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
let key = alt self.parse_str() {
|
|
|
|
ok(key) { key }
|
|
|
|
err(e) { ret err(e); }
|
|
|
|
};
|
|
|
|
|
|
|
|
self.parse_whitespace();
|
|
|
|
|
|
|
|
if self.ch != ':' {
|
|
|
|
if self.eof() { break; }
|
2012-07-14 00:57:48 -05:00
|
|
|
ret self.error(~"expected `:`");
|
2012-02-25 18:39:32 -06:00
|
|
|
}
|
|
|
|
self.bump();
|
|
|
|
|
|
|
|
alt self.parse_value() {
|
2012-06-12 19:20:51 -05:00
|
|
|
ok(value) { values.insert(copy *key, value); }
|
2012-02-25 18:39:32 -06:00
|
|
|
e { ret e; }
|
|
|
|
}
|
|
|
|
self.parse_whitespace();
|
|
|
|
|
|
|
|
alt self.ch {
|
|
|
|
',' { self.bump(); }
|
2012-02-29 02:47:17 -06:00
|
|
|
'}' { self.bump(); ret ok(dict(values)); }
|
2012-02-25 18:39:32 -06:00
|
|
|
_ {
|
|
|
|
if self.eof() { break; }
|
2012-07-14 00:57:48 -05:00
|
|
|
ret self.error(~"expected `,` or `}`");
|
2012-02-25 18:39:32 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
ret self.error(~"EOF while parsing object");
|
2011-12-21 14:36:43 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Deserializes a json value from an io::reader
|
2012-03-13 16:39:28 -05:00
|
|
|
fn from_reader(rdr: io::reader) -> result<json, error> {
|
2012-02-25 18:39:32 -06:00
|
|
|
let parser = {
|
|
|
|
rdr: rdr,
|
2012-03-26 20:35:18 -05:00
|
|
|
mut ch: rdr.read_char(),
|
|
|
|
mut line: 1u,
|
|
|
|
mut col: 1u,
|
2012-02-25 18:39:32 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
parser.parse()
|
2011-11-07 13:01:28 -06:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Deserializes a json value from a string
|
2012-07-14 00:57:48 -05:00
|
|
|
fn from_str(s: ~str) -> result<json, error> {
|
2012-02-29 12:48:57 -06:00
|
|
|
io::with_str_reader(s, from_reader)
|
2012-02-25 18:39:32 -06:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Test if two json values are equal
|
2012-02-25 18:39:32 -06:00
|
|
|
fn eq(value0: json, value1: json) -> bool {
|
|
|
|
alt (value0, value1) {
|
|
|
|
(num(f0), num(f1)) { f0 == f1 }
|
|
|
|
(string(s0), string(s1)) { s0 == s1 }
|
|
|
|
(boolean(b0), boolean(b1)) { b0 == b1 }
|
2012-06-12 19:20:51 -05:00
|
|
|
(list(l0), list(l1)) { vec::all2(*l0, *l1, eq) }
|
2012-02-25 18:39:32 -06:00
|
|
|
(dict(d0), dict(d1)) {
|
|
|
|
if d0.size() == d1.size() {
|
2012-03-14 13:03:56 -05:00
|
|
|
let mut equal = true;
|
2012-06-30 18:19:07 -05:00
|
|
|
for d0.each |k, v0| {
|
2012-02-25 18:39:32 -06:00
|
|
|
alt d1.find(k) {
|
|
|
|
some(v1) {
|
|
|
|
if !eq(v0, v1) { equal = false; } }
|
|
|
|
none { equal = false; }
|
|
|
|
}
|
|
|
|
};
|
|
|
|
equal
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(null, null) { true }
|
|
|
|
_ { false }
|
|
|
|
}
|
2011-11-07 13:01:28 -06:00
|
|
|
}
|
2012-01-17 21:05:07 -06:00
|
|
|
|
2012-05-28 14:10:32 -05:00
|
|
|
iface to_json { fn to_json() -> json; }
|
|
|
|
|
|
|
|
impl of to_json for json {
|
|
|
|
fn to_json() -> json { self }
|
|
|
|
}
|
|
|
|
|
2012-06-13 10:30:54 -05:00
|
|
|
impl of to_json for @json {
|
|
|
|
fn to_json() -> json { *self }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl of to_json for int {
|
|
|
|
fn to_json() -> json { num(self as float) }
|
|
|
|
}
|
|
|
|
|
2012-05-28 14:10:32 -05:00
|
|
|
impl of to_json for i8 {
|
|
|
|
fn to_json() -> json { num(self as float) }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl of to_json for i16 {
|
|
|
|
fn to_json() -> json { num(self as float) }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl of to_json for i32 {
|
|
|
|
fn to_json() -> json { num(self as float) }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl of to_json for i64 {
|
|
|
|
fn to_json() -> json { num(self as float) }
|
|
|
|
}
|
|
|
|
|
2012-06-13 10:30:54 -05:00
|
|
|
impl of to_json for uint {
|
|
|
|
fn to_json() -> json { num(self as float) }
|
|
|
|
}
|
|
|
|
|
2012-05-28 14:10:32 -05:00
|
|
|
impl of to_json for u8 {
|
|
|
|
fn to_json() -> json { num(self as float) }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl of to_json for u16 {
|
|
|
|
fn to_json() -> json { num(self as float) }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl of to_json for u32 {
|
|
|
|
fn to_json() -> json { num(self as float) }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl of to_json for u64 {
|
|
|
|
fn to_json() -> json { num(self as float) }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl of to_json for float {
|
|
|
|
fn to_json() -> json { num(self) }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl of to_json for f32 {
|
|
|
|
fn to_json() -> json { num(self as float) }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl of to_json for f64 {
|
|
|
|
fn to_json() -> json { num(self as float) }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl of to_json for () {
|
|
|
|
fn to_json() -> json { null }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl of to_json for bool {
|
|
|
|
fn to_json() -> json { boolean(self) }
|
|
|
|
}
|
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
impl of to_json for ~str {
|
2012-06-12 19:20:51 -05:00
|
|
|
fn to_json() -> json { string(@copy self) }
|
|
|
|
}
|
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
impl of to_json for @~str {
|
2012-05-28 14:10:32 -05:00
|
|
|
fn to_json() -> json { string(self) }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl <A: to_json copy, B: to_json copy> of to_json for (A, B) {
|
|
|
|
fn to_json() -> json {
|
|
|
|
let (a, b) = self;
|
2012-06-29 18:26:56 -05:00
|
|
|
list(@~[a.to_json(), b.to_json()])
|
2012-05-28 14:10:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl <A: to_json copy, B: to_json copy, C: to_json copy>
|
|
|
|
of to_json for (A, B, C) {
|
|
|
|
fn to_json() -> json {
|
|
|
|
let (a, b, c) = self;
|
2012-06-29 18:26:56 -05:00
|
|
|
list(@~[a.to_json(), b.to_json(), c.to_json()])
|
2012-05-28 14:10:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
impl <A: to_json> of to_json for ~[A] {
|
2012-06-30 18:19:07 -05:00
|
|
|
fn to_json() -> json { list(@self.map(|elt| elt.to_json())) }
|
2012-05-28 14:10:32 -05:00
|
|
|
}
|
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
impl <A: to_json copy> of to_json for hashmap<~str, A> {
|
2012-05-28 14:10:32 -05:00
|
|
|
fn to_json() -> json {
|
|
|
|
let d = map::str_hash();
|
2012-06-30 18:19:07 -05:00
|
|
|
for self.each() |key, value| {
|
2012-06-12 19:20:51 -05:00
|
|
|
d.insert(copy key, value.to_json());
|
2012-05-28 14:10:32 -05:00
|
|
|
}
|
|
|
|
dict(d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl <A: to_json> of to_json for option<A> {
|
|
|
|
fn to_json() -> json {
|
|
|
|
alt self {
|
|
|
|
none { null }
|
|
|
|
some(value) { value.to_json() }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl of to_str::to_str for json {
|
2012-07-14 00:57:48 -05:00
|
|
|
fn to_str() -> ~str { to_str(self) }
|
2012-05-28 14:10:32 -05:00
|
|
|
}
|
|
|
|
|
2012-06-11 10:32:38 -05:00
|
|
|
impl of to_str::to_str for error {
|
2012-07-14 00:57:48 -05:00
|
|
|
fn to_str() -> ~str {
|
2012-06-12 19:20:51 -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 {
|
2012-07-14 00:57:48 -05:00
|
|
|
fn mk_dict(items: ~[(~str, json)]) -> json {
|
2012-03-14 14:07:23 -05:00
|
|
|
let d = map::str_hash();
|
2012-02-25 18:39:32 -06:00
|
|
|
|
2012-06-30 18:19:07 -05:00
|
|
|
do vec::iter(items) |item| {
|
2012-06-12 19:20:51 -05:00
|
|
|
let (key, value) = copy item;
|
2012-02-25 18:39:32 -06:00
|
|
|
d.insert(key, value);
|
|
|
|
};
|
|
|
|
|
|
|
|
dict(d)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_write_null() {
|
2012-07-14 00:57:48 -05:00
|
|
|
assert to_str(null) == ~"null";
|
2012-02-25 18:39:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_write_num() {
|
2012-07-14 00:57:48 -05:00
|
|
|
assert to_str(num(3f)) == ~"3";
|
|
|
|
assert to_str(num(3.1f)) == ~"3.1";
|
|
|
|
assert to_str(num(-1.5f)) == ~"-1.5";
|
|
|
|
assert to_str(num(0.5f)) == ~"0.5";
|
2012-02-25 18:39:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_write_str() {
|
2012-07-14 00:57:48 -05:00
|
|
|
assert to_str(string(@~"")) == ~"\"\"";
|
|
|
|
assert to_str(string(@~"foo")) == ~"\"foo\"";
|
2012-02-25 18:39:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_write_bool() {
|
2012-07-14 00:57:48 -05:00
|
|
|
assert to_str(boolean(true)) == ~"true";
|
|
|
|
assert to_str(boolean(false)) == ~"false";
|
2012-02-25 18:39:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_write_list() {
|
2012-07-14 00:57:48 -05:00
|
|
|
assert to_str(list(@~[])) == ~"[]";
|
|
|
|
assert to_str(list(@~[boolean(true)])) == ~"[true]";
|
2012-06-29 18:26:56 -05:00
|
|
|
assert to_str(list(@~[
|
2012-02-25 18:39:32 -06:00
|
|
|
boolean(false),
|
|
|
|
null,
|
2012-07-14 00:57:48 -05:00
|
|
|
list(@~[string(@~"foo\nbar"), num(3.5f)])
|
|
|
|
])) == ~"[false, null, [\"foo\\nbar\", 3.5]]";
|
2012-02-25 18:39:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_write_dict() {
|
2012-07-14 00:57:48 -05:00
|
|
|
assert to_str(mk_dict(~[])) == ~"{}";
|
|
|
|
assert to_str(mk_dict(~[(~"a", boolean(true))]))
|
|
|
|
== ~"{ \"a\": true }";
|
2012-06-29 18:26:56 -05:00
|
|
|
assert to_str(mk_dict(~[
|
2012-07-14 00:57:48 -05:00
|
|
|
(~"a", boolean(true)),
|
|
|
|
(~"b", list(@~[
|
|
|
|
mk_dict(~[(~"c", string(@~"\x0c\r"))]),
|
|
|
|
mk_dict(~[(~"d", string(@~""))])
|
2012-06-29 18:26:56 -05:00
|
|
|
]))
|
|
|
|
])) ==
|
2012-07-14 00:57:48 -05:00
|
|
|
~"{ " +
|
|
|
|
~"\"a\": true, " +
|
|
|
|
~"\"b\": [" +
|
|
|
|
~"{ \"c\": \"\\f\\r\" }, " +
|
|
|
|
~"{ \"d\": \"\" }" +
|
|
|
|
~"]" +
|
|
|
|
~" }";
|
2012-02-25 18:39:32 -06:00
|
|
|
}
|
|
|
|
|
2012-01-17 21:05:07 -06:00
|
|
|
#[test]
|
2012-02-25 18:39:32 -06:00
|
|
|
fn test_trailing_characters() {
|
2012-07-14 00:57:48 -05:00
|
|
|
assert from_str(~"nulla") ==
|
|
|
|
err({line: 1u, col: 5u, msg: @~"trailing characters"});
|
|
|
|
assert from_str(~"truea") ==
|
|
|
|
err({line: 1u, col: 5u, msg: @~"trailing characters"});
|
|
|
|
assert from_str(~"falsea") ==
|
|
|
|
err({line: 1u, col: 6u, msg: @~"trailing characters"});
|
|
|
|
assert from_str(~"1a") ==
|
|
|
|
err({line: 1u, col: 2u, msg: @~"trailing characters"});
|
|
|
|
assert from_str(~"[]a") ==
|
|
|
|
err({line: 1u, col: 3u, msg: @~"trailing characters"});
|
|
|
|
assert from_str(~"{}a") ==
|
|
|
|
err({line: 1u, col: 3u, msg: @~"trailing characters"});
|
2012-02-25 18:39:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_read_identifiers() {
|
2012-07-14 00:57:48 -05:00
|
|
|
assert from_str(~"n") ==
|
|
|
|
err({line: 1u, col: 2u, msg: @~"invalid syntax"});
|
|
|
|
assert from_str(~"nul") ==
|
|
|
|
err({line: 1u, col: 4u, msg: @~"invalid syntax"});
|
2012-02-25 18:39:32 -06:00
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
assert from_str(~"t") ==
|
|
|
|
err({line: 1u, col: 2u, msg: @~"invalid syntax"});
|
|
|
|
assert from_str(~"truz") ==
|
|
|
|
err({line: 1u, col: 4u, msg: @~"invalid syntax"});
|
2012-02-25 18:39:32 -06:00
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
assert from_str(~"f") ==
|
|
|
|
err({line: 1u, col: 2u, msg: @~"invalid syntax"});
|
|
|
|
assert from_str(~"faz") ==
|
|
|
|
err({line: 1u, col: 3u, msg: @~"invalid syntax"});
|
2012-02-25 18:39:32 -06:00
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
assert from_str(~"null") == ok(null);
|
|
|
|
assert from_str(~"true") == ok(boolean(true));
|
|
|
|
assert from_str(~"false") == ok(boolean(false));
|
|
|
|
assert from_str(~" null ") == ok(null);
|
|
|
|
assert from_str(~" true ") == ok(boolean(true));
|
|
|
|
assert from_str(~" false ") == ok(boolean(false));
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-02-25 18:39:32 -06:00
|
|
|
fn test_read_num() {
|
2012-07-14 00:57:48 -05:00
|
|
|
assert from_str(~"+") ==
|
|
|
|
err({line: 1u, col: 1u, msg: @~"invalid syntax"});
|
|
|
|
assert from_str(~".") ==
|
|
|
|
err({line: 1u, col: 1u, msg: @~"invalid syntax"});
|
|
|
|
|
|
|
|
assert from_str(~"-") ==
|
|
|
|
err({line: 1u, col: 2u, msg: @~"invalid number"});
|
|
|
|
assert from_str(~"00") ==
|
|
|
|
err({line: 1u, col: 2u, msg: @~"invalid number"});
|
|
|
|
assert from_str(~"1.") ==
|
|
|
|
err({line: 1u, col: 3u, msg: @~"invalid number"});
|
|
|
|
assert from_str(~"1e") ==
|
|
|
|
err({line: 1u, col: 3u, msg: @~"invalid number"});
|
|
|
|
assert from_str(~"1e+") ==
|
|
|
|
err({line: 1u, col: 4u, msg: @~"invalid number"});
|
|
|
|
|
|
|
|
assert from_str(~"3") == ok(num(3f));
|
|
|
|
assert from_str(~"3.1") == ok(num(3.1f));
|
|
|
|
assert from_str(~"-1.2") == ok(num(-1.2f));
|
|
|
|
assert from_str(~"0.4") == ok(num(0.4f));
|
|
|
|
assert from_str(~"0.4e5") == ok(num(0.4e5f));
|
|
|
|
assert from_str(~"0.4e+15") == ok(num(0.4e15f));
|
|
|
|
assert from_str(~"0.4e-01") == ok(num(0.4e-01f));
|
|
|
|
assert from_str(~" 3 ") == ok(num(3f));
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-02-25 18:39:32 -06:00
|
|
|
fn test_read_str() {
|
2012-07-14 00:57:48 -05:00
|
|
|
assert from_str(~"\"") ==
|
|
|
|
err({line: 1u, col: 2u, msg: @~"EOF while parsing string"});
|
|
|
|
assert from_str(~"\"lol") ==
|
|
|
|
err({line: 1u, col: 5u, msg: @~"EOF while parsing string"});
|
2012-02-25 18:39:32 -06:00
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
assert from_str(~"\"\"") == ok(string(@~""));
|
|
|
|
assert from_str(~"\"foo\"") == ok(string(@~"foo"));
|
|
|
|
assert from_str(~"\"\\\"\"") == ok(string(@~"\""));
|
|
|
|
assert from_str(~"\"\\b\"") == ok(string(@~"\x08"));
|
|
|
|
assert from_str(~"\"\\n\"") == ok(string(@~"\n"));
|
|
|
|
assert from_str(~"\"\\r\"") == ok(string(@~"\r"));
|
|
|
|
assert from_str(~"\"\\t\"") == ok(string(@~"\t"));
|
|
|
|
assert from_str(~" \"foo\" ") == ok(string(@~"foo"));
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-02-25 18:39:32 -06:00
|
|
|
fn test_read_list() {
|
2012-07-14 00:57:48 -05:00
|
|
|
assert from_str(~"[") ==
|
|
|
|
err({line: 1u, col: 2u, msg: @~"EOF while parsing value"});
|
|
|
|
assert from_str(~"[1") ==
|
|
|
|
err({line: 1u, col: 3u, msg: @~"EOF while parsing list"});
|
|
|
|
assert from_str(~"[1,") ==
|
|
|
|
err({line: 1u, col: 4u, msg: @~"EOF while parsing value"});
|
|
|
|
assert from_str(~"[1,]") ==
|
|
|
|
err({line: 1u, col: 4u, msg: @~"invalid syntax"});
|
|
|
|
assert from_str(~"[6 7]") ==
|
|
|
|
err({line: 1u, col: 4u, msg: @~"expected `,` or `]`"});
|
|
|
|
|
|
|
|
assert from_str(~"[]") == ok(list(@~[]));
|
|
|
|
assert from_str(~"[ ]") == ok(list(@~[]));
|
|
|
|
assert from_str(~"[true]") == ok(list(@~[boolean(true)]));
|
|
|
|
assert from_str(~"[ false ]") == ok(list(@~[boolean(false)]));
|
|
|
|
assert from_str(~"[null]") == ok(list(@~[null]));
|
|
|
|
assert from_str(~"[3, 1]") == ok(list(@~[num(3f), num(1f)]));
|
|
|
|
assert from_str(~"\n[3, 2]\n") == ok(list(@~[num(3f), num(2f)]));
|
|
|
|
assert from_str(~"[2, [4, 1]]") ==
|
2012-06-29 18:26:56 -05:00
|
|
|
ok(list(@~[num(2f), list(@~[num(4f), num(1f)])]));
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-02-25 18:39:32 -06:00
|
|
|
fn test_read_dict() {
|
2012-07-14 00:57:48 -05:00
|
|
|
assert from_str(~"{") ==
|
|
|
|
err({line: 1u, col: 2u, msg: @~"EOF while parsing object"});
|
|
|
|
assert from_str(~"{ ") ==
|
|
|
|
err({line: 1u, col: 3u, msg: @~"EOF while parsing object"});
|
|
|
|
assert from_str(~"{1") ==
|
|
|
|
err({line: 1u, col: 2u, msg: @~"key must be a string"});
|
|
|
|
assert from_str(~"{ \"a\"") ==
|
|
|
|
err({line: 1u, col: 6u, msg: @~"EOF while parsing object"});
|
|
|
|
assert from_str(~"{\"a\"") ==
|
|
|
|
err({line: 1u, col: 5u, msg: @~"EOF while parsing object"});
|
|
|
|
assert from_str(~"{\"a\" ") ==
|
|
|
|
err({line: 1u, col: 6u, msg: @~"EOF while parsing object"});
|
|
|
|
|
|
|
|
assert from_str(~"{\"a\" 1") ==
|
|
|
|
err({line: 1u, col: 6u, msg: @~"expected `:`"});
|
|
|
|
assert from_str(~"{\"a\":") ==
|
|
|
|
err({line: 1u, col: 6u, msg: @~"EOF while parsing value"});
|
|
|
|
assert from_str(~"{\"a\":1") ==
|
|
|
|
err({line: 1u, col: 7u, msg: @~"EOF while parsing object"});
|
|
|
|
assert from_str(~"{\"a\":1 1") ==
|
|
|
|
err({line: 1u, col: 8u, msg: @~"expected `,` or `}`"});
|
|
|
|
assert from_str(~"{\"a\":1,") ==
|
|
|
|
err({line: 1u, col: 8u, msg: @~"EOF while parsing object"});
|
|
|
|
|
|
|
|
assert eq(result::get(from_str(~"{}")), mk_dict(~[]));
|
|
|
|
assert eq(result::get(from_str(~"{\"a\": 3}")),
|
|
|
|
mk_dict(~[(~"a", num(3.0f))]));
|
|
|
|
|
|
|
|
assert eq(result::get(from_str(~"{ \"a\": null, \"b\" : true }")),
|
2012-06-29 18:26:56 -05:00
|
|
|
mk_dict(~[
|
2012-07-14 00:57:48 -05:00
|
|
|
(~"a", null),
|
|
|
|
(~"b", boolean(true))]));
|
|
|
|
assert eq(result::get(from_str(~"\n{ \"a\": null, \"b\" : true }\n")),
|
2012-06-29 18:26:56 -05:00
|
|
|
mk_dict(~[
|
2012-07-14 00:57:48 -05:00
|
|
|
(~"a", null),
|
|
|
|
(~"b", boolean(true))]));
|
|
|
|
assert eq(result::get(from_str(~"{\"a\" : 1.0 ,\"b\": [ true ]}")),
|
2012-06-29 18:26:56 -05:00
|
|
|
mk_dict(~[
|
2012-07-14 00:57:48 -05:00
|
|
|
(~"a", num(1.0)),
|
|
|
|
(~"b", list(@~[boolean(true)]))
|
2012-06-29 18:26:56 -05:00
|
|
|
]));
|
2012-02-25 18:39:32 -06:00
|
|
|
assert eq(result::get(from_str(
|
2012-07-14 00:57:48 -05:00
|
|
|
~"{" +
|
|
|
|
~"\"a\": 1.0, " +
|
|
|
|
~"\"b\": [" +
|
|
|
|
~"true," +
|
|
|
|
~"\"foo\\nbar\", " +
|
|
|
|
~"{ \"c\": {\"d\": null} } " +
|
|
|
|
~"]" +
|
|
|
|
~"}")),
|
2012-06-29 18:26:56 -05:00
|
|
|
mk_dict(~[
|
2012-07-14 00:57:48 -05:00
|
|
|
(~"a", num(1.0f)),
|
|
|
|
(~"b", list(@~[
|
2012-02-25 18:39:32 -06:00
|
|
|
boolean(true),
|
2012-07-14 00:57:48 -05:00
|
|
|
string(@~"foo\nbar"),
|
2012-06-29 18:26:56 -05:00
|
|
|
mk_dict(~[
|
2012-07-14 00:57:48 -05:00
|
|
|
(~"c", mk_dict(~[(~"d", null)]))
|
2012-06-29 18:26:56 -05:00
|
|
|
])
|
|
|
|
]))
|
|
|
|
]));
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-02-25 18:39:32 -06:00
|
|
|
fn test_multiline_errors() {
|
2012-07-14 00:57:48 -05:00
|
|
|
assert from_str(~"{\n \"foo\":\n \"bar\"") ==
|
|
|
|
err({line: 3u, col: 8u, msg: @~"EOF while parsing object"});
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
}
|