auto merge of #8679 : singingboyo/rust/json-to-impl, r=alexcrichton

to_str, to_pretty_str, to_writer, and to_pretty_writer were at the top
level of extra::json, this moves them into an impl for Json to match
with what's been done for the rest of libextra and libstd.  (or at least for vec and str)

Also meant changing some tests.

Closes #8676.
This commit is contained in:
bors 2013-08-24 03:31:25 -07:00
commit 424e8f0fd5
2 changed files with 61 additions and 65 deletions

View File

@ -459,26 +459,24 @@ fn encode(&self, e: &mut E) {
}
}
/// Encodes a json value into a io::writer
pub fn to_writer(wr: @io::Writer, json: &Json) {
let mut encoder = Encoder(wr);
json.encode(&mut encoder)
}
impl Json{
/// Encodes a json value into a io::writer. Uses a single line.
pub fn to_writer(&self, wr: @io::Writer) {
let mut encoder = Encoder(wr);
self.encode(&mut encoder)
}
/// Encodes a json value into a string
pub fn to_str(json: &Json) -> ~str {
io::with_str_writer(|wr| to_writer(wr, json))
}
/// Encodes a json value into a io::writer.
/// Pretty-prints in a more readable format.
pub fn to_pretty_writer(&self, wr: @io::Writer) {
let mut encoder = PrettyEncoder(wr);
self.encode(&mut encoder)
}
/// Encodes a json value into a io::writer
pub fn to_pretty_writer(wr: @io::Writer, json: &Json) {
let mut encoder = PrettyEncoder(wr);
json.encode(&mut encoder)
}
/// Encodes a json value into a string
pub fn to_pretty_str(json: &Json) -> ~str {
io::with_str_writer(|wr| to_pretty_writer(wr, json))
/// Encodes a json value into a string
pub fn to_pretty_str(&self) -> ~str {
io::with_str_writer(|wr| self.to_pretty_writer(wr))
}
}
pub struct Parser<T> {
@ -1307,7 +1305,10 @@ fn to_json(&self) -> Json {
}
impl to_str::ToStr for Json {
fn to_str(&self) -> ~str { to_str(self) }
/// Encodes a json value into a string
fn to_str(&self) -> ~str {
io::with_str_writer(|wr| self.to_writer(wr))
}
}
impl to_str::ToStr for Error {
@ -1358,69 +1359,67 @@ fn mk_object(items: &[(~str, Json)]) -> Json {
#[test]
fn test_write_null() {
assert_eq!(to_str(&Null), ~"null");
assert_eq!(to_pretty_str(&Null), ~"null");
assert_eq!(Null.to_str(), ~"null");
assert_eq!(Null.to_pretty_str(), ~"null");
}
#[test]
fn test_write_number() {
assert_eq!(to_str(&Number(3f)), ~"3");
assert_eq!(to_pretty_str(&Number(3f)), ~"3");
assert_eq!(Number(3f).to_str(), ~"3");
assert_eq!(Number(3f).to_pretty_str(), ~"3");
assert_eq!(to_str(&Number(3.1f)), ~"3.1");
assert_eq!(to_pretty_str(&Number(3.1f)), ~"3.1");
assert_eq!(Number(3.1f).to_str(), ~"3.1");
assert_eq!(Number(3.1f).to_pretty_str(), ~"3.1");
assert_eq!(to_str(&Number(-1.5f)), ~"-1.5");
assert_eq!(to_pretty_str(&Number(-1.5f)), ~"-1.5");
assert_eq!(Number(-1.5f).to_str(), ~"-1.5");
assert_eq!(Number(-1.5f).to_pretty_str(), ~"-1.5");
assert_eq!(to_str(&Number(0.5f)), ~"0.5");
assert_eq!(to_pretty_str(&Number(0.5f)), ~"0.5");
assert_eq!(Number(0.5f).to_str(), ~"0.5");
assert_eq!(Number(0.5f).to_pretty_str(), ~"0.5");
}
#[test]
fn test_write_str() {
assert_eq!(to_str(&String(~"")), ~"\"\"");
assert_eq!(to_pretty_str(&String(~"")), ~"\"\"");
assert_eq!(String(~"").to_str(), ~"\"\"");
assert_eq!(String(~"").to_pretty_str(), ~"\"\"");
assert_eq!(to_str(&String(~"foo")), ~"\"foo\"");
assert_eq!(to_pretty_str(&String(~"foo")), ~"\"foo\"");
assert_eq!(String(~"foo").to_str(), ~"\"foo\"");
assert_eq!(String(~"foo").to_pretty_str(), ~"\"foo\"");
}
#[test]
fn test_write_bool() {
assert_eq!(to_str(&Boolean(true)), ~"true");
assert_eq!(to_pretty_str(&Boolean(true)), ~"true");
assert_eq!(Boolean(true).to_str(), ~"true");
assert_eq!(Boolean(true).to_pretty_str(), ~"true");
assert_eq!(to_str(&Boolean(false)), ~"false");
assert_eq!(to_pretty_str(&Boolean(false)), ~"false");
assert_eq!(Boolean(false).to_str(), ~"false");
assert_eq!(Boolean(false).to_pretty_str(), ~"false");
}
#[test]
fn test_write_list() {
assert_eq!(to_str(&List(~[])), ~"[]");
assert_eq!(to_pretty_str(&List(~[])), ~"[]");
assert_eq!(List(~[]).to_str(), ~"[]");
assert_eq!(List(~[]).to_pretty_str(), ~"[]");
assert_eq!(to_str(&List(~[Boolean(true)])), ~"[true]");
assert_eq!(List(~[Boolean(true)]).to_str(), ~"[true]");
assert_eq!(
to_pretty_str(&List(~[Boolean(true)])),
List(~[Boolean(true)]).to_pretty_str(),
~"\
[\n \
true\n\
]"
);
assert_eq!(to_str(&List(~[
let longTestList = List(~[
Boolean(false),
Null,
List(~[String(~"foo\nbar"), Number(3.5f)])
])), ~"[false,null,[\"foo\\nbar\",3.5]]");
List(~[String(~"foo\nbar"), Number(3.5f)])]);
assert_eq!(longTestList.to_str(),
~"[false,null,[\"foo\\nbar\",3.5]]");
assert_eq!(
to_pretty_str(&List(~[
Boolean(false),
Null,
List(~[String(~"foo\nbar"), Number(3.5f)])
])),
longTestList.to_pretty_str(),
~"\
[\n \
false,\n \
@ -1435,28 +1434,30 @@ fn test_write_list() {
#[test]
fn test_write_object() {
assert_eq!(to_str(&mk_object([])), ~"{}");
assert_eq!(to_pretty_str(&mk_object([])), ~"{}");
assert_eq!(mk_object([]).to_str(), ~"{}");
assert_eq!(mk_object([]).to_pretty_str(), ~"{}");
assert_eq!(
to_str(&mk_object([(~"a", Boolean(true))])),
mk_object([(~"a", Boolean(true))]).to_str(),
~"{\"a\":true}"
);
assert_eq!(
to_pretty_str(&mk_object([(~"a", Boolean(true))])),
mk_object([(~"a", Boolean(true))]).to_pretty_str(),
~"\
{\n \
\"a\": true\n\
}"
);
assert_eq!(
to_str(&mk_object([
let complexObj = mk_object([
(~"b", List(~[
mk_object([(~"c", String(~"\x0c\r"))]),
mk_object([(~"d", String(~""))])
]))
])),
]);
assert_eq!(
complexObj.to_str(),
~"{\
\"b\":[\
{\"c\":\"\\f\\r\"},\
@ -1465,12 +1466,7 @@ fn test_write_object() {
}"
);
assert_eq!(
to_pretty_str(&mk_object([
(~"b", List(~[
mk_object([(~"c", String(~"\x0c\r"))]),
mk_object([(~"d", String(~""))])
]))
])),
complexObj.to_pretty_str(),
~"\
{\n \
\"b\": [\n \
@ -1494,8 +1490,8 @@ fn test_write_object() {
// We can't compare the strings directly because the object fields be
// printed in a different order.
assert_eq!(a.clone(), from_str(to_str(&a)).unwrap());
assert_eq!(a.clone(), from_str(to_pretty_str(&a)).unwrap());
assert_eq!(a.clone(), from_str(a.to_str()).unwrap());
assert_eq!(a.clone(), from_str(a.to_pretty_str()).unwrap());
}
#[test]

View File

@ -907,7 +907,7 @@ pub fn load(p: &Path) -> MetricMap {
/// Write MetricDiff to a file.
pub fn save(&self, p: &Path) {
let f = io::file_writer(p, [io::Create, io::Truncate]).unwrap();
json::to_pretty_writer(f, &self.to_json());
self.to_json().to_pretty_writer(f);
}
/// Compare against another MetricMap. Optionally compare all