With Swift 3 JSONSerialization, if a part of your data model was completely dynamic, you could always just leave the deserialized data at Any and let the consumer deal with it.
With Swift 4's Codable, I'd like to do this:
struct Foo : Codable {
let bar: Any;
}
But I get
JSONPlayground.playground:4:9: note: cannot automatically synthesize 'Decodable' because 'Any' does not conform to 'Decodable'
let bar: Any;
^
That alone wouldn't be the end of the world if I could implement my own Decodable, but that would require the Decoder to support decoding to Any, which as far as I can tell it does not. For example:
extension Foo {
init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
let result = try container.decode(AnyClass.self)
}
}
gives
error: JSONPlayground.playground:4:36: error: cannot invoke 'decode' with an argument list of type '(AnyClass.Protocol)'
let result = try container.decode(AnyClass.self)
^
Is there any solution to this?