Added init(jsonString) and made init(data) throw#731
Closed
Conversation
wongzigii
reviewed
Nov 30, 2016
| - returns: The created JSON | ||
| */ | ||
| public init(data:Data, options opt: JSONSerialization.ReadingOptions = .allowFragments, error: NSErrorPointer = nil) { | ||
| public init(data: Data, options: JSONSerialization.ReadingOptions = .allowFragments) throws { |
wongzigii
reviewed
Nov 30, 2016
| /// - Throws: Throws if the string cannot be converted to raw data, or if it is not a valid JSON string | ||
| public init(jsonString: String, encoding: String.Encoding = .utf8, options: JSONSerialization.ReadingOptions = .allowFragments) throws { | ||
| guard let data = jsonString.data(using: encoding) else { | ||
| throw NSError(domain: ErrorDomain, code: ErrorInvalidJSON, userInfo: [NSLocalizedDescriptionKey: "Provided string is not a valid JSON string"]) |
Member
There was a problem hiding this comment.
Since NSError Bridging was launched in Swift 3, we could consider to move NSError to Swift's Error. Another PR for this is welcomed, though.
Contributor
Author
There was a problem hiding this comment.
@wongzigii Great suggestion! Let's track it here: #749
Contributor
Author
|
@wongzigii Are we good on this PR? |
Member
|
I would like to defer to @zhigang1992 and move on. |
Member
|
Since this is a breaking change, what about adding a change log entry for this? |
Contributor
Author
|
@wongzigii Great suggestion. Updated the CHANGELOG. |
wongzigii
approved these changes
Dec 4, 2016
Contributor
Author
|
@zhigang1992 Bump |
Member
Member
|
Closing in favor of #833 |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
@zhigang1992 @wongzigii
Fixes #459 and #456
This is an implementation of my proposal in #459 (comment). At a high level the changes are as follow:
init(jsonString: String) throwswhich creates a JSON object from a valid json string. If the string cannot be converted to data or parsed by NSJSONSerialization, the initializer will throw an appropriate error as opposed to failing silently.JSON.parsewhich was doing the same thing (but would fail silently)init(data: Data)throw as well, to handle invalid JSON data coming through.This is a code breaking change since
init(data: Data)now throws.Let me know what you think.