Search code examples
swiftcodablejsondecoder

How to keep a flexible structure when using Codable in Swift


I've got a API response structure, representing a user object, that looks like this:

{
    "statuscode": 200,
    "response_type": 3,
    "errormessage": null,
    "detailresponse": {
        "id": "2",
        "shopifyautosync": null,
        "platformfeepercentage": null,
        "invited": null,
        "requiresyoutubesocialmediaupdate": 1,
        // Other properties ...
}

I'm using JSONDecoder().decode to decode to following structures:

import Foundation

class Response: Decodable {
    var statuscode: Int?
    var response_type: Int?
    // Other properties
    var detailresponse: User?
}

import Foundation

class User: Codable {
    var id: String?
    var street: String?
    var supporturl: String?
    var verifiedaccount: Int?
    var showfeatureupdatemodal: Int?
    var admin: Int?
    var email: String?
    // Other properties
}

Here's how I then decode:

let response = try JSONDecoder().decode(Response.self, from: jsonData)

My main problem now is that the Response class' detailresponse property is hard-wired to a User struct. However, I need a little flexibility in my setup, as the detailresponse will, of course, carry other data structures when calling different endpoints (e.g. a cooperation object instead of a user object).

Is there an elegant way to keep the detailresponse inside the Response class flexible instead of hard-wiring it? Or a generally better approach to the problem instead?


Solution

  • You need to make use of generics

    class Response<T:Decodable>: Decodable {
        var statuscode: Int?
        var response_type: Int?
        // Other properties
        var detailresponse: T?
    }
    

    Then

    let response = try JSONDecoder().decode(Response<User>.self, from: jsonData)