0001 // JSON.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 public struct JSON{ 0026 0027 private var object
JSON.swift:68 return JSON(dictionary[key])JSON.swift:71 return JSON()JSON.swift:90 extension JSON: CustomStringConvertible {JSON.swift:109 extension JSON: CustomDebugStringConvertible {JSON.swift:117 extension JSON: NilLiteralConvertible {JSON.swift:125 extension JSON: StringLiteralConvertible {JSON.swift:141 extension JSON: FloatLiteralConvertible {JSON.swift:149 extension JSON: IntegerLiteralConvertible {JSON.swift:157 extension JSON: BooleanLiteralConvertible {JSON.swift:165 extension JSON: ArrayLiteralConvertible {JSON.swift:173 extension JSON: DictionaryLiteralConvertible {JSON.swift:187 extension JSON: CollectionType {JSON.swift:188 public func generate() -> IndexingGenerator<[JSON]> {JSON.swift:207 extension JSON {JSON.swift:214 extension JSON {JSON.swift:221 extension JSON {JSON.swift:228 extension JSON {JSON.swift:235 extension JSON {JSON.swift:242 extension JSON {JSON.swift:249 extension JSON {JSON.swift:256 extension JSON {JSON.swift:267 extension JSON {JSON.swift:270 return array.map { JSON($0) }JSON.swift:268 public var array: [JSON]? {JSON.swift:274 public var arrayValue: [JSON] { return array ?? [] }JSON.swift:279 extension JSON {JSON.swift:282 return Dictionary(dictionary.map { ($0, JSON($1)) })JSON.swift:280 public var dictionary: [String : JSON]? {JSON.swift:286 public var dictionaryValue: [String : JSON] { return dictionary ?? [:] }JSON.swift:300 extension JSON: RawRepresentable {JSON.swift:327 extension JSON: Equatable {}JSON.swift:329 public func ==(lhs: JSON, rhs: JSON) -> Bool {JSON.swift:329 public func ==(lhs: JSON, rhs: JSON) -> Bool {JSON.swift:350 return left.map { JSON($0) } == right.map { JSON ($0) }JSON.swift:350 return left.map { JSON($0) } == right.map { JSON ($0) }JSON.swift:352 return Dictionary(left.map { ($0, JSON($1)) }) == Dictionary(right.map { ($0, JSON($1)) })JSON.swift:352 return Dictionary(left.map { ($0, JSON($1)) }) == Dictionary(right.map { ($0, JSON($1)) })JSONTransformable.swift:33 static func fromJSON(json: JSON) -> T?JSONTransformable.swift:42 public static func fromJSON(json: JSON) -> Self? {JSONTransformable.swift:53 public static func fromJSON(json: JSON) -> Self? {JSONTransformable.swift:62 public static func fromJSON(json: JSON) -> Self? {JSONTransformable.swift:73 public static func fromJSON(json: JSON) -> String? {JSONTransformable.swift:91 public class func fromJSON(json: JSON) -> NSDate? {JSONTransformable.swift:144 public static func fromJSON(json: JSON) -> Bool? {JSONTransformable.swift:155 public class func fromJSON(json: JSON) -> NSURL? {JSONTransformable.swift:166 public class func fromJSON(json: JSON) -> NSNumber? {JSONTransformable.swift:177 public static func fromJSON(json: JSON) -> Double? {JSONTransformable.swift:188 public static func fromJSON(json: JSON) -> Float? {JSONTransformable.swift:200 public static func fromJSON(json: JSON) -> Int? {JSONTransformable.swift:211 public static func fromJSON(json: JSON) -> UInt? {Model.swift:51 public required init(json: JSON) {Model.swift:57 public required init?(strictJSON: JSON) {Model.swift:88 public class func modelForJSON(json: JSON) -> Model {Model.swift:92 public class func modelForStrictJSON(json: JSON) -> Model? {Model.swift:111 public func json() -> (dictionary: [String : AnyObject], json: JSON?, data: NSData?) {Model.swift:134 let json = JSON(data: jsonData)Property.swift:63 public func fromJSON(json: JSON) -> Bool {PropertyArray.swift:62 public func fromJSON(json: JSON) -> Bool {PropertyArray.swift:63 var jsonValue: JSON = jsonPropertyDescription.swift:37 func fromJSON(json: JSON) -> BoolPropertyDictionary.swift:62 public func fromJSON(json: JSON) -> Bool {PropertyDictionary.swift:63 var jsonValue: JSON = json: AnyObject? 0028 0029 public init
JSON.swift:30 self.object = nilJSON.swift:34 self.object = objectJSON.swift:67 if let dictionary = object as? NSDictionary {JSON.swift:48 if let tempObject = object as? [String : AnyObject] {JSON.swift:51 object[key] = newValue.objectJSON.swift:52 self.object = objectJSON.swift:56 tempObject[key] = newValue.objectJSON.swift:57 self.object = tempObjectJSON.swift:76 return (object == nil)JSON.swift:80 return object != nilJSON.swift:84 return object != nil && !(object is NSNull)JSON.swift:84 return object != nil && !(object is NSNull)JSON.swift:92 if let object: AnyObject = object {JSON.swift:103 return "\(object)"JSON.swift:208 public var string: String? { return object as? String }JSON.swift:215 public var number: NSNumber? { return object as? NSNumber }JSON.swift:222 public var float: Float? { return object as? Float }JSON.swift:229 public var double: Double? { return object as? Double }JSON.swift:236 public var int: Int? { return object as? Int }JSON.swift:243 public var uInt: UInt? { return object as? UInt }JSON.swift:250 public var bool: Bool? { return object as? Bool }JSON.swift:269 if let array = object as? [AnyObject] {JSON.swift:281 if let dictionary = object as? [String : AnyObject] {JSON.swift:314 return object ?? NSNull()JSON.swift:318 guard let object = object else { throw DataError.MissingObject }JSON.swift:330 guard let lhsObject: AnyObject = lhs.object, rhsObject: AnyObject = rhs.object else { return false }JSON.swift:330 guard let lhsObject: AnyObject = lhs.object, rhsObject: AnyObject = rhs.object else { return false }() { 0030 self.object = nil 0031 } 0032 0033 public init
JSON.swift:42 self.init()JSON.swift:71 return JSON()JSON.swift:119 self.init()(_ object: AnyObject?) { 0034 self.object = object 0035 } 0036 0037 public init
JSON.swift:39 self.init(object)JSON.swift:68 return JSON(dictionary[key])JSON.swift:127 self.init(value)JSON.swift:131 self.init(value)JSON.swift:135 self.init(value)JSON.swift:143 self.init(value)JSON.swift:151 self.init(value)JSON.swift:159 self.init(value)JSON.swift:167 self.init(elements)JSON.swift:181 self.init(object)JSON.swift:270 return array.map { JSON($0) }JSON.swift:282 return Dictionary(dictionary.map { ($0, JSON($1)) })JSON.swift:310 self.init(rawValue)JSON.swift:350 return left.map { JSON($0) } == right.map { JSON ($0) }JSON.swift:350 return left.map { JSON($0) } == right.map { JSON ($0) }JSON.swift:352 return Dictionary(left.map { ($0, JSON($1)) }) == Dictionary(right.map { ($0, JSON($1)) })JSON.swift:352 return Dictionary(left.map { ($0, JSON($1)) }) == Dictionary(right.map { ($0, JSON($1)) })(data: NSData?) { 0038 if let data = data, let object: AnyObject = try? NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments) { 0039 self.init(object) 0040 } 0041 else { 0042 self.init() 0043 } 0044 } 0045 0046 public subscript
Model.swift:134 let json = JSON(data: jsonData)(key: String) -> JSON { 0047 set { 0048 if let tempObject = object as? [String : AnyObject] { 0049 var object = tempObject 0050 0051 object[key] = newValue.object 0052 self.object = object 0053 } 0054 else { 0055 var tempObject: [String : AnyObject] = [:] 0056 tempObject[key] = newValue.object 0057 self.object = tempObject 0058 } 0059 } 0060 get { 0061 /** 0062 NSDictionary is used because it currently performs better than a native Swift dictionary. 0063 The reason for this is that [String : AnyObject] is bridged to NSDictionary deep down the 0064 call stack, and this bridging operation is relatively expensive. Until Swift is ABI stable 0065 and/or doesn't require a bridge to Objective-C, NSDictionary will be used here 0066 */ 0067 if let dictionary = object as? NSDictionary { 0068 return JSON(dictionary[key]) 0069 } 0070 0071 return JSON() 0072 } 0073 } 0074 0075 @available(*, deprecated=1.2, message="Use !hasKey instead.") public var isNil: Bool { 0076 return (object == nil) 0077 } 0078 0079 public var hasKey: Bool { 0080 return object != nil 0081 } 0082 0083 public var hasValue: Bool { 0084 return object != nil && !(object is NSNull) 0085 } 0086 } 0087 0088 // MARK: - CustomStringConvertible 0089 0090 extension JSON: CustomStringConvertible { 0091 public var description
Property.swift:67 jsonValue = jsonValue[$0]PropertyArray.swift:66 jsonValue = jsonValue[$0]PropertyDictionary.swift:66 jsonValue = jsonValue[key]: String { 0092 if let object: AnyObject = object { 0093 switch object { 0094 case is String, is NSNumber, is Float, is Double, is Int, is UInt, is Bool: return "\(object)" 0095 case is [AnyObject], is [String : AnyObject]: 0096 if let data = try? NSJSONSerialization.dataWithJSONObject(object, options: .PrettyPrinted) { 0097 return NSString(data: data, encoding: NSUTF8StringEncoding) as? String ?? "" 0098 } 0099 default: return "" 0100 } 0101 } 0102 0103 return "\(object)" 0104 } 0105 } 0106 0107 // MARK: - CustomDebugStringConvertible 0108 0109 extension JSON: CustomDebugStringConvertible { 0110 public var debugDescription: String { 0111 return description 0112 } 0113 } 0114 0115 // MARK: - NilLiteralConvertible 0116 0117 extension JSON: NilLiteralConvertible { 0118 public init(nilLiteral: ()) { 0119 self.init() 0120 } 0121 } 0122 0123 // MARK: - StringLiteralConvertible 0124 0125 extension JSON: StringLiteralConvertible { 0126 public init(stringLiteral value: StringLiteralType) { 0127 self.init(value) 0128 } 0129 0130 public init(extendedGraphemeClusterLiteral value: StringLiteralType) { 0131 self.init(value) 0132 } 0133 0134 public init(unicodeScalarLiteral value: StringLiteralType) { 0135 self.init(value) 0136 } 0137 } 0138 0139 // MARK: - FloatLiteralConvertible 0140 0141 extension JSON: FloatLiteralConvertible { 0142 public init(floatLiteral value: FloatLiteralType) { 0143 self.init(value) 0144 } 0145 } 0146 0147 // MARK: - IntegerLiteralConvertible 0148 0149 extension JSON: IntegerLiteralConvertible { 0150 public init(integerLiteral value: IntegerLiteralType) { 0151 self.init(value) 0152 } 0153 } 0154 0155 // MARK: - BooleanLiteralConvertible 0156 0157 extension JSON: BooleanLiteralConvertible { 0158 public init(booleanLiteral value: BooleanLiteralType) { 0159 self.init(value) 0160 } 0161 } 0162 0163 // MARK: - ArrayLiteralConvertible 0164 0165 extension JSON: ArrayLiteralConvertible { 0166 public init(arrayLiteral elements: AnyObject...) { 0167 self.init(elements) 0168 } 0169 } 0170 0171 // MARK: - DictionaryLiteralConvertible 0172 0173 extension JSON: DictionaryLiteralConvertible { 0174 public init(dictionaryLiteral elements: (String, AnyObject)...) { 0175 var object: [String : AnyObject] = [:] 0176 0177 for (key, value) in elements { 0178 object[key] = value 0179 } 0180 0181 self.init(object) 0182 } 0183 } 0184 0185 // MARK: - CollectionType 0186 0187 extension JSON: CollectionType { 0188 public func generate() -> IndexingGenerator<[JSON]> { 0189 return arrayValue.generate() 0190 } 0191 0192 public var startIndex: Int { 0193 return 0 0194 } 0195 0196 public var endIndex: Int { 0197 return arrayValue.count 0198 } 0199 0200 public subscript(position: Int) -> JSON { 0201 return arrayValue[position] 0202 } 0203 } 0204 0205 // MARK: - String 0206 0207 extension JSON { 0208 public var string
JSON.swift:111 return description: String? { return object as? String } 0209 public var stringValue
JSON.swift:209 public var stringValue: String { return string ?? "" }JSON.swift:258 if let urlString = string {JSONTransformable.swift:74 return json.stringJSONTransformable.swift:92 if let dateString = json.string {: String { return string ?? "" } 0210 } 0211 0212 // MARK: - NSNumber 0213 0214 extension JSON { 0215 public var number
JSONTransformable.swift:54 return Self(rawValue: json.stringValue): NSNumber? { return object as? NSNumber } 0216 public var numberValue: NSNumber { return number ?? 0 } 0217 } 0218 0219 // MARK: - Float 0220 0221 extension JSON { 0222 public var float
JSON.swift:216 public var numberValue: NSNumber { return number ?? 0 }JSONTransformable.swift:167 return json.number: Float? { return object as? Float } 0223 public var floatValue: Float { return float ?? 0 } 0224 } 0225 0226 // MARK: - Double 0227 0228 extension JSON { 0229 public var double
JSON.swift:223 public var floatValue: Float { return float ?? 0 }JSONTransformable.swift:189 return json.float: Double? { return object as? Double } 0230 public var doubleValue: Double { return double ?? 0 } 0231 } 0232 0233 // MARK: - Int 0234 0235 extension JSON { 0236 public var int
JSON.swift:230 public var doubleValue: Double { return double ?? 0 }JSONTransformable.swift:178 return json.double: Int? { return object as? Int } 0237 public var intValue
JSON.swift:237 public var intValue: Int { return int ?? 0 }JSONTransformable.swift:201 return json.int: Int { return int ?? 0 } 0238 } 0239 0240 // MARK: - UInt 0241 0242 extension JSON { 0243 public var uInt
JSONTransformable.swift:63 return Self(rawValue: json.intValue): UInt? { return object as? UInt } 0244 public var uIntValue: UInt { return uInt ?? 0 } 0245 } 0246 0247 // MARK: - Bool 0248 0249 extension JSON { 0250 public var bool
JSON.swift:244 public var uIntValue: UInt { return uInt ?? 0 }JSONTransformable.swift:212 return json.uInt: Bool? { return object as? Bool } 0251 public var boolValue: Bool { return bool ?? false } 0252 } 0253 0254 // MARK: - NSURL 0255 0256 extension JSON { 0257 public var URL
JSON.swift:251 public var boolValue: Bool { return bool ?? false }JSONTransformable.swift:145 return json.bool: NSURL? { 0258 if let urlString = string { 0259 return NSURL(string: urlString) 0260 } 0261 return nil 0262 } 0263 } 0264 0265 // MARK: - Array 0266 0267 extension JSON { 0268 public var array
JSONTransformable.swift:156 return json.URL: [JSON]? { 0269 if let array = object as? [AnyObject] { 0270 return array.map { JSON($0) } 0271 } 0272 return nil 0273 } 0274 public var arrayValue
JSON.swift:274 public var arrayValue: [JSON] { return array ?? [] }: [JSON] { return array ?? [] } 0275 } 0276 0277 // MARK: - Dictionary 0278 0279 extension JSON { 0280 public var dictionary
JSON.swift:189 return arrayValue.generate()JSON.swift:197 return arrayValue.countJSON.swift:201 return arrayValue[position]: [String : JSON]? { 0281 if let dictionary = object as? [String : AnyObject] { 0282 return Dictionary(dictionary.map { ($0, JSON($1)) }) 0283 } 0284 return nil 0285 } 0286 public var dictionaryValue: [String : JSON] { return dictionary ?? [:] } 0287 } 0288 0289 extension Dictionary { 0290 private init
JSON.swift:286 public var dictionaryValue: [String : JSON] { return dictionary ?? [:] }PropertyDictionary.swift:70 for object in jsonValue.dictionary ?? [:] {(_ pairs: [Element]) { 0291 self.init() 0292 for (key, value) in pairs { 0293 self[key] = value 0294 } 0295 } 0296 } 0297 0298 // MARK: - Raw 0299 0300 extension JSON: RawRepresentable { 0301 0302 public enum DataError
JSON.swift:282 return Dictionary(dictionary.map { ($0, JSON($1)) })JSON.swift:352 return Dictionary(left.map { ($0, JSON($1)) }) == Dictionary(right.map { ($0, JSON($1)) })JSON.swift:352 return Dictionary(left.map { ($0, JSON($1)) }) == Dictionary(right.map { ($0, JSON($1)) }): ErrorType { 0303 case MissingObject
JSON.swift:318 guard let object = object else { throw DataError.MissingObject }JSON.swift:319 guard NSJSONSerialization.isValidJSONObject(object) else { throw DataError.InvalidObject }0304 case InvalidObject
JSON.swift:318 guard let object = object else { throw DataError.MissingObject }0305 } 0306 0307 public init?(rawValue: AnyObject) { 0308 guard NSJSONSerialization.isValidJSONObject(rawValue) else { return nil } 0309 0310 self.init(rawValue) 0311 } 0312 0313 public var rawValue: AnyObject { 0314 return object ?? NSNull() 0315 } 0316 0317 public func rawData(options: NSJSONWritingOptions = []) throws -> NSData { 0318 guard let object = object else { throw DataError.MissingObject } 0319 guard NSJSONSerialization.isValidJSONObject(object) else { throw DataError.InvalidObject } 0320 0321 return try NSJSONSerialization.dataWithJSONObject(object, options: options) 0322 } 0323 } 0324 0325 // MARK: - Equatable 0326 0327 extension JSON: Equatable {} 0328 0329 public func ==(lhs: JSON, rhs: JSON) -> Bool { 0330 guard let lhsObject: AnyObject = lhs.object, rhsObject: AnyObject = rhs.object else { return false } 0331 0332 switch (lhsObject, rhsObject) { 0333 case (let left as String, let right as String): 0334 return left == right 0335 case (let left as Double, let right as Double): 0336 return left == right 0337 case (let left as Float, let right as Float): 0338 return left == right 0339 case (let left as Int, let right as Int): 0340 return left == right 0341 case (let left as UInt, let right as UInt): 0342 return left == right 0343 case (let left as Bool, let right as Bool): 0344 return left == right 0345 case (let left as NSURL, let right as NSURL): 0346 return left == right 0347 case (let left as NSNumber, let right as NSNumber): 0348 return left == right 0349 case (let left as [AnyObject], let right as [AnyObject]): 0350 return left.map { JSON($0) } == right.map { JSON ($0) } 0351 case (let left as [String : AnyObject], let right as [String : AnyObject]): 0352 return Dictionary(left.map { ($0, JSON($1)) }) == Dictionary(right.map { ($0, JSON($1)) }) 0353 default: return false 0354 } 0355 } 0356
JSON.swift:319 guard NSJSONSerialization.isValidJSONObject(object) else { throw DataError.InvalidObject }