Here's how I would attempt to parse the JSON from https://pastebin.com/raw/LALX2PvV
Since the keys in the MACPro
object aren't known in advance, it uses dynamic keys and stores the parsed results in a dictionary.
struct BundleData: Codable {
let cfBundleName, cfBundleIdentifier,
cfBundleInfoDictionaryVersion, osBundleRequired: String
let cfBundleVersion, cfBundleExecutable,
cfBundleGetInfoString, cfBundleSignature: String
let cfBundlePackageType: String
let ioKitPersonalities: IOKitPersonalities
let buildMachineOSBuild, lsMinimumSystemVersion,
cfBundleDevelopmentRegion, nsHumanReadableCopyright: String
let cfBundleShortVersionString: String
enum CodingKeys: String, CodingKey {
case cfBundleName = "CFBundleName"
case cfBundleIdentifier = "CFBundleIdentifier"
case cfBundleInfoDictionaryVersion = "CFBundleInfoDictionaryVersion"
case osBundleRequired = "OSBundleRequired"
case cfBundleVersion = "CFBundleVersion"
case cfBundleExecutable = "CFBundleExecutable"
case cfBundleGetInfoString = "CFBundleGetInfoString"
case cfBundleSignature = "CFBundleSignature"
case cfBundlePackageType = "CFBundlePackageType"
case ioKitPersonalities = "IOKitPersonalities"
case buildMachineOSBuild = "BuildMachineOSBuild"
case lsMinimumSystemVersion = "LSMinimumSystemVersion"
case cfBundleDevelopmentRegion = "CFBundleDevelopmentRegion"
case nsHumanReadableCopyright = "NSHumanReadableCopyright"
case cfBundleShortVersionString = "CFBundleShortVersionString"
}
}
struct IOKitPersonalities: Codable {
let agpm: Agpm
enum CodingKeys: String, CodingKey {
case agpm = "AGPM"
}
}
struct Agpm: Codable {
let cfBundleIdentifier, ioProviderClass, ioClass: String
let machines: Machines
let ioNameMatch: String
enum CodingKeys: String, CodingKey {
case cfBundleIdentifier = "CFBundleIdentifier"
case ioProviderClass = "IOProviderClass"
case ioClass = "IOClass"
case machines = "Machines"
case ioNameMatch = "IONameMatch"
}
}
struct Machines: Codable {
let macPro41, macPro51: MACPro
enum CodingKeys: String, CodingKey {
case macPro41 = "MacPro4,1"
case macPro51 = "MacPro5,1"
}
}
struct MACPro: Codable {
var defaultControlID: Int
let devices: [String: DeviceInfo]
enum CodingKeys: String, CodingKey {
case defaultControlID = "default-control-id"
}
struct DeviceKey: CodingKey {
var stringValue: String
var intValue: Int?
init?(stringValue: String) {
self.stringValue = stringValue
}
init?(intValue: Int) {
self.stringValue = "\(intValue)";
self.intValue = intValue
}
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: DeviceKey.self)
self.defaultControlID = 0
var devices = [String: DeviceInfo]()
for key in container.allKeys {
if let device = try? container.decode(DeviceInfo.self, forKey: key) {
devices[key.stringValue] = device
} else if let controlId = try? container.decode(Int.self, forKey: key) {
self.defaultControlID = controlId
}
}
self.devices = devices
}
}
struct DeviceInfo: Codable {
let heuristic: Heuristic
let logControl, controlID: Int
enum CodingKeys: String, CodingKey {
case heuristic = "Heuristic"
case logControl = "LogControl"
case controlID = "control-id"
}
}
struct Heuristic: Codable {
let thresholdLow: [Int]
let idleInterval, sensorSampleRate, targetCount, sensorOption: Int
let thresholdHigh: [Int]
let id: Int
enum CodingKeys: String, CodingKey {
case thresholdLow = "Threshold_Low"
case idleInterval = "IdleInterval"
case sensorSampleRate = "SensorSampleRate"
case targetCount = "TargetCount"
case sensorOption = "SensorOption"
case thresholdHigh = "Threshold_High"
case id = "ID"
}
}
let data = json.data(using: .utf8)!
do {
let bundleData = try JSONDecoder().decode(BundleData.self, from: data)
print(bundleData)
}
catch {
print(error)
}