docs/Classes/DecodableResponseSerializer.html
public final class DecodableResponseSerializer<T> : ResponseSerializer where T : Decodable, T : Sendable
A ResponseSerializer that decodes the response data as a Decodable value using any decoder that conforms to DataDecoder. By default, this is an instance of JSONDecoder.
Note
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.
Note
JSONDecoder and PropertyListDecoder are not Sendable on Apple platforms until macOS 13+ or iOS 16+, so instances passed to a serializer should not be used outside of the serializer. Additionally, ensure a new serializer is created for each request, do not use a single, shared serializer, so as to ensure separate decoder instances.
`
dataPreprocessor
`
Swift
public let dataPreprocessor: any DataPreprocessor
`
decoder
`
The DataDecoder instance used to decode responses.
Swift
public let decoder: any DataDecoder
`
emptyResponseCodes
`
Swift
public let emptyResponseCodes: Set<Int>
`
emptyRequestMethods
`
Swift
public let emptyRequestMethods: Set<HTTPMethod>
`
init(dataPreprocessor:decoder:emptyResponseCodes:emptyRequestMethods:)
`
Creates an instance using the values provided.
Swift
public init(dataPreprocessor: any DataPreprocessor = DecodableResponseSerializer.defaultDataPreprocessor,
decoder: any DataDecoder = JSONDecoder(),
emptyResponseCodes: Set<Int> = DecodableResponseSerializer.defaultEmptyResponseCodes,
emptyRequestMethods: Set<HTTPMethod> = DecodableResponseSerializer.defaultEmptyRequestMethods)
| dataPreprocessor |
DataPreprocessor used to prepare the received Data 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.
|
`
serialize(request:response:data:error:)
`
Swift
public func serialize(request: URLRequest?, response: HTTPURLResponse?, data: Data?, error: (any Error)?) throws -> T