2015-04-23 09:23:10 -05:00
|
|
|
#![feature(custom_derive, plugin, test, custom_attribute)]
|
2015-03-09 00:56:33 -05:00
|
|
|
#![plugin(serde_macros)]
|
2015-03-03 22:33:25 -06:00
|
|
|
|
|
|
|
extern crate test;
|
2015-03-09 00:56:33 -05:00
|
|
|
extern crate serde;
|
2015-03-03 22:33:25 -06:00
|
|
|
|
|
|
|
use std::fmt::Debug;
|
|
|
|
use std::collections::BTreeMap;
|
|
|
|
|
2015-03-09 00:56:33 -05:00
|
|
|
use serde::de;
|
|
|
|
use serde::ser;
|
2015-03-03 22:33:25 -06:00
|
|
|
|
2015-03-09 00:56:33 -05:00
|
|
|
use serde::json::{
|
2015-03-03 22:33:25 -06:00
|
|
|
self,
|
|
|
|
Value,
|
|
|
|
from_str,
|
|
|
|
from_value,
|
|
|
|
to_value,
|
|
|
|
};
|
|
|
|
|
2015-03-09 00:56:33 -05:00
|
|
|
use serde::json::error::{Error, ErrorCode};
|
2015-03-03 22:33:25 -06:00
|
|
|
|
|
|
|
macro_rules! treemap {
|
|
|
|
($($k:expr => $v:expr),*) => ({
|
2015-03-05 21:49:17 -06:00
|
|
|
let mut _m = BTreeMap::new();
|
2015-03-03 22:33:25 -06:00
|
|
|
$(_m.insert($k, $v);)*
|
|
|
|
_m
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-03-19 09:49:42 -05:00
|
|
|
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
2015-03-03 22:33:25 -06:00
|
|
|
enum Animal {
|
|
|
|
Dog,
|
2015-03-04 09:37:40 -06:00
|
|
|
Frog(String, Vec<isize>),
|
|
|
|
Cat { age: usize, name: String },
|
|
|
|
|
2015-03-03 22:33:25 -06:00
|
|
|
}
|
|
|
|
|
2015-03-19 09:49:42 -05:00
|
|
|
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
2015-03-03 22:33:25 -06:00
|
|
|
struct Inner {
|
|
|
|
a: (),
|
|
|
|
b: usize,
|
2015-03-05 21:49:17 -06:00
|
|
|
c: Vec<String>,
|
2015-03-03 22:33:25 -06:00
|
|
|
}
|
|
|
|
|
2015-03-19 09:49:42 -05:00
|
|
|
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
2015-03-03 22:33:25 -06:00
|
|
|
struct Outer {
|
|
|
|
inner: Vec<Inner>,
|
|
|
|
}
|
|
|
|
|
2015-03-06 00:16:11 -06:00
|
|
|
fn test_encode_ok<T>(errors: &[(T, &str)])
|
|
|
|
where T: PartialEq + Debug + ser::Serialize,
|
|
|
|
{
|
2015-03-03 22:33:25 -06:00
|
|
|
for &(ref value, out) in errors {
|
|
|
|
let out = out.to_string();
|
|
|
|
|
|
|
|
let s = json::to_string(value).unwrap();
|
|
|
|
assert_eq!(s, out);
|
|
|
|
|
|
|
|
let v = to_value(&value);
|
|
|
|
let s = json::to_string(&v).unwrap();
|
|
|
|
assert_eq!(s, out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-06 00:16:11 -06:00
|
|
|
fn test_pretty_encode_ok<T>(errors: &[(T, &str)])
|
|
|
|
where T: PartialEq + Debug + ser::Serialize,
|
|
|
|
{
|
2015-03-03 22:33:25 -06:00
|
|
|
for &(ref value, out) in errors {
|
|
|
|
let out = out.to_string();
|
|
|
|
|
2015-03-06 00:16:11 -06:00
|
|
|
let s = json::to_string_pretty(value).unwrap();
|
2015-03-03 22:33:25 -06:00
|
|
|
assert_eq!(s, out);
|
|
|
|
|
2015-03-06 00:16:11 -06:00
|
|
|
let v = to_value(&value);
|
|
|
|
let s = json::to_string_pretty(&v).unwrap();
|
2015-03-03 22:33:25 -06:00
|
|
|
assert_eq!(s, out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_write_null() {
|
|
|
|
let tests = &[
|
|
|
|
((), "null"),
|
|
|
|
];
|
|
|
|
test_encode_ok(tests);
|
2015-03-06 00:16:11 -06:00
|
|
|
test_pretty_encode_ok(tests);
|
2015-03-03 22:33:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_write_i64() {
|
|
|
|
let tests = &[
|
2015-03-03 23:55:48 -06:00
|
|
|
(3i64, "3"),
|
|
|
|
(-2i64, "-2"),
|
|
|
|
(-1234i64, "-1234"),
|
2015-03-03 22:33:25 -06:00
|
|
|
];
|
|
|
|
test_encode_ok(tests);
|
2015-03-06 00:16:11 -06:00
|
|
|
test_pretty_encode_ok(tests);
|
2015-03-03 22:33:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_write_f64() {
|
|
|
|
let tests = &[
|
2015-04-12 12:43:29 -05:00
|
|
|
(3.0, "3.0"),
|
2015-03-03 22:33:25 -06:00
|
|
|
(3.1, "3.1"),
|
|
|
|
(-1.5, "-1.5"),
|
|
|
|
(0.5, "0.5"),
|
|
|
|
];
|
|
|
|
test_encode_ok(tests);
|
2015-03-06 00:16:11 -06:00
|
|
|
test_pretty_encode_ok(tests);
|
2015-03-03 22:33:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_write_str() {
|
|
|
|
let tests = &[
|
|
|
|
("", "\"\""),
|
|
|
|
("foo", "\"foo\""),
|
|
|
|
];
|
|
|
|
test_encode_ok(tests);
|
2015-03-06 00:16:11 -06:00
|
|
|
test_pretty_encode_ok(tests);
|
2015-03-03 22:33:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_write_bool() {
|
|
|
|
let tests = &[
|
|
|
|
(true, "true"),
|
|
|
|
(false, "false"),
|
|
|
|
];
|
|
|
|
test_encode_ok(tests);
|
2015-03-06 00:16:11 -06:00
|
|
|
test_pretty_encode_ok(tests);
|
2015-03-03 22:33:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_write_list() {
|
|
|
|
test_encode_ok(&[
|
2015-03-06 00:16:11 -06:00
|
|
|
(vec![], "[]"),
|
|
|
|
(vec![true], "[true]"),
|
|
|
|
(vec![true, false], "[true,false]"),
|
2015-03-03 22:33:25 -06:00
|
|
|
]);
|
|
|
|
|
2015-03-17 00:30:02 -05:00
|
|
|
test_encode_ok(&[
|
|
|
|
(vec![vec![], vec![], vec![]], "[[],[],[]]"),
|
|
|
|
(vec![vec![1, 2, 3], vec![], vec![]], "[[1,2,3],[],[]]"),
|
|
|
|
(vec![vec![], vec![1, 2, 3], vec![]], "[[],[1,2,3],[]]"),
|
|
|
|
(vec![vec![], vec![], vec![1, 2, 3]], "[[],[],[1,2,3]]"),
|
|
|
|
]);
|
|
|
|
|
|
|
|
test_pretty_encode_ok(&[
|
|
|
|
(
|
|
|
|
vec![vec![], vec![], vec![]],
|
|
|
|
concat!(
|
|
|
|
"[\n",
|
|
|
|
" [],\n",
|
|
|
|
" [],\n",
|
|
|
|
" []\n",
|
|
|
|
"]"
|
|
|
|
),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
vec![vec![1, 2, 3], vec![], vec![]],
|
|
|
|
concat!(
|
|
|
|
"[\n",
|
|
|
|
" [\n",
|
|
|
|
" 1,\n",
|
|
|
|
" 2,\n",
|
|
|
|
" 3\n",
|
|
|
|
" ],\n",
|
|
|
|
" [],\n",
|
|
|
|
" []\n",
|
|
|
|
"]"
|
|
|
|
),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
vec![vec![], vec![1, 2, 3], vec![]],
|
|
|
|
concat!(
|
|
|
|
"[\n",
|
|
|
|
" [],\n",
|
|
|
|
" [\n",
|
|
|
|
" 1,\n",
|
|
|
|
" 2,\n",
|
|
|
|
" 3\n",
|
|
|
|
" ],\n",
|
|
|
|
" []\n",
|
|
|
|
"]"
|
|
|
|
),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
vec![vec![], vec![], vec![1, 2, 3]],
|
|
|
|
concat!(
|
|
|
|
"[\n",
|
|
|
|
" [],\n",
|
|
|
|
" [],\n",
|
|
|
|
" [\n",
|
|
|
|
" 1,\n",
|
|
|
|
" 2,\n",
|
|
|
|
" 3\n",
|
|
|
|
" ]\n",
|
|
|
|
"]"
|
|
|
|
),
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
|
2015-03-03 22:33:25 -06:00
|
|
|
test_pretty_encode_ok(&[
|
2015-03-06 00:16:11 -06:00
|
|
|
(vec![], "[]"),
|
2015-03-03 22:33:25 -06:00
|
|
|
(
|
2015-03-06 00:16:11 -06:00
|
|
|
vec![true],
|
2015-03-03 22:33:25 -06:00
|
|
|
concat!(
|
|
|
|
"[\n",
|
|
|
|
" true\n",
|
|
|
|
"]"
|
|
|
|
),
|
|
|
|
),
|
|
|
|
(
|
2015-03-06 00:16:11 -06:00
|
|
|
vec![true, false],
|
2015-03-03 22:33:25 -06:00
|
|
|
concat!(
|
|
|
|
"[\n",
|
|
|
|
" true,\n",
|
|
|
|
" false\n",
|
|
|
|
"]"
|
|
|
|
),
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
|
|
|
|
let long_test_list = Value::Array(vec![
|
|
|
|
Value::Bool(false),
|
|
|
|
Value::Null,
|
|
|
|
Value::Array(vec![Value::String("foo\nbar".to_string()), Value::F64(3.5)])]);
|
|
|
|
|
|
|
|
test_encode_ok(&[
|
2015-03-06 00:16:11 -06:00
|
|
|
(
|
|
|
|
long_test_list.clone(),
|
|
|
|
"[false,null,[\"foo\\nbar\",3.5]]",
|
|
|
|
),
|
2015-03-03 22:33:25 -06:00
|
|
|
]);
|
|
|
|
|
|
|
|
test_pretty_encode_ok(&[
|
|
|
|
(
|
|
|
|
long_test_list,
|
|
|
|
concat!(
|
|
|
|
"[\n",
|
|
|
|
" false,\n",
|
|
|
|
" null,\n",
|
|
|
|
" [\n",
|
|
|
|
" \"foo\\nbar\",\n",
|
|
|
|
" 3.5\n",
|
|
|
|
" ]\n",
|
|
|
|
"]"
|
2015-03-06 00:16:11 -06:00
|
|
|
),
|
2015-03-03 22:33:25 -06:00
|
|
|
)
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_write_object() {
|
|
|
|
test_encode_ok(&[
|
|
|
|
(treemap!(), "{}"),
|
|
|
|
(treemap!("a".to_string() => true), "{\"a\":true}"),
|
|
|
|
(
|
|
|
|
treemap!(
|
|
|
|
"a".to_string() => true,
|
|
|
|
"b".to_string() => false
|
|
|
|
),
|
|
|
|
"{\"a\":true,\"b\":false}"),
|
|
|
|
]);
|
|
|
|
|
2015-03-17 00:30:02 -05:00
|
|
|
test_encode_ok(&[
|
|
|
|
(
|
|
|
|
treemap![
|
|
|
|
"a".to_string() => treemap![],
|
|
|
|
"b".to_string() => treemap![],
|
|
|
|
"c".to_string() => treemap![]
|
|
|
|
],
|
|
|
|
"{\"a\":{},\"b\":{},\"c\":{}}",
|
|
|
|
),
|
|
|
|
(
|
|
|
|
treemap![
|
|
|
|
"a".to_string() => treemap![
|
|
|
|
"a".to_string() => treemap!["a" => vec![1,2,3]],
|
|
|
|
"b".to_string() => treemap![],
|
|
|
|
"c".to_string() => treemap![]
|
|
|
|
],
|
|
|
|
"b".to_string() => treemap![],
|
|
|
|
"c".to_string() => treemap![]
|
|
|
|
],
|
|
|
|
"{\"a\":{\"a\":{\"a\":[1,2,3]},\"b\":{},\"c\":{}},\"b\":{},\"c\":{}}",
|
|
|
|
),
|
|
|
|
(
|
|
|
|
treemap![
|
|
|
|
"a".to_string() => treemap![],
|
|
|
|
"b".to_string() => treemap![
|
|
|
|
"a".to_string() => treemap!["a" => vec![1,2,3]],
|
|
|
|
"b".to_string() => treemap![],
|
|
|
|
"c".to_string() => treemap![]
|
|
|
|
],
|
|
|
|
"c".to_string() => treemap![]
|
|
|
|
],
|
|
|
|
"{\"a\":{},\"b\":{\"a\":{\"a\":[1,2,3]},\"b\":{},\"c\":{}},\"c\":{}}",
|
|
|
|
),
|
|
|
|
(
|
|
|
|
treemap![
|
|
|
|
"a".to_string() => treemap![],
|
|
|
|
"b".to_string() => treemap![],
|
|
|
|
"c".to_string() => treemap![
|
|
|
|
"a".to_string() => treemap!["a" => vec![1,2,3]],
|
|
|
|
"b".to_string() => treemap![],
|
|
|
|
"c".to_string() => treemap![]
|
|
|
|
]
|
|
|
|
],
|
|
|
|
"{\"a\":{},\"b\":{},\"c\":{\"a\":{\"a\":[1,2,3]},\"b\":{},\"c\":{}}}",
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
|
|
|
|
test_pretty_encode_ok(&[
|
|
|
|
(
|
|
|
|
treemap![
|
|
|
|
"a".to_string() => treemap![],
|
|
|
|
"b".to_string() => treemap![],
|
|
|
|
"c".to_string() => treemap![]
|
|
|
|
],
|
|
|
|
concat!(
|
|
|
|
"{\n",
|
|
|
|
" \"a\": {},\n",
|
|
|
|
" \"b\": {},\n",
|
|
|
|
" \"c\": {}\n",
|
|
|
|
"}",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
treemap![
|
|
|
|
"a".to_string() => treemap![
|
|
|
|
"a".to_string() => treemap!["a" => vec![1,2,3]],
|
|
|
|
"b".to_string() => treemap![],
|
|
|
|
"c".to_string() => treemap![]
|
|
|
|
],
|
|
|
|
"b".to_string() => treemap![],
|
|
|
|
"c".to_string() => treemap![]
|
|
|
|
],
|
|
|
|
concat!(
|
|
|
|
"{\n",
|
|
|
|
" \"a\": {\n",
|
|
|
|
" \"a\": {\n",
|
|
|
|
" \"a\": [\n",
|
|
|
|
" 1,\n",
|
|
|
|
" 2,\n",
|
|
|
|
" 3\n",
|
|
|
|
" ]\n",
|
|
|
|
" },\n",
|
|
|
|
" \"b\": {},\n",
|
|
|
|
" \"c\": {}\n",
|
|
|
|
" },\n",
|
|
|
|
" \"b\": {},\n",
|
|
|
|
" \"c\": {}\n",
|
|
|
|
"}"
|
|
|
|
),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
treemap![
|
|
|
|
"a".to_string() => treemap![],
|
|
|
|
"b".to_string() => treemap![
|
|
|
|
"a".to_string() => treemap!["a" => vec![1,2,3]],
|
|
|
|
"b".to_string() => treemap![],
|
|
|
|
"c".to_string() => treemap![]
|
|
|
|
],
|
|
|
|
"c".to_string() => treemap![]
|
|
|
|
],
|
|
|
|
concat!(
|
|
|
|
"{\n",
|
|
|
|
" \"a\": {},\n",
|
|
|
|
" \"b\": {\n",
|
|
|
|
" \"a\": {\n",
|
|
|
|
" \"a\": [\n",
|
|
|
|
" 1,\n",
|
|
|
|
" 2,\n",
|
|
|
|
" 3\n",
|
|
|
|
" ]\n",
|
|
|
|
" },\n",
|
|
|
|
" \"b\": {},\n",
|
|
|
|
" \"c\": {}\n",
|
|
|
|
" },\n",
|
|
|
|
" \"c\": {}\n",
|
|
|
|
"}"
|
|
|
|
),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
treemap![
|
|
|
|
"a".to_string() => treemap![],
|
|
|
|
"b".to_string() => treemap![],
|
|
|
|
"c".to_string() => treemap![
|
|
|
|
"a".to_string() => treemap!["a" => vec![1,2,3]],
|
|
|
|
"b".to_string() => treemap![],
|
|
|
|
"c".to_string() => treemap![]
|
|
|
|
]
|
|
|
|
],
|
|
|
|
concat!(
|
|
|
|
"{\n",
|
|
|
|
" \"a\": {},\n",
|
|
|
|
" \"b\": {},\n",
|
|
|
|
" \"c\": {\n",
|
|
|
|
" \"a\": {\n",
|
|
|
|
" \"a\": [\n",
|
|
|
|
" 1,\n",
|
|
|
|
" 2,\n",
|
|
|
|
" 3\n",
|
|
|
|
" ]\n",
|
|
|
|
" },\n",
|
|
|
|
" \"b\": {},\n",
|
|
|
|
" \"c\": {}\n",
|
|
|
|
" }\n",
|
|
|
|
"}"
|
|
|
|
),
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
|
2015-03-03 22:33:25 -06:00
|
|
|
test_pretty_encode_ok(&[
|
|
|
|
(treemap!(), "{}"),
|
|
|
|
(
|
|
|
|
treemap!("a".to_string() => true),
|
|
|
|
concat!(
|
|
|
|
"{\n",
|
|
|
|
" \"a\": true\n",
|
|
|
|
"}"
|
|
|
|
),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
treemap!(
|
|
|
|
"a".to_string() => true,
|
|
|
|
"b".to_string() => false
|
|
|
|
),
|
|
|
|
concat!(
|
|
|
|
"{\n",
|
|
|
|
" \"a\": true,\n",
|
|
|
|
" \"b\": false\n",
|
|
|
|
"}"
|
|
|
|
),
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
|
|
|
|
let complex_obj = Value::Object(treemap!(
|
2015-03-06 00:16:11 -06:00
|
|
|
"b".to_string() => Value::Array(vec![
|
2015-03-03 22:33:25 -06:00
|
|
|
Value::Object(treemap!("c".to_string() => Value::String("\x0c\r".to_string()))),
|
|
|
|
Value::Object(treemap!("d".to_string() => Value::String("".to_string())))
|
2015-03-06 00:16:11 -06:00
|
|
|
])
|
2015-03-03 22:33:25 -06:00
|
|
|
));
|
|
|
|
|
|
|
|
test_encode_ok(&[
|
|
|
|
(
|
|
|
|
complex_obj.clone(),
|
|
|
|
"{\
|
|
|
|
\"b\":[\
|
|
|
|
{\"c\":\"\\f\\r\"},\
|
|
|
|
{\"d\":\"\"}\
|
|
|
|
]\
|
|
|
|
}"
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
|
|
|
|
test_pretty_encode_ok(&[
|
|
|
|
(
|
|
|
|
complex_obj.clone(),
|
|
|
|
concat!(
|
|
|
|
"{\n",
|
|
|
|
" \"b\": [\n",
|
|
|
|
" {\n",
|
|
|
|
" \"c\": \"\\f\\r\"\n",
|
|
|
|
" },\n",
|
|
|
|
" {\n",
|
|
|
|
" \"d\": \"\"\n",
|
|
|
|
" }\n",
|
|
|
|
" ]\n",
|
|
|
|
"}"
|
|
|
|
),
|
|
|
|
)
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_write_tuple() {
|
|
|
|
test_encode_ok(&[
|
|
|
|
(
|
2015-03-03 23:55:48 -06:00
|
|
|
(5,),
|
2015-03-03 22:33:25 -06:00
|
|
|
"[5]",
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
|
|
|
|
test_pretty_encode_ok(&[
|
|
|
|
(
|
2015-03-03 23:55:48 -06:00
|
|
|
(5,),
|
2015-03-03 22:33:25 -06:00
|
|
|
concat!(
|
|
|
|
"[\n",
|
|
|
|
" 5\n",
|
|
|
|
"]"
|
|
|
|
),
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
|
|
|
|
test_encode_ok(&[
|
|
|
|
(
|
2015-03-03 23:55:48 -06:00
|
|
|
(5, (6, "abc")),
|
2015-03-03 22:33:25 -06:00
|
|
|
"[5,[6,\"abc\"]]",
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
|
|
|
|
test_pretty_encode_ok(&[
|
|
|
|
(
|
2015-03-03 23:55:48 -06:00
|
|
|
(5, (6, "abc")),
|
2015-03-03 22:33:25 -06:00
|
|
|
concat!(
|
|
|
|
"[\n",
|
|
|
|
" 5,\n",
|
|
|
|
" [\n",
|
|
|
|
" 6,\n",
|
|
|
|
" \"abc\"\n",
|
|
|
|
" ]\n",
|
|
|
|
"]"
|
|
|
|
),
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_write_enum() {
|
|
|
|
test_encode_ok(&[
|
2015-03-04 09:37:40 -06:00
|
|
|
(
|
|
|
|
Animal::Dog,
|
|
|
|
"{\"Dog\":[]}",
|
|
|
|
),
|
|
|
|
(
|
2015-03-06 00:16:11 -06:00
|
|
|
Animal::Frog("Henry".to_string(), vec![]),
|
2015-03-04 09:37:40 -06:00
|
|
|
"{\"Frog\":[\"Henry\",[]]}",
|
|
|
|
),
|
|
|
|
(
|
2015-03-06 00:16:11 -06:00
|
|
|
Animal::Frog("Henry".to_string(), vec![349]),
|
2015-03-04 09:37:40 -06:00
|
|
|
"{\"Frog\":[\"Henry\",[349]]}",
|
|
|
|
),
|
|
|
|
(
|
2015-03-06 00:16:11 -06:00
|
|
|
Animal::Frog("Henry".to_string(), vec![349, 102]),
|
2015-03-04 09:37:40 -06:00
|
|
|
"{\"Frog\":[\"Henry\",[349,102]]}",
|
|
|
|
),
|
|
|
|
(
|
|
|
|
Animal::Cat { age: 5, name: "Kate".to_string() },
|
|
|
|
"{\"Cat\":{\"age\":5,\"name\":\"Kate\"}}"
|
|
|
|
),
|
2015-03-03 22:33:25 -06:00
|
|
|
]);
|
|
|
|
|
|
|
|
test_pretty_encode_ok(&[
|
|
|
|
(
|
|
|
|
Animal::Dog,
|
|
|
|
concat!(
|
|
|
|
"{\n",
|
|
|
|
" \"Dog\": []\n",
|
|
|
|
"}"
|
|
|
|
),
|
|
|
|
),
|
|
|
|
(
|
2015-03-06 00:16:11 -06:00
|
|
|
Animal::Frog("Henry".to_string(), vec![]),
|
2015-03-03 22:33:25 -06:00
|
|
|
concat!(
|
|
|
|
"{\n",
|
|
|
|
" \"Frog\": [\n",
|
|
|
|
" \"Henry\",\n",
|
|
|
|
" []\n",
|
|
|
|
" ]\n",
|
|
|
|
"}"
|
|
|
|
),
|
|
|
|
),
|
|
|
|
(
|
2015-03-06 00:16:11 -06:00
|
|
|
Animal::Frog("Henry".to_string(), vec![349]),
|
2015-03-03 22:33:25 -06:00
|
|
|
concat!(
|
|
|
|
"{\n",
|
|
|
|
" \"Frog\": [\n",
|
|
|
|
" \"Henry\",\n",
|
|
|
|
" [\n",
|
|
|
|
" 349\n",
|
|
|
|
" ]\n",
|
|
|
|
" ]\n",
|
|
|
|
"}"
|
|
|
|
),
|
|
|
|
),
|
|
|
|
(
|
2015-03-06 00:16:11 -06:00
|
|
|
Animal::Frog("Henry".to_string(), vec![349, 102]),
|
2015-03-03 22:33:25 -06:00
|
|
|
concat!(
|
|
|
|
"{\n",
|
|
|
|
" \"Frog\": [\n",
|
|
|
|
" \"Henry\",\n",
|
|
|
|
" [\n",
|
|
|
|
" 349,\n",
|
|
|
|
" 102\n",
|
|
|
|
" ]\n",
|
|
|
|
" ]\n",
|
|
|
|
"}"
|
|
|
|
),
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_write_option() {
|
|
|
|
test_encode_ok(&[
|
|
|
|
(None, "null"),
|
|
|
|
(Some("jodhpurs"), "\"jodhpurs\""),
|
|
|
|
]);
|
|
|
|
|
|
|
|
test_encode_ok(&[
|
|
|
|
(None, "null"),
|
2015-03-06 00:16:11 -06:00
|
|
|
(Some(vec!["foo", "bar"]), "[\"foo\",\"bar\"]"),
|
2015-03-03 22:33:25 -06:00
|
|
|
]);
|
|
|
|
|
|
|
|
test_pretty_encode_ok(&[
|
|
|
|
(None, "null"),
|
|
|
|
(Some("jodhpurs"), "\"jodhpurs\""),
|
|
|
|
]);
|
|
|
|
|
|
|
|
test_pretty_encode_ok(&[
|
|
|
|
(None, "null"),
|
|
|
|
(
|
2015-03-06 00:16:11 -06:00
|
|
|
Some(vec!["foo", "bar"]),
|
2015-03-03 22:33:25 -06:00
|
|
|
concat!(
|
|
|
|
"[\n",
|
|
|
|
" \"foo\",\n",
|
|
|
|
" \"bar\"\n",
|
|
|
|
"]"
|
|
|
|
),
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2015-03-18 09:35:05 -05:00
|
|
|
fn test_parse_ok<T>(errors: Vec<(&'static str, T)>)
|
|
|
|
where T: Clone + Debug + PartialEq + ser::Serialize + de::Deserialize,
|
2015-03-04 11:12:32 -06:00
|
|
|
{
|
2015-03-18 09:35:05 -05:00
|
|
|
for (s, value) in errors {
|
2015-04-02 21:13:25 -05:00
|
|
|
let v: T = from_str(s).unwrap();
|
|
|
|
assert_eq!(v, value.clone());
|
2015-03-03 22:33:25 -06:00
|
|
|
|
2015-03-04 11:12:32 -06:00
|
|
|
// Make sure we can deserialize into a `Value`.
|
2015-04-02 21:13:25 -05:00
|
|
|
let json_value: Value = from_str(s).unwrap();
|
|
|
|
assert_eq!(json_value, to_value(&value));
|
2015-03-03 22:33:25 -06:00
|
|
|
|
2015-03-04 11:12:32 -06:00
|
|
|
// Make sure we can deserialize from a `Value`.
|
2015-04-02 21:13:25 -05:00
|
|
|
let v: T = from_value(json_value.clone()).unwrap();
|
|
|
|
assert_eq!(v, value);
|
2015-03-03 22:33:25 -06:00
|
|
|
|
2015-03-04 11:12:32 -06:00
|
|
|
// Make sure we can round trip back to `Value`.
|
2015-04-02 21:13:25 -05:00
|
|
|
let json_value2: Value = from_value(json_value.clone()).unwrap();
|
|
|
|
assert_eq!(json_value2, json_value);
|
2015-03-03 22:33:25 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-05 21:49:17 -06:00
|
|
|
// FIXME (#5527): these could be merged once UFCS is finished.
|
2015-03-18 09:35:05 -05:00
|
|
|
fn test_parse_err<T>(errors: Vec<(&'static str, Error)>)
|
|
|
|
where T: Debug + PartialEq + de::Deserialize,
|
2015-03-05 21:49:17 -06:00
|
|
|
{
|
2015-03-18 09:35:05 -05:00
|
|
|
for (s, err) in errors {
|
2015-04-02 21:13:25 -05:00
|
|
|
match (err, from_str::<T>(s).unwrap_err()) {
|
|
|
|
(
|
|
|
|
Error::SyntaxError(expected_code, expected_line, expected_col),
|
|
|
|
Error::SyntaxError(actual_code, actual_line, actual_col),
|
|
|
|
) => {
|
|
|
|
assert_eq!(
|
|
|
|
(expected_code, expected_line, expected_col),
|
|
|
|
(actual_code, actual_line, actual_col)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
(expected_err, actual_err) => {
|
|
|
|
panic!("unexpected errors {} != {}", expected_err, actual_err)
|
|
|
|
}
|
|
|
|
}
|
2015-03-05 21:49:17 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-03 22:33:25 -06:00
|
|
|
#[test]
|
|
|
|
fn test_parse_null() {
|
2015-03-18 09:35:05 -05:00
|
|
|
test_parse_err::<()>(vec![
|
2015-04-16 08:50:42 -05:00
|
|
|
("n", Error::SyntaxError(ErrorCode::ExpectedSomeIdent, 1, 1)),
|
|
|
|
("nul", Error::SyntaxError(ErrorCode::ExpectedSomeIdent, 1, 3)),
|
2015-03-05 21:49:17 -06:00
|
|
|
("nulla", Error::SyntaxError(ErrorCode::TrailingCharacters, 1, 5)),
|
2015-03-03 22:33:25 -06:00
|
|
|
]);
|
|
|
|
|
2015-03-18 09:35:05 -05:00
|
|
|
test_parse_ok(vec![
|
2015-03-03 22:33:25 -06:00
|
|
|
("null", ()),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parse_bool() {
|
2015-03-18 09:35:05 -05:00
|
|
|
test_parse_err::<bool>(vec![
|
2015-04-16 08:50:42 -05:00
|
|
|
("t", Error::SyntaxError(ErrorCode::ExpectedSomeIdent, 1, 1)),
|
2015-03-05 21:49:17 -06:00
|
|
|
("truz", Error::SyntaxError(ErrorCode::ExpectedSomeIdent, 1, 4)),
|
2015-04-16 08:50:42 -05:00
|
|
|
("f", Error::SyntaxError(ErrorCode::ExpectedSomeIdent, 1, 1)),
|
2015-03-05 21:49:17 -06:00
|
|
|
("faz", Error::SyntaxError(ErrorCode::ExpectedSomeIdent, 1, 3)),
|
|
|
|
("truea", Error::SyntaxError(ErrorCode::TrailingCharacters, 1, 5)),
|
|
|
|
("falsea", Error::SyntaxError(ErrorCode::TrailingCharacters, 1, 6)),
|
2015-03-03 22:33:25 -06:00
|
|
|
]);
|
|
|
|
|
2015-03-18 09:35:05 -05:00
|
|
|
test_parse_ok(vec![
|
2015-03-03 22:33:25 -06:00
|
|
|
("true", true),
|
2015-03-05 21:49:17 -06:00
|
|
|
(" true ", true),
|
2015-03-03 22:33:25 -06:00
|
|
|
("false", false),
|
2015-03-05 21:49:17 -06:00
|
|
|
(" false ", false),
|
2015-03-03 22:33:25 -06:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parse_number_errors() {
|
2015-03-18 09:35:05 -05:00
|
|
|
test_parse_err::<f64>(vec![
|
2015-03-05 21:49:17 -06:00
|
|
|
("+", Error::SyntaxError(ErrorCode::ExpectedSomeValue, 1, 1)),
|
|
|
|
(".", Error::SyntaxError(ErrorCode::ExpectedSomeValue, 1, 1)),
|
2015-04-16 08:50:42 -05:00
|
|
|
("-", Error::SyntaxError(ErrorCode::InvalidNumber, 1, 1)),
|
2015-03-05 21:49:17 -06:00
|
|
|
("00", Error::SyntaxError(ErrorCode::InvalidNumber, 1, 2)),
|
2015-04-16 08:50:42 -05:00
|
|
|
("1.", Error::SyntaxError(ErrorCode::InvalidNumber, 1, 2)),
|
|
|
|
("1e", Error::SyntaxError(ErrorCode::InvalidNumber, 1, 2)),
|
|
|
|
("1e+", Error::SyntaxError(ErrorCode::InvalidNumber, 1, 3)),
|
2015-03-05 21:49:17 -06:00
|
|
|
("1a", Error::SyntaxError(ErrorCode::TrailingCharacters, 1, 2)),
|
2015-03-03 22:33:25 -06:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parse_i64() {
|
2015-03-18 09:35:05 -05:00
|
|
|
test_parse_ok(vec![
|
2015-03-03 22:33:25 -06:00
|
|
|
("-2", -2),
|
|
|
|
("-1234", -1234),
|
2015-03-05 21:49:17 -06:00
|
|
|
(" -1234 ", -1234),
|
2015-03-03 22:33:25 -06:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2015-03-18 22:51:48 -05:00
|
|
|
#[test]
|
|
|
|
fn test_parse_u64() {
|
|
|
|
test_parse_ok(vec![
|
|
|
|
("3", 3u64),
|
|
|
|
("1234", 1234),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2015-03-03 22:33:25 -06:00
|
|
|
#[test]
|
|
|
|
fn test_parse_f64() {
|
2015-03-18 09:35:05 -05:00
|
|
|
test_parse_ok(vec![
|
2015-03-03 22:33:25 -06:00
|
|
|
("3.0", 3.0f64),
|
|
|
|
("3.1", 3.1),
|
|
|
|
("-1.2", -1.2),
|
|
|
|
("0.4", 0.4),
|
|
|
|
("0.4e5", 0.4e5),
|
|
|
|
("0.4e15", 0.4e15),
|
|
|
|
("0.4e-01", 0.4e-01),
|
2015-03-05 21:49:17 -06:00
|
|
|
(" 0.4e-01 ", 0.4e-01),
|
2015-03-03 22:33:25 -06:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2015-03-05 09:36:30 -06:00
|
|
|
fn test_parse_string() {
|
2015-03-18 09:35:05 -05:00
|
|
|
test_parse_err::<String>(vec![
|
2015-04-16 08:50:42 -05:00
|
|
|
("\"", Error::SyntaxError(ErrorCode::EOFWhileParsingString, 1, 1)),
|
|
|
|
("\"lol", Error::SyntaxError(ErrorCode::EOFWhileParsingString, 1, 4)),
|
2015-03-05 21:49:17 -06:00
|
|
|
("\"lol\"a", Error::SyntaxError(ErrorCode::TrailingCharacters, 1, 6)),
|
2015-03-03 22:33:25 -06:00
|
|
|
]);
|
|
|
|
|
2015-03-18 09:35:05 -05:00
|
|
|
test_parse_ok(vec![
|
2015-03-03 22:33:25 -06:00
|
|
|
("\"\"", "".to_string()),
|
|
|
|
("\"foo\"", "foo".to_string()),
|
2015-03-05 09:36:30 -06:00
|
|
|
(" \"foo\" ", "foo".to_string()),
|
2015-03-03 22:33:25 -06:00
|
|
|
("\"\\\"\"", "\"".to_string()),
|
|
|
|
("\"\\b\"", "\x08".to_string()),
|
|
|
|
("\"\\n\"", "\n".to_string()),
|
|
|
|
("\"\\r\"", "\r".to_string()),
|
|
|
|
("\"\\t\"", "\t".to_string()),
|
|
|
|
("\"\\u12ab\"", "\u{12ab}".to_string()),
|
|
|
|
("\"\\uAB12\"", "\u{AB12}".to_string()),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parse_list() {
|
2015-03-18 09:35:05 -05:00
|
|
|
test_parse_err::<Vec<f64>>(vec![
|
2015-04-16 08:50:42 -05:00
|
|
|
("[", Error::SyntaxError(ErrorCode::EOFWhileParsingValue, 1, 1)),
|
|
|
|
("[ ", Error::SyntaxError(ErrorCode::EOFWhileParsingValue, 1, 2)),
|
|
|
|
("[1", Error::SyntaxError(ErrorCode::EOFWhileParsingList, 1, 2)),
|
|
|
|
("[1,", Error::SyntaxError(ErrorCode::EOFWhileParsingValue, 1, 3)),
|
2015-03-05 21:49:17 -06:00
|
|
|
("[1,]", Error::SyntaxError(ErrorCode::ExpectedSomeValue, 1, 4)),
|
|
|
|
("[1 2]", Error::SyntaxError(ErrorCode::ExpectedListCommaOrEnd, 1, 4)),
|
|
|
|
("[]a", Error::SyntaxError(ErrorCode::TrailingCharacters, 1, 3)),
|
2015-03-03 22:33:25 -06:00
|
|
|
]);
|
|
|
|
|
2015-03-18 09:35:05 -05:00
|
|
|
test_parse_ok(vec![
|
2015-03-05 21:49:17 -06:00
|
|
|
("[]", vec![]),
|
|
|
|
("[ ]", vec![]),
|
|
|
|
("[null]", vec![()]),
|
|
|
|
(" [ null ] ", vec![()]),
|
2015-03-03 22:33:25 -06:00
|
|
|
]);
|
|
|
|
|
2015-03-18 09:35:05 -05:00
|
|
|
test_parse_ok(vec![
|
2015-03-05 21:49:17 -06:00
|
|
|
("[true]", vec![true]),
|
2015-03-03 22:33:25 -06:00
|
|
|
]);
|
|
|
|
|
2015-03-18 09:35:05 -05:00
|
|
|
test_parse_ok(vec![
|
2015-03-18 22:51:48 -05:00
|
|
|
("[3,1]", vec![3u64, 1]),
|
2015-03-05 21:49:17 -06:00
|
|
|
(" [ 3 , 1 ] ", vec![3, 1]),
|
2015-03-03 22:33:25 -06:00
|
|
|
]);
|
|
|
|
|
2015-03-18 09:35:05 -05:00
|
|
|
test_parse_ok(vec![
|
2015-03-18 22:51:48 -05:00
|
|
|
("[[3], [1, 2]]", vec![vec![3u64], vec![1, 2]]),
|
2015-03-03 22:33:25 -06:00
|
|
|
]);
|
|
|
|
|
2015-03-18 09:35:05 -05:00
|
|
|
test_parse_ok(vec![
|
2015-03-18 22:51:48 -05:00
|
|
|
("[1]", (1u64,)),
|
2015-03-05 21:49:17 -06:00
|
|
|
]);
|
|
|
|
|
2015-03-18 09:35:05 -05:00
|
|
|
test_parse_ok(vec![
|
2015-03-18 22:51:48 -05:00
|
|
|
("[1, 2]", (1u64, 2u64)),
|
2015-03-05 21:49:17 -06:00
|
|
|
]);
|
2015-03-03 22:33:25 -06:00
|
|
|
|
2015-03-18 09:35:05 -05:00
|
|
|
test_parse_ok(vec![
|
2015-03-18 22:51:48 -05:00
|
|
|
("[1, 2, 3]", (1u64, 2u64, 3u64)),
|
2015-03-03 22:33:25 -06:00
|
|
|
]);
|
2015-03-05 21:49:17 -06:00
|
|
|
|
2015-03-18 09:35:05 -05:00
|
|
|
test_parse_ok(vec![
|
2015-03-18 22:51:48 -05:00
|
|
|
("[1, [2, 3]]", (1u64, (2u64, 3u64))),
|
2015-03-05 21:49:17 -06:00
|
|
|
]);
|
|
|
|
|
|
|
|
let v: () = from_str("[]").unwrap();
|
|
|
|
assert_eq!(v, ());
|
2015-03-03 22:33:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parse_object() {
|
2015-03-18 09:35:05 -05:00
|
|
|
test_parse_err::<BTreeMap<String, u32>>(vec![
|
2015-04-16 08:50:42 -05:00
|
|
|
("{", Error::SyntaxError(ErrorCode::EOFWhileParsingValue, 1, 1)),
|
|
|
|
("{ ", Error::SyntaxError(ErrorCode::EOFWhileParsingValue, 1, 2)),
|
2015-03-05 21:49:17 -06:00
|
|
|
("{1", Error::SyntaxError(ErrorCode::KeyMustBeAString, 1, 2)),
|
2015-04-16 08:50:42 -05:00
|
|
|
("{ \"a\"", Error::SyntaxError(ErrorCode::EOFWhileParsingObject, 1, 5)),
|
|
|
|
("{\"a\"", Error::SyntaxError(ErrorCode::EOFWhileParsingObject, 1, 4)),
|
|
|
|
("{\"a\" ", Error::SyntaxError(ErrorCode::EOFWhileParsingObject, 1, 5)),
|
2015-03-05 21:49:17 -06:00
|
|
|
("{\"a\" 1", Error::SyntaxError(ErrorCode::ExpectedColon, 1, 6)),
|
2015-04-16 08:50:42 -05:00
|
|
|
("{\"a\":", Error::SyntaxError(ErrorCode::EOFWhileParsingValue, 1, 5)),
|
|
|
|
("{\"a\":1", Error::SyntaxError(ErrorCode::EOFWhileParsingObject, 1, 6)),
|
2015-03-05 21:49:17 -06:00
|
|
|
("{\"a\":1 1", Error::SyntaxError(ErrorCode::ExpectedObjectCommaOrEnd, 1, 8)),
|
2015-04-16 08:50:42 -05:00
|
|
|
("{\"a\":1,", Error::SyntaxError(ErrorCode::EOFWhileParsingValue, 1, 7)),
|
2015-03-05 21:49:17 -06:00
|
|
|
("{}a", Error::SyntaxError(ErrorCode::TrailingCharacters, 1, 3)),
|
2015-03-03 22:33:25 -06:00
|
|
|
]);
|
|
|
|
|
2015-03-18 09:35:05 -05:00
|
|
|
test_parse_ok(vec![
|
2015-03-03 22:33:25 -06:00
|
|
|
("{}", treemap!()),
|
|
|
|
("{ }", treemap!()),
|
|
|
|
(
|
|
|
|
"{\"a\":3}",
|
2015-03-18 22:51:48 -05:00
|
|
|
treemap!("a".to_string() => 3u64)
|
2015-03-03 22:33:25 -06:00
|
|
|
),
|
|
|
|
(
|
|
|
|
"{ \"a\" : 3 }",
|
2015-03-04 11:12:32 -06:00
|
|
|
treemap!("a".to_string() => 3)
|
2015-03-03 22:33:25 -06:00
|
|
|
),
|
|
|
|
(
|
|
|
|
"{\"a\":3,\"b\":4}",
|
2015-03-03 23:55:48 -06:00
|
|
|
treemap!("a".to_string() => 3, "b".to_string() => 4)
|
2015-03-03 22:33:25 -06:00
|
|
|
),
|
|
|
|
(
|
2015-03-05 09:36:30 -06:00
|
|
|
" { \"a\" : 3 , \"b\" : 4 } ",
|
2015-03-03 23:55:48 -06:00
|
|
|
treemap!("a".to_string() => 3, "b".to_string() => 4),
|
2015-03-03 22:33:25 -06:00
|
|
|
),
|
|
|
|
]);
|
|
|
|
|
2015-03-18 09:35:05 -05:00
|
|
|
test_parse_ok(vec![
|
2015-03-03 22:33:25 -06:00
|
|
|
(
|
|
|
|
"{\"a\": {\"b\": 3, \"c\": 4}}",
|
2015-03-18 22:51:48 -05:00
|
|
|
treemap!(
|
|
|
|
"a".to_string() => treemap!(
|
|
|
|
"b".to_string() => 3u64,
|
|
|
|
"c".to_string() => 4
|
|
|
|
)
|
|
|
|
),
|
2015-03-03 22:33:25 -06:00
|
|
|
),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parse_struct() {
|
2015-03-18 09:35:05 -05:00
|
|
|
test_parse_err::<Outer>(vec![
|
2015-04-16 08:50:42 -05:00
|
|
|
("5", Error::SyntaxError(ErrorCode::ExpectedSomeValue, 1, 1)),
|
|
|
|
("\"hello\"", Error::SyntaxError(ErrorCode::ExpectedSomeValue, 1, 7)),
|
2015-03-30 21:50:41 -05:00
|
|
|
("{\"inner\": true}", Error::SyntaxError(ErrorCode::ExpectedSomeValue, 1, 15)),
|
2015-03-05 22:06:03 -06:00
|
|
|
]);
|
|
|
|
|
2015-03-18 09:35:05 -05:00
|
|
|
test_parse_ok(vec![
|
2015-03-03 22:33:25 -06:00
|
|
|
(
|
|
|
|
"{
|
|
|
|
\"inner\": []
|
|
|
|
}",
|
|
|
|
Outer {
|
|
|
|
inner: vec![]
|
|
|
|
},
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"{
|
|
|
|
\"inner\": [
|
|
|
|
{ \"a\": null, \"b\": 2, \"c\": [\"abc\", \"xyz\"] }
|
|
|
|
]
|
|
|
|
}",
|
|
|
|
Outer {
|
|
|
|
inner: vec![
|
|
|
|
Inner { a: (), b: 2, c: vec!["abc".to_string(), "xyz".to_string()] }
|
|
|
|
]
|
|
|
|
},
|
|
|
|
)
|
|
|
|
]);
|
2015-03-20 10:47:33 -05:00
|
|
|
|
2015-04-02 21:13:25 -05:00
|
|
|
let v: Outer = from_str("{}").unwrap();
|
|
|
|
|
2015-03-20 10:47:33 -05:00
|
|
|
assert_eq!(
|
2015-04-02 21:13:25 -05:00
|
|
|
v,
|
|
|
|
Outer {
|
2015-03-20 10:47:33 -05:00
|
|
|
inner: vec![],
|
2015-04-02 21:13:25 -05:00
|
|
|
}
|
2015-03-20 10:47:33 -05:00
|
|
|
);
|
2015-03-03 22:33:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parse_option() {
|
2015-03-18 09:35:05 -05:00
|
|
|
test_parse_ok(vec![
|
2015-03-04 11:42:51 -06:00
|
|
|
("null", None::<String>),
|
2015-03-03 22:33:25 -06:00
|
|
|
("\"jodhpurs\"", Some("jodhpurs".to_string())),
|
|
|
|
]);
|
|
|
|
|
2015-03-19 09:49:42 -05:00
|
|
|
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
2015-03-03 22:33:25 -06:00
|
|
|
struct Foo {
|
|
|
|
x: Option<isize>,
|
|
|
|
}
|
|
|
|
|
|
|
|
let value: Foo = from_str("{}").unwrap();
|
|
|
|
assert_eq!(value, Foo { x: None });
|
|
|
|
|
2015-03-18 09:35:05 -05:00
|
|
|
test_parse_ok(vec![
|
2015-03-04 11:42:51 -06:00
|
|
|
("{\"x\": null}", Foo { x: None }),
|
|
|
|
("{\"x\": 5}", Foo { x: Some(5) }),
|
2015-03-03 22:33:25 -06:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2015-03-18 09:35:05 -05:00
|
|
|
fn test_parse_enum_errors() {
|
|
|
|
test_parse_err::<Animal>(vec![
|
2015-03-16 00:04:17 -05:00
|
|
|
("{}", Error::SyntaxError(ErrorCode::ExpectedSomeValue, 1, 2)),
|
2015-04-16 08:50:42 -05:00
|
|
|
("{\"Dog\":", Error::SyntaxError(ErrorCode::EOFWhileParsingValue, 1, 7)),
|
2015-03-18 09:35:05 -05:00
|
|
|
("{\"Dog\":}", Error::SyntaxError(ErrorCode::ExpectedSomeValue, 1, 8)),
|
2015-04-12 02:30:20 -05:00
|
|
|
("{\"unknown\":[]}", Error::SyntaxError(ErrorCode::UnknownField("unknown".to_string()), 1, 11)),
|
2015-03-30 21:50:41 -05:00
|
|
|
("{\"Dog\":{}}", Error::SyntaxError(ErrorCode::ExpectedSomeValue, 1, 9)),
|
|
|
|
("{\"Frog\":{}}", Error::SyntaxError(ErrorCode::ExpectedSomeValue, 1, 10)),
|
|
|
|
("{\"Cat\":[]}", Error::SyntaxError(ErrorCode::ExpectedSomeValue, 1, 9)),
|
2015-03-05 09:36:30 -06:00
|
|
|
]);
|
2015-03-18 09:35:05 -05:00
|
|
|
}
|
2015-03-05 09:36:30 -06:00
|
|
|
|
2015-03-18 09:35:05 -05:00
|
|
|
#[test]
|
|
|
|
fn test_parse_enum() {
|
|
|
|
test_parse_ok(vec![
|
2015-03-05 09:36:30 -06:00
|
|
|
("{\"Dog\":[]}", Animal::Dog),
|
|
|
|
(" { \"Dog\" : [ ] } ", Animal::Dog),
|
2015-03-03 22:33:25 -06:00
|
|
|
(
|
2015-03-05 09:36:30 -06:00
|
|
|
"{\"Frog\":[\"Henry\",[]]}",
|
2015-03-06 00:16:11 -06:00
|
|
|
Animal::Frog("Henry".to_string(), vec![]),
|
2015-03-03 22:33:25 -06:00
|
|
|
),
|
|
|
|
(
|
2015-03-05 09:36:30 -06:00
|
|
|
" { \"Frog\": [ \"Henry\" , [ 349, 102 ] ] } ",
|
2015-03-06 00:16:11 -06:00
|
|
|
Animal::Frog("Henry".to_string(), vec![349, 102]),
|
2015-03-03 22:33:25 -06:00
|
|
|
),
|
|
|
|
(
|
2015-03-05 09:36:30 -06:00
|
|
|
"{\"Cat\": {\"age\": 5, \"name\": \"Kate\"}}",
|
|
|
|
Animal::Cat { age: 5, name: "Kate".to_string() },
|
|
|
|
),
|
|
|
|
(
|
|
|
|
" { \"Cat\" : { \"age\" : 5 , \"name\" : \"Kate\" } } ",
|
|
|
|
Animal::Cat { age: 5, name: "Kate".to_string() },
|
2015-03-03 22:33:25 -06:00
|
|
|
),
|
|
|
|
]);
|
|
|
|
|
2015-03-18 09:35:05 -05:00
|
|
|
test_parse_ok(vec![
|
2015-03-03 22:33:25 -06:00
|
|
|
(
|
|
|
|
concat!(
|
|
|
|
"{",
|
|
|
|
" \"a\": {\"Dog\": []},",
|
|
|
|
" \"b\": {\"Frog\":[\"Henry\", []]}",
|
|
|
|
"}"
|
|
|
|
),
|
|
|
|
treemap!(
|
|
|
|
"a".to_string() => Animal::Dog,
|
2015-03-06 00:16:11 -06:00
|
|
|
"b".to_string() => Animal::Frog("Henry".to_string(), vec![])
|
2015-03-03 22:33:25 -06:00
|
|
|
)
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2015-03-05 21:49:17 -06:00
|
|
|
#[test]
|
|
|
|
fn test_parse_trailing_whitespace() {
|
2015-03-18 09:35:05 -05:00
|
|
|
test_parse_ok(vec![
|
2015-03-18 22:51:48 -05:00
|
|
|
("[1, 2] ", vec![1u64, 2]),
|
2015-03-05 21:49:17 -06:00
|
|
|
("[1, 2]\n", vec![1, 2]),
|
|
|
|
("[1, 2]\t", vec![1, 2]),
|
|
|
|
("[1, 2]\t \n", vec![1, 2]),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2015-03-03 22:33:25 -06:00
|
|
|
#[test]
|
|
|
|
fn test_multiline_errors() {
|
2015-03-18 09:35:05 -05:00
|
|
|
test_parse_err::<BTreeMap<String, String>>(vec![
|
2015-04-16 08:50:42 -05:00
|
|
|
("{\n \"foo\":\n \"bar\"", Error::SyntaxError(ErrorCode::EOFWhileParsingObject, 3, 6)),
|
2015-03-03 22:33:25 -06:00
|
|
|
]);
|
|
|
|
}
|
2015-03-12 22:44:16 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_missing_field() {
|
2015-03-19 09:49:42 -05:00
|
|
|
#[derive(Debug, PartialEq, Deserialize)]
|
2015-03-12 22:44:16 -05:00
|
|
|
struct Foo {
|
|
|
|
x: Option<u32>,
|
|
|
|
}
|
|
|
|
|
|
|
|
let value: Foo = from_str("{}").unwrap();
|
|
|
|
assert_eq!(value, Foo { x: None });
|
|
|
|
|
|
|
|
let value: Foo = from_str("{\"x\": 5}").unwrap();
|
|
|
|
assert_eq!(value, Foo { x: Some(5) });
|
|
|
|
|
|
|
|
let value: Foo = from_value(Value::Object(treemap!())).unwrap();
|
|
|
|
assert_eq!(value, Foo { x: None });
|
|
|
|
|
|
|
|
let value: Foo = from_value(Value::Object(treemap!(
|
|
|
|
"x".to_string() => Value::I64(5)
|
|
|
|
))).unwrap();
|
|
|
|
assert_eq!(value, Foo { x: Some(5) });
|
|
|
|
}
|
2015-04-23 09:23:10 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_missing_renamed_field() {
|
|
|
|
#[derive(Debug, PartialEq, Deserialize)]
|
|
|
|
struct Foo {
|
2015-04-27 17:05:54 -05:00
|
|
|
#[serde(rename="y")]
|
2015-04-23 09:23:10 -05:00
|
|
|
x: Option<u32>,
|
|
|
|
}
|
|
|
|
|
|
|
|
let value: Foo = from_str("{}").unwrap();
|
|
|
|
assert_eq!(value, Foo { x: None });
|
|
|
|
|
|
|
|
let value: Foo = from_str("{\"y\": 5}").unwrap();
|
|
|
|
assert_eq!(value, Foo { x: Some(5) });
|
|
|
|
|
|
|
|
let value: Foo = from_value(Value::Object(treemap!())).unwrap();
|
|
|
|
assert_eq!(value, Foo { x: None });
|
|
|
|
|
|
|
|
let value: Foo = from_value(Value::Object(treemap!(
|
|
|
|
"y".to_string() => Value::I64(5)
|
|
|
|
))).unwrap();
|
|
|
|
assert_eq!(value, Foo { x: Some(5) });
|
|
|
|
}
|
2015-04-27 17:05:54 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_missing_fmt_renamed_field() {
|
|
|
|
#[derive(Debug, PartialEq, Deserialize)]
|
|
|
|
struct Foo {
|
2015-04-30 15:50:51 -05:00
|
|
|
#[serde(rename(json="y"))]
|
2015-04-27 17:05:54 -05:00
|
|
|
x: Option<u32>,
|
|
|
|
}
|
|
|
|
|
|
|
|
let value: Foo = from_str("{}").unwrap();
|
|
|
|
assert_eq!(value, Foo { x: None });
|
|
|
|
|
|
|
|
let value: Foo = from_str("{\"y\": 5}").unwrap();
|
|
|
|
assert_eq!(value, Foo { x: Some(5) });
|
|
|
|
|
|
|
|
let value: Foo = from_value(Value::Object(treemap!())).unwrap();
|
|
|
|
assert_eq!(value, Foo { x: None });
|
|
|
|
|
|
|
|
let value : Foo = from_value(Value::Object(treemap!(
|
2015-04-30 15:50:51 -05:00
|
|
|
"y".to_string() => Value::I64(5)
|
2015-04-27 17:05:54 -05:00
|
|
|
))).unwrap();
|
|
|
|
assert_eq!(value, Foo { x: Some(5) });
|
|
|
|
}
|
2015-05-17 23:24:00 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_lookup() {
|
|
|
|
let obj: Value = json::from_str(r#"{"x": {"a": 1}, "y": 2}"#).unwrap();
|
|
|
|
|
|
|
|
assert!(obj.lookup("x.a").unwrap() == &Value::U64(1));
|
|
|
|
assert!(obj.lookup("y").unwrap() == &Value::U64(2));
|
|
|
|
assert!(obj.lookup("z").is_none());
|
|
|
|
}
|