@@ -39,7 +39,7 @@ class JSONTests: XCTestCase {
39
39
let traits = try ? JSON ( [ " email " : " [email protected] " ] )
40
40
let userInfo = UserInfo ( anonymousId: " 1234 " , userId: " brandon " , traits: traits, referrer: nil )
41
41
42
- let encoder = JSONEncoder ( )
42
+ let encoder = JSONEncoder . default
43
43
encoder. outputFormatting = . prettyPrinted
44
44
45
45
do {
@@ -51,6 +51,34 @@ class JSONTests: XCTestCase {
51
51
}
52
52
}
53
53
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
+
54
82
func testJSONCollectionTypes( ) throws {
55
83
let testSet : Set = [ " 1 " , " 2 " , " 3 " ]
56
84
let traits = try ! JSON ( [ " type " : NSNull ( ) , " preferences " : [ " bwack " ] , " key " : testSet] )
@@ -63,13 +91,13 @@ class JSONTests: XCTestCase {
63
91
64
92
func testJSONNil( ) throws {
65
93
let traits = try JSON ( [ " type " : NSNull ( ) , " preferences " : [ " bwack " ] , " key " : nil ] as [ String : Any ? ] )
66
- let encoder = JSONEncoder ( )
94
+ let encoder = JSONEncoder . default
67
95
encoder. outputFormatting = . prettyPrinted
68
96
69
97
do {
70
98
let json = try encoder. encode ( traits)
71
99
XCTAssertNotNil ( json)
72
- let decoded = try JSONDecoder ( ) . decode ( Personal . self, from: json)
100
+ let decoded = try JSONDecoder . default . decode ( Personal . self, from: json)
73
101
XCTAssertNil ( decoded. type, " Type should be nil " )
74
102
}
75
103
}
@@ -81,7 +109,7 @@ class JSONTests: XCTestCase {
81
109
82
110
let test = TestStruct ( blah: " hello " )
83
111
let object = try JSON ( with: test)
84
- let encoder = JSONEncoder ( )
112
+ let encoder = JSONEncoder . default
85
113
encoder. outputFormatting = . prettyPrinted
86
114
87
115
do {
0 commit comments