Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift Codable with nested json as string

The services I use for backend calls returns all this json-structure:

{
    "status" : "OK",
    "payload" : **something**
}

Where something can be a simple string:

{
    "status" : "OK",
    "payload" : "nothing changed"
}

or a nested json (any json with any properties), for example:

{
    "status" : "OK",
    "payload" : {
                    "someInt" : 2,
                    "someString" : "hi",
                    ...
                }
}

This is my struct:

struct GenericResponseModel: Codable {

    let status:String?
    let payload:String?
}

I want to decode "payload" always as a string. So in the second case I want that the payload property of my "GenericResponseModel" contains the json string of that field, but If I try to decode that response I get the error:

Type 'String' mismatch: Expected to decode String but found a dictionary instead

Is possible to archive what I want?

Many thanks

like image 602
smukamuka Avatar asked Oct 16 '22 11:10

smukamuka


1 Answers

How about this…

Declare a PayloadType protocol…

protocol PayloadType: Decodable { }

and make String, and struct Payload conform to it…

extension String: PayloadType { }

struct Payload: Decodable, PayloadType {
    let someInt: Int
    let someString: String
}

Then make GenericResponseModel generic…

struct GenericResponseModel<T: PayloadType>: Decodable {

    let status: String
    let payload: T

    enum CodingKeys: CodingKey {
        case status, payload
    }

    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        status = try container.decode(String.self, forKey: .status)            
        payload = try container.decode(T.self, forKey: .payload)
    }
}

Then you can decode as follows…

let data = """
{
"status" : "OK",
"payload" : "nothing changed"
}
""".data(using: .utf8)!

print(try JSONDecoder().decode(GenericResponseModel<String>.self, from: data))

// GenericResponseModel<String>(status: "OK", payload: "nothing changed")

and

let data2 = """
{
"status" : "OK",
"payload" : {
"someInt" : 2,
"someString" : "hi"
}
}
""".data(using: .utf8)!

print(try JSONDecoder().decode(GenericResponseModel<Payload>.self, from: data2))

// GenericResponseModel<Payload>(status: "OK", payload: Payload(someInt: 2, someString: "hi"))


Of course, this relies on you knowing the payload type in advance. You could get around this by throwing a specific error if payload is the wrong type…

enum GenericResponseModelError: Error {
    case wrongPayloadType
}

and then…

init(from decoder: Decoder) throws {
    let container = try decoder.container(keyedBy: CodingKeys.self)
    status = try container.decode(String.self, forKey: .status)

    do {
        payload = try container.decode(T.self, forKey: .payload)
    } catch {
        throw GenericResponseModelError.wrongPayloadType
    }
}

Then handle this error when you decode…

let data = """
{
"status" : "OK",
"payload" : {
"someInt" : 2,
"someString" : "hi"
}
}
""".data(using: .utf8)!

do {
    let response = try JSONDecoder().decode(GenericResponseModel<String>.self, from: data) // Throws
    print(response) 
} catch let error as GenericResponseModelError where error == .wrongPayloadType {
    let response = try JSONDecoder().decode(GenericResponseModel<Payload>.self, from: data2) // Success!
    print(response)
}
like image 157
Ashley Mills Avatar answered Nov 04 '22 01:11

Ashley Mills