ipbc-Client_macOS/ipbc-Client/Pods/SwiftHTTP/HTTPResponseSerializer.swift
2015-09-13 23:37:18 +02:00

37 lines
1.5 KiB
Swift

//////////////////////////////////////////////////////////////////////////////////////////////////
//
// HTTPResponseSerializer.swift
//
// Created by Dalton Cherry on 6/16/14.
// Copyright (c) 2014 Vluxe. All rights reserved.
//
//////////////////////////////////////////////////////////////////////////////////////////////////
import Foundation
/// This protocol provides a way to implement a custom serializer.
public protocol HTTPResponseSerializer {
/// This can be used if you want to have your data parsed/serialized into something instead of just a NSData blob.
func responseObjectFromResponse(response: NSURLResponse, data: NSData) -> (object: AnyObject?, error: NSError?)
}
/// Serialize the data into a JSON object.
public struct JSONResponseSerializer : HTTPResponseSerializer {
/// Initializes a new JSONResponseSerializer Object.
public init(){}
/**
Creates a HTTPOperation that can be scheduled on a NSOperationQueue. Called by convenience HTTP verb methods below.
:param: response The NSURLResponse.
:param: data The response data to be parsed into JSON.
:returns: Returns a object from JSON data and an NSError if an error occured while parsing the data.
*/
public func responseObjectFromResponse(response: NSURLResponse, data: NSData) -> (object: AnyObject?, error: NSError?) {
var error: NSError?
let response: AnyObject? = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions(), error: &error)
return (response,error)
}
}