Codable enum with default case in Swift 4
Asked Answered
P

12

74

I have defined an enum as follows:

enum Type: String, Codable {
    case text = "text"
    case image = "image"
    case document = "document"
    case profile = "profile"
    case sign = "sign"
    case inputDate = "input_date"
    case inputText = "input_text"
    case inputNumber = "input_number"
    case inputOption = "input_option"

    case unknown
}

that maps a JSON string property. The automatic serialization and deserialization works fine, but I found that if a different string is encountered, the deserialization fails.

Is it possible to define an unknown case that maps any other available case?

This can be very useful, since this data comes from a RESTFul API that, maybe, can change in the future.

Punjabi answered 6/4, 2018 at 15:6 Comment(3)
You could declare the variable of your Type to be an optional.Hbomb
@AndréSlotta I already tried this solution, but it doesn't work. I have an error during the deserialization.Punjabi
Can you show some more of your code?Hbomb
M
194

You can extend your Codable Type and assign a default value in case of failure:

enum Type: String {
    case text,
         image,
         document,
         profile,
         sign,
         inputDate = "input_date",
         inputText = "input_text" ,
         inputNumber = "input_number",
         inputOption = "input_option",
         unknown
}
extension Type: Codable {
    public init(from decoder: Decoder) throws {
        self = try Type(rawValue: decoder.singleValueContainer().decode(RawValue.self)) ?? .unknown
    }
}

edit/update:

Xcode 11.2 • Swift 5.1 or later

Create a protocol that defaults to last case of a CaseIterable & Decodable enumeration:

protocol CaseIterableDefaultsLast: Decodable & CaseIterable & RawRepresentable
where RawValue: Decodable, AllCases: BidirectionalCollection { }

extension CaseIterableDefaultsLast {
    init(from decoder: Decoder) throws {
        self = try Self(rawValue: decoder.singleValueContainer().decode(RawValue.self)) ?? Self.allCases.last!
    }
}

Playground testing:

enum Type: String, CaseIterableDefaultsLast {
    case text, image, document, profile, sign, inputDate = "input_date", inputText = "input_text" , inputNumber = "input_number", inputOption = "input_option", unknown
}

