Files
hyperoslo-Cache/Source/Shared/Extensions/JSON+Cache.swift
T
2015-12-21 13:04:03 +01:00

34 lines
729 B
Swift

import Foundation
// MARK: - Cachable
extension JSON: Cachable {
public typealias CacheType = JSON
public static func decode(data: NSData) -> CacheType? {
var result: CacheType?
do {
let object = try NSJSONSerialization.JSONObjectWithData(data,
options: NSJSONReadingOptions())
switch (object) {
case let dictionary as [String : AnyObject]:
result = JSON.Dictionary(dictionary)
case let array as [AnyObject]:
result = JSON.Array(array)
default:
result = nil
}
} catch {}
return result
}
public func encode() -> NSData? {
return try? NSJSONSerialization.dataWithJSONObject(object,
options: NSJSONWritingOptions())
}
}