-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Port JSON key strategy to swift-corelibs-foundation #1347
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
@swift-ci please test |
@itaiferber I put two sections of code in an |
Nice. I wonder if we could use the |
Yah that may be possible. @itaiferber and I believe we have a few errors lurking in here since there are some |
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.
Looks good overall, just need to remove the parts that don't bridge over correctly to Linux.
Foundation/JSONEncoder.swift
Outdated
} | ||
|
||
public mutating func nestedContainer<NestedKey>(keyedBy keyType: NestedKey.Type, forKey key: Key) -> KeyedEncodingContainer<NestedKey> { | ||
let dictionary = NSMutableDictionary() | ||
self.container[key.stringValue._bridgeToObjectiveC()] = dictionary |
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 should keep _bridgeToObjectiveC()
here
Foundation/JSONEncoder.swift
Outdated
} | ||
|
||
public mutating func nestedUnkeyedContainer(forKey key: Key) -> UnkeyedEncodingContainer { | ||
let array = NSMutableArray() | ||
self.container[key.stringValue._bridgeToObjectiveC()] = array |
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.
Same
Foundation/JSONEncoder.swift
Outdated
|
||
// This method is called "box_" instead of "box" to disambiguate it from the overloads. Because the return type here is different from all of the "box" overloads (and is more general), any "box" calls in here would call back into "box" recursively instead of calling the appropriate overload, which is not what we want. | ||
fileprivate func box_<T : Encodable>(_ value: T) throws -> NSObject? { | ||
if T.self == Date.self || T.self == NSDate.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.
All of these casts will fail on Linux because of bridging. We can keep the behavior by turning all of the NS cases into actual conversions
Foundation/JSONEncoder.swift
Outdated
@@ -1750,6 +1750,25 @@ extension _JSONDecoder { | |||
fileprivate func unbox(_ value: Any, as type: Bool.Type) throws -> Bool? { | |||
guard !(value is NSNull) else { return nil } | |||
|
|||
#if false |
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.
The #if false
es are good candidates for converting to DEPLOYMENT_RUNTIME_SWIFT
Foundation/JSONEncoder.swift
Outdated
} | ||
|
||
fileprivate func unbox<T : Decodable>(_ value: Any, as type: T.Type) throws -> T? { | ||
let decoded: T | ||
if T.self == Date.self { | ||
if T.self == Date.self || T.self == NSDate.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.
Same here — all of the || T.self == ...
lead to as!
-casts which are not valid on Linux
e6b2667
to
a177ddc
Compare
@swift-ci smoke test |
@swift-ci please test |
a177ddc
to
22dace8
Compare
@swift-ci please test |
@swift-ci Please test |
@swift-ci please test |
Looks like this failed with a core dump:
|
Fixed the core dump (we needed Codable.swift changes too), but have some bridging failures to figure now now. |
@swift-ci please test |
I already know this fails. Just need some time to come back to this and sort out the bridging issues. |
@parkera Any updates on this? I'm hoping this makes into 4.1 keyDecodingStragety is a really nice API and cleans up so much code. |
Unfortunately I got stuck on this one, having trouble figuring out why the tests don't pass after merging in the prerequisite changes from the Swift overlay version. |
So in case it helps, this seems to be a problem initially isolated to the decoder. Here's basically the smallest repro I can give you: import Foundation
struct Test: Codable {
var field: Int
}
let json = "{\"field\": 5}".data(using: .utf8)!
let decoder = JSONDecoder()
let result = try decoder.decode(Test.self, from: json)
print(result.field) In Swift 4.0, this works fine and prints 5, as expected. In Swift 4.1, it explodes with
This seems to be related to the underlying parsing producing an |
@itaiferber I know we are still talking about the returned types from |
None that I've seen; I can't find any PRs that changed |
@itaiferber Sorry, I could really have made that clearer. That's Ubuntu 16.04, using a hand-compiled Swift toolchain from the If I just use the |
Ah, ok. |
Any update on this? |
We are dealing with some bridging landing fallout and WWDC. My plan is to look at this starting late this week, unless someone else can. |
We can expect this merged in swift 4.2? :( |
@parkera can you rebase this so that we can run the tests again, or close it if it has gone stale? |
I'm going to close this, since I think #1561 should cover it. |
This is a work in progress branch.
First step is to bring JSONEncoder.swift up to date with changes to the file in the overlay before the key strategy changes landed.