-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Swift Archival & Serialization API #8124
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
Conversation
Add the * Codable protocol * CodingKey protocol * Encoder & Decoder protocols * SingleValueEncodingContainer & SingleValueDecodingContainer protocols * KeyedEncodingContainer & KeyedDecodingContainer classes * Extensions on primitive Codable types * Extensions on RawRepresentable types whose RawValue is a primitive Codable type * New NSError codes for failure modes
Integrate preliminary implementations of the JSONEncoder and JSONDecoder classes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a lot of repetitive code here - please consider using gyb
/// | ||
/// - parameter type: The type to decode as. | ||
/// - returns: A value of the requested type. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you use gyb to generate these extensions?
/// | ||
/// - parameter type: The type to decode as. | ||
/// - returns: A value of the requested type. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Stray space before self
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this comment refers to a different line than what's showing up for me here on Github? The referenced doc comment does not contain self
/// | ||
/// - parameter type: The type to decode as. | ||
/// - returns: A value of the requested type. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you use gyb here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For readability, we'd like to avoid the introduction of an additional gyb
file. We're willing to allow the repetition inline
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's less readable to have dozens of copy and pasted versions of the same piece of code. What if you have to change all of them, but forget to change one place?
case string(String) | ||
|
||
/// An array container. | ||
case array(NSMutableArray) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are these Foundation arrays and not Swift arrays?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need the reference semantics of the Foundation collection types
|
||
// MARK: - SingleValueDecodingContainer Methods | ||
|
||
func decode(_ type: Bool.Type) throws -> Bool { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can overload on return value in Swift - no need to take the metatype parameter
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's true, but we'd like to avoid the potential ambiguity. It's also consistent with the keyed encoding and decoding methods.
Actually instead of special casing everything on Int8/Int16/Int32 etc can we use the new integer protocols instead? |
on Thu Mar 16 2017, Slava Pestov <notifications-AT-i.8713187.xyz> wrote:
Actually instead of special casing everything on Int8/Int16/Int32 etc can we use the new integer
protocols instead?
Yes please.
…--
-Dave
|
Hey @dabrahams @slavapestov - thanks for your feedback. If it's ok with you I'd like to keep the API part of the discussion on the swift-evolution list. Thanks! |
/// | ||
/// - parameter type: The type to decode as. | ||
/// - returns: A value of the requested type. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, the stray space is here (and in the other 10000 copies of this method :-) )
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is so weird. Github is showing this comment on the same exact line as before... 😕 Which line number are you referring to?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Search for " self)"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed, thanks!
* Split Codable protocol into Encodable and Decodable * Make CodingKey.stringValue non-optional * Make KeyedEncodingContainer no longer a class * Add UnkeyedEncodingContainers * Rename codingKeyContext to codingPath
…ultiple incorrect values
…ListEncoder and Decoder
* Remove Data as an encoding primitive * Move core Codable.swift file into stdlib * Introduce EncodingError and DecodingError as Swift errors * Add extensions in Foundation overlay to bridge to NSErrors
BTW, I'm currently working on shuffling PRs (this and #8125) so that all the Swift stdlib/derived conformance stuff will be part of one PR and can land together, and the Foundation bits can land separately. This should NOT be merged. |
The work put up here has been subsumed into #9004. |
What's in this pull request?
This PR integrates preliminary work in introducing a new Swift-focused archival and serialization API as part of the Foundation framework.
This PR focuses on what is available as external public API. As such, it provides new types for consumers of the API and developers wishing to provide their own encoders and decoders with the API.
A swift-evolution proposal will be introduced momentarily and will link to this PR.
This work is not complete. Remaining top-level features which will be iterated upon:
JSONEncoder
andJSONDecoder
PropertyListEncoder
andPropertyListDecoder
PropertyListEncoder
andPropertyListDecoder