diff options
| author | bors <bors@rust-lang.org> | 2013-08-24 03:31:25 -0700 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2013-08-24 03:31:25 -0700 |
| commit | 424e8f0fd55b94a45ebe112dc77312e3fab1ebfe (patch) | |
| tree | cb56ed2b6d6122c93b72888ea508e9661693fe0b /src | |
| parent | bb9c71fe82005d2d85f459988d9986f7c817717e (diff) | |
| parent | 3b9e2efd64c7683a4fd8dd9126cc0473dc2201af (diff) | |
| download | rust-424e8f0fd55b94a45ebe112dc77312e3fab1ebfe.tar.gz rust-424e8f0fd55b94a45ebe112dc77312e3fab1ebfe.zip | |
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.
Diffstat (limited to 'src')
| -rw-r--r-- | src/libextra/json.rs | 124 | ||||
| -rw-r--r-- | src/libextra/test.rs | 2 |
2 files changed, 61 insertions, 65 deletions
diff --git a/src/libextra/json.rs b/src/libextra/json.rs index 2287384b53a..a13836d87bd 100644 --- a/src/libextra/json.rs +++ b/src/libextra/json.rs @@ -459,26 +459,24 @@ impl<E: serialize::Encoder> serialize::Encodable<E> for Json { } } -/// 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) -} - -/// Encodes a json value into a string -pub fn to_str(json: &Json) -> ~str { - io::with_str_writer(|wr| to_writer(wr, json)) -} +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 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 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 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 @@ impl<A:ToJson> ToJson for Option<A> { } 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 @@ mod tests { #[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 @@ mod tests { #[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 @@ mod tests { }" ); 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 @@ mod tests { // 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] diff --git a/src/libextra/test.rs b/src/libextra/test.rs index f477235bdfc..ccade8f19be 100644 --- a/src/libextra/test.rs +++ b/src/libextra/test.rs @@ -907,7 +907,7 @@ impl 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 |