let types = try! JSONDecoder().decode([Type].self , from: Data(#"["text","image","sound"]"#.utf8))  // [text, image, unknown]
Mcdaniels answered 6/4, 2018 at 16:29 Comment(3)
Slightly more generic if you're using this frequently. Replace try Type with try type(of: self).initJolandajolanta
@Jolandajolanta is there any way to make a fully generic CodableWithUnknown protocol or something like that?Appointment
Downvoter a comment explaining the reason of it would be appreciated and would allow me to fix and/or improve what’s wrong with my answer. A downvote without a reason makes no senseMcdaniels
N
17

You can drop the raw type for your Type and make unknown case that handles associated value. But this comes at a cost. You somehow need the raw values for your cases. Inspired from this and this SO answers I came up with this elegant solution to your problem.

To be able to store the raw values, we will maintain another enum, but as private:

enum Type {
    case text
    case image
    case document
    case profile
    case sign
    case inputDate
    case inputText
    case inputNumber
    case inputOption
    case unknown(String)

    // Make this private
    private enum RawValues: String, Codable {
        case text = "text"
        case image = "image"
        case document = "document"
        case profile = "profile"
        case sign = "sign"
        case inputDate = "input_date"
        case inputText = "input_text"
        case inputNumber = "input_number"
        case inputOption = "input_option"
        // No such case here for the unknowns
    }
}

Move the encoding & decoding part to extensions:

Decodable part:

extension Type: Decodable {
    init(from decoder: Decoder) throws {
        let container = try decoder.singleValueContainer()
        // As you already know your RawValues is String actually, you decode String here
        let stringForRawValues = try container.decode(String.self) 
        // This is the trick here...
        switch stringForRawValues { 
        // Now You can switch over this String with cases from RawValues since it is String
        case RawValues.text.rawValue:
            self = .text
        case RawValues.image.rawValue:
            self = .image
        case RawValues.document.rawValue:
            self = .document
        case RawValues.profile.rawValue:
            self = .profile
        case RawValues.sign.rawValue:
            self = .sign
        case RawValues.inputDate.rawValue:
            self = .inputDate
        case RawValues.inputText.rawValue:
            self = .inputText
        case RawValues.inputNumber.rawValue:
            self = .inputNumber
        case RawValues.inputOption.rawValue:
            self = .inputOption

        // Now handle all unknown types. You just pass the String to Type's unknown case. 
        // And this is true for every other unknowns that aren't defined in your RawValues
        default: 
            self = .unknown(stringForRawValues)
        }
    }
}

Encodable part:

extension Type: Encodable {
    func encode(to encoder: Encoder) throws {
        var container = encoder.singleValueContainer()
        switch self {
        case .text:
            try container.encode(RawValues.text)
        case .image:
            try container.encode(RawValues.image)
        case .document:
            try container.encode(RawValues.document)
        case .profile:
            try container.encode(RawValues.profile)
        case .sign:
            try container.encode(RawValues.sign)
        case .inputDate:
            try container.encode(RawValues.inputDate)
        case .inputText:
            try container.encode(RawValues.inputText)
        case .inputNumber:
            try container.encode(RawValues.inputNumber)
        case .inputOption:
            try container.encode(RawValues.inputOption)

        case .unknown(let string): 
            // You get the actual String here from the associated value and just encode it
            try container.encode(string)
        }
    }
}

Examples:

I just wrapped it in a container structure(because we'll be using JSONEncoder/JSONDecoder) as:

struct Root: Codable {
    let type: Type
}

For values other than unknown case:

let rootObject = Root(type: Type.document)
do {
    let encodedRoot = try JSONEncoder().encode(rootObject)
    do {
        let decodedRoot = try JSONDecoder().decode(Root.self, from: encodedRoot)
        print(decodedRoot.type) // document
    } catch {
        print(error)
    }
} catch {
    print(error)
}

For values with unknown case:

let rootObject = Root(type: Type.unknown("new type"))
do {
    let encodedRoot = try JSONEncoder().encode(rootObject)
    do {
        let decodedRoot = try JSONDecoder().decode(Root.self, from: encodedRoot)
        print(decodedRoot.type) // unknown("new type")
    } catch {
        print(error)
    }
} catch {
    print(error)
}

I put the example with local objects. You can try with your REST API response.

Nole answered 6/4, 2018 at 19:28 Comment(0)
B
10
enum Type: String, Codable, Equatable {
    case image
    case document
    case unknown

    public init(from decoder: Decoder) throws {
        guard let rawValue = try? decoder.singleValueContainer().decode(String.self) else {
            self = .unknown
            return
        }
        self = Type(rawValue: rawValue) ?? .unknown
    }
}
Bag answered 3/10, 2019 at 12:22 Comment(1)
Add an explanationGreenwald
C
7

Here's an alternative based on nayem's answer that offers a slightly more streamlined syntax by using optional binding of the inner RawValues initialization:

enum MyEnum: Codable {

    case a, b, c
    case other(name: String)

    private enum RawValue: String, Codable {

        case a = "a"
        case b = "b"
        case c = "c"
    }

    init(from decoder: Decoder) throws {
        let container = try decoder.singleValueContainer()
        let decodedString = try container.decode(String.self)

        if let value = RawValue(rawValue: decodedString) {
            switch value {
            case .a:
                self = .a
            case .b:
                self = .b
            case .c:
                self = .c
            }
        } else {
            self = .other(name: decodedString)
        }
    }

    func encode(to encoder: Encoder) throws {
        var container = encoder.singleValueContainer()

        switch self {
        case .a:
            try container.encode(RawValue.a)
        case .b:
            try container.encode(RawValue.b)
        case .c:
            try container.encode(RawValue.c)
        case .other(let name):
            try container.encode(name)
        }
    }
}

If you are certain that all your existing enum case names match the underlying string values they represent, you could streamline RawValue to:

private enum RawValue: String, Codable {

    case a, b, c
}

...and encode(to:) to:

func encode(to encoder: Encoder) throws {
    var container = encoder.singleValueContainer()

    if let rawValue = RawValue(rawValue: String(describing: self)) {
        try container.encode(rawValue)
    } else if case .other(let name) = self {
        try container.encode(name)
    }
}

Here's a practical example of using this, e.g., you want to model SomeValue that has a property you want to model as an enum:

struct SomeValue: Codable {

    enum MyEnum: Codable {

        case a, b, c
        case other(name: String)

        private enum RawValue: String, Codable {

            case a = "a"
            case b = "b"
            case c = "letter_c"
        }

        init(from decoder: Decoder) throws {
            let container = try decoder.singleValueContainer()
            let decodedString = try container.decode(String.self)

            if let value = RawValue(rawValue: decodedString) {
                switch value {
                case .a:
                    self = .a
                case .b:
                    self = .b
                case .c:
                    self = .c
                }
            } else {
                self = .other(name: decodedString)
            }
        }

        func encode(to encoder: Encoder) throws {
            var container = encoder.singleValueContainer()

            switch self {
            case .a:
                try container.encode(RawValue.a)
            case .b:
                try container.encode(RawValue.b)
            case .c:
                try container.encode(RawValue.c)
            case .other(let name):
                try container.encode(name)
            }
        }
    }

}

let jsonData = """
[
    { "value": "a" },
    { "value": "letter_c" },
    { "value": "c" },
    { "value": "Other value" }
]
""".data(using: .utf8)!

let decoder = JSONDecoder()

if let values = try? decoder.decode([SomeValue].self, from: jsonData) {
    values.forEach { print($0.value) }

    let encoder = JSONEncoder()

    if let encodedJson = try? encoder.encode(values) {
        print(String(data: encodedJson, encoding: .utf8)!)
    }
}


/* Prints:
 a
 c
 other(name: "c")
 other(name: "Other value")
 [{"value":"a"},{"value":"letter_c"},{"value":"c"},{"value":"Other value"}]
 */
Coimbra answered 30/11, 2018 at 20:27 Comment(0)
C
5

Let's start with a test case. We expect this to pass:

    func testCodableEnumWithUnknown() throws {
        enum Fruit: String, Decodable, CodableEnumWithUnknown {
            case banana
            case apple

            case unknown
        }
        struct Container: Decodable {
            let fruit: Fruit
        }
        let data = #"{"fruit": "orange"}"#.data(using: .utf8)!
        let val = try JSONDecoder().decode(Container.self, from: data)
        XCTAssert(val.fruit == .unknown)
    }

Our protocol CodableEnumWithUnknown denotes the support of the unknown case that should be used by the decoder if an unknown value arises in the data.

And then the solution:

public protocol CodableEnumWithUnknown: Codable, RawRepresentable {
    static var unknown: Self { get }
}

public extension CodableEnumWithUnknown where Self: RawRepresentable, Self.RawValue == String {

    init(from decoder: Decoder) throws {
        self = (try? Self(rawValue: decoder.singleValueContainer().decode(RawValue.self))) ?? Self.unknown
    }
}

The trick is make your enum implement with the CodableEnumWithUnknown protocol and add the unknown case.

I favor this solution above using the .allCases.last! implementation mentioned in other posts, because i find them a bit brittle, as they are not typechecked by the compiler.

Coil answered 17/2, 2021 at 13:16 Comment(1)
This is better and more common. And Self.RawValue == String could be Self.RawValue : Decodable.Piddle
H
3

You have to implement the init(from decoder: Decoder) throws initializer and check for a valid value:

struct SomeStruct: Codable {

    enum SomeType: String, Codable {
        case text
        case image
        case document
        case profile
        case sign
        case inputDate = "input_date"
        case inputText = "input_text"
        case inputNumber = "input_number"
        case inputOption = "input_option"

        case unknown
    }

    var someType: SomeType

    init(from decoder: Decoder) throws {
        let values = try decoder.container(keyedBy: CodingKeys.self)
        someType = (try? values.decode(SomeType.self, forKey: .someType)) ?? .unknown
    }

}
Hendecasyllable answered 6/4, 2018 at 16:20 Comment(0)
A
2

@LeoDabus thanks for your answers. I modified them a bit to make a protocol for String enums that seems to work for me:

protocol CodableWithUnknown: Codable {}
extension CodableWithUnknown where Self: RawRepresentable, Self.RawValue == String {
    init(from decoder: Decoder) throws {
        do {
            try self = Self(rawValue: decoder.singleValueContainer().decode(RawValue.self))!
        } catch {
            if let unknown = Self(rawValue: "unknown") {
                self = unknown
            } else {
                throw error
            }
        }
    }
}
Appointment answered 12/6, 2019 at 18:41 Comment(1)
I wouldnt force unwrap and or use a do catch there. If you want to constrain the enumeration type to String you can do something like: protocol CaseIterableDefaultsLast: Codable & CaseIterable { } extension CaseIterableDefaultsLast where Self: RawRepresentable, Self.RawValue == String, Self.AllCases: BidirectionalCollection { init(from decoder: Decoder) throws { self = try Self(rawValue: decoder.singleValueContainer().decode(RawValue.self)) ?? Self.allCases.last! } }Mcdaniels
C
1

Add this extension and set YourEnumName .

extension <#YourEnumName#>: Codable {
    public init(from decoder: Decoder) throws {
        self = try <#YourEnumName#>(rawValue: decoder.singleValueContainer().decode(RawValue.self)) ?? .unknown
    }
}
Consolatory answered 29/4, 2019 at 7:45 Comment(0)
I
1

the following method will decode all types of enums with RawValue of type Decodable (Int, String, ..) and returns nil if it fails. This will prevent crashes caused by non-existent raw values inside the JSON response.

Definition:

extension Decodable {
    static func decode<T: RawRepresentable, R, K: CodingKey>(rawValue _: R.Type, forKey key: K, decoder: Decoder) throws -> T? where T.RawValue == R, R: Decodable {
        let container = try decoder.container(keyedBy: K.self)
        guard let rawValue = try container.decodeIfPresent(R.self, forKey: key) else { return nil }
        return T(rawValue: rawValue)
    }
}

Usage:

enum Status: Int, Decodable {
        case active = 1
        case disabled = 2
    }
    
    struct Model: Decodable {
        let id: String
        let status: Status?
        
        init(from decoder: Decoder) throws {
            let container = try decoder.container(keyedBy: CodingKeys.self)
            id = try container.decodeIfPresent(String.self, forKey: .id)
            status = try .decode(rawValue: Int.self, forKey: .status, decoder: decoder)
        }
    }

// status: -1 reutrns nil
// status:  2 returns .disabled 
Intendment answered 25/5, 2022 at 12:44 Comment(0)
A
1

Handling Unknown Enum Cases in Swift with Codable

protocol UnknownCodable: RawRepresentable, Codable where RawValue: Codable {
    static var unknown: Self { get }
}

extension UnknownCodable {
    init(from decoder: Decoder) throws {
        let container = try decoder.singleValueContainer()
        let decodedValue = try container.decode(RawValue.self)
        self = Self(rawValue: decodedValue) ?? Self.unknown
    }
}

Usage example

enum Type: String, UnknownCodable {
    case text
    case image
    case document
    case unknown
}

let types = try! JSONDecoder().decode([Type].self , from: Data(#"["text","image","sound"]"#.utf8))

print(types) // [text, image, unknown]
Acrobat answered 29/3 at 4:30 Comment(4)
enum Type doesn't conform to UnknownCodable protocol in your exampleIntoxicating
@Roman, have you checked this solution?Acrobat
Sorry, you're right, it works. Unbelievable. How come an enum that doesn't have any static properties at all, conforms to a protocol which requires a static property!? 🤯 Well... I guess I'll even create a separate post with this question 😳Intoxicating
#78376767Intoxicating
V
0

You can use this extension to encode / decode (this snippet supports Int an String RawValue type enums, but can be easy extended to fit other types)

extension NSCoder {
    
    func encodeEnum<T: RawRepresentable>(_ value: T?, forKey key: String) {
        guard let rawValue = value?.rawValue else {
            return
        }
        if let s = rawValue as? String {
            encode(s, forKey: key)
        } else if let i = rawValue as? Int {
            encode(i, forKey: key)
        } else {
            assert(false, "Unsupported type")
        }
    }
    
    func decodeEnum<T: RawRepresentable>(forKey key: String, defaultValue: T) -> T {
        if let s = decodeObject(forKey: key) as? String, s is T.RawValue {
            return T(rawValue: s as! T.RawValue) ?? defaultValue
        } else {
            let i = decodeInteger(forKey: key)
            if i is T.RawValue {
                return T(rawValue: i as! T.RawValue) ?? defaultValue
            }
        }
        return defaultValue
    }
    
}

than use it

// encode
coder.encodeEnum(source, forKey: "source")
// decode
source = coder.decodeEnum(forKey: "source", defaultValue: Source.home)
Vender answered 23/4, 2021 at 9:4 Comment(0)
M
0

To avoid decoding failure in case of wrong data type in JSON.

extension Type: Decodable {

    init(from decoder: Decoder) {
        do {
            let rawValue = try decoder.singleValueContainer().decode(RawValue.self)
            self = Type(rawValue: rawValue) ?? .unknown
        } catch {
            self = .unknown
        }
    }
}

or in case where RawValue is String

extension Type: Decodable {

    init(from decoder: Decoder) {
        let rawValue = (try? decoder.singleValueContainer().decode(String.self)) ?? "unknown"
        self = Type(rawValue: rawValue) ?? .unknown
    }
}

For example: {"foo": "image", "bar": "cake", "baz": 42} -> ["foo": .image, "bar": .unknown, "baz": .unknown]

Motherly answered 13/12, 2023 at 21:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.