0001    // JSONTransformable.swift
0002    //
0003    // Copyright (c) 2015 Oven Bits, LLC
0004    //
0005    // Permission is hereby granted, free of charge, to any person obtaining a copy
0006    // of this software and associated documentation files (the "Software"), to deal
0007    // in the Software without restriction, including without limitation the rights
0008    // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
0009    // copies of the Software, and to permit persons to whom the Software is
0010    // furnished to do so, subject to the following conditions:
0011    //
0012    // The above copyright notice and this permission notice shall be included in all
0013    // copies or substantial portions of the Software.
0014    //
0015    // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0016    // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0017    // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
0018    // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0019    // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
0020    // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
0021    // SOFTWARE.
0022    
0023    import Foundation
0024    
0025    #if os(iOS)
0026    import UIKit
0027    #endif
0028    
0029    public protocol JSONTransformable
JSONTransformable.swift:41
extension JSONTransformable where Self: Model {
JSONTransformable.swift:52
extension JSONTransformable where Self: RawRepresentable, Self.RawValue == String {
JSONTransformable.swift:61
extension JSONTransformable where Self: RawRepresentable, Self.RawValue == Int {
JSONTransformable.swift:72
extension String: JSONTransformable {
JSONTransformable.swift:83
extension NSDate: JSONTransformable {
JSONTransformable.swift:143
extension Bool: JSONTransformable {
JSONTransformable.swift:154
extension NSURL: JSONTransformable {
JSONTransformable.swift:165
extension NSNumber: JSONTransformable {
JSONTransformable.swift:176
extension Double: JSONTransformable {
JSONTransformable.swift:187
extension Float: JSONTransformable {
JSONTransformable.swift:199
extension Int: JSONTransformable {
JSONTransformable.swift:210
extension UInt: JSONTransformable {
{ 0030 typealias T
JSONTransformable.swift:33
    static func fromJSON(json: JSON) -> T?
0031 0032 /// Extract object from JSON 0033 static func fromJSON
Property.swift:70
        if let newValue = PropertyType.fromJSON(jsonValue) as? PropertyType {
PropertyArray.swift:69
        values = jsonValue.flatMap { PropertyType.fromJSON($0) as? PropertyType }
PropertyDictionary.swift:71
            if let property = PropertyType.fromJSON(object.1) as? PropertyType {
(json: JSON) -> T? 0034 0035 /// Convert object to JSON 0036 func toJSON
Property.swift:79
        return value?.toJSON()
PropertyArray.swift:76
        return values.map { $0.toJSON() }
PropertyDictionary.swift:83
            jsonDictionary[key] = value.toJSON()
() -> AnyObject 0037 } 0038 0039 // MARK: Default implementation for Model subclasses 0040 0041 extension JSONTransformable where Self: Model { 0042 public static func fromJSON(json: JSON) -> Self? { 0043 return Self.init(strictJSON: json) 0044 } 0045 public func toJSON() -> AnyObject { 0046 return json().dictionary 0047 } 0048 } 0049 0050 // MARK: Default implementation for RawRepresentable types 0051 0052 extension JSONTransformable where Self: RawRepresentable, Self.RawValue == String { 0053 public static func fromJSON(json: JSON) -> Self? { 0054 return Self(rawValue: json.stringValue) 0055 } 0056 public func toJSON() -> AnyObject { 0057 return rawValue 0058 } 0059 } 0060 0061 extension JSONTransformable where Self: RawRepresentable, Self.RawValue == Int { 0062 public static func fromJSON(json: JSON) -> Self? { 0063 return Self(rawValue: json.intValue) 0064 } 0065 public func toJSON() -> AnyObject { 0066 return rawValue 0067 } 0068 } 0069 0070 // MARK: String 0071 0072 extension String: JSONTransformable { 0073 public static func fromJSON(json: JSON) -> String? { 0074 return json.string 0075 } 0076 public func toJSON() -> AnyObject { 0077 return self 0078 } 0079 } 0080 0081 // MARK: NSDate 0082 0083 extension NSDate: JSONTransformable { 0084 private class var JSONTransformableDateFormatter
JSONTransformable.swift:93
            return JSONTransformableDateFormatter.dateFromString(dateString)
JSONTransformable.swift:98
        return NSDate.JSONTransformableDateFormatter.stringFromDate(self)
: NSDateFormatter { 0085 let dateFormatter = NSDateFormatter() 0086 dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ" 0087 dateFormatter.timeZone = NSTimeZone(forSecondsFromGMT: 0) 0088 dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX") 0089 return dateFormatter 0090 } 0091 public class func fromJSON(json: JSON) -> NSDate? { 0092 if let dateString = json.string { 0093 return JSONTransformableDateFormatter.dateFromString(dateString) 0094 } 0095 return nil 0096 } 0097 public func toJSON() -> AnyObject { 0098 return NSDate.JSONTransformableDateFormatter.stringFromDate(self) 0099 } 0100 } 0101 0102 // MARK: UIColor 0103 #if os(iOS) 0104 extension UIColor: JSONTransformable { 0105 public class func fromJSON(json: JSON) -> UIColor? { 0106 if let string = json.string { 0107 var hexString = string 0108 0109 if (hexString.hasPrefix("#")) { 0110 hexString = (hexString as NSString).substringFromIndex(1) 0111 } 0112 0113 let scanner = NSScanner(string: hexString) 0114 var hex: UInt32 = 0 0115 scanner.scanHexInt(&hex) 0116 0117 let r = (hex >> 16) & 0xFF 0118 let g = (hex >> 8) & 0xFF 0119 let b = hex & 0xFF 0120 0121 return UIColor(red: CGFloat(r)/255.0, green: CGFloat(g)/255.0, blue: CGFloat(b)/255.0, alpha: 1.0) 0122 } 0123 return nil 0124 } 0125 public func toJSON() -> AnyObject { 0126 0127 var r: CGFloat = 0 0128 var g: CGFloat = 0 0129 var b: CGFloat = 0 0130 var a: CGFloat = 0 0131 0132 getRed(&r, green: &g, blue: &b, alpha: &a) 0133 0134 let hexString = NSString(format: "#%02x%02x%02x", Int(255*r),Int(255*g),Int(255*b)).uppercaseString 0135 0136 return hexString 0137 } 0138 } 0139 #endif 0140 0141 // MARK: Bool 0142 0143 extension Bool: JSONTransformable { 0144 public static func fromJSON(json: JSON) -> Bool? { 0145 return json.bool 0146 } 0147 public func toJSON() -> AnyObject { 0148 return self 0149 } 0150 } 0151 0152 // MARK: NSURL 0153 0154 extension NSURL: JSONTransformable { 0155 public class func fromJSON(json: JSON) -> NSURL? { 0156 return json.URL 0157 } 0158 public func toJSON() -> AnyObject { 0159 return absoluteString 0160 } 0161 } 0162 0163 // MARK: NSNumber 0164 0165 extension NSNumber: JSONTransformable { 0166 public class func fromJSON(json: JSON) -> NSNumber? { 0167 return json.number 0168 } 0169 public func toJSON() -> AnyObject { 0170 return self 0171 } 0172 } 0173 0174 // MARK: Double 0175 0176 extension Double: JSONTransformable { 0177 public static func fromJSON(json: JSON) -> Double? { 0178 return json.double 0179 } 0180 public func toJSON() -> AnyObject { 0181 return self 0182 } 0183 } 0184 0185 // MARK: Float 0186 0187 extension Float: JSONTransformable { 0188 public static func fromJSON(json: JSON) -> Float? { 0189 return json.float 0190 } 0191 public func toJSON() -> AnyObject { 0192 return self 0193 } 0194 } 0195 0196 0197 // MARK: Int 0198 0199 extension Int: JSONTransformable { 0200 public static func fromJSON(json: JSON) -> Int? { 0201 return json.int 0202 } 0203 public func toJSON() -> AnyObject { 0204 return self 0205 } 0206 } 0207 0208 // MARK: UInt 0209 0210 extension UInt: JSONTransformable { 0211 public static func fromJSON(json: JSON) -> UInt? { 0212 return json.uInt 0213 } 0214 public func toJSON() -> AnyObject { 0215 return self 0216 } 0217 } 0218