I've got a WebSocket that uses an array format of [command, data] to exchange messages. I'm struggling with the Swift Decoder to handle the mixed formats of the data portion. For instance Message #1:
["CFG",{"dimmerDelay":5000,"keyboardShortcuts":true}]
Message #2:
["LFM",true]
I'm using this to decode Message #1
struct Message: Decodable {
let command: String
let config: Config
init(from decoder: Decoder) throws {
var container = try decoder.unkeyedContainer()
command = try container.decode(String.self)
config = try container.decode(Config.self)
}
}
struct Config: Decodable {
let dimmerDelay: Int
let keyboardShortcuts: Bool
}
What I really want is to split this into a container more like this:
struct Message: Decodable {
let command: String
let data: String
}
Then if the message is of type "CFG" I would pass the data to a decoder that would create the Config object. If the message is of type "LFM" I would check to ensure the result is true and if the message is of another type, I'd pass that data to another decoder that would create the relevant decoded objects.