Hej, it is nice to have JSON as an intermediate correctly-typed abstraction over same JSON data, but at some point I might still want to work with some dedicated data class. However, at that point I might only have the JSON left and I can not make use of some Decoder to turn my JSON into an appropriate Decodable for the task at hand.
I have a concret use case for that, where the concrete structure of my JSON is only clear at a later point in time.
Thus, I would suggest to provide a GenericJSONDecoder to turn a JSON into a Decodable. Analogous to JSONDecoder and Data.
The solution proposed here solves the issue in the meantime, but is really wasteful and rather just a short-time fix:
func decode<T: Decodable>(from json: JSON) throws -> T {
let data = try JSONEncoder().encode(json)
return try JSONDecoder().decode(T.self, from: data)
}
Hej, it is nice to have
JSONas an intermediate correctly-typed abstraction over same JSON data, but at some point I might still want to work with some dedicated data class. However, at that point I might only have theJSONleft and I can not make use of someDecoderto turn myJSONinto an appropriateDecodablefor the task at hand.I have a concret use case for that, where the concrete structure of my JSON is only clear at a later point in time.
Thus, I would suggest to provide a
GenericJSONDecoderto turn aJSONinto aDecodable. Analogous toJSONDecoderandData.The solution proposed here solves the issue in the meantime, but is really wasteful and rather just a short-time fix: