forked from ishkawa/APIKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSONDataParser.swift
More file actions
29 lines (23 loc) · 947 Bytes
/
JSONDataParser.swift
File metadata and controls
29 lines (23 loc) · 947 Bytes
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
import Foundation
/// `JSONDataParser` response JSON data.
public class JSONDataParser: DataParser {
/// Options for reading the JSON data and creating the objects.
public let readingOptions: JSONSerialization.ReadingOptions
/// Returns `JSONDataParser` with the reading options.
public init(readingOptions: JSONSerialization.ReadingOptions) {
self.readingOptions = readingOptions
}
// MARK: - DataParser
/// Value for `Accept` header field of HTTP request.
public var contentType: String? {
return "application/json"
}
/// Return `Any` that expresses structure of JSON response.
/// - Throws: `NSError` when `JSONSerialization` fails to deserialize `Data` into `Any`.
public func parse(data: Data) throws -> Any {
guard data.count > 0 else {
return [:]
}
return try JSONSerialization.jsonObject(with: data, options: readingOptions)
}
}