JSONTransaction
open class JSONTransaction<JSONIntermediateType, ResponseDataType>: HTTPTransaction<ResponseDataType>
Connects to a network service to retrieve a JSON document of the generic type
JSONIntermediateType. The JSONPayloadProcessor then attempts to convert
the JSON intermediate type into the final ResponseDataType.
-
The
JSONEnvelopeUnwrapperthat will be used to remove the JSON payload from any JSON envelope it may be wrapped in. The default implementation assumes there is no envelope, and simply returns the passed-in JSON object.Declaration
Swift
public var unwrapJSON: JSONEnvelopeUnwrapper = { _, jsonObject in -
The signature of a JSON payload processing function. This function accepts a JSON object (of type
JSONIntermediateType) and attempts to convert it toHTTPDataType.Declaration
Swift
public typealias JSONPayloadProcessor = (JSONTransaction<JSONIntermediateType, ResponseDataType>, JSONIntermediateType) throws -> ResponseDataType -
The signature of a function to remove envelope wrapping from the JSON structure returned by the server. Certain JSON-based services embed the
JSONIntermediateTypepayload inside envelope metadata about the transaction. In such cases, it is necessary to remove the envelope and return aJSONIntermediateTypewithout it so theJSONPayloadProcessorcan properly parse the JSON into theResponseDataType.Declaration
Swift
public typealias JSONEnvelopeUnwrapper = (JSONTransaction<JSONIntermediateType, ResponseDataType>, JSONIntermediateType) throws -> JSONIntermediateType -
The
JSONPayloadProcessorthat will be used to convert a JSON object (of typeAny) to theDataTypeof the transaction. The default implementation callsextractPayloadFromParsedJSON(), which simply performs a typecast. If that is insufficient, you may either replace the function stored inprocessJSONor subclass to provide an alternate implementation ofextractPayloadFromParsedJSON().Declaration
Swift
public var processJSON: JSONPayloadProcessor = { txn, jsonObject in -
Initializes a
JSONTransactionto connect to the network service at the given URL.Declaration
Swift
public init(url: URL, method: HTTPRequestMethod? = nil, upload data: Data? = nil, contentType: MIMEType? = nil, processingQueue queue: DispatchQueue = .transactionProcessing)Parameters
urlThe URL to use for conducting the transaction.
methodThe HTTP request method. When not explicitly set, defaults to
.getunlessdatais non-nil, in which case the value defaults to.post.dataOptional binary data to send to the network service.
contentTypeThe MIME type of
data. If present, this value is sent as theContent-Typeheader for the HTTP request.queueA
DispatchQueueto use for processing transaction responses. -
The payload processing function used by default for
JSONTransactioninstances.This function extracts a JSON object from binary
Datausing thejsonObject(from:)function and—if successful—attempts to convert the result to typeDataTypeusing theJSONPayloadProcessor.Throws
If
contentcould not be interpreted into a JSON object, or if that JSON object could not be interpreted as an instance of typeDataType.Declaration
Swift
open func extractPayloadFromData(transaction: HTTPTransaction<ResponseDataType>, content: Data, meta: HTTPResponseMetadata) throws -> ResponseDataTypeParameters
transactionThe transaction for which the function is executing. Under normal circumstances, this is the same as
self.contentThe content (body) of the HTTP response.
metaThe response metadata.
Return Value
The payload, an instance of
DataType. -
Attempts to convert the content of an HTTP response into a JSON object.
This implementation calls
JSONSerialization.jsonObject()with no options. Subclasses may override to provide different behavior.Throws
If
contentcould not be interpreted into a JSON object.Declaration
Swift
open func jsonObject(from content: Data) throws -> AnyParameters
contentThe content (body) of the HTTP response.
Return Value
The JSON object.
-
Attempts to interpret a JSON object as an instance of
ResponseDataType.The default implementation simply attempts to cast
jsonObjectas typeDataType. This is sufficient if you’re expecting aJSONDictionaryor aJSONArray. In other cases, subclassing or replacing theJSONPayloadProcessorfunction in theprocessJSONproperty is necessary.Throws
If
jsonObjectcould not be interpreted as an instance of typeDataType.Declaration
Swift
open func extractPayloadFromParsedJSON(_ jsonObject: JSONIntermediateType) throws -> ResponseDataTypeParameters
jsonObjectThe JSON object.
Return Value
An instance
DataType, representing the value extracted from thejsonObject.
View on GitHub
JSONTransaction Class Reference