Skip to content

Interface for extending JSONDecoder? #67

@bejar37

Description

@bejar37

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions