All Verbs | /echo/complex |
---|
<?php namespace dtos;
use DateTime;
use Exception;
use DateInterval;
use JsonSerializable;
use ServiceStack\{IReturn,IReturnVoid,IGet,IPost,IPut,IDelete,IPatch,IMeta,IHasSessionId,IHasBearerToken,IHasVersion};
use ServiceStack\{ICrud,ICreateDb,IUpdateDb,IPatchDb,IDeleteDb,ISaveDb,AuditBase,QueryDb,QueryDb2,QueryData,QueryData2,QueryResponse};
use ServiceStack\{ResponseStatus,ResponseError,EmptyResponse,IdResponse,ArrayList,KeyValuePair2,StringResponse,StringsResponse,Tuple2,Tuple3,ByteArray};
use ServiceStack\{JsonConverters,Returns,TypeContext};
class SubType implements JsonSerializable
{
public function __construct(
/** @var int */
public int $id=0,
/** @var string */
public string $name=''
) {
}
/** @throws Exception */
public function fromMap($o): void {
if (isset($o['id'])) $this->id = $o['id'];
if (isset($o['name'])) $this->name = $o['name'];
}
/** @throws Exception */
public function jsonSerialize(): mixed
{
$o = [];
if (isset($this->id)) $o['id'] = $this->id;
if (isset($this->name)) $o['name'] = $this->name;
return empty($o) ? new class(){} : $o;
}
}
class EchoComplexTypes implements JsonSerializable
{
public function __construct(
/** @var SubType|null */
public ?SubType $subType=null,
/** @var array<SubType>|null */
public ?array $subTypes=null,
/** @var array<string,SubType>|null */
public ?array $subTypeMap=null,
/** @var array<string,string>|null */
public ?array $stringMap=null,
/** @var array<string,string>|null */
public ?array $intStringMap=null
) {
}
/** @throws Exception */
public function fromMap($o): void {
if (isset($o['subType'])) $this->subType = JsonConverters::from('SubType', $o['subType']);
if (isset($o['subTypes'])) $this->subTypes = JsonConverters::fromArray('SubType', $o['subTypes']);
if (isset($o['subTypeMap'])) $this->subTypeMap = JsonConverters::from(JsonConverters::context('Dictionary',genericArgs:['string','SubType']), $o['subTypeMap']);
if (isset($o['stringMap'])) $this->stringMap = JsonConverters::from(JsonConverters::context('Dictionary',genericArgs:['string','string']), $o['stringMap']);
if (isset($o['intStringMap'])) $this->intStringMap = JsonConverters::from(JsonConverters::context('Dictionary',genericArgs:['int','string']), $o['intStringMap']);
}
/** @throws Exception */
public function jsonSerialize(): mixed
{
$o = [];
if (isset($this->subType)) $o['subType'] = JsonConverters::to('SubType', $this->subType);
if (isset($this->subTypes)) $o['subTypes'] = JsonConverters::toArray('SubType', $this->subTypes);
if (isset($this->subTypeMap)) $o['subTypeMap'] = JsonConverters::to(JsonConverters::context('Dictionary',genericArgs:['string','SubType']), $this->subTypeMap);
if (isset($this->stringMap)) $o['stringMap'] = JsonConverters::to(JsonConverters::context('Dictionary',genericArgs:['string','string']), $this->stringMap);
if (isset($this->intStringMap)) $o['intStringMap'] = JsonConverters::to(JsonConverters::context('Dictionary',genericArgs:['int','string']), $this->intStringMap);
return empty($o) ? new class(){} : $o;
}
}
To override the Content-type in your clients, use the HTTP Accept Header, append the .jsv suffix or ?format=jsv
The following are sample HTTP requests and responses. The placeholders shown need to be replaced with actual values.
POST /echo/complex HTTP/1.1
Host: test.servicestack.net
Accept: text/jsv
Content-Type: text/jsv
Content-Length: length
{
subType:
{
id: 0,
name: String
},
subTypes:
[
{
id: 0,
name: String
}
],
subTypeMap:
{
String:
{
id: 0,
name: String
}
},
stringMap:
{
String: String
},
intStringMap:
{
0: String
}
}
HTTP/1.1 200 OK Content-Type: text/jsv Content-Length: length { subType: { id: 0, name: String }, subTypes: [ { id: 0, name: String } ], subTypeMap: { String: { id: 0, name: String } }, stringMap: { String: String }, intStringMap: { 0: String } }