Codable [String: Any] or generic Dictionary in Swift 5

It looks like the solution in this example for Swift 4 no longer works in Swift 5.

https://forums.developer.apple.com/thread/80288


struct Customer: Codable { 
     let id: String 
     let email: String 
     let metadata: [String: Any] 
}


What's the best way to decode an arbitrary dictionary in Swift 5? Is the codable protocol even an option? Do we manually parse it using JSONSerialization?

Replies

Looks like that was the same in Swift 4.


Issue comes from Any. If you replace by a codable as String, it works.


struct Customer: Codable {  
     let id: String  
     let email: String  
     let metadata: [String: String]  
}


Should read this to see how to handle for Any.


h ttps://forums.swift.org/t/dealing-with-heterogenous-lists-dictionary-with-codable/6854/4