Here is my JSON I am trying to parse
{
"rows": [
{
"layout": "Y",
},
{
"layout": "A",
}
]
}
I want to be able to filter out the layout types that are not supported. I am using the JSONDecoder to to convert the JSON data into structs but I am having an issue processing the rows. I am attempting to convert each row using let row = try? rowContainer.decode(Row.self) but I can figure out how to move to the next one if it fails unless I decode it to an empty struct. I tried just decoding it to a Dictionary but it won't except Any for the value field.
enum RowType: String, Codable {
case a = "A"
case b = "B"
}
public struct Page: Codable {
let rows: [Row]
public init(from decoder: Decoder) throws {
// Get Container
let container = try decoder.container(keyedBy: CodingKeys.self)
// Create Result Array
var rowResult: [Row] = []
// Get Rows Container
var rowContainer = try container.nestedUnkeyedContainer(forKey: .rows)
// Process Rows
while !rowContainer.isAtEnd {
// Create Row
if let row = try? rowContainer.decode(Row.self) {
rowResult.append(row)
}
else {
// Increment Row Container
_ = try rowContainer.decode(Empty.self)
}
}
// Set Result
rows = rowResult
}
// Used For Unsupported Rows
struct Empty: Codable {}
}
public struct Row: Codable {
let layout: RowType
}