Test

<back to all web services

HelloWithGenericInheritance

import Foundation
import ServiceStack

public class HelloWithGenericInheritance : HelloBase<Poco>
{
    public var result:String

    required public init(){ super.init() }

    private enum CodingKeys : String, CodingKey {
        case result
    }

    required public init(from decoder: Decoder) throws {
        try super.init(from: decoder)
        let container = try decoder.container(keyedBy: CodingKeys.self)
        result = try container.decodeIfPresent(String.self, forKey: .result)
    }

    public override func encode(to encoder: Encoder) throws {
        try super.encode(to: encoder)
        var container = encoder.container(keyedBy: CodingKeys.self)
        if result != nil { try container.encode(result, forKey: .result) }
    }
}

public class HelloBase<T : Codable> : Codable
{
    public var items:[T] = []
    public var counts:[Int] = []

    required public init(){}
}

public class Poco : Codable
{
    public var name:String

    required public init(){}
}


Swift HelloWithGenericInheritance DTOs

To override the Content-type in your clients, use the HTTP Accept Header, append the .json suffix or ?format=json

To embed the response in a jsonp callback, append ?callback=myCallback

HTTP + JSON

The following are sample HTTP requests and responses. The placeholders shown need to be replaced with actual values.

POST /json/oneway/HelloWithGenericInheritance HTTP/1.1 
Host: test.servicestack.net 
Accept: application/json
Content-Type: application/json
Content-Length: length

{"result":"String","items":[{"name":"String"}],"counts":[0]}