@@ -459,26 +459,24 @@ impl<E: serialize::Encoder> serialize::Encodable<E> for Json {
459
459
}
460
460
}
461
461
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
+ }
472
468
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
+ }
478
475
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
+ }
482
480
}
483
481
484
482
pub struct Parser < T > {
@@ -1307,7 +1305,10 @@ impl<A:ToJson> ToJson for Option<A> {
1307
1305
}
1308
1306
1309
1307
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
+ }
1311
1312
}
1312
1313
1313
1314
impl to_str:: ToStr for Error {
@@ -1358,69 +1359,67 @@ mod tests {
1358
1359
1359
1360
#[ test]
1360
1361
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");
1363
1364
}
1364
1365
1365
1366
1366
1367
#[test]
1367
1368
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 ");
1370
1371
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 ");
1373
1374
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 ");
1376
1377
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 ");
1379
1380
}
1380
1381
1381
1382
#[test]
1382
1383
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 ( ) , ~"\" \" ") ;
1385
1386
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\" " ) ;
1388
1389
}
1389
1390
1390
1391
#[ test]
1391
1392
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 ");
1394
1395
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 ");
1397
1398
}
1398
1399
1399
1400
#[test]
1400
1401
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( ), ~" [ ] ");
1403
1404
1404
- assert_eq!(to_str(& List(~[Boolean(true)])), ~" [ true ] ");
1405
+ assert_eq!(List(~[Boolean(true)]).to_str( ), ~" [ true ] ");
1405
1406
assert_eq!(
1406
- to_pretty_str(& List(~[Boolean(true)])),
1407
+ List(~[Boolean(true)]).to_pretty_str( ),
1407
1408
~"\
1408
1409
[\n \
1409
1410
true\n \
1410
1411
]"
1411
1412
) ;
1412
1413
1413
- assert_eq! ( to_str ( & List ( ~[
1414
+ let longTestList = List ( ~[
1414
1415
Boolean ( false ) ,
1415
1416
Null ,
1416
- List ( ~[ String ( ~"foo\n bar"), Number(3.5f)])
1417
- ])), ~" [ false , null, [ \" foo\\ nbar\" , 3.5 ] ] ");
1417
+ List ( ~[ String ( ~"foo\n bar"), Number(3.5f)])]);
1418
+
1419
+ assert_eq!(longTestList.to_str(),
1420
+ ~" [ false , null, [ \" foo\\ nbar\" , 3.5 ] ] ");
1418
1421
assert_eq!(
1419
- to_pretty_str(&List(~[
1420
- Boolean(false),
1421
- Null,
1422
- List(~[String(~" foo\n bar"), Number(3.5f)])
1423
- ])),
1422
+ longTestList.to_pretty_str(),
1424
1423
~"\
1425
1424
[\n \
1426
1425
false,\n \
@@ -1435,28 +1434,30 @@ mod tests {
1435
1434
1436
1435
#[ test]
1437
1436
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( ), ~" { } ");
1440
1439
1441
1440
assert_eq!(
1442
- to_str(& mk_object([(~" a", Boolean(true))])),
1441
+ mk_object([(~" a", Boolean(true))]).to_str( ),
1443
1442
~" { \" a\" : true } "
1444
1443
);
1445
1444
assert_eq!(
1446
- to_pretty_str(& mk_object([(~" a", Boolean(true))])),
1445
+ mk_object([(~" a", Boolean(true))]).to_pretty_str( ),
1447
1446
~"\
1448
1447
{\n \
1449
1448
\" a\" : true\n \
1450
1449
}"
1451
1450
) ;
1452
1451
1453
- assert_eq!(
1454
- to_str( & mk_object( [
1452
+ let complexObj = mk_object( [
1455
1453
( ~"b", List(~[
1456
1454
mk_object([(~" c", String(~"\x0c \r " ) ) ] ) ,
1457
1455
mk_object( [ ( ~"d", String(~" "))])
1458
1456
]))
1459
- ])),
1457
+ ]);
1458
+
1459
+ assert_eq!(
1460
+ complexObj.to_str(),
1460
1461
~" { \
1461
1462
\"b\" : [ \
1462
1463
{ \" c\" : \" \\ f\\ r\" } , \
@@ -1465,12 +1466,7 @@ mod tests {
1465
1466
} "
1466
1467
);
1467
1468
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(),
1474
1470
~"\
1475
1471
{\n \
1476
1472
\" b\" : [\n \
@@ -1494,8 +1490,8 @@ mod tests {
1494
1490
1495
1491
// We can't compare the strings directly because the object fields be
1496
1492
// 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());
1499
1495
}
1500
1496
1501
1497
#[test]
0 commit comments