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