0

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] }
  }
}
New Dev
  • 48,427
  • 12
  • 87
  • 129

1 Answers1

2

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] }
    }
}
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
  • So for example : if i had a user object with a profile object inside. I want to convert the user object to a dictionary but within that dictionary convert the profile object to a dictionary. [“user”: [“profile” : some data]] – TheCoderateKid Aug 14 '20 at 01:56
  • Show your encodable object and the expected output – Leo Dabus Aug 14 '20 at 01:56
  • struct Example: Codable { let nestedExample: NestedExample } struct NestedExample: Codable { let nestedExample: String } Expected output let dict = ["example" : ["nestedExample": "nestedString"]] Thanks for the help – TheCoderateKid Aug 14 '20 at 21:47
  • I will say this wont work for an object that looks like this struct Example: Codable { let nestedExample: NestedExample let samepleString: String } – TheCoderateKid Aug 18 '20 at 20:41
  • So what do you want? Do you want to discard those properties? You can also return nil instead of an empty dictionary. – Leo Dabus Aug 18 '20 at 20:43
  • Check my last edit. It is not clear whats the result you are trying to achieve. – Leo Dabus Aug 18 '20 at 20:54
  • no i want to have a dictionary create that has all properties whether its a nested object which can be converted to a dict and have a key with a string value. – TheCoderateKid Aug 18 '20 at 20:55
  • So you need to return single dictionary – Leo Dabus Aug 18 '20 at 20:56
  • check my last edit. There is no magic type declaration (other than `[Stirng:Any]`) to decode a `jsonObject` into. You would need to cast the dictionary values from `Any` to the actual types. – Leo Dabus Aug 18 '20 at 20:59