Skip to content

[4.0] JSONEncoder conditional conformance workarounds #10818

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

itaiferber
Copy link
Contributor

What's in this pull request?
Cherry-picks #10766 to swift-4.0-branch.

Explanation: One of the limitations of not having conditional conformance at the moment is that the implementation of init(from:) and encode(to:) on types which require it is that failure to cast dependent types to Encodable or Decodable is a runtime failure. There is no way to statically guarantee that the wrapped type is Encodable or Decodable.

As such, in those implementations, at best we can directly call (element as! Encodable).encode(to: encoder), or similar. However, this encodes the element directly into an encoder, without giving the encoder a chance to intercept the type. This is problematic for JSONEncoder because it cannot apply a strategy if it doesn't get to intercept the type.

This gives a temporary workaround for JSON strategies because of internal Foundation knowledge.
Scope: Affects anyone encoding and decoding Dates, Data, URLs, and Decimals with JSONEncoder and JSONDecoder
Radar: rdar://problem/32535991
Risk: Low
Testing: Updates unit tests to confirm expected behavior.

One of the limitations of not having conditional conformance at the
moment is that the implementation of `init(from:)` and `encode(to:)` on
types which require it is that failure to cast dependent types to
`Encodable` or `Decodable` is a runtime failure. There is no way to
statically guarantee that the wrapped type is `Encodable` or
`Decodable`.

As such, in those implementations, at best we can directly call
`(element as! Encodable).encode(to: encoder)`, or similar. However, this
encodes the element directly into an encoder, without giving the encoder
a chance to intercept the type. This is problematic for `JSONEncoder`
because it cannot apply a strategy if it doesn't get to intercept the
type.

This gives a temporary workaround for JSON strategies because of
internal Foundation knowledge.
@itaiferber
Copy link
Contributor Author

@swift-ci Please test macOS

@itaiferber
Copy link
Contributor Author

@swift-ci Please smoke test Linux

@itaiferber
Copy link
Contributor Author

@swift-ci Please test Linux

@itaiferber itaiferber merged commit 26e35dd into swiftlang:swift-4.0-branch Jul 10, 2017
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.

1 participant