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

View File

@ -907,7 +907,7 @@ impl MetricMap {
/// Write MetricDiff to a file. /// Write MetricDiff to a file.
pub fn save(&self, p: &Path) { pub fn save(&self, p: &Path) {
let f = io::file_writer(p, [io::Create, io::Truncate]).unwrap(); 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 /// Compare against another MetricMap. Optionally compare all