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] }
  }
}
0

1 Answer 1

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] }
    }
}
Sign up to request clarification or add additional context in comments.

9 Comments

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]]
Show your encodable object and the expected output
struct Example: Codable { let nestedExample: NestedExample } struct NestedExample: Codable { let nestedExample: String } Expected output let dict = ["example" : ["nestedExample": "nestedString"]] Thanks for the help
I will say this wont work for an object that looks like this struct Example: Codable { let nestedExample: NestedExample let samepleString: String }
So what do you want? Do you want to discard those properties? You can also return nil instead of an empty dictionary.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.