@@ -39,7 +39,7 @@ class JSONTests: XCTestCase {
3939 let traits = try ? JSON ( [ " email " : " [email protected] " ] ) 4040 let userInfo = UserInfo ( anonymousId: " 1234 " , userId: " brandon " , traits: traits, referrer: nil )
4141
42- let encoder = JSONEncoder ( )
42+ let encoder = JSONEncoder . default
4343 encoder. outputFormatting = . prettyPrinted
4444
4545 do {
@@ -51,6 +51,34 @@ class JSONTests: XCTestCase {
5151 }
5252 }
5353
54+ func testJSONDateHandling( ) throws {
55+ struct TestStruct : Codable {
56+ let myDate : Date
57+ }
58+
59+ let now = Date ( timeIntervalSinceNow: 0 )
60+
61+ let test = TestStruct ( myDate: now)
62+ let object = try JSON ( with: test)
63+ let encoder = JSONEncoder . default
64+ encoder. outputFormatting = . prettyPrinted
65+
66+ do {
67+ let json = try encoder. encode ( object)
68+ XCTAssertNotNil ( json)
69+ let newTest = try ! JSONDecoder . default. decode ( TestStruct . self, from: json)
70+ XCTAssertEqual ( newTest. myDate. toString ( ) , now. toString ( ) )
71+ } catch {
72+ print ( error)
73+ XCTFail ( )
74+ }
75+
76+ let dummyProps = [ " myDate " : now] // <- conforms to Codable
77+ let j = try ! JSON ( dummyProps)
78+ let anotherTest : TestStruct ! = j. codableValue ( )
79+ XCTAssertEqual ( anotherTest. myDate. toString ( ) , now. toString ( ) )
80+ }
81+
5482 func testJSONCollectionTypes( ) throws {
5583 let testSet : Set = [ " 1 " , " 2 " , " 3 " ]
5684 let traits = try ! JSON ( [ " type " : NSNull ( ) , " preferences " : [ " bwack " ] , " key " : testSet] )
@@ -63,13 +91,13 @@ class JSONTests: XCTestCase {
6391
6492 func testJSONNil( ) throws {
6593 let traits = try JSON ( [ " type " : NSNull ( ) , " preferences " : [ " bwack " ] , " key " : nil ] as [ String : Any ? ] )
66- let encoder = JSONEncoder ( )
94+ let encoder = JSONEncoder . default
6795 encoder. outputFormatting = . prettyPrinted
6896
6997 do {
7098 let json = try encoder. encode ( traits)
7199 XCTAssertNotNil ( json)
72- let decoded = try JSONDecoder ( ) . decode ( Personal . self, from: json)
100+ let decoded = try JSONDecoder . default . decode ( Personal . self, from: json)
73101 XCTAssertNil ( decoded. type, " Type should be nil " )
74102 }
75103 }
@@ -81,7 +109,7 @@ class JSONTests: XCTestCase {
81109
82110 let test = TestStruct ( blah: " hello " )
83111 let object = try JSON ( with: test)
84- let encoder = JSONEncoder ( )
112+ let encoder = JSONEncoder . default
85113 encoder. outputFormatting = . prettyPrinted
86114
87115 do {
0 commit comments