Stack Exchange
how to decode json from DeveloperPreview to enum decoder in swift
I try to decode json from api, for this is use https://app.quicktype.io/
I parse my json response and get struct
This is my model :
struct Trait: Codable {
    let name: String
    let value: Value
    let filterable, visible, highlight: Bool
}

enum Value: Codable {
    case bool(Bool)
    case integer(Int)
    case string(String)

    init(from decoder: Decoder) throws {
        let container = try decoder.singleValueContainer()
        if let x = try? container.decode(Bool.self) {
            self = .bool(x)
            return
        }
        if let x = try? container.decode(Int.self) {
            self = .integer(x)
            return
        }
        if let x = try? container.decode(String.self) {
            self = .string(x)
            return
        }
        throw DecodingError.typeMismatch(Value.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Wrong type for Value"))
    }

    func encode(to encoder: Encoder) throws {
        var container = encoder.singleValueContainer()
        switch self {
        case .bool(let x):
            try container.encode(x)
        case .integer(let x):
            try container.encode(x)
        case .string(let x):
            try container.encode(x)
        }
    }
}

This is my preview provider :
let traits = Trait(
   name:"Style",
   value: Value(from: .bool),
   filterable:true,
   visible:true,
   highlight:true
)

this is the json (traits is list)
         "traits":[
            {
               "name":"Style",
               "value":"DD1399-105",
               "filterable":true,
               "visible":true,
               "highlight":true
            },
            {
               "name":"Colorway",
               "value":"White/Black-Total Orange",
               "filterable":true,
               "visible":true,
               "highlight":true
            },
            {
               "name":"Retail Price",
               "value":110,
               "filterable":true,
               "visible":true,
               "highlight":false
            },
            {
               "name":"Featured",
               "value":false,
               "filterable":false,
               "visible":false,
               "highlight":false
            }
         ],

I'm stuck here, I got some error because I don't know how to write a value in preview provider.
This is the error for the code above:


You have to specify one of the enum cases with its associated value.  Value(from: doesn't exist.
If you need a Bool it's either
let traits = Trait(
   name: "Style",
   value: .bool(true),
   filterable: true,
   visible: true,
   highlight: true
)

or
let traits = Trait(
   name: "Style",
   value: .bool(false),
   filterable: true,
   visible: true,
   highlight: true
)


ok thank you, and if a got a list of 3 traits that can change something ?

Look at the encode(to encoder: method. As I said you can use one of the three enum cases, the let x represents the associated value.

thank you for your answer
