-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix user-specified Date objects not being output in ISO8601 format (#273
) * Fix user-specified dates not being output in ISO8601 * Who knew `now` was such a recent swift addition.
- Loading branch information
Showing
8 changed files
with
78 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,7 +39,7 @@ class JSONTests: XCTestCase { | |
let traits = try? JSON(["email": "[email protected]"]) | ||
let userInfo = UserInfo(anonymousId: "1234", userId: "brandon", traits: traits, referrer: nil) | ||
|
||
let encoder = JSONEncoder() | ||
let encoder = JSONEncoder.default | ||
encoder.outputFormatting = .prettyPrinted | ||
|
||
do { | ||
|
@@ -51,6 +51,34 @@ class JSONTests: XCTestCase { | |
} | ||
} | ||
|
||
func testJSONDateHandling() throws { | ||
struct TestStruct: Codable { | ||
let myDate: Date | ||
} | ||
|
||
let now = Date(timeIntervalSinceNow: 0) | ||
|
||
let test = TestStruct(myDate: now) | ||
let object = try JSON(with: test) | ||
let encoder = JSONEncoder.default | ||
encoder.outputFormatting = .prettyPrinted | ||
|
||
do { | ||
let json = try encoder.encode(object) | ||
XCTAssertNotNil(json) | ||
let newTest = try! JSONDecoder.default.decode(TestStruct.self, from: json) | ||
XCTAssertEqual(newTest.myDate.toString(), now.toString()) | ||
} catch { | ||
print(error) | ||
XCTFail() | ||
} | ||
|
||
let dummyProps = ["myDate": now] // <- conforms to Codable | ||
let j = try! JSON(dummyProps) | ||
let anotherTest: TestStruct! = j.codableValue() | ||
XCTAssertEqual(anotherTest.myDate.toString(), now.toString()) | ||
} | ||
|
||
func testJSONCollectionTypes() throws { | ||
let testSet: Set = ["1", "2", "3"] | ||
let traits = try! JSON(["type": NSNull(), "preferences": ["bwack"], "key": testSet]) | ||
|
@@ -63,13 +91,13 @@ class JSONTests: XCTestCase { | |
|
||
func testJSONNil() throws { | ||
let traits = try JSON(["type": NSNull(), "preferences": ["bwack"], "key": nil] as [String : Any?]) | ||
let encoder = JSONEncoder() | ||
let encoder = JSONEncoder.default | ||
encoder.outputFormatting = .prettyPrinted | ||
|
||
do { | ||
let json = try encoder.encode(traits) | ||
XCTAssertNotNil(json) | ||
let decoded = try JSONDecoder().decode(Personal.self, from: json) | ||
let decoded = try JSONDecoder.default.decode(Personal.self, from: json) | ||
XCTAssertNil(decoded.type, "Type should be nil") | ||
} | ||
} | ||
|
@@ -81,7 +109,7 @@ class JSONTests: XCTestCase { | |
|
||
let test = TestStruct(blah: "hello") | ||
let object = try JSON(with: test) | ||
let encoder = JSONEncoder() | ||
let encoder = JSONEncoder.default | ||
encoder.outputFormatting = .prettyPrinted | ||
|
||
do { | ||
|