This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct EnumModelRawValue: Decodable { | |
enum Keys: CodingKey { | |
case status | |
} | |
enum Status: String { | |
case enabled | |
case disabled | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
init(from decoder: Decoder) throws { | |
let container = try decoder.container(keyedBy: Keys.self) | |
self.status = try? Container.decodeIfPresent(Status.self, forKey: .status) ?? .disabled | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"status": "new" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct EnumModelIfPresent: Decodable { | |
enum Keys: CodingKey { | |
case status | |
} | |
enum Status: String, Decodable { | |
case enabled | |
case disabled | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Decoding method | Value | Null | Attribute Missing | Null element | Invalid element | |
---|---|---|---|---|---|---|
synthesized [Int] property | ✅ | ❌ | ❌ | ❌ | ❌ | |
decode([Int], ...) | ✅ | ❌ | ❌ | ❌ | ❌ | |
decode([Int]?, ...) | ✅ | ✅ | ❌ | ❌ | ❌ | |
synthesized [Int]? property | ✅ | ✅ | ✅ | ❌ | ❌ | |
decodeIfPresent([Int], ...) | ✅ | ✅ | ✅ | ❌ | ❌ | |
decode([Int?]?, ...) | ✅ | ✅ | ✅ | ✅ | ❌ | |
decodeArrayOmitingInvalidObjects([Int], ...) | ✅ | ✅ | ✅ | ✅ | ✅ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct DummyDecodable: Decodable {} | |
extension KeyedDecodingContainer { | |
func decodeArrayOmitingInvalidObjects<T: Decodable>(forKey key: KeyedDecodingContainer.Key) -> [T] { | |
guard var nestedUnkeyedContainer = try? self.nestedUnkeyedContainer(forKey: key) else { | |
return [] | |
} | |
var result = [T]() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"array": [ | |
1, | |
"2", | |
3 | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"array": [ | |
1, | |
null, | |
3 | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"array": null | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"array": [ | |
1, | |
2, | |
3 | |
] | |
} |
NewerOlder