Skip to content

Commit 424e8f0

Browse files
committed
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.
2 parents bb9c71f + 3b9e2ef commit 424e8f0

File tree

2 files changed

+61
-65
lines changed

2 files changed

+61
-65
lines changed

src/libextra/json.rs

+60-64
Original file line numberDiff line numberDiff line change
@@ -459,26 +459,24 @@ impl<E: serialize::Encoder> serialize::Encodable<E> for Json {
459459
}
460460
}
461461

462-
/// Encodes a json value into a io::writer
463-
pub fn to_writer(wr: @io::Writer, json: &Json) {
464-
let mut encoder = Encoder(wr);
465-
json.encode(&mut encoder)
466-
}
467-
468-
/// Encodes a json value into a string
469-
pub fn to_str(json: &Json) -> ~str {
470-
io::with_str_writer(|wr| to_writer(wr, json))
471-
}
462+
impl Json{
463+
/// Encodes a json value into a io::writer. Uses a single line.
464+
pub fn to_writer(&self, wr: @io::Writer) {
465+
let mut encoder = Encoder(wr);
466+
self.encode(&mut encoder)
467+
}
472468

473-
/// Encodes a json value into a io::writer
474-
pub fn to_pretty_writer(wr: @io::Writer, json: &Json) {
475-
let mut encoder = PrettyEncoder(wr);
476-
json.encode(&mut encoder)
477-
}
469+
/// Encodes a json value into a io::writer.
470+
/// Pretty-prints in a more readable format.
471+
pub fn to_pretty_writer(&self, wr: @io::Writer) {
472+
let mut encoder = PrettyEncoder(wr);
473+
self.encode(&mut encoder)
474+
}
478475

479-
/// Encodes a json value into a string
480-
pub fn to_pretty_str(json: &Json) -> ~str {
481-
io::with_str_writer(|wr| to_pretty_writer(wr, json))
476+
/// Encodes a json value into a string
477+
pub fn to_pretty_str(&self) -> ~str {
478+
io::with_str_writer(|wr| self.to_pretty_writer(wr))
479+
}
482480
}
483481

484482
pub struct Parser<T> {
@@ -1307,7 +1305,10 @@ impl<A:ToJson> ToJson for Option<A> {
13071305
}
13081306

13091307
impl to_str::ToStr for Json {
1310-
fn to_str(&self) -> ~str { to_str(self) }
1308+
/// Encodes a json value into a string
1309+
fn to_str(&self) -> ~str {
1310+
io::with_str_writer(|wr| self.to_writer(wr))
1311+
}
13111312
}
13121313

13131314
impl to_str::ToStr for Error {
@@ -1358,69 +1359,67 @@ mod tests {
13581359

13591360
#[test]
13601361
fn test_write_null() {
1361-
assert_eq!(to_str(&Null), ~"null");
1362-
assert_eq!(to_pretty_str(&Null), ~"null");
1362+
assert_eq!(Null.to_str(), ~"null");
1363+
assert_eq!(Null.to_pretty_str(), ~"null");
13631364
}
13641365
13651366
13661367
#[test]
13671368
fn test_write_number() {
1368-
assert_eq!(to_str(&Number(3f)), ~"3");
1369-
assert_eq!(to_pretty_str(&Number(3f)), ~"3");
1369+
assert_eq!(Number(3f).to_str(), ~"3");
1370+
assert_eq!(Number(3f).to_pretty_str(), ~"3");
13701371
1371-
assert_eq!(to_str(&Number(3.1f)), ~"3.1");
1372-
assert_eq!(to_pretty_str(&Number(3.1f)), ~"3.1");
1372+
assert_eq!(Number(3.1f).to_str(), ~"3.1");
1373+
assert_eq!(Number(3.1f).to_pretty_str(), ~"3.1");
13731374
1374-
assert_eq!(to_str(&Number(-1.5f)), ~"-1.5");
1375-
assert_eq!(to_pretty_str(&Number(-1.5f)), ~"-1.5");
1375+
assert_eq!(Number(-1.5f).to_str(), ~"-1.5");
1376+
assert_eq!(Number(-1.5f).to_pretty_str(), ~"-1.5");
13761377
1377-
assert_eq!(to_str(&Number(0.5f)), ~"0.5");
1378-
assert_eq!(to_pretty_str(&Number(0.5f)), ~"0.5");
1378+
assert_eq!(Number(0.5f).to_str(), ~"0.5");
1379+
assert_eq!(Number(0.5f).to_pretty_str(), ~"0.5");
13791380
}
13801381
13811382
#[test]
13821383
fn test_write_str() {
1383-
assert_eq!(to_str(&String(~"")), ~"\"\"");
1384-
assert_eq!(to_pretty_str(&String(~"")), ~"\"\"");
1384+
assert_eq!(String(~"").to_str(), ~"\"\"");
1385+
assert_eq!(String(~"").to_pretty_str(), ~"\"\"");
13851386

1386-
assert_eq!(to_str(&String(~"foo")), ~"\"foo\"");
1387-
assert_eq!(to_pretty_str(&String(~"foo")), ~"\"foo\"");
1387+
assert_eq!(String(~"foo").to_str(), ~"\"foo\"");
1388+
assert_eq!(String(~"foo").to_pretty_str(), ~"\"foo\"");
13881389
}
13891390

13901391
#[test]
13911392
fn test_write_bool() {
1392-
assert_eq!(to_str(&Boolean(true)), ~"true");
1393-
assert_eq!(to_pretty_str(&Boolean(true)), ~"true");
1393+
assert_eq!(Boolean(true).to_str(), ~"true");
1394+
assert_eq!(Boolean(true).to_pretty_str(), ~"true");
13941395
1395-
assert_eq!(to_str(&Boolean(false)), ~"false");
1396-
assert_eq!(to_pretty_str(&Boolean(false)), ~"false");
1396+
assert_eq!(Boolean(false).to_str(), ~"false");
1397+
assert_eq!(Boolean(false).to_pretty_str(), ~"false");
13971398
}
13981399
13991400
#[test]
14001401
fn test_write_list() {
1401-
assert_eq!(to_str(&List(~[])), ~"[]");
1402-
assert_eq!(to_pretty_str(&List(~[])), ~"[]");
1402+
assert_eq!(List(~[]).to_str(), ~"[]");
1403+
assert_eq!(List(~[]).to_pretty_str(), ~"[]");
14031404
1404-
assert_eq!(to_str(&List(~[Boolean(true)])), ~"[true]");
1405+
assert_eq!(List(~[Boolean(true)]).to_str(), ~"[true]");
14051406
assert_eq!(
1406-
to_pretty_str(&List(~[Boolean(true)])),
1407+
List(~[Boolean(true)]).to_pretty_str(),
14071408
~"\
14081409
[\n \
14091410
true\n\
14101411
]"
14111412
);
14121413

1413-
assert_eq!(to_str(&List(~[
1414+
let longTestList = List(~[
14141415
Boolean(false),
14151416
Null,
1416-
List(~[String(~"foo\nbar"), Number(3.5f)])
1417-
])), ~"[false,null,[\"foo\\nbar\",3.5]]");
1417+
List(~[String(~"foo\nbar"), Number(3.5f)])]);
1418+
1419+
assert_eq!(longTestList.to_str(),
1420+
~"[false,null,[\"foo\\nbar\",3.5]]");
14181421
assert_eq!(
1419-
to_pretty_str(&List(~[
1420-
Boolean(false),
1421-
Null,
1422-
List(~[String(~"foo\nbar"), Number(3.5f)])
1423-
])),
1422+
longTestList.to_pretty_str(),
14241423
~"\
14251424
[\n \
14261425
false,\n \
@@ -1435,28 +1434,30 @@ mod tests {
14351434

14361435
#[test]
14371436
fn test_write_object() {
1438-
assert_eq!(to_str(&mk_object([])), ~"{}");
1439-
assert_eq!(to_pretty_str(&mk_object([])), ~"{}");
1437+
assert_eq!(mk_object([]).to_str(), ~"{}");
1438+
assert_eq!(mk_object([]).to_pretty_str(), ~"{}");
14401439
14411440
assert_eq!(
1442-
to_str(&mk_object([(~"a", Boolean(true))])),
1441+
mk_object([(~"a", Boolean(true))]).to_str(),
14431442
~"{\"a\":true}"
14441443
);
14451444
assert_eq!(
1446-
to_pretty_str(&mk_object([(~"a", Boolean(true))])),
1445+
mk_object([(~"a", Boolean(true))]).to_pretty_str(),
14471446
~"\
14481447
{\n \
14491448
\"a\": true\n\
14501449
}"
14511450
);
14521451

1453-
assert_eq!(
1454-
to_str(&mk_object([
1452+
let complexObj = mk_object([
14551453
(~"b", List(~[
14561454
mk_object([(~"c", String(~"\x0c\r"))]),
14571455
mk_object([(~"d", String(~""))])
14581456
]))
1459-
])),
1457+
]);
1458+
1459+
assert_eq!(
1460+
complexObj.to_str(),
14601461
~"{\
14611462
\"b\":[\
14621463
{\"c\":\"\\f\\r\"},\
@@ -1465,12 +1466,7 @@ mod tests {
14651466
}"
14661467
);
14671468
assert_eq!(
1468-
to_pretty_str(&mk_object([
1469-
(~"b", List(~[
1470-
mk_object([(~"c", String(~"\x0c\r"))]),
1471-
mk_object([(~"d", String(~""))])
1472-
]))
1473-
])),
1469+
complexObj.to_pretty_str(),
14741470
~"\
14751471
{\n \
14761472
\"b\": [\n \
@@ -1494,8 +1490,8 @@ mod tests {
14941490
14951491
// We can't compare the strings directly because the object fields be
14961492
// printed in a different order.
1497-
assert_eq!(a.clone(), from_str(to_str(&a)).unwrap());
1498-
assert_eq!(a.clone(), from_str(to_pretty_str(&a)).unwrap());
1493+
assert_eq!(a.clone(), from_str(a.to_str()).unwrap());
1494+
assert_eq!(a.clone(), from_str(a.to_pretty_str()).unwrap());
14991495
}
15001496
15011497
#[test]

src/libextra/test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -907,7 +907,7 @@ impl MetricMap {
907907
/// Write MetricDiff to a file.
908908
pub fn save(&self, p: &Path) {
909909
let f = io::file_writer(p, [io::Create, io::Truncate]).unwrap();
910-
json::to_pretty_writer(f, &self.to_json());
910+
self.to_json().to_pretty_writer(f);
911911
}
912912

913913
/// Compare against another MetricMap. Optionally compare all

0 commit comments

Comments
 (0)