0001 // 0002 // Accessors.swift 0003 // PMJSON 0004 // 0005 // Created by Kevin Ballard on 10/9/15. 0006 // Copyright © 2016 Postmates. 0007 // 0008 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or 0009 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license 0010 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your 0011 // option. This file may not be copied, modified, or distributed 0012 // except according to those terms. 0013 // 0014 0015 public extension JSON { 0016 /// Returns `true` iff the receiver is `.Null`. 0017 var isNull: Swift.Bool { 0018 switch self { 0019 case .Null: return true 0020 default: return false 0021 } 0022 } 0023 0024 /// Returns `true` iff the receiver is `.Bool`. 0025 var isBool: Swift.Bool { 0026 switch self { 0027 case .Bool: return true 0028 default: return false 0029 } 0030 } 0031 0032 /// Returns `true` iff the receiver is `.String`. 0033 var isString: Swift.Bool { 0034 switch self { 0035 case .String: return true 0036 default: return false 0037 } 0038 } 0039 0040 /// Returns `true` iff the receiver is `.Int64`. 0041 var isInt64: Swift.Bool { 0042 switch self { 0043 case .Int64: return true 0044 default: return false 0045 } 0046 } 0047 0048 /// Returns `true` iff the receiver is `.Double`. 0049 var isDouble: Swift.Bool { 0050 switch self { 0051 case .Double: return true 0052 default: return false 0053 } 0054 } 0055 0056 /// Returns `true` iff the receiver is `.Int64` or `.Double`. 0057 var isNumber: Swift.Bool { 0058 switch self { 0059 case .Int64, .Double: return true 0060 default: return false 0061 } 0062 } 0063 0064 /// Returns `true` iff the receiver is `.Object`. 0065 var isObject: Swift.Bool { 0066 switch self { 0067 case .Object: return true 0068 default: return false 0069 } 0070 } 0071 0072 /// Returns `true` iff the receiver is `.Array`. 0073 var isArray: Swift.Bool { 0074 switch self { 0075 case .Array: return true 0076 default: return false 0077 } 0078 } 0079 } 0080 0081 public extension JSON { 0082 /// Returns the boolean value if the receiver is `.Bool`, otherwise `nil`. 0083 var bool
DecimalNumber.swift:89 else if isNull { return nil }JSONError.swift:116 else if isNull { return nil }JSONError.swift:133 else if isNull { return nil }JSONError.swift:150 else if isNull { return nil }JSONError.swift:174 } else if isNull { return nil }JSONError.swift:191 else if isNull { return nil }JSONError.swift:208 else if isNull { return nil }JSONError.swift:225 else if isNull { return nil }: Swift.Bool? { 0084 switch self { 0085 case .Bool(let b): return b 0086 default: return nil 0087 } 0088 } 0089 0090 /// Returns the string value if the receiver is `.String`, otherwise `nil`. 0091 var string
JSONError.swift:107 guard let b = bool else { throw JSONError.MissingOrInvalidType(path: nil, expected: .Required(.Bool), actual: .forValue(self)) }JSONError.swift:115 if let b = bool { return b }: Swift.String? { 0092 switch self { 0093 case .String(let s): return s 0094 default: return nil 0095 } 0096 } 0097 0098 /// Returns the 64-bit integral value if the receiver is `.Int64` or `.Double`, otherwise `nil`. 0099 /// If the receiver is `.Double`, the value is truncated. If it does not fit in 64 bits, `nil` is returned. 0100 var int64
JSONError.swift:124 guard let str = string else { throw JSONError.MissingOrInvalidType(path: nil, expected: .Required(.String), actual: .forValue(self)) }JSONError.swift:132 if let str = string { return str }: Swift.Int64? { 0101 switch self { 0102 case .Int64(let i): return i 0103 case .Double(let d): return convertDoubleToInt64(d) 0104 default: return nil 0105 } 0106 } 0107 0108 /// Returns the integral value if the receiver is `.Int64` or `.Double`, otherwise `nil`. 0109 /// If the receiver is `.Double`, the value is truncated. If it does not fit in an `Int`, `nil` is returned. 0110 /// If the receiver is `.Int64` and the value does not fit in an `.Int`, `nil` is returned. 0111 var int: Int? { 0112 guard let value = int64 else { return nil} 0113 let truncated = Int(truncatingBitPattern: value) 0114 guard Swift.Int64(truncated) == value else { return nil } 0115 return truncated 0116 } 0117 0118 /// Returns the numeric value as a `Double` if the receiver is `.Int64` or `.Double`, otherwise `nil`. 0119 var double
Accessors.swift:112 guard let value = int64 else { return nil}JSONError.swift:141 guard let val = int64 else { throw JSONError.MissingOrInvalidType(path: nil, expected: .Required(.Number), actual: .forValue(self)) }JSONError.swift:149 if let val = int64 { return val }JSONError.swift:159 guard let val = int64 else { throw JSONError.MissingOrInvalidType(path: nil, expected: .Required(.Number), actual: .forValue(self)) }JSONError.swift:170 if let val = int64 {: Swift.Double? { 0120 switch self { 0121 case .Int64(let i): return Swift.Double(i) 0122 case .Double(let d): return d 0123 default: return nil 0124 } 0125 } 0126 0127 /// Returns the object dictionary if the receiver is `.Object`, otherwise `nil`. 0128 var object
JSONError.swift:182 guard let val = double else { throw JSONError.MissingOrInvalidType(path: nil, expected: .Required(.Number), actual: .forValue(self)) }JSONError.swift:190 if let val = double { return val }: JSONObject? { 0129 switch self { 0130 case .Object(let obj): return obj 0131 default: return nil 0132 } 0133 } 0134 0135 /// Returns the array if the receiver is `.Array`, otherwise `nil`. 0136 var array
Accessors.swift:181 return object?[key]JSONError.swift:199 guard let dict = object else { throw JSONError.MissingOrInvalidType(path: nil, expected: .Required(.Object), actual: .forValue(self)) }JSONError.swift:207 if let dict = object { return dict }: JSONArray? { 0137 switch self { 0138 case .Array(let ary): return ary 0139 default: return nil 0140 } 0141 } 0142 } 0143 0144 public extension JSON { 0145 /// Returns the string value if the receiver is `.String`, coerces the value to a string if 0146 /// the receiver is `.Bool`, `.Null`, `.Int64`, or `.Double`, or otherwise returns `nil`. 0147 var asString: Swift.String? { 0148 return try? toString() 0149 } 0150 0151 /// Returns the 64-bit integral value if the receiver is `.Int64` or `.Double`, coerces the value 0152 /// if the receiver is `.String`, otherwise returns `nil`. 0153 /// If the receiver is `.Double`, the value is truncated. If it does not fit in 64 bits, `nil` is returned. 0154 /// If the receiver is `.String`, it must parse fully as an integral or floating-point number. 0155 /// If it parses as a floating-point number, it is truncated. If it does not fit in 64 bits, `nil` is returned. 0156 var asInt64: Swift.Int64? { 0157 return try? toInt64() 0158 } 0159 0160 /// Returns the integral value if the receiver is `.Int64` or `.Double`, coerces the value 0161 /// if the receiver is `.String`, otherwise returns `nil`. 0162 /// If the receiver is `.Double`, the value is truncated. If it does not fit in an `Int`, `nil` is returned. 0163 /// If the receiver is `.String`, it must parse fully as an integral or floating-point number. 0164 /// If it parses as a floating-point number, it is truncated. If it does not fit in an `Int`, `nil` is returned. 0165 var asInt: Int? { 0166 return try? toInt() 0167 } 0168 0169 /// Returns the double value if the receiver is `.Int64` or `.Double`, coerces the value 0170 /// if the receiver is `.String`, otherwise returns `nil`. 0171 /// If the receiver is `.String`, it must parse fully as a floating-point number. 0172 var asDouble: Swift.Double? { 0173 return try? toDouble() 0174 } 0175 } 0176 0177 public extension JSON { 0178 /// If the receiver is `.Object`, returns the result of subscripting the object. 0179 /// Otherwise, returns `nil`. 0180 subscript(key: Swift.String) -> JSON? { 0181 return object?[key] 0182 } 0183 0184 /// If the receiver is `.Array` and the index is in range of the array, returns the result of subscripting the array. 0185 /// Otherwise returns `nil`. 0186 subscript(index: Int) -> JSON? { 0187 guard let ary = array else { return nil } 0188 guard index >= ary.startIndex && index < ary.endIndex else { return nil } 0189 return ary[index] 0190 } 0191 } 0192 0193 internal func convertDoubleToInt64
Accessors.swift:187 guard let ary = array else { return nil }JSONError.swift:216 guard let ary = array else { throw JSONError.MissingOrInvalidType(path: nil, expected: .Required(.Array), actual: .forValue(self)) }JSONError.swift:224 if let ary = array { return ary }(d: Double) -> Int64? { 0194 // Int64(Double(Int64.max)) asserts because it interprets it as out of bounds. 0195 // Int64(Double(Int64.min)) works just fine. 0196 if d >= Double(Int64.max) || d < Double(Int64.min) { 0197 return nil 0198 } 0199 return Int64(d) 0200 } 0201
Accessors.swift:103 case .Double(let d): return convertDoubleToInt64(d)JSONError.swift:286 guard let val = convertDoubleToInt64(d) else {JSONError.swift:294 guard let val = convertDoubleToInt64(d) else {