matthewcheok / JSONCodable

Hassle-free JSON encoding and decoding in Swift
MIT License
602 stars 66 forks source link

Interface for extending `JSONDecoder`? #67

Open bejar37 opened 7 years ago

bejar37 commented 7 years ago

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:

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).