Suppose I have a struct like this:
struct Result: Decodable {
let animal: Animal?
}
And an enum like this:
enum Animal: String, Decodable {
case cat = "cat"
case dog = "dog"
}
but the JSON returned is like this:
{
"animal": ""
}
If I try to use a JSONDecoder
to decode this into a Result
struct, I get Cannot initialize Animal from invalid String value
as an error message. How can I properly decode this JSON result into a Result
where the animal property is nil?