JSONCodable icon indicating copy to clipboard operation
JSONCodable copied to clipboard

Interface for extending `JSONDecoder`?

Open bejar37 opened this issue 9 years ago • 0 comments

Hi, It's probably easiest for me to explain my use case: The application that I work on uses an API which sends down information that looks vaguely like this:

{
   ...
  "car": {
     "make": "Honda",
     "model": "Accord"
  }
}

The application itself holds separate caches with rich models referring to "Honda Accord". During parsing, I'd like to deserialize this model into a SimpleCar model, and then perform a lookup in my cache for the richer model, throwing an error if the model is not found.

My feeling is that this should be done during parse time, so I want to be able to add this contextual information to the JSONDecoder. In the current design JSONDecoder is not extensible. What are your thoughts on:

  • Making JSONDecoder a protocol
  • Use a protocol extension to provide all of the JSON parsing functionality we have now
  • Provide a BaseJSONDecoder type implementing JSONDecoder
  • Add an associatedtype to JSONDecodable
protocol JSONDecodable {
   associatedtype Decoder: JSONDecoder
   ...
}

I'm sketching this out and I don't see a particular reason why we couldn't do this. Another possibility is making JSONDecoder open to subclassing, which I don't think it is (and object is internal).

bejar37 avatar Dec 18 '16 15:12 bejar37