You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
New to Swift 5.5, attested on Ubuntu, CentOS and Amazon Linux (and proven not present on macOS)
Additional Detail from JIRA
Votes
1
Component/s
Labels
Bug
Assignee
None
Priority
Medium
md5: 0bbfd99264529883580e23a5166acfac
Issue Description:
For a key type that normally makes the dictionary encode as an array, empty instances end up encoded as empty dictionaries instead of empty arrays, leading to a type‐mismatch error when decoding.
structKey:Codable,Hashable{varstring:String}structValue:Codable{varstring:String}structContext:Codable{vardictionary:[Key:Value]=[:]}letencoded=tryJSONEncoder().encode([Context(dictionary:[Key(string:"..."):Value(string:"...")])])print(String(data: encoded, encoding:.utf8))// Optional("[{\"dictionary\":[{\"string\":\"...\"},{\"string\":\"...\"}]}]")letdecoded=tryJSONDecoder().decode([Context].self, from: encoded)print(decoded)// ✓ Works just fine.letemptyEncoded=tryJSONEncoder().encode([Context()])print(String(data: emptyEncoded, encoding:.utf8))// Optional("[{\"dictionary\":{}}]")letemptyDecoded=tryJSONDecoder().decode([Context].self, from: emptyEncoded)print(emptyDecoded)// ✗ Throws: “typeMismatch(Swift.Array<Foundation.JSONValue>, Swift.DecodingError.Context(codingPath: [_JSONKey(stringValue: "Index 0", intValue: 0), CodingKeys(stringValue: "dictionary", intValue: nil)], debugDescription: "Expected to decode Array<JSONValue> but found a dictionary instead.", underlyingError: nil))”
I haven’t figured out yet if the problem comes from the Codable conformance of Dictionary in the standard library, the Codable synthesis in the compiler, or just JSONEncoder in Foundation.
The text was updated successfully, but these errors were encountered:
Environment
New to Swift 5.5, attested on Ubuntu, CentOS and Amazon Linux (and proven not present on macOS)
Additional Detail from JIRA
md5: 0bbfd99264529883580e23a5166acfac
Issue Description:
For a key type that normally makes the dictionary encode as an array, empty instances end up encoded as empty dictionaries instead of empty arrays, leading to a type‐mismatch error when decoding.
I haven’t figured out yet if the problem comes from the Codable conformance of Dictionary in the standard library, the Codable synthesis in the compiler, or just JSONEncoder in Foundation.
The text was updated successfully, but these errors were encountered: