Since an enum
with associated types does not match any JSON type you need a bit more handwork and write a custom mapping.
The following code covers three options.
- A dictionary with the case as key and an containing dictionary with parameter labels as keys
- Encoding / Decoding each associated value as separate key / value pair
- Encoding / Decoding all associated values in an array with an arbitrary key.
First of all the enum must not conform to Codable
enum PostType {
case fast(value: Int, value2: Int)
case middle(bool: Bool)
case slow(string: String, string2: String)
}
The case fast
uses an array, middle
a sub-dictionary, slow
separate key / value pairs.
Then declare the MyStruct
struct, adopt Codable
and declare the type
struct MyStruct : Codable {
var type : PostType
This solution requires custom keys
enum CodingKeys: String, CodingKey {
case value, string, string2, middle
}
The encode
method switch
es on the cases and creates the appropriate types
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
switch type {
case .fast(let value, let value2) :
try container.encode([value, value2], forKey: .value)
case .slow(let string, let string2) :
try container.encode(string, forKey: .string)
try container.encode(string2, forKey: .string2)
case .middle(let bool):
try container.encode(["bool" : bool], forKey: .middle)
}
}
In the decode
method you can distinguish the cases by the passed keys, make sure that they are unique.
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
let allKeys = values.allKeys
if allKeys.contains(.middle) {
let value = try values.decode([String:Bool].self, forKey: .middle)
type = PostType.middle(bool: value["bool"]!)
} else if allKeys.contains(.value) {
let value = try values.decode([Int].self, forKey: .value)
type = PostType.fast(value: value[0], value2: value[1])
} else {
let string = try values.decode(String.self, forKey: .string)
let string2 = try values.decode(String.self, forKey: .string2)
type = PostType.slow(string: string, string2: string2)
}
}
}
Although some keys are hard-coded the first option seems to be the most suitable one.
Finally an example to use it:
let jsonString = "[{\"value\": [2, 6]}, {\"string\" : \"foo\", \"string2\" : \"bar\"}, {\"middle\" : {\"bool\" : true}}]"
let jsonData = jsonString.data(using: .utf8)!
do {
let decoded = try JSONDecoder().decode([MyStruct].self, from: jsonData)
print("decoded:", decoded)
let newEncoded = try JSONEncoder().encode(decoded)
print("re-encoded:", String(data: newEncoded, encoding: .utf8)!)
} catch {
print(error)
}