0001    //
0002    //  JSON.swift
0003    //  Freddy
0004    //
0005    //  Created by Matthew D. Mathias on 3/17/15.
0006    //  Copyright © 2015 Big Nerd Ranch. Licensed under MIT.
0007    //
0008    
0009    /// An enum to describe the structure of JSON.
0010    public enum JSON
JSON.swift:12
    case Array([JSON])
JSON.swift:14
    case Dictionary([Swift.String: JSON])
JSON.swift:29
extension JSON {
JSON.swift:43
        case ValueNotConvertible(value: JSON, to: Any.Type)
JSON.swift:51
public func ==(lhs: JSON, rhs: JSON) -> Bool {
JSON.swift:51
public func ==(lhs: JSON, rhs: JSON) -> Bool {
JSON.swift:76
extension JSON: Equatable {}
JSON.swift:80
extension JSON: CustomStringConvertible {
JSONDecodable.swift:18
    init(json: JSON) throws
JSONDecodable.swift:29
    public init(json: JSON) throws {
JSONDecodable.swift:36
            throw JSON.Error.ValueNotConvertible(value: json, to: Swift.Double)
JSONDecodable.swift:49
    public init(json: JSON) throws {
JSONDecodable.swift:56
            throw JSON.Error.ValueNotConvertible(value: json, to: Swift.Int)
JSONDecodable.swift:69
    public init(json: JSON) throws {
JSONDecodable.swift:71
            throw JSON.Error.ValueNotConvertible(value: json, to: Swift.String)
JSONDecodable.swift:85
    public init(json: JSON) throws {
JSONDecodable.swift:87
            throw JSON.Error.ValueNotConvertible(value: json, to: Swift.Bool)
JSONDecodable.swift:101
    public init(json: JSON) throws {
JSONDecodable.swift:104
            throw JSON.Error.ValueNotConvertible(value: json, to: Self.self)
JSONDecodable.swift:110
internal extension JSON {
JSONDecodable.swift:116
    static func getArray(json: JSON) throws -> [JSON] {
JSONDecodable.swift:116
    static func getArray(json: JSON) throws -> [JSON] {
JSONDecodable.swift:128
    static func getDictionary(json: JSON) throws -> [Swift.String: JSON] {
JSONDecodable.swift:128
    static func getDictionary(json: JSON) throws -> [Swift.String: JSON] {
JSONDecodable.swift:145
    static func getArrayOf<Decoded: JSONDecodable>(json: JSON) throws -> [Decoded] {
JSONEncodable.swift:17
    func toJSON() -> JSON
JSONEncodable.swift:23
    public func toJSON() -> JSON {
JSONEncodable.swift:33
    public func toJSON() -> JSON {
JSONEncodable.swift:34
        var jsonDictionary = [String: JSON]()
JSONEncodable.swift:48
    public func toJSON() -> JSON {
JSONEncodable.swift:56
    public func toJSON() -> JSON {
JSONEncodable.swift:64
    public func toJSON() -> JSON {
JSONEncodable.swift:72
    public func toJSON() -> JSON {
JSONEncodable.swift:80
    public func toJSON() -> JSON {
JSONLiteralConvertible.swift:11
extension JSON: ArrayLiteralConvertible {
JSONLiteralConvertible.swift:15
    public init<Collection: CollectionType where Collection.Generator.Element == JSON>(_ collection: Collection) {
JSONLiteralConvertible.swift:20
    public init(arrayLiteral elements: JSON...) {
JSONLiteralConvertible.swift:28
extension JSON: DictionaryLiteralConvertible {
JSONLiteralConvertible.swift:32
    public init<Dictionary: SequenceType where Dictionary.Generator.Element == (Swift.String, JSON)>(_ pairs: Dictionary) {
JSONLiteralConvertible.swift:41
    public init(dictionaryLiteral pairs: (Swift.String, JSON)...) {
JSONLiteralConvertible.swift:49
extension JSON: FloatLiteralConvertible {
JSONLiteralConvertible.swift:65
extension JSON: IntegerLiteralConvertible {
JSONLiteralConvertible.swift:81
extension JSON: StringLiteralConvertible {
JSONLiteralConvertible.swift:107
extension JSON: BooleanLiteralConvertible {
JSONLiteralConvertible.swift:123
extension JSON: NilLiteralConvertible {
JSONParser.swift:98
    public mutating func parse() throws -> JSON {
JSONParser.swift:108
    private mutating func parseValue() throws -> JSON {
JSONParser.swift:182
    private mutating func decodeNull() throws -> JSON {
JSONParser.swift:197
    private mutating func decodeTrue() throws -> JSON {
JSONParser.swift:212
    private mutating func decodeFalse() throws -> JSON {
JSONParser.swift:229
    private mutating func decodeString() throws -> JSON {
JSONParser.swift:340
    private mutating func decodeArray() throws -> JSON {
JSONParser.swift:343
        var items = [JSON]()
JSONParser.swift:374
        var buffers = [[(String,JSON)]]()
JSONParser.swift:376
        mutating func getBuffer() -> [(String,JSON)] {
JSONParser.swift:382
            return [(String,JSON)]()
JSONParser.swift:385
        mutating func putBuffer(buffer: [(String,JSON)]) {
JSONParser.swift:392
    private mutating func decodeObject() throws -> JSON {
JSONParser.swift:402
                var obj = [String:JSON](minimumCapacity: pairs.count)
JSONParser.swift:437
    private mutating func decodeNumberNegative(start: Int) throws -> JSON {
JSONParser.swift:455
    private mutating func decodeNumberLeadingZero(start: Int, sign: Sign = .Positive) throws -> JSON {
JSONParser.swift:473
    private mutating func decodeNumberPreDecimalDigits(start: Int, sign: Sign = .Positive) throws -> JSON {
JSONParser.swift:497
    private mutating func decodeNumberDecimal(start: Int, sign: Sign, value: Double) throws -> JSON {
JSONParser.swift:512
    private mutating func decodeNumberPostDecimalDigits(start: Int, sign: Sign, value inValue: Double) throws -> JSON {
JSONParser.swift:535
    private mutating func decodeNumberExponent(start: Int, sign: Sign, value: Double) throws -> JSON {
JSONParser.swift:556
    private mutating func decodeNumberExponentSign(start: Int, sign: Sign, value: Double, expSign: Sign) throws -> JSON {
JSONParser.swift:571
    private mutating func decodeNumberExponentDigits(start: Int, sign: Sign, value: Double, expSign: Sign) throws -> JSON {
JSONParser.swift:622
    public static func createJSONFromData(data: NSData) throws -> JSON {
JSONParsing.swift:20
    static func createJSONFromData(data: NSData) throws -> JSON
JSONParsing.swift:24
extension JSON {
JSONParsing.swift:48
    public static func createJSONFromData(data: NSData) throws -> JSON {
JSONParsing.swift:57
    private static func makeJSON(object: AnyObject) -> JSON {
JSONParsing.swift:84
    private static func makeJSONArray(jsonArray: [AnyObject]) -> JSON {
JSONParsing.swift:93
    private static func makeJSONDictionary(jsonDict: [Swift.String: AnyObject]) -> JSON {
JSONParsing.swift:94
        return JSON(jsonDict.lazy.map { (key, value) in
JSONSerializing.swift:7
extension JSON {
JSONSubscripting.swift:22
    func valueInDictionary(dictionary: [Swift.String : JSON]) throws -> JSON
JSONSubscripting.swift:22
    func valueInDictionary(dictionary: [Swift.String : JSON]) throws -> JSON
JSONSubscripting.swift:30
    func valueInArray(array: [JSON]) throws -> JSON
JSONSubscripting.swift:30
    func valueInArray(array: [JSON]) throws -> JSON
JSONSubscripting.swift:37
    public func valueInDictionary(dictionary: [Swift.String : JSON]) throws -> JSON {
JSONSubscripting.swift:37
    public func valueInDictionary(dictionary: [Swift.String : JSON]) throws -> JSON {
JSONSubscripting.swift:38
        throw JSON.Error.UnexpectedSubscript(type: Self.self)
JSONSubscripting.swift:43
    public func valueInArray(array: [JSON]) throws -> JSON {
JSONSubscripting.swift:43
    public func valueInArray(array: [JSON]) throws -> JSON {
JSONSubscripting.swift:44
        throw JSON.Error.UnexpectedSubscript(type: Self.self)
JSONSubscripting.swift:55
    public func valueInDictionary(dictionary: [Swift.String : JSON]) throws -> JSON {
JSONSubscripting.swift:55
    public func valueInDictionary(dictionary: [Swift.String : JSON]) throws -> JSON {
JSONSubscripting.swift:57
            throw JSON.Error.KeyNotFound(key: self)
JSONSubscripting.swift:70
    public func valueInArray(array: [JSON]) throws -> JSON {
JSONSubscripting.swift:70
    public func valueInArray(array: [JSON]) throws -> JSON {
JSONSubscripting.swift:72
            throw JSON.Error.IndexOutOfBounds(index: self)
JSONSubscripting.swift:81
private extension JSON {
JSONSubscripting.swift:87
    func valueForPathFragment(fragment: JSONPathType, detectNull: Swift.Bool) throws -> JSON {
JSONSubscripting.swift:100
    func valueAtPath(path: [JSONPathType], detectNull: Swift.Bool = false) throws -> JSON {
JSONSubscripting.swift:112
extension JSON {
JSONSubscripting.swift:126
extension JSON {
JSONSubscripting.swift:188
    public func array(path: JSONPathType...) throws -> [JSON] {
JSONSubscripting.swift:189
        return try JSON.getArray(valueAtPath(path))
JSONSubscripting.swift:203
        return try JSON.getArrayOf(valueAtPath(path))
JSONSubscripting.swift:211
    public func dictionary(path: JSONPathType...) throws -> [Swift.String: JSON] {
JSONSubscripting.swift:212
        return try JSON.getDictionary(valueAtPath(path))
JSONSubscripting.swift:219
extension JSON {
JSONSubscripting.swift:221
    private func optionalAtPath(path: [JSONPathType], ifNotFound: Swift.Bool) throws -> JSON? {
JSONSubscripting.swift:344
    public func array(path: JSONPathType..., ifNotFound: Swift.Bool) throws -> [JSON]? {
JSONSubscripting.swift:345
        return try optionalAtPath(path, ifNotFound: ifNotFound).map(JSON.getArray)
JSONSubscripting.swift:365
        return try optionalAtPath(path, ifNotFound: ifNotFound).map(JSON.getArrayOf)
JSONSubscripting.swift:383
    public func dictionary(path: JSONPathType..., ifNotFound: Swift.Bool) throws -> [Swift.String: JSON]? {
JSONSubscripting.swift:384
        return try optionalAtPath(path, ifNotFound: ifNotFound).map(JSON.getDictionary)
JSONSubscripting.swift:391
extension JSON {
JSONSubscripting.swift:393
    private func mapOptionalAtPath<Value>(path: [JSONPathType], @noescape fallback: () -> Value, @noescape transform: JSON throws -> Value) throws -> Value {
JSONSubscripting.swift:485
    public func array(path: JSONPathType..., @autoclosure or fallback: () -> [JSON]) throws -> [JSON] {
JSONSubscripting.swift:485
    public func array(path: JSONPathType..., @autoclosure or fallback: () -> [JSON]) throws -> [JSON] {
JSONSubscripting.swift:486
        return try mapOptionalAtPath(path, fallback: fallback, transform: JSON.getArray)
JSONSubscripting.swift:505
        return try mapOptionalAtPath(path, fallback: fallback, transform: JSON.getArrayOf)
JSONSubscripting.swift:522
    public func dictionary(path: JSONPathType..., @autoclosure or fallback: () -> [Swift.String: JSON]) throws -> [Swift.String: JSON] {
JSONSubscripting.swift:522
    public func dictionary(path: JSONPathType..., @autoclosure or fallback: () -> [Swift.String: JSON]) throws -> [Swift.String: JSON] {
JSONSubscripting.swift:523
        return try mapOptionalAtPath(path, fallback: fallback, transform: JSON.getDictionary)
JSONSubscripting.swift:530
extension JSON {
JSONSubscripting.swift:532
    private func mapOptionalAtPath<Value>(path: [JSONPathType], ifNull: Swift.Bool, @noescape transform: JSON throws -> Value) throws -> Value? {
JSONSubscripting.swift:533
        var json: JSON?
JSONSubscripting.swift:653
    public func array(path: JSONPathType..., ifNull: Swift.Bool) throws -> [JSON]? {
JSONSubscripting.swift:654
        return try mapOptionalAtPath(path, ifNull: ifNull, transform: JSON.getArray)
JSONSubscripting.swift:674
        return try mapOptionalAtPath(path, ifNull: ifNull, transform: JSON.getArrayOf)
JSONSubscripting.swift:693
    public func dictionary(path: JSONPathType..., ifNull: Swift.Bool) throws -> [Swift.String: JSON]? {
JSONSubscripting.swift:694
        return try mapOptionalAtPath(path, ifNull: ifNull, transform: JSON.getDictionary)
{ 0011 /// A case for denoting an array with an associated value of `[JSON]` 0012 case Array
JSON.swift:53
    case (.Array(let arrL), .Array(let arrR)):
JSON.swift:53
    case (.Array(let arrL), .Array(let arrR)):
JSON.swift:85
        case .Array(let arr):       return Swift.String(arr)
JSONDecodable.swift:118
        guard case let .Array(array) = json else {
JSONEncodable.swift:25
        return .Array(arrayOfJSON)
JSONLiteralConvertible.swift:16
        self = .Array(Swift.Array(collection))
JSONParser.swift:350
                return .Array(items)
JSONParsing.swift:85
        return .Array(jsonArray.map(makeJSON))
JSONSerializing.swift:22
        case .Array(let jsonArray):
JSONSubscripting.swift:93
        case let .Array(array):
([JSON]) 0013 /// A case for denoting a dictionary with an associated value of `[Swift.String: JSON]` 0014 case Dictionary
JSON.swift:55
    case (.Dictionary(let dictL), .Dictionary(let dictR)):
JSON.swift:55
    case (.Dictionary(let dictL), .Dictionary(let dictR)):
JSON.swift:86
        case .Dictionary(let dict): return Swift.String(dict)
JSONDecodable.swift:130
        guard case let .Dictionary(dictionary) = json else {
JSONEncodable.swift:41
        return .Dictionary(jsonDictionary)
JSONLiteralConvertible.swift:37
        self = .Dictionary(dictionary)
JSONParser.swift:407
                return .Dictionary(obj)
JSONSerializing.swift:24
        case .Dictionary(let jsonDictionary):
JSONSubscripting.swift:91
        case let .Dictionary(dict):
([Swift.String: JSON]) 0015 /// A case for denoting a double with an associated value of `Swift.Double`. 0016 case Double
JSON.swift:59
    case (.Double(let dubL), .Double(let dubR)):
JSON.swift:59
    case (.Double(let dubL), .Double(let dubR)):
JSON.swift:61
    case (.Double(let dubL), .Int(let intR)):
JSON.swift:65
    case (.Int(let intL), .Double(let dubR)):
JSON.swift:88
        case .Double(let double):   return Swift.String(double)
JSONDecodable.swift:31
        case let .Double(double):
JSONDecodable.swift:51
        case let .Double(double):
JSONEncodable.swift:57
        return .Double(self)
JSONLiteralConvertible.swift:53
        self = .Double(value)
JSONParser.swift:466
            return .Double(-0.0)
JSONParser.swift:532
        return .Double(Double(sign.rawValue) * value)
JSONParser.swift:586
        return .Double(Double(sign.rawValue) * value * pow(10, Double(expSign.rawValue) * exponent))
JSONParsing.swift:66
                return .Double(n.doubleValue)
JSONSerializing.swift:32
        case .Double(let num):
(Swift.Double) 0017 /// A case for denoting an integer with an associated value of `Swift.Int`. 0018 case Int
JSON.swift:61
    case (.Double(let dubL), .Int(let intR)):
JSON.swift:63
    case (.Int(let intL), .Int(let intR)):
JSON.swift:63
    case (.Int(let intL), .Int(let intR)):
JSON.swift:65
    case (.Int(let intL), .Double(let dubR)):
JSON.swift:89
        case .Int(let int):         return Swift.String(int)
JSONDecodable.swift:33
        case let .Int(int):
JSONDecodable.swift:53
        case let .Int(int):
JSONEncodable.swift:49
        return .Int(self)
JSONLiteralConvertible.swift:69
        self = .Int(value)
JSONParser.swift:458
            return .Int(0)
JSONParser.swift:469
            return .Int(0)
JSONParser.swift:494
        return .Int(sign.rawValue * value)
JSONParsing.swift:64
                return .Int(n.integerValue)
JSONSerializing.swift:34
        case .Int(let int):
(Swift.Int) 0019 /// A case for denoting a string with an associated value of `Swift.String`. 0020 case String
JSON.swift:57
    case (.String(let strL), .String(let strR)):
JSON.swift:57
    case (.String(let strL), .String(let strR)):
JSON.swift:87
        case .String(let string):   return string
JSONDecodable.swift:70
        guard case let .String(string) = json else {
JSONEncodable.swift:65
        return .String(self)
JSONLiteralConvertible.swift:85
        self = .String(text)
JSONParser.swift:269
                return .String(string)
JSONParsing.swift:73
            return .String(s)
JSONSerializing.swift:30
        case .String(let str):
(Swift.String) 0021 /// A case for denoting a boolean with an associated value of `Swift.Bool`. 0022 case Bool
JSON.swift:67
    case (.Bool(let bL), .Bool(let bR)):
JSON.swift:67
    case (.Bool(let bL), .Bool(let bR)):
JSON.swift:90
        case .Bool(let bool):       return Swift.String(bool)
JSONDecodable.swift:86
        guard case let .Bool(bool) = json else {
JSONEncodable.swift:73
        return .Bool(self)
JSONLiteralConvertible.swift:111
        self = .Bool(value)
JSONParser.swift:209
        return .Bool(true)
JSONParser.swift:225
        return .Bool(false)
JSONParsing.swift:62
                return .Bool(n.boolValue)
JSONSerializing.swift:36
        case .Bool(let b):
(Swift.Bool) 0023 /// A case for denoting null. 0024 case Null
JSON.swift:69
    case (.Null, .Null):
JSON.swift:69
    case (.Null, .Null):
JSON.swift:91
        case .Null:                 return "null"
JSONLiteralConvertible.swift:127
        self = .Null
JSONParser.swift:194
        return .Null
JSONParsing.swift:75
            return .Null
JSONSerializing.swift:38
        case .Null:
JSONSubscripting.swift:89
        case .Null where detectNull:
JSONSubscripting.swift:539
        } catch Error.ValueNotConvertible where ifNull && json == .Null {
0025 } 0026 0027 // MARK: - Errors 0028 0029 extension JSON { 0030 0031 /// An enum to encapsulate errors that may arise in working with `JSON`. 0032 public enum Error
JSONDecodable.swift:36
            throw JSON.Error.ValueNotConvertible(value: json, to: Swift.Double)
JSONDecodable.swift:56
            throw JSON.Error.ValueNotConvertible(value: json, to: Swift.Int)
JSONDecodable.swift:71
            throw JSON.Error.ValueNotConvertible(value: json, to: Swift.String)
JSONDecodable.swift:87
            throw JSON.Error.ValueNotConvertible(value: json, to: Swift.Bool)
JSONDecodable.swift:104
            throw JSON.Error.ValueNotConvertible(value: json, to: Self.self)
JSONDecodable.swift:119
            throw Error.ValueNotConvertible(value: json, to: Swift.Array<JSON>)
JSONDecodable.swift:131
            throw Error.ValueNotConvertible(value: json, to: Swift.Dictionary<Swift.String, JSON>)
JSONSubscripting.swift:38
        throw JSON.Error.UnexpectedSubscript(type: Self.self)
JSONSubscripting.swift:44
        throw JSON.Error.UnexpectedSubscript(type: Self.self)
JSONSubscripting.swift:57
            throw JSON.Error.KeyNotFound(key: self)
JSONSubscripting.swift:72
            throw JSON.Error.IndexOutOfBounds(index: self)
JSONSubscripting.swift:96
            throw Error.UnexpectedSubscript(type: fragment.dynamicType)
JSONSubscripting.swift:231
            throw Error.UnexpectedSubscript(type: fragment.dynamicType)
: ErrorType { 0033 /// The `index` is out of bounds for a JSON array 0034 case IndexOutOfBounds
JSONSubscripting.swift:72
            throw JSON.Error.IndexOutOfBounds(index: self)
JSONSubscripting.swift:224
        } catch Error.IndexOutOfBounds where ifNotFound {
(index: Swift.Int) 0035 0036 /// The `key` was not found in the JSON dictionary 0037 case KeyNotFound
JSONSubscripting.swift:57
            throw JSON.Error.KeyNotFound(key: self)
JSONSubscripting.swift:226
        } catch Error.KeyNotFound where ifNotFound {
(key: Swift.String) 0038 0039 /// The JSON is not subscriptable with `type` 0040 case UnexpectedSubscript
JSONSubscripting.swift:38
        throw JSON.Error.UnexpectedSubscript(type: Self.self)
JSONSubscripting.swift:44
        throw JSON.Error.UnexpectedSubscript(type: Self.self)
JSONSubscripting.swift:96
            throw Error.UnexpectedSubscript(type: fragment.dynamicType)
JSONSubscripting.swift:231
            throw Error.UnexpectedSubscript(type: fragment.dynamicType)
(type: JSONPathType.Type) 0041 0042 /// Unexpected JSON `value` was found that is not convertible `to` type 0043 case ValueNotConvertible
JSONDecodable.swift:36
            throw JSON.Error.ValueNotConvertible(value: json, to: Swift.Double)
JSONDecodable.swift:56
            throw JSON.Error.ValueNotConvertible(value: json, to: Swift.Int)
JSONDecodable.swift:71
            throw JSON.Error.ValueNotConvertible(value: json, to: Swift.String)
JSONDecodable.swift:87
            throw JSON.Error.ValueNotConvertible(value: json, to: Swift.Bool)
JSONDecodable.swift:104
            throw JSON.Error.ValueNotConvertible(value: json, to: Self.self)
JSONDecodable.swift:119
            throw Error.ValueNotConvertible(value: json, to: Swift.Array<JSON>)
JSONDecodable.swift:131
            throw Error.ValueNotConvertible(value: json, to: Swift.Dictionary<Swift.String, JSON>)
JSONSubscripting.swift:539
        } catch Error.ValueNotConvertible where ifNull && json == .Null {
(value: JSON, to: Any.Type) 0044 } 0045 0046 } 0047 0048 // MARK: - Test Equality 0049 0050 /// Return `true` if `lhs` is equal to `rhs`. 0051 public func ==(lhs: JSON, rhs: JSON) -> Bool { 0052 switch (lhs, rhs) { 0053 case (.Array(let arrL), .Array(let arrR)): 0054 return arrL == arrR 0055 case (.Dictionary(let dictL), .Dictionary(let dictR)): 0056 return dictL == dictR 0057 case (.String(let strL), .String(let strR)): 0058 return strL == strR 0059 case (.Double(let dubL), .Double(let dubR)): 0060 return dubL == dubR 0061 case (.Double(let dubL), .Int(let intR)): 0062 return dubL == Double(intR) 0063 case (.Int(let intL), .Int(let intR)): 0064 return intL == intR 0065 case (.Int(let intL), .Double(let dubR)): 0066 return Double(intL) == dubR 0067 case (.Bool(let bL), .Bool(let bR)): 0068 return bL == bR 0069 case (.Null, .Null): 0070 return true 0071 default: 0072 return false 0073 } 0074 } 0075 0076 extension JSON: Equatable {} 0077 0078 // MARK: - Printing 0079 0080 extension JSON: CustomStringConvertible { 0081 0082 /// A textual representation of `self`. 0083 public var description: Swift.String { 0084 switch self { 0085 case .Array(let arr): return Swift.String(arr) 0086 case .Dictionary(let dict): return Swift.String(dict) 0087 case .String(let string): return string 0088 case .Double(let double): return Swift.String(double) 0089 case .Int(let int): return Swift.String(int) 0090 case .Bool(let bool): return Swift.String(bool) 0091 case .Null: return "null" 0092 } 0093 } 0094 0095 } 0096