I have to deserialize a JSON string like this:
{ "name" : "John Smith", "value" : "someValue" }
in Swift 4, where "value" should be a enum and the whole object is a struct like:
struct MyType {
name: String?
value: Value?
}
At some point in the future, there might be new enum values added in the backend so I thought it would be smart to have some fallback.
I thought I could create a enum like
enum Value {
case someValue
case someOtherValue
case unknown(value: String)
}
but I just can't wrap my head around how to deserialize that enum and make it work. Previously I simply used a String
enum, but deserializing unknown values throws errors.
Is there a simple way to make that work or should I deserialize the value as a String
and create a custom getter in the struct with a switch statement to return one of the cases (probably not even in the struct itself but in my view model)?