Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing ISS-543 by appending a new body json interface. #11

Merged
merged 2 commits into from
Jan 17, 2018

Conversation

RockfordWei
Copy link
Contributor

public func bodyJSON<T: Decodable>(_ type: T.Type) -> T?

`public func bodyJSON<T: Decodable>(_ type: T.Type) -> T?`
@RockfordWei
Copy link
Contributor Author

SMTP has its own repo now, so we can remove it from the tests.

@@ -300,6 +301,9 @@ public extension CURLResponse {
/// Get the response body decoded from JSON into a [String:Any] dictionary.
/// Invalid/non-JSON body data will result in an empty dictionary being returned.
public var bodyJSON: [String:Any] { do { return try bodyString.jsonDecode() as? [String:Any] ?? [:] } catch { return [:] } }
/// Get the response body decoded from JSON into a decodable structure
/// Invalid/non-JSON body data will result in nil
public func bodyJSON<T: Decodable>(_ type: T.Type) -> T? { do { return try JSONDecoder().decode(type, from: Data(bytes: bodyBytes)) } catch { return nil } }
Copy link
Member

@kjessup kjessup Jan 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should throw whatever the JSONDecoder throws. Instead of squelching the problem and giving no feedback. Users can always get this nil return behaviour by using try?.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding this func is a good way to solve this one. Nice.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first thought was to throw but I changed my mind after reading public var bodyJSON ... surely I would like to throw!!! :-)

@kjessup kjessup merged commit 3df2178 into master Jan 17, 2018
@kjessup kjessup deleted the ISS-543-New-JSON-Codable branch January 17, 2018 23:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants