Skip to content
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

[SR-9965] Decodable: type mismatch error when decoding string to date as dictionary key #3530

Closed
swift-ci opened this issue Feb 20, 2019 · 5 comments

Comments

@swift-ci
Copy link
Contributor

Previous ID SR-9965
Radar None
Original Reporter vadian (JIRA User)
Type Bug
Status Resolved
Resolution Duplicate
Environment

MacPro 5.1 16 GB RAM

macOS 10.13.6

Xcode 10.1 (10B61)

Additional Detail from JIRA
Votes 0
Component/s Foundation
Labels Bug, Codable
Assignee bendjones (JIRA)
Priority Medium

md5: e77591025a62597c54cf4a4375ecad36

duplicates:

  • SR-9023 Decoding a JSON Dictionary with a UUID key either doesn't work, or shows the wrong error message

Issue Description:

If you are going to decode a string to date with the .formatted date decoding strategy as dictionary key you get a type mismatch error about an expected array:

typeMismatch(Swift.Array<Any>, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "bar", intValue: nil)], debugDescription: "Expected to decode Array<Any> but found a dictionary instead.", underlyingError: nil))

Code:

let json = """
{"bar": { "2019-02-20": 100.10 }}
"""
struct Foo: Decodable {
    let bar: [Date: Double]
}
 
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .formatted(dateFormatter)
let data = Data(json.utf8)
do {
    let decoded = try decoder.decode(Foo.self, from: data)
    print(decoded)
} catch {
    print(error)
}
@belkadan
Copy link

cc bendjones (JIRA User)

@swift-ci
Copy link
Contributor Author

Comment by Ben D. Jones (JIRA)

Hmm that's interesting... I'll take a look.

@itaiferber
Copy link
Contributor

This is essentially SR-9023 but with Date instead of UUID. The issue here is that Dictionary doesn't know anything about encoding strategies, and has no idea that Date will encode as a string at the end of the day. All it knows how to encode are dictionaries keyed by String or Int, and all other types fall into encoding and decoding as an unkeyed container. It tries to decode from an unkeyed container, but fails since the JSON actually contains a dictionary (a keyed container).

There's a bit more info in SR-9023 that explains what's going on here.

@swift-ci
Copy link
Contributor Author

Comment by Ben D. Jones (JIRA)

Thanks @itaiferber!

@swift-ci
Copy link
Contributor Author

Comment by Stefan Klieme (JIRA)

Thanks Itai Ferber for the explanation. But maybe it's possible to display a less misleading error message.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 5, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants