0001    //////////////////////////////////////////////////////////////////////////////////////////////////
0002    //
0003    //  JSONJoy.swift
0004    //
0005    //  Created by Dalton Cherry on 9/17/14.
0006    //  Copyright (c) 2014 Vluxe. All rights reserved.
0007    //
0008    //////////////////////////////////////////////////////////////////////////////////////////////////
0009    
0010    import Foundation
0011    
0012    public enum JSONError
JSONJoy.swift:83
        guard let str = string else {throw JSONError.WrongType}
JSONJoy.swift:89
        guard let i = integer else {throw JSONError.WrongType}
JSONJoy.swift:95
        guard let i = unsigned else {throw JSONError.WrongType}
JSONJoy.swift:101
        guard let i = double else {throw JSONError.WrongType}
JSONJoy.swift:107
        guard let i = float else {throw JSONError.WrongType}
JSONJoy.swift:113
        guard let i = number else {throw JSONError.WrongType}
: ErrorType { 0013 case WrongType
JSONJoy.swift:83
        guard let str = string else {throw JSONError.WrongType}
JSONJoy.swift:89
        guard let i = integer else {throw JSONError.WrongType}
JSONJoy.swift:95
        guard let i = unsigned else {throw JSONError.WrongType}
JSONJoy.swift:101
        guard let i = double else {throw JSONError.WrongType}
JSONJoy.swift:107
        guard let i = float else {throw JSONError.WrongType}
JSONJoy.swift:113
        guard let i = number else {throw JSONError.WrongType}
0014 } 0015 0016 public class JSONDecoder
JSONJoy.swift:74
        return value as? Dictionary<String,JSONDecoder>
JSONJoy.swift:73
    public var dictionary: Dictionary<String,JSONDecoder>? {
JSONJoy.swift:78
        return value as? Array<JSONDecoder>
JSONJoy.swift:77
    public var array: Array<JSONDecoder>? {
JSONJoy.swift:119
        if let array = value as? Array<JSONDecoder> {
JSONJoy.swift:132
        if let dictionary = value as? Dictionary<String,JSONDecoder> {
JSONJoy.swift:158
            var collect = [JSONDecoder]()
JSONJoy.swift:160
                collect.append(JSONDecoder(val))
JSONJoy.swift:164
            var collect = Dictionary<String,JSONDecoder>()
JSONJoy.swift:166
                collect[key as! String] = JSONDecoder(val)
JSONJoy.swift:178
                    return array[index] as! JSONDecoder
JSONJoy.swift:181
            return JSONDecoder(createError("index: \(index) is greater than array or this is not an Array type."))
JSONJoy.swift:189
                    return value as! JSONDecoder
JSONJoy.swift:192
            return JSONDecoder(createError("key: \(key) does not exist or this is not a Dictionary type"))
JSONJoy.swift:231
    init(_ decoder: JSONDecoder) throws
{ 0017 var value
JSONJoy.swift:21
        return value
JSONJoy.swift:29
        return value as? String
JSONJoy.swift:34
        return value as? Int
JSONJoy.swift:38
        return value as? UInt
JSONJoy.swift:42
        return value as? Double
JSONJoy.swift:46
        return value as? Float
JSONJoy.swift:50
        return value as? NSNumber
JSONJoy.swift:70
        return value as? NSError
JSONJoy.swift:74
        return value as? Dictionary<String,JSONDecoder>
JSONJoy.swift:78
        return value as? Array<JSONDecoder>
JSONJoy.swift:119
        if let array = value as? Array<JSONDecoder> {
JSONJoy.swift:124
                if let obj = decoder.value as? T {
JSONJoy.swift:132
        if let dictionary = value as? Dictionary<String,JSONDecoder> {
JSONJoy.swift:137
                if let obj = decoder.value as? T {
JSONJoy.swift:153
                value = error
JSONJoy.swift:162
            value = collect
JSONJoy.swift:168
            value = collect
JSONJoy.swift:170
            value = rawObject
JSONJoy.swift:176
            if let array = self.value as? NSArray {
JSONJoy.swift:187
            if let dict = self.value as? NSDictionary {
JSONJoy.swift:217
        if value != nil {
JSONJoy.swift:219
                return "\"\(value!)\""
JSONJoy.swift:220
            } else if let _ = value as? NSNull {
JSONJoy.swift:223
            return "\(value!)"
: AnyObject? 0018 0019 ///return the value raw 0020 public var rawValue: AnyObject? { 0021 return value 0022 } 0023 ///print the description of the JSONDecoder 0024 public var description: String { 0025 return self.print() 0026 } 0027 ///convert the value to a String 0028 public var string
JSONJoy.swift:54
        if let str = self.string {
JSONJoy.swift:83
        guard let str = string else {throw JSONError.WrongType}
JSONJoy.swift:218
            if let _ = self.string {
: String? { 0029 return value as? String 0030 } 0031 0032 ///convert the value to an Int 0033 public var integer
JSONJoy.swift:59
        } else if let num = self.integer {
JSONJoy.swift:89
        guard let i = integer else {throw JSONError.WrongType}
: Int? { 0034 return value as? Int 0035 } 0036 ///convert the value to an UInt 0037 public var unsigned
JSONJoy.swift:95
        guard let i = unsigned else {throw JSONError.WrongType}
: UInt? { 0038 return value as? UInt 0039 } 0040 ///convert the value to a Double 0041 public var double
JSONJoy.swift:61
        } else if let num = self.double {
JSONJoy.swift:101
        guard let i = double else {throw JSONError.WrongType}
: Double? { 0042 return value as? Double 0043 } 0044 ///convert the value to a float 0045 public var float
JSONJoy.swift:63
        } else if let num = self.float {
JSONJoy.swift:107
        guard let i = float else {throw JSONError.WrongType}
: Float? { 0046 return value as? Float 0047 } 0048 ///convert the value to an NSNumber 0049 public var number
JSONJoy.swift:113
        guard let i = number else {throw JSONError.WrongType}
: NSNumber? { 0050 return value as? NSNumber 0051 } 0052 ///treat the value as a bool 0053 public var bool: Bool { 0054 if let str = self.string { 0055 let lower = str.lowercaseString 0056 if lower == "true" || Int(lower) > 0 { 0057 return true 0058 } 0059 } else if let num = self.integer { 0060 return num > 0 0061 } else if let num = self.double { 0062 return num > 0.99 0063 } else if let num = self.float { 0064 return num > 0.99 0065 } 0066 return false 0067 } 0068 //get the value if it is an error 0069 public var error: NSError? { 0070 return value as? NSError 0071 } 0072 //get the value if it is a dictionary 0073 public var dictionary
JSONJoy.swift:209
        } else if let dict = self.dictionary {
: Dictionary<String,JSONDecoder>? { 0074 return value as? Dictionary<String,JSONDecoder> 0075 } 0076 //get the value if it is an array 0077 public var array
JSONJoy.swift:202
        if let arr = self.array {
: Array<JSONDecoder>? { 0078 return value as? Array<JSONDecoder> 0079 } 0080 0081 //get the string and have it throw if it doesn't work 0082 public func getString() throws -> String { 0083 guard let str = string else {throw JSONError.WrongType} 0084 return str 0085 } 0086 0087 //get the int and have it throw if it doesn't work 0088 public func getInt() throws -> Int { 0089 guard let i = integer else {throw JSONError.WrongType} 0090 return i 0091 } 0092 0093 //get the unsigned and have it throw if it doesn't work 0094 public func getUnsigned() throws -> UInt { 0095 guard let i = unsigned else {throw JSONError.WrongType} 0096 return i 0097 } 0098 0099 //get the double and have it throw if it doesn't work 0100 public func getDouble() throws -> Double { 0101 guard let i = double else {throw JSONError.WrongType} 0102 return i 0103 } 0104 0105 //get the Float and have it throw if it doesn't work 0106 public func getFloat() throws -> Float { 0107 guard let i = float else {throw JSONError.WrongType} 0108 return i 0109 } 0110 0111 //get the number and have it throw if it doesn't work 0112 public func getFloat() throws -> NSNumber { 0113 guard let i = number else {throw JSONError.WrongType} 0114 return i 0115 } 0116 0117 //pull the raw values out of an array 0118 public func getArray<T>(inout collect: Array<T>?) { 0119 if let array = value as? Array<JSONDecoder> { 0120 if collect == nil { 0121 collect = Array<T>() 0122 } 0123 for decoder in array { 0124 if let obj = decoder.value as? T { 0125 collect?.append(obj) 0126 } 0127 } 0128 } 0129 } 0130 ///pull the raw values out of a dictionary. 0131 public func getDictionary<T>(inout collect: Dictionary<String,T>?) { 0132 if let dictionary = value as? Dictionary<String,JSONDecoder> { 0133 if collect == nil { 0134 collect = Dictionary<String,T>() 0135 } 0136 for (key,decoder) in dictionary { 0137 if let obj = decoder.value as? T { 0138 collect?[key] = obj 0139 } 0140 } 0141 } 0142 } 0143 ///the init that converts everything to something nice 0144 public init
JSONJoy.swift:160
                collect.append(JSONDecoder(val))
JSONJoy.swift:166
                collect[key as! String] = JSONDecoder(val)
JSONJoy.swift:181
            return JSONDecoder(createError("index: \(index) is greater than array or this is not an Array type."))
JSONJoy.swift:192
            return JSONDecoder(createError("key: \(key) does not exist or this is not a Dictionary type"))
(_ raw: AnyObject) { 0145 var rawObject: AnyObject = raw 0146 if let data = rawObject as? NSData { 0147 var response: AnyObject? 0148 do { 0149 try response = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions()) 0150 rawObject = response! 0151 } 0152 catch let error as NSError { 0153 value = error 0154 return 0155 } 0156 } 0157 if let array = rawObject as? NSArray { 0158 var collect = [JSONDecoder]() 0159 for val: AnyObject in array { 0160 collect.append(JSONDecoder(val)) 0161 } 0162 value = collect 0163 } else if let dict = rawObject as? NSDictionary { 0164 var collect = Dictionary<String,JSONDecoder>() 0165 for (key,val) in dict { 0166 collect[key as! String] = JSONDecoder(val) 0167 } 0168 value = collect 0169 } else { 0170 value = rawObject 0171 } 0172 } 0173 ///Array access support 0174 public subscript(index: Int) -> JSONDecoder { 0175 get { 0176 if let array = self.value as? NSArray { 0177 if array.count > index { 0178 return array[index] as! JSONDecoder 0179 } 0180 } 0181 return JSONDecoder(createError("index: \(index) is greater than array or this is not an Array type.")) 0182 } 0183 } 0184 ///Dictionary access support 0185 public subscript(key: String) -> JSONDecoder { 0186 get { 0187 if let dict = self.value as? NSDictionary { 0188 if let value: AnyObject = dict[key] { 0189 return value as! JSONDecoder 0190 } 0191 } 0192 return JSONDecoder(createError("key: \(key) does not exist or this is not a Dictionary type")) 0193 } 0194 } 0195 ///private method to create an error 0196 func createError
JSONJoy.swift:181
            return JSONDecoder(createError("index: \(index) is greater than array or this is not an Array type."))
JSONJoy.swift:192
            return JSONDecoder(createError("key: \(key) does not exist or this is not a Dictionary type"))
(text: String) -> NSError { 0197 return NSError(domain: "JSONJoy", code: 1002, userInfo: [NSLocalizedDescriptionKey: text]); 0198 } 0199 0200 ///print the decoder in a JSON format. Helpful for debugging. 0201 public func print
JSONJoy.swift:25
        return self.print()
JSONJoy.swift:205
                str += decoder.print() + ","
JSONJoy.swift:212
                str += "\"\(key)\": \(decoder.print()),"
() -> String { 0202 if let arr = self.array { 0203 var str = "[" 0204 for decoder in arr { 0205 str += decoder.print() + "," 0206 } 0207 str.removeAtIndex(str.endIndex.advancedBy(-1)) 0208 return str + "]" 0209 } else if let dict = self.dictionary { 0210 var str = "{" 0211 for (key, decoder) in dict { 0212 str += "\"\(key)\": \(decoder.print())," 0213 } 0214 str.removeAtIndex(str.endIndex.advancedBy(-1)) 0215 return str + "}" 0216 } 0217 if value != nil { 0218 if let _ = self.string { 0219 return "\"\(value!)\"" 0220 } else if let _ = value as? NSNull { 0221 return "null" 0222 } 0223 return "\(value!)" 0224 } 0225 return "" 0226 } 0227 } 0228 0229 ///Implement this protocol on all objects you want to use JSONJoy with 0230 public protocol JSONJoy { 0231 init(_ decoder: JSONDecoder) throws 0232 } 0233