Test

<back to all web services

HelloSubAllTypes

import Foundation
import ServiceStack

public class HelloSubAllTypes : AllTypesBase
{
    public var hierarchy:Int

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

    private enum CodingKeys : String, CodingKey {
        case hierarchy
    }

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

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

public class AllTypesBase : Codable
{
    public var id:Int
    public var nullableId:Int?
    public var byte:UInt8
    public var short:Int16
    public var int:Int
    public var long:Int
    public var uShort:UInt16
    public var uInt:UInt32
    public var uLong:UInt64
    public var float:Float
    public var double:Double
    public var decimal:Double
    public var string:String
    public var dateTime:Date
    @TimeSpan public var timeSpan:TimeInterval
    public var dateTimeOffset:Date
    public var guid:String
    public var char:String
    public var keyValuePair:KeyValuePair<String, String>
    public var nullableDateTime:Date?
    @TimeSpan public var nullableTimeSpan:TimeInterval?
    public var stringList:[String] = []
    public var stringArray:[String] = []
    public var stringMap:[String:String] = [:]
    public var intStringMap:[Int:String] = [:]
    public var subType:SubType

    required public init(){}

    private enum CodingKeys : String, CodingKey {
        case id
        case nullableId
        case byte
        case short
        case int
        case long
        case uShort
        case uInt
        case uLong
        case float
        case double
        case decimal
        case string
        case dateTime
        case timeSpan
        case dateTimeOffset
        case guid
        case char
        case keyValuePair
        case nullableDateTime
        case nullableTimeSpan
        case stringList
        case stringArray
        case stringMap
        case intStringMap
        case subType
    }

    required public init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        id = try container.decodeIfPresent(Int.self, forKey: .id)
        nullableId = try container.decodeIfPresent(Int.self, forKey: .nullableId)
        byte = try container.decodeIfPresent(UInt8.self, forKey: .byte)
        short = try container.decodeIfPresent(Int16.self, forKey: .short)
        int = try container.decodeIfPresent(Int.self, forKey: .int)
        long = try container.decodeIfPresent(Int.self, forKey: .long)
        uShort = try container.decodeIfPresent(UInt16.self, forKey: .uShort)
        uInt = try container.decodeIfPresent(UInt32.self, forKey: .uInt)
        uLong = try container.decodeIfPresent(UInt64.self, forKey: .uLong)
        float = try container.decodeIfPresent(Float.self, forKey: .float)
        double = try container.decodeIfPresent(Double.self, forKey: .double)
        decimal = try container.decodeIfPresent(Double.self, forKey: .decimal)
        string = try container.decodeIfPresent(String.self, forKey: .string)
        dateTime = try container.decodeIfPresent(Date.self, forKey: .dateTime)
        timeSpan = try container.convertIfPresent(TimeInterval.self, forKey: .timeSpan)
        dateTimeOffset = try container.decodeIfPresent(Date.self, forKey: .dateTimeOffset)
        guid = try container.decodeIfPresent(String.self, forKey: .guid)
        char = try container.decodeIfPresent(String.self, forKey: .char)
        keyValuePair = try container.decodeIfPresent(KeyValuePair<String, String>.self, forKey: .keyValuePair)
        nullableDateTime = try container.decodeIfPresent(Date.self, forKey: .nullableDateTime)
        nullableTimeSpan = try container.convertIfPresent(TimeInterval.self, forKey: .nullableTimeSpan)
        stringList = try container.decodeIfPresent([String].self, forKey: .stringList) ?? []
        stringArray = try container.decodeIfPresent([String].self, forKey: .stringArray) ?? []
        stringMap = try container.decodeIfPresent([String:String].self, forKey: .stringMap) ?? [:]
        intStringMap = try container.decodeIfPresent([Int:String].self, forKey: .intStringMap) ?? [:]
        subType = try container.decodeIfPresent(SubType.self, forKey: .subType)
    }

    public func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        if id != nil { try container.encode(id, forKey: .id) }
        if nullableId != nil { try container.encode(nullableId, forKey: .nullableId) }
        if byte != nil { try container.encode(byte, forKey: .byte) }
        if short != nil { try container.encode(short, forKey: .short) }
        if int != nil { try container.encode(int, forKey: .int) }
        if long != nil { try container.encode(long, forKey: .long) }
        if uShort != nil { try container.encode(uShort, forKey: .uShort) }
        if uInt != nil { try container.encode(uInt, forKey: .uInt) }
        if uLong != nil { try container.encode(uLong, forKey: .uLong) }
        if float != nil { try container.encode(float, forKey: .float) }
        if double != nil { try container.encode(double, forKey: .double) }
        if decimal != nil { try container.encode(decimal, forKey: .decimal) }
        if string != nil { try container.encode(string, forKey: .string) }
        if dateTime != nil { try container.encode(dateTime, forKey: .dateTime) }
        if timeSpan != nil { try container.encode(timeSpan, forKey: .timeSpan) }
        if dateTimeOffset != nil { try container.encode(dateTimeOffset, forKey: .dateTimeOffset) }
        if guid != nil { try container.encode(guid, forKey: .guid) }
        if char != nil { try container.encode(char, forKey: .char) }
        if keyValuePair != nil { try container.encode(keyValuePair, forKey: .keyValuePair) }
        if nullableDateTime != nil { try container.encode(nullableDateTime, forKey: .nullableDateTime) }
        if nullableTimeSpan != nil { try container.encode(nullableTimeSpan, forKey: .nullableTimeSpan) }
        if stringList.count > 0 { try container.encode(stringList, forKey: .stringList) }
        if stringArray.count > 0 { try container.encode(stringArray, forKey: .stringArray) }
        if stringMap.count > 0 { try container.encode(stringMap, forKey: .stringMap) }
        if intStringMap.count > 0 { try container.encode(intStringMap, forKey: .intStringMap) }
        if subType != nil { try container.encode(subType, forKey: .subType) }
    }
}

public class SubType : Codable
{
    public var id:Int
    public var name:String

    required public init(){}
}

public class SubAllTypes : AllTypesBase
{
    public var hierarchy:Int

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

    private enum CodingKeys : String, CodingKey {
        case hierarchy
    }

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

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


Swift HelloSubAllTypes DTOs

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

HTTP + JSV

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

POST /jsv/reply/HelloSubAllTypes HTTP/1.1 
Host: test.servicestack.net 
Accept: text/jsv
Content-Type: text/jsv
Content-Length: length

{
	hierarchy: 0,
	id: 0,
	nullableId: 0,
	byte: 0,
	short: 0,
	int: 0,
	long: 0,
	uShort: 0,
	uInt: 0,
	uLong: 0,
	float: 0,
	double: 0,
	decimal: 0,
	string: String,
	dateTime: 0001-01-01,
	timeSpan: PT0S,
	dateTimeOffset: 0001-01-01T00:00:00.0000000+00:00,
	guid: 00000000000000000000000000000000,
	char: ,
	keyValuePair: 
	{
		key: String,
		value: String
	},
	nullableDateTime: 0001-01-01,
	nullableTimeSpan: PT0S,
	stringList: 
	[
		String
	],
	stringArray: 
	[
		String
	],
	stringMap: 
	{
		String: String
	},
	intStringMap: 
	{
		0: String
	},
	subType: 
	{
		id: 0,
		name: String
	}
}
HTTP/1.1 200 OK
Content-Type: text/jsv
Content-Length: length

{
	hierarchy: 0,
	id: 0,
	nullableId: 0,
	byte: 0,
	short: 0,
	int: 0,
	long: 0,
	uShort: 0,
	uInt: 0,
	uLong: 0,
	float: 0,
	double: 0,
	decimal: 0,
	string: String,
	dateTime: 0001-01-01,
	timeSpan: PT0S,
	dateTimeOffset: 0001-01-01T00:00:00.0000000+00:00,
	guid: 00000000000000000000000000000000,
	char: ,
	keyValuePair: 
	{
		key: String,
		value: String
	},
	nullableDateTime: 0001-01-01,
	nullableTimeSpan: PT0S,
	stringList: 
	[
		String
	],
	stringArray: 
	[
		String
	],
	stringMap: 
	{
		String: String
	},
	intStringMap: 
	{
		0: String
	},
	subType: 
	{
		id: 0,
		name: String
	}
}