Skip to content

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

Closed

Conversation

parkera
Copy link
Contributor

@parkera parkera commented Nov 29, 2017

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.

@parkera
Copy link
Contributor Author

parkera commented Nov 29, 2017

@swift-ci please test

@parkera parkera requested a review from itaiferber November 29, 2017 17:51
@parkera
Copy link
Contributor Author

parkera commented Nov 29, 2017

@itaiferber I put two sections of code in an #if block that look to me like they are intentionally different, at least for the time being.

@ianpartridge
Copy link
Contributor

Nice. I wonder if we could use the DEPLOYMENT_RUNTIME_SWIFT define for the differences, then share an identical file in the overlay?

@parkera
Copy link
Contributor Author

parkera commented Nov 29, 2017

Yah that may be possible. @itaiferber and I believe we have a few errors lurking in here since there are some as casts. Maybe we can wrap those.

Copy link
Contributor

@itaiferber itaiferber left a 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.

}

public mutating func nestedContainer<NestedKey>(keyedBy keyType: NestedKey.Type, forKey key: Key) -> KeyedEncodingContainer<NestedKey> {
let dictionary = NSMutableDictionary()
self.container[key.stringValue._bridgeToObjectiveC()] = dictionary
Copy link
Contributor

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

}

public mutating func nestedUnkeyedContainer(forKey key: Key) -> UnkeyedEncodingContainer {
let array = NSMutableArray()
self.container[key.stringValue._bridgeToObjectiveC()] = array
Copy link
Contributor

Choose a reason for hiding this comment

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

Same


// 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 {
Copy link
Contributor

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

@@ -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
Copy link
Contributor

@itaiferber itaiferber Nov 29, 2017

Choose a reason for hiding this comment

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

The #if falsees are good candidates for converting to DEPLOYMENT_RUNTIME_SWIFT

}

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 {
Copy link
Contributor

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

@parkera parkera force-pushed the parkera/json_key_strategy_impl branch from e6b2667 to a177ddc Compare November 29, 2017 18:47
@parkera
Copy link
Contributor Author

parkera commented Nov 29, 2017

@swift-ci smoke test

@parkera
Copy link
Contributor Author

parkera commented Nov 29, 2017

@swift-ci please test

@parkera parkera force-pushed the parkera/json_key_strategy_impl branch from a177ddc to 22dace8 Compare November 29, 2017 21:01
@parkera
Copy link
Contributor Author

parkera commented Nov 29, 2017

@swift-ci please test

@itaiferber
Copy link
Contributor

@swift-ci Please test

@parkera
Copy link
Contributor Author

parkera commented Nov 29, 2017

@swift-ci please test

@alblue
Copy link
Contributor

alblue commented Nov 30, 2017

Looks like this failed with a core dump:

Test Case 'TestJSONEncoder.test_encodingTopLevelSingleValueEnum' started at 2017-11-30 00:49:44.323
/home/buildnode/jenkins/workspace/swift-corelibs-foundation-PR-Linux/swift/utils/build-script-impl: line 264: 26232 Illegal instruction     (core dumped) "$@"
/home/buildnode/jenkins/workspace/swift-corelibs-foundation-PR-Linux/swift/utils/build-script: fatal error: command terminated with a non-zero exit status 132, aborting

@parkera
Copy link
Contributor Author

parkera commented Dec 4, 2017

Fixed the core dump (we needed Codable.swift changes too), but have some bridging failures to figure now now.

@alblue
Copy link
Contributor

alblue commented Dec 6, 2017

@swift-ci please test

@parkera
Copy link
Contributor Author

parkera commented Dec 6, 2017

I already know this fails. Just need some time to come back to this and sort out the bridging issues.

@Andrewangeta
Copy link

@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.

@parkera
Copy link
Contributor Author

parkera commented Mar 21, 2018

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.

@Lukasa
Copy link
Contributor

Lukasa commented Mar 22, 2018

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

typeMismatch(Swift.Int, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "field", intValue: nil)], debugDescription: "Expected to decode Int but found Int instead.", underlyingError: nil))

This seems to be related to the underlying parsing producing an Int instead of an NSNumber. As identified before, this is likely a bridging issue, and of the two of us I suspect you're vastly better equipped to go further with this than I am @parkera.

@parkera
Copy link
Contributor Author

parkera commented Mar 22, 2018

@itaiferber I know we are still talking about the returned types from JSONSerialization in a few other PRs, but I don't recall any changes here for 4.1 specifically. Do you?

@itaiferber
Copy link
Contributor

None that I've seen; I can't find any PRs that changed JSONSerialization in Swift 4.1 on here either (but I'll keep looking). @Lukasa What platform are you on? Which toolchain are you running there?

@Lukasa
Copy link
Contributor

Lukasa commented Mar 23, 2018

@itaiferber Sorry, I could really have made that clearer. That's Ubuntu 16.04, using a hand-compiled Swift toolchain from the swift-4.1-DEVELOPMENT-SNAPSHOT-2018-03-21-a tag, with a Foundation branched from that tag with this patch merged on top of it.

If I just use the swift-4.1-DEVELOPMENT-SNAPSHOT-2018-03-21-a tag directly without this patch, the above test passes just fine. So it's this patch that is affecting the return type.

@parkera
Copy link
Contributor Author

parkera commented Mar 23, 2018

Ah, ok.

@Andrewangeta
Copy link

Any updates on this issue? @parkera

Sent with GitHawk

@gperdomor
Copy link

Any update on this?

@millenomi
Copy link
Contributor

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.

@gperdomor
Copy link

We can expect this merged in swift 4.2? :(

@alblue
Copy link
Contributor

alblue commented Aug 7, 2018

@parkera can you rebase this so that we can run the tests again, or close it if it has gone stale?

@parkera
Copy link
Contributor Author

parkera commented Aug 8, 2018

I'm going to close this, since I think #1561 should cover it.

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.

8 participants