0001 // 0002 // JSONParsing.swift 0003 // Freddy 0004 // 0005 // Created by Matthew D. Mathias on 3/17/15. 0006 // Copyright © 2015 Big Nerd Ranch. All rights reserved. 0007 // 0008 0009 import Foundation 0010 0011 // MARK: - Deserialize JSON 0012 0013 /// Protocol describing a backend parser that can produce `JSON` from `NSData`. 0014 public protocol JSONParserType{ 0015 0016 /// Creates an instance of `JSON` from `NSData`. 0017 /// - parameter data: An instance of `NSData` to use to create `JSON`. 0018 /// - throws: An error that may arise from calling `JSONObjectWithData(_:options:)` on `NSJSONSerialization` with the given data. 0019 /// - returns: An instance of `JSON`. 0020 static func createJSONFromData
JSONParser.swift:616 extension JSONParser: JSONParserType {JSONParsing.swift:28 public init(data: NSData, usingParser parser: JSONParserType.Type = JSONParser.self) throws {JSONParsing.swift:33 public init(jsonString: Swift.String, usingParser parser: JSONParserType.Type = JSONParser.self) throws {JSONParsing.swift:40 extension NSJSONSerialization: JSONParserType {(data: NSData) throws -> JSON 0021 0022 } 0023 0024 extension JSON { 0025 0026 /// Create `JSON` from UTF-8 `data`. By default, parses using the 0027 /// Swift-native `JSONParser` backend. 0028 public init(data: NSData, usingParser parser: JSONParserType.Type = JSONParser.self) throws { 0029 self = try parser.createJSONFromData(data) 0030 } 0031 0032 /// Create `JSON` from UTF-8 `string`. 0033 public init(jsonString: Swift.String, usingParser parser: JSONParserType.Type = JSONParser.self) throws { 0034 self = try parser.createJSONFromData((jsonString as NSString).dataUsingEncoding(NSUTF8StringEncoding) ?? NSData()) 0035 } 0036 } 0037 0038 // MARK: - NSJSONSerialization 0039 0040 extension NSJSONSerialization: JSONParserType { 0041 0042 // MARK: Decode NSData 0043 0044 /// Use the built-in, Objective-C based JSON parser to create `JSON`. 0045 /// - parameter data: An instance of `NSData`. 0046 /// - returns: An instance of `JSON`. 0047 /// - throws: An error that may arise if the `NSData` cannot be parsed into an object. 0048 public static func createJSONFromData(data: NSData) throws -> JSON { 0049 return makeJSON(try NSJSONSerialization.JSONObjectWithData(data, options: [])) 0050 } 0051 0052 // MARK: Make JSON 0053 0054 /// Makes a `JSON` object by matching its argument to a case in the `JSON` enum. 0055 /// - parameter object: The instance of `AnyObject` returned from serializing the JSON. 0056 /// - returns: An instance of `JSON` matching the JSON given to the function. 0057 private static func makeJSON
JSONParsing.swift:29 self = try parser.createJSONFromData(data)JSONParsing.swift:34 self = try parser.createJSONFromData((jsonString as NSString).dataUsingEncoding(NSUTF8StringEncoding) ?? NSData())(object: AnyObject) -> JSON { 0058 switch object { 0059 case let n as NSNumber: 0060 switch n { 0061 case _ where CFNumberGetType(n) == .CharType || CFGetTypeID(n) == CFBooleanGetTypeID(): 0062 return .Bool(n.boolValue) 0063 case _ where !CFNumberIsFloatType(n): 0064 return .Int(n.integerValue) 0065 default: 0066 return .Double(n.doubleValue) 0067 } 0068 case let arr as [AnyObject]: 0069 return makeJSONArray(arr) 0070 case let dict as [Swift.String: AnyObject]: 0071 return makeJSONDictionary(dict) 0072 case let s as Swift.String: 0073 return .String(s) 0074 default: 0075 return .Null 0076 } 0077 } 0078 0079 // MARK: Make a JSON Array 0080 0081 /// Makes a `JSON` array from the object passed in. 0082 /// - parameter jsonArray: The array to transform into a `JSON`. 0083 /// - returns: An instance of `JSON` matching the array. 0084 private static func makeJSONArray
JSONParsing.swift:49 return makeJSON(try NSJSONSerialization.JSONObjectWithData(data, options: []))JSONParsing.swift:85 return .Array(jsonArray.map(makeJSON))JSONParsing.swift:95 (key, makeJSON(value))(jsonArray: [AnyObject]) -> JSON { 0085 return .Array(jsonArray.map(makeJSON)) 0086 } 0087 0088 // MARK: Make a JSON Dictionary 0089 0090 /// Makes a `JSON` dictionary from the Cocoa dictionary passed in. 0091 /// - parameter jsonDict: The dictionary to transform into `JSON`. 0092 /// - returns: An instance of `JSON` matching the dictionary. 0093 private static func makeJSONDictionary
JSONParsing.swift:69 return makeJSONArray(arr)(jsonDict: [Swift.String: AnyObject]) -> JSON { 0094 return JSON(jsonDict.lazy.map { (key, value) in 0095 (key, makeJSON(value)) 0096 }) 0097 } 0098 0099 } 0100
JSONParsing.swift:71 return makeJSONDictionary(dict)