[SR-6112] arrays and dictionaries do not call container encode for the elements #48667
Labels
bug
A deviation from expected or documented behavior. Also: expected but undesirable behavior.
Codable
Area → standard library: `Codable` and co.
standard library
Area: Standard library umbrella
Environment
Swift 4
Xcode 9.6 beta
Mac OSX
Additional Detail from JIRA
md5: 517a5763d90fe8049146529badff5abd
duplicates:
Optional<URL>
are different betweenContainer.encode (_: ...)
andContainer.encodeIfPresent (_: ...)
.Issue Description:
Arrays and Dictionaries do not call encode on the container protocols and then encodes the values as their underlying encode type.
example 1:
let value = URL(string: "test.com")
try! [value].encode(to: TestEncoder())
// call stack (a url value never gets encoded)
_Encoder unkeyedContainer()
_UnkeyedEncodingContainer superEncoder()
_Encoder singleValueContainer()
_Encoder singleValueContainer()
_Encoder container(keyedBy: ) (CodingKeys in _FB6C8C127CB51A72D58E049FC6F7743F)
_KeyedEncodingContainerProtocol encode(:forKey: ) String test.com relative
example 2:
let value = Date()
try! [1: value].encode(to: TestEncoder())
// call stack (a date value never gets encoded)
_Encoder container(keyedBy: ) _DictionaryCodingKey
_KeyedEncodingContainerProtocol<_DictionaryCodingKey> superEncoder(forKey: ) _DictionaryCodingKey(stringValue: "1", intValue: Optional(1))
_Encoder singleValueContainer()
_Encoder encode Double 529368384.419609
Is there a way to get around this? Like in the JSONEncoder at:
swift/stdlib/public/SDK/Foundation/JSONEncoder.swift
Line 1774 in 1457e4d
// code used for testing
class _Encoder: Encoder, SingleValueEncodingContainer {
}
struct _UnkeyedEncodingContainer: UnkeyedEncodingContainer {
}
struct _KeyedEncodingContainerProtocol<K: CodingKey>: KeyedEncodingContainerProtocol {
}
}
The text was updated successfully, but these errors were encountered: