forked from daltoniam/SwiftHTTP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTTPResponseSerializer.swift
More file actions
36 lines (30 loc) · 1.54 KB
/
HTTPResponseSerializer.swift
File metadata and controls
36 lines (30 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//////////////////////////////////////////////////////////////////////////////////////////////////
//
// 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)
}
}