I am currently using this extension which works fine for basic objects. I am having a hard time figuring out how to convert nested Codable objects to dictionaries with this call...thanks in advance!
public extension Encodable {
var dictionary: [String: Any]? {
guard let data = try? JSONEncoder().encode(self) else { return nil }
return (try? JSONSerialization.jsonObject(with: data, options: .allowFragments)).flatMap { $0 as? [String: Any] }
}
}
I am not sure what do you mean by nested dictionaries but it looks like you don't want to return an array of dictionaries. I think what you are looking for is a dictionary where its values are dictionaries. Anyway I will post both options:
extension Encodable {
// this would try to encode an encodable type and decode it into an a dictionary
var dictionary: [String: Any] {
guard let data = try? JSONEncoder().encode(self) else { return [:] }
return (try? JSONSerialization.jsonObject(with: data)) as? [String: Any] ?? [:]
}
// this would try to encode an encodable type and decode it into an array of dictionaries
var dictionaries: [[String: Any]] {
guard let data = try? JSONEncoder().encode(self) else { return [] }
return (try? JSONSerialization.jsonObject(with: data)) as? [[String: Any]] ?? []
}
// this would try to encode an encodable type and decode it into a dictionary with dictionaries as its value
var nestedDictionaries: [String: [String: Any]] {
guard let data = try? JSONEncoder().encode(self) else { return [:] }
return (try? JSONSerialization.jsonObject(with: data)) as? [String: [String: Any]] ?? [:]
}
// this will return only the properties that are referring to a nested structure/classe
var nestedDictionariesOnly: [String: [String: Any]] {
guard let data = try? JSONEncoder().encode(self) else { return [:] }
return ((try? JSONSerialization.jsonObject(with: data)) as? [String: Any] ?? [:]).compactMapValues { $0 as? [String:Any] }
}
}