Skip to content

Commit

Permalink
Merge pull request #23 from watson-developer-cloud/develop
Browse files Browse the repository at this point in the history
Release 3.0.3
  • Loading branch information
Mike Kistler committed Jun 13, 2019
2 parents af96ca6 + de61a4e commit c4fa7c1
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .bumpversion.cfg
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 3.0.2
current_version = 3.0.3
commit = True
message = Release version {new_version}

Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
@@ -1,4 +1,10 @@

## 3.0.3 (https://github.com/watson-developer-cloud/restkit/compare/3.0.2...3.0.3) (2019-06-12)

### Bug Fixes

* Allow date values to be specified with or without fractional seconds

## 3.0.2 (https://github.com/watson-developer-cloud/restkit/compare/3.0.1...3.0.2) (2019-06-02)

### Bug Fixes
Expand Down
2 changes: 1 addition & 1 deletion IBMWatsonRestKit.podspec
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|

s.name = 'IBMWatsonRestKit'
s.version = '3.0.2'
s.version = '3.0.3'
s.summary = 'Networking layer for the IBM Watson Swift SDK'
s.license = { :type => 'Apache License, Version 2.0', :file => 'LICENSE' }
s.homepage = 'https://www.ibm.com/watson/'
Expand Down
6 changes: 3 additions & 3 deletions README.md
Expand Up @@ -40,7 +40,7 @@ If your project does not yet have a Podfile, use the `pod init` command in the r
use_frameworks!

target 'MyApp' do
pod 'IBMWatsonRestKit', '~> 3.0.2'
pod 'IBMWatsonRestKit', '~> 3.0.3'
end
```

Expand All @@ -60,7 +60,7 @@ $ brew install carthage
If your project does not have a Cartfile yet, use the `touch Cartfile` command in the root directory of your project. To install RestKit using Carthage, add the following to your Cartfile.

```
github "watson-developer-cloud/restkit" ~> 3.0.2
github "watson-developer-cloud/restkit" ~> 3.0.3
```

Then run the following command to build the dependencies and frameworks:
Expand All @@ -77,7 +77,7 @@ Add the following to your `Package.swift` file to identify RestKit as a dependen

```swift
dependencies: [
.package(url: "https://github.com/watson-developer-cloud/restkit", from: "3.0.2")
.package(url: "https://github.com/watson-developer-cloud/restkit", from: "3.0.3")
]
```

Expand Down
36 changes: 31 additions & 5 deletions Sources/RestKit/JSON.swift
Expand Up @@ -117,14 +117,11 @@ public enum JSON: Equatable, Codable {
return encoder
}

// Date values can be provided with or without fractional seconds
public static var decoder: JSONDecoder {
let decoder = JSONDecoder()
do {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"
formatter.timeZone = TimeZone(secondsFromGMT: 0)
formatter.locale = Locale(identifier: "en_US_POSIX")
decoder.dateDecodingStrategy = .formatted(formatter)
decoder.dateDecodingStrategy = .customDate
}
return decoder
}
Expand Down Expand Up @@ -197,3 +194,32 @@ public enum JSON: Equatable, Codable {
}
}
}

extension Formatter {
static let customNoFS: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
formatter.timeZone = TimeZone(secondsFromGMT: 0)
formatter.locale = Locale(identifier: "en_US_POSIX")
return formatter
}()
static let customFS: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSSSSZZZZZ"
formatter.timeZone = TimeZone(secondsFromGMT: 0)
formatter.locale = Locale(identifier: "en_US_POSIX")
return formatter
}()
}

extension JSONDecoder.DateDecodingStrategy {
static let customDate = custom {
let container = try $0.singleValueContainer()
let string = try container.decode(String.self)
if let date = Formatter.customFS.date(from: string)
?? Formatter.customNoFS.date(from: string) {
return date
}
throw DecodingError.dataCorruptedError(in: container, debugDescription: "Date string does not match format expected by formatter.")
}
}
24 changes: 18 additions & 6 deletions Tests/RestKitTests/JSONTests.swift
Expand Up @@ -316,21 +316,33 @@ class JSONTests: XCTestCase {

func testDecodeDate() {
let dateFormatter = DateFormatter()
dateFormatter.calendar = Calendar(identifier: .iso8601)
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
dateFormatter.locale = Locale(identifier: "en_US_POSIX")

let stringDate1 = "2018-05-17T18:45:32Z"
let stringDate2 = "2018-05-17T18:45:32.189Z"
let stringDate3 = "2018-05-17T18:45:32.189456Z"

dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
let date1 = dateFormatter.date(from: stringDate1)
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"
let stringDate = "2018-05-17T18:45:32.189Z"
guard let expected = dateFormatter.date(from: stringDate) else {
XCTFail("Failed to construct expected date")
let date2 = dateFormatter.date(from: stringDate2)
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSSSSZZZZZ"
let date3 = dateFormatter.date(from: stringDate3)
guard let expected1 = date1, let expected2 = date2, let expected3 = date3 else {
XCTFail("Failed to construct expected dates")
return
}

let json = "{ \"key\": \"\(stringDate)\" }"
let json = "{ \"key1\": \"\(stringDate1)\", \"key2\": \"\(stringDate2)\", \"key3\": \"\(stringDate3)\" }"
let data = json.data(using: .utf8)!
guard let object = try? JSON.decoder.decode([String: JSON].self, from: data) else {
XCTFail("Failed to decode object with date value")
return
}
XCTAssertEqual(object["key"], .date(expected))
XCTAssertEqual(object["key1"], .date(expected1))
XCTAssertEqual(object["key2"], .date(expected2))
XCTAssertEqual(object["key3"], .date(expected3))
}

func testDecodeString() {
Expand Down

0 comments on commit c4fa7c1

Please sign in to comment.