DecodableResponseSerializer
public final class DecodableResponseSerializer<T> : ResponseSerializer where T : Decodable
A ResponseSerializer
that decodes the response data as a generic value using any type that conforms to
DataDecoder
. By default, this is an instance of JSONDecoder
. Additionally, a request returning nil
or no data
is considered an error. However, if the request has an HTTPMethod
or the response has an HTTP status code valid
for empty responses then an empty value will be returned. If the decoded type conforms to EmptyResponse
, the
type’s emptyValue()
will be returned. If the decoded type is Empty
, the .value
instance is returned. If the
decoded type does not conform to EmptyResponse
and isn’t Empty
, an error will be produced.
-
Declaration
Swift
public let dataPreprocessor: DataPreprocessor
-
The
DataDecoder
instance used to decode responses.Declaration
Swift
public let decoder: DataDecoder
-
Declaration
Swift
public let emptyResponseCodes: Set<Int>
-
Declaration
Swift
public let emptyRequestMethods: Set<HTTPMethod>
-
Creates an instance using the values provided.
Declaration
Swift
public init(dataPreprocessor: DataPreprocessor = DecodableResponseSerializer.defaultDataPreprocessor, decoder: DataDecoder = JSONDecoder(), emptyResponseCodes: Set<Int> = DecodableResponseSerializer.defaultEmptyResponseCodes, emptyRequestMethods: Set<HTTPMethod> = DecodableResponseSerializer.defaultEmptyRequestMethods)
Parameters
dataPreprocessor
DataPreprocessor
used to prepare the receivedData
for serialization.decoder
The
DataDecoder
.JSONDecoder()
by default.emptyResponseCodes
The HTTP response codes for which empty responses are allowed.
[204, 205]
by default.emptyRequestMethods
The HTTP request methods for which empty responses are allowed.
[.head]
by default. -
Declaration
Swift
public func serialize(request: URLRequest?, response: HTTPURLResponse?, data: Data?, error: Error?) throws -> T