@@ -465,7 +465,8 @@ extension TestNSJSONSerialization {
465
465
( " test_serialize_emptyArray " , test_serialize_emptyArray) ,
466
466
( " test_serialize_multiPODArray " , test_serialize_multiPODArray) ,
467
467
468
- ( " test_serialize_values " , test_serialize_nested) ,
468
+ ( " test_serialize_nested " , test_serialize_nested) ,
469
+ ( " test_invalid_json " , test_invalidJSON)
469
470
]
470
471
}
471
472
@@ -517,8 +518,29 @@ extension TestNSJSONSerialization {
517
518
518
519
func test_serialize_complicatedObject( ) {
519
520
let json : [ String : Any ] = [ " a " : 4.0 , " b " : NSNull ( ) , " c " : " string " , " d " : false ]
520
- XCTAssertEqual ( try trySerialize ( json) , " { \" b \" :null, \" a \" :4.0, \" d \" :false, \" c \" : \" string \" } " )
521
- XCTAssertEqual ( try trySerializePretty ( json) , " { \n \t \" b \" : null, \n \t \" a \" : 4.0, \n \t \" d \" : false, \n \t \" c \" : \" string \" \n } " )
521
+
522
+ var normalString = " { "
523
+ var prettyString = " { "
524
+ for key in json. keys {
525
+ normalString += " \" \( key) \" : "
526
+ prettyString += " \n \t \" \( key) \" : "
527
+ let valueString : String
528
+ if key == " a " {
529
+ valueString = " 4.0 "
530
+ } else if key == " b " {
531
+ valueString = " null "
532
+ } else if key == " c " {
533
+ valueString = " \" string \" "
534
+ } else {
535
+ valueString = " false "
536
+ }
537
+ normalString += " \( valueString) , "
538
+ prettyString += " \( valueString) , "
539
+ }
540
+ normalString = normalString. substringToIndex ( normalString. endIndex. predecessor ( ) ) + " } "
541
+ prettyString = prettyString. substringToIndex ( prettyString. endIndex. predecessor ( ) ) + " \n } "
542
+ XCTAssertEqual ( try trySerialize ( json) , normalString)
543
+ XCTAssertEqual ( try trySerializePretty ( json) , prettyString)
522
544
}
523
545
524
546
//MARK: - Array Deserialization
@@ -549,12 +571,67 @@ extension TestNSJSONSerialization {
549
571
//MARK: - Nested Serialization
550
572
func test_serialize_nested( ) {
551
573
let first : [ String : Any ] = [ " a " : 4.0 , " b " : NSNull ( ) , " c " : " string " , " d " : false ]
574
+
575
+ var normalString = " { "
576
+ var prettyString = " \n \t { "
577
+
578
+ for key in first. keys {
579
+ normalString += " \" \( key) \" : "
580
+ prettyString += " \n \t \t \" \( key) \" : "
581
+ let valueString : String
582
+ if key == " a " {
583
+ valueString = " 4.0 "
584
+ } else if key == " b " {
585
+ valueString = " null "
586
+ } else if key == " c " {
587
+ valueString = " \" string \" "
588
+ } else {
589
+ valueString = " false "
590
+ }
591
+ normalString += " \( valueString) , "
592
+ prettyString += " \( valueString) , "
593
+ }
594
+ normalString = normalString. substringToIndex ( normalString. endIndex. predecessor ( ) ) + " } "
595
+ prettyString = prettyString. substringToIndex ( prettyString. endIndex. predecessor ( ) ) + " \n \t } "
596
+
552
597
let second : [ Any ] = [ NSNull ( ) , " hello " , 1.0 ]
553
598
let json : [ Any ] = [ first, second, NSNull ( ) , " hello " , 1.0 ]
554
- XCTAssertEqual ( try trySerialize ( json) , " [{ \" b \" :null, \" a \" :4.0, \" d \" :false, \" c \" : \" string \" },[null, \" hello \" ,1.0],null, \" hello \" ,1.0] " )
555
- XCTAssertEqual ( try trySerializePretty ( json) , " [ \n \t { \n \t \t \" b \" : null, \n \t \t \" a \" : 4.0, \n \t \t \" d \" : false, \n \t \t \" c \" : \" string \" \n \t }, \n \t [ \n \t \t null, \n \t \t \" hello \" , \n \t \t 1.0 \n \t ], \n \t null, \n \t \" hello \" , \n \t 1.0 \n ] " )
599
+
600
+ XCTAssertEqual ( try trySerialize ( json) , " [ \( normalString) ,[null, \" hello \" ,1.0],null, \" hello \" ,1.0] " )
601
+ XCTAssertEqual ( try trySerializePretty ( json) , " [ \( prettyString) , \n \t [ \n \t \t null, \n \t \t \" hello \" , \n \t \t 1.0 \n \t ], \n \t null, \n \t \" hello \" , \n \t 1.0 \n ] " )
556
602
557
603
}
604
+
605
+ // MARK: - Error checkers
606
+ func test_invalidJSON( ) {
607
+ let str = " Invalid json "
608
+ do {
609
+ let _ = try NSJSONSerialization . dataWithJSONObject ( str, options: [ ] )
610
+ XCTFail ( " Parsed string to JSON object " )
611
+ } catch {
612
+ }
613
+ let doub = 4.0
614
+ do {
615
+ let _ = try NSJSONSerialization . dataWithJSONObject ( doub, options: [ ] )
616
+ XCTFail ( " Parsed double to JSON object " )
617
+ } catch {
618
+ }
619
+ struct Temp {
620
+ var x : Int
621
+ }
622
+ let t = Temp ( x: 1 )
623
+ do {
624
+ let _ = try NSJSONSerialization . dataWithJSONObject ( t, options: [ ] )
625
+ XCTFail ( " Parsed non serializable object to JSON " )
626
+ } catch {
627
+ }
628
+ let invalidJSON = [ " some " : t]
629
+ do {
630
+ let _ = try NSJSONSerialization . dataWithJSONObject ( invalidJSON, options: [ ] )
631
+ XCTFail ( " Parsed non serializable object in dictionary to JSON " )
632
+ } catch {
633
+ }
634
+ }
558
635
}
559
636
560
637
// MARK: - isValidJSONObjectTests
0 commit comments