0001    // JSON.swift
0002    //
0003    // The MIT License (MIT)
0004    //
0005    // Copyright (c) 2015 Zewo
0006    //
0007    // Permission is hereby granted, free of charge, to any person obtaining a copy
0008    // of this software and associated documentation files (the "Software"), to deal
0009    // in the Software without restriction, including without limitation the rights
0010    // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
0011    // copies of the Software, and to permit persons to whom the Software is
0012    // furnished to do so, subject to the following conditions:
0013    //
0014    // The above copyright notice and this permission notice shall be included in all
0015    // copies or substantial portions of the Software.
0016    //
0017    // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0018    // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0019    // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
0020    // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0021    // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
0022    // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
0023    // SOFTWARE.
0024    
0025    public enum JSONError
JSON.swift:192
        throw JSONError.IncompatibleType
JSON.swift:199
        throw JSONError.IncompatibleType
JSON.swift:206
        throw JSONError.IncompatibleType
JSON.swift:213
        throw JSONError.IncompatibleType
JSON.swift:220
        throw JSONError.IncompatibleType
JSON.swift:227
        throw JSONError.IncompatibleType
JSON.swift:234
        throw JSONError.IncompatibleType
JSON.swift:241
        throw JSONError.IncompatibleType
: ErrorType { 0026 case IncompatibleType
JSON.swift:192
        throw JSONError.IncompatibleType
JSON.swift:199
        throw JSONError.IncompatibleType
JSON.swift:206
        throw JSONError.IncompatibleType
JSON.swift:213
        throw JSONError.IncompatibleType
JSON.swift:220
        throw JSONError.IncompatibleType
JSON.swift:227
        throw JSONError.IncompatibleType
JSON.swift:234
        throw JSONError.IncompatibleType
JSON.swift:241
        throw JSONError.IncompatibleType
0027 } 0028 0029 public enum JSON
JSON.swift:34
    case ArrayValue([JSON])
JSON.swift:35
    case ObjectValue([String: JSON])
JSON.swift:37
    public static func from(value: Bool) -> JSON {
JSON.swift:41
    public static func from(value: Double) -> JSON {
JSON.swift:45
    public static func from(value: Int) -> JSON {
JSON.swift:49
    public static func from(value: String) -> JSON {
JSON.swift:53
    public static func from(value: [JSON]) -> JSON {
JSON.swift:53
    public static func from(value: [JSON]) -> JSON {
JSON.swift:57
    public static func from(value: [String: JSON]) -> JSON {
JSON.swift:57
    public static func from(value: [String: JSON]) -> JSON {
JSON.swift:131
    public var array: [JSON]? {
JSON.swift:138
    public var dictionary: [String: JSON]? {
JSON.swift:230
    public func asArray() throws -> [JSON] {
JSON.swift:237
    public func asDictionary() throws -> [String: JSON] {
JSON.swift:289
extension JSON: Equatable {}
JSON.swift:291
public func ==(lhs: JSON, rhs: JSON) -> Bool {
JSON.swift:291
public func ==(lhs: JSON, rhs: JSON) -> Bool {
JSON.swift:326
extension JSON: NilLiteralConvertible {
JSON.swift:332
extension JSON: BooleanLiteralConvertible {
JSON.swift:338
extension JSON: IntegerLiteralConvertible {
JSON.swift:344
extension JSON: FloatLiteralConvertible {
JSON.swift:350
extension JSON: StringLiteralConvertible {
JSON.swift:364
extension JSON: StringInterpolationConvertible {
JSON.swift:365
    public init(stringInterpolation strings: JSON...) {
JSON.swift:380
extension JSON: ArrayLiteralConvertible {
JSON.swift:381
    public init(arrayLiteral elements: JSON...) {
JSON.swift:386
extension JSON: DictionaryLiteralConvertible {
JSON.swift:387
    public init(dictionaryLiteral elements: (String, JSON)...) {
JSON.swift:388
        var dictionary = [String: JSON](minimumCapacity: elements.count)
JSON.swift:398
extension JSON: CustomStringConvertible {
JSON.swift:404
extension JSON: CustomDebugStringConvertible {
JSONParser.swift:64
    public func parse(data: Data) throws -> JSON {
JSONParser.swift:86
    func parse() throws -> JSON {
JSONParser.swift:104
    private func parseValue() throws -> JSON {
JSONParser.swift:115
        case Char(ascii: "n"): return try parseSymbol("null", JSON.NullValue)
JSONParser.swift:116
        case Char(ascii: "t"): return try parseSymbol("true", JSON.BooleanValue(true))
JSONParser.swift:117
        case Char(ascii: "f"): return try parseSymbol("false", JSON.BooleanValue(false))
JSONParser.swift:142
    private func parseSymbol(target: StaticString, @autoclosure _ iftrue: Void -> JSON) throws -> JSON {
JSONParser.swift:142
    private func parseSymbol(target: StaticString, @autoclosure _ iftrue: Void -> JSON) throws -> JSON {
JSONParser.swift:154
    private func parseString() throws -> JSON {
JSONParser.swift:230
    private func parseNumber() throws -> JSON {
JSONParser.swift:323
    private func parseObject() throws -> JSON {
JSONParser.swift:327
        var object: [String: JSON] = [:]
JSONParser.swift:372
    private func parseArray() throws -> JSON {
JSONParser.swift:377
        var array: [JSON] = []
JSONSerializer.swift:36
    public func serializeToString(json: JSON) -> String {
JSONSerializer.swift:55
    func serializeArray(a: [JSON]) -> String {
JSONSerializer.swift:69
    func serializeObject(o: [String: JSON]) -> String {
JSONSerializer.swift:88
    override public func serializeArray(a: [JSON]) -> String {
JSONSerializer.swift:106
    override public func serializeObject(o: [String: JSON]) -> String {
{ 0030 case NullValue
JSON.swift:147
        case NullValue:
JSON.swift:253
                        array[index] = .NullValue
JSON.swift:293
    case .NullValue:
JSON.swift:295
        case .NullValue: return true
JSON.swift:328
        self = .NullValue
JSONParser.swift:115
        case Char(ascii: "n"): return try parseSymbol("null", JSON.NullValue)
JSONSerializer.swift:38
        case .NullValue: return "null"
0031 case BooleanValue
JSON.swift:38
        return .BooleanValue(value)
JSON.swift:63
        case .BooleanValue: return true
JSON.swift:98
        case .BooleanValue(let b): return b
JSON.swift:149
        case BooleanValue(let bool):
JSON.swift:164
        case BooleanValue(let bool):
JSON.swift:298
    case .BooleanValue(let lhsValue):
JSON.swift:300
        case .BooleanValue(let rhsValue): return lhsValue == rhsValue
JSON.swift:334
        self = .BooleanValue(value)
JSONParser.swift:116
        case Char(ascii: "t"): return try parseSymbol("true", JSON.BooleanValue(true))
JSONParser.swift:117
        case Char(ascii: "f"): return try parseSymbol("false", JSON.BooleanValue(false))
JSONSerializer.swift:39
        case .BooleanValue(let b): return b ? "true" : "false"
(Bool) 0032 case NumberValue
JSON.swift:42
        return .NumberValue(value)
JSON.swift:46
        return .NumberValue(Double(value))
JSON.swift:70
        case .NumberValue: return true
JSON.swift:105
        case .NumberValue(let n): return n
JSON.swift:151
        case NumberValue(let number):
JSON.swift:169
        case NumberValue(let number):
JSON.swift:308
    case .NumberValue(let lhsValue):
JSON.swift:310
        case .NumberValue(let rhsValue): return lhsValue == rhsValue
JSON.swift:340
        self = .NumberValue(Double(value))
JSON.swift:346
        self = .NumberValue(Double(value))
JSONParser.swift:320
        return .NumberValue(sign * (Double(integer) + fraction) * pow(10, Double(exponent)))
JSONSerializer.swift:40
        case .NumberValue(let n): return serializeNumber(n)
(Double) 0033 case StringValue
JSON.swift:50
        return .StringValue(value)
JSON.swift:77
        case .StringValue: return true
JSON.swift:126
        case .StringValue(let s): return s
JSON.swift:153
        case StringValue(let string):
JSON.swift:174
        case StringValue(let string):
JSON.swift:303
    case .StringValue(let lhsValue):
JSON.swift:305
        case .StringValue(let rhsValue): return lhsValue == rhsValue
JSON.swift:352
        self = .StringValue(value)
JSON.swift:356
        self = .StringValue(value)
JSON.swift:360
        self = .StringValue(value)
JSON.swift:372
        self = .StringValue(String(string))
JSON.swift:376
        self = .StringValue(String(expr))
JSONParser.swift:198
        return .StringValue(s)
JSONParser.swift:333
            case .StringValue(let key):
JSONSerializer.swift:41
        case .StringValue(let s): return escapeAsJSONString(s)
(String) 0034 case ArrayValue
JSON.swift:54
        return .ArrayValue(value)
JSON.swift:84
        case .ArrayValue: return true
JSON.swift:133
        case .ArrayValue(let array): return array
JSON.swift:155
        case ArrayValue(let array):
JSON.swift:179
        case ArrayValue(let array):
JSON.swift:262
            case .ArrayValue(let array):
JSON.swift:247
            case .ArrayValue(let array):
JSON.swift:255
                    self = .ArrayValue(array)
JSON.swift:313
    case .ArrayValue(let lhsValue):
JSON.swift:315
        case .ArrayValue(let rhsValue): return lhsValue == rhsValue
JSON.swift:382
        self = .ArrayValue(elements)
JSONParser.swift:397
        return .ArrayValue(array)
JSONSerializer.swift:42
        case .ArrayValue(let a): return serializeArray(a)
([JSON]) 0035 case ObjectValue
JSON.swift:58
        return .ObjectValue(value)
JSON.swift:91
        case .ObjectValue: return true
JSON.swift:140
        case .ObjectValue(let dictionary): return dictionary
JSON.swift:157
        case ObjectValue(let object):
JSON.swift:184
        case ObjectValue(let object):
JSON.swift:281
            case .ObjectValue(let object):
JSON.swift:272
            case .ObjectValue(let object):
JSON.swift:275
                self = .ObjectValue(object)
JSON.swift:318
    case .ObjectValue(let lhsValue):
JSON.swift:320
        case .ObjectValue(let rhsValue): return lhsValue == rhsValue
JSON.swift:394
        self = .ObjectValue(dictionary)
JSONParser.swift:369
        return .ObjectValue(object)
JSONSerializer.swift:43
        case .ObjectValue(let o): return serializeObject(o)
([String: JSON]) 0036 0037 public static func from(value: Bool) -> JSON { 0038 return .BooleanValue(value) 0039 } 0040 0041 public static func from(value: Double) -> JSON { 0042 return .NumberValue(value) 0043 } 0044 0045 public static func from(value: Int) -> JSON { 0046 return .NumberValue(Double(value)) 0047 } 0048 0049 public static func from(value: String) -> JSON { 0050 return .StringValue(value) 0051 } 0052 0053 public static func from(value: [JSON]) -> JSON { 0054 return .ArrayValue(value) 0055 } 0056 0057 public static func from(value: [String: JSON]) -> JSON { 0058 return .ObjectValue(value) 0059 } 0060 0061 public var isBoolean: Bool { 0062 switch self { 0063 case .BooleanValue: return true 0064 default: return false 0065 } 0066 } 0067 0068 public var isNumber: Bool { 0069 switch self { 0070 case .NumberValue: return true 0071 default: return false 0072 } 0073 } 0074 0075 public var isString: Bool { 0076 switch self { 0077 case .StringValue: return true 0078 default: return false 0079 } 0080 } 0081 0082 public var isArray: Bool { 0083 switch self { 0084 case .ArrayValue: return true 0085 default: return false 0086 } 0087 } 0088 0089 public var isObject: Bool { 0090 switch self { 0091 case .ObjectValue: return true 0092 default: return false 0093 } 0094 } 0095 0096 public var bool
JSON.swift:196
        if let value = bool {
: Bool? { 0097 switch self { 0098 case .BooleanValue(let b): return b 0099 default: return nil 0100 } 0101 } 0102 0103 public var double
JSON.swift:111
        if let v = double {
JSON.swift:118
        if let v = double {
JSON.swift:203
        if let value = double {
: Double? { 0104 switch self { 0105 case .NumberValue(let n): return n 0106 default: return nil 0107 } 0108 } 0109 0110 public var int
JSON.swift:210
        if let value = int {
: Int? { 0111 if let v = double { 0112 return Int(v) 0113 } 0114 return nil 0115 } 0116 0117 public var uint
JSON.swift:217
        if let value = uint {
: UInt? { 0118 if let v = double { 0119 return UInt(v) 0120 } 0121 return nil 0122 } 0123 0124 public var string
JSON.swift:224
        if let value = string {
JSON.swift:369
            string += s.string!
: String? { 0125 switch self { 0126 case .StringValue(let s): return s 0127 default: return nil 0128 } 0129 } 0130 0131 public var array
JSON.swift:231
        if let value = array {
: [JSON]? { 0132 switch self { 0133 case .ArrayValue(let array): return array 0134 default: return nil 0135 } 0136 } 0137 0138 public var dictionary
JSON.swift:238
        if let value = dictionary {
: [String: JSON]? { 0139 switch self { 0140 case .ObjectValue(let dictionary): return dictionary 0141 default: return nil 0142 } 0143 } 0144 0145 public func get<T>() -> T? { 0146 switch self { 0147 case NullValue: 0148 return nil 0149 case BooleanValue(let bool): 0150 return bool as? T 0151 case NumberValue(let number): 0152 return number as? T 0153 case StringValue(let string): 0154 return string as? T 0155 case ArrayValue(let array): 0156 return array as? T 0157 case ObjectValue(let object): 0158 return object as? T 0159 } 0160 } 0161 0162 public func get<T>() throws -> T { 0163 switch self { 0164 case BooleanValue(let bool): 0165 if let value = bool as? T { 0166 return value 0167 } 0168 0169 case NumberValue(let number): 0170 if let value = number as? T { 0171 return value 0172 } 0173 0174 case StringValue(let string): 0175 if let value = string as? T { 0176 return value 0177 } 0178 0179 case ArrayValue(let array): 0180 if let value = array as? T { 0181 return value 0182 } 0183 0184 case ObjectValue(let object): 0185 if let value = object as? T { 0186 return value 0187 } 0188 0189 default: break 0190 } 0191 0192 throw JSONError.IncompatibleType 0193 } 0194 0195 public func asBool() throws -> Bool { 0196 if let value = bool { 0197 return value 0198 } 0199 throw JSONError.IncompatibleType 0200 } 0201 0202 public func asDouble() throws -> Double { 0203 if let value = double { 0204 return value 0205 } 0206 throw JSONError.IncompatibleType 0207 } 0208 0209 public func asInt() throws -> Int { 0210 if let value = int { 0211 return value 0212 } 0213 throw JSONError.IncompatibleType 0214 } 0215 0216 public func asUInt() throws -> UInt { 0217 if let value = uint { 0218 return UInt(value) 0219 } 0220 throw JSONError.IncompatibleType 0221 } 0222 0223 public func asString() throws -> String { 0224 if let value = string { 0225 return value 0226 } 0227 throw JSONError.IncompatibleType 0228 } 0229 0230 public func asArray() throws -> [JSON] { 0231 if let value = array { 0232 return value 0233 } 0234 throw JSONError.IncompatibleType 0235 } 0236 0237 public func asDictionary() throws -> [String: JSON] { 0238 if let value = dictionary { 0239 return value 0240 } 0241 throw JSONError.IncompatibleType 0242 } 0243 0244 public subscript(index: Int) -> JSON? { 0245 set { 0246 switch self { 0247 case .ArrayValue(let array): 0248 var array = array 0249 if index < array.count { 0250 if let json = newValue { 0251 array[index] = json 0252 } else { 0253 array[index] = .NullValue 0254 } 0255 self = .ArrayValue(array) 0256 } 0257 default: break 0258 } 0259 } 0260 get { 0261 switch self { 0262 case .ArrayValue(let array): 0263 return index < array.count ? array[index] : nil 0264 default: return nil 0265 } 0266 } 0267 } 0268 0269 public subscript(key: String) -> JSON? { 0270 set { 0271 switch self { 0272 case .ObjectValue(let object): 0273 var object = object 0274 object[key] = newValue 0275 self = .ObjectValue(object) 0276 default: break 0277 } 0278 } 0279 get { 0280 switch self { 0281 case .ObjectValue(let object): 0282 return object[key] 0283 default: return nil 0284 } 0285 } 0286 } 0287 } 0288 0289 extension JSON: Equatable {} 0290 0291 public func ==(lhs: JSON, rhs: JSON) -> Bool { 0292 switch lhs { 0293 case .NullValue: 0294 switch rhs { 0295 case .NullValue: return true 0296 default: return false 0297 } 0298 case .BooleanValue(let lhsValue): 0299 switch rhs { 0300 case .BooleanValue(let rhsValue): return lhsValue == rhsValue 0301 default: return false 0302 } 0303 case .StringValue(let lhsValue): 0304 switch rhs { 0305 case .StringValue(let rhsValue): return lhsValue == rhsValue 0306 default: return false 0307 } 0308 case .NumberValue(let lhsValue): 0309 switch rhs { 0310 case .NumberValue(let rhsValue): return lhsValue == rhsValue 0311 default: return false 0312 } 0313 case .ArrayValue(let lhsValue): 0314 switch rhs { 0315 case .ArrayValue(let rhsValue): return lhsValue == rhsValue 0316 default: return false 0317 } 0318 case .ObjectValue(let lhsValue): 0319 switch rhs { 0320 case .ObjectValue(let rhsValue): return lhsValue == rhsValue 0321 default: return false 0322 } 0323 } 0324 } 0325 0326 extension JSON: NilLiteralConvertible { 0327 public init(nilLiteral value: Void) { 0328 self = .NullValue 0329 } 0330 } 0331 0332 extension JSON: BooleanLiteralConvertible { 0333 public init(booleanLiteral value: BooleanLiteralType) { 0334 self = .BooleanValue(value) 0335 } 0336 } 0337 0338 extension JSON: IntegerLiteralConvertible { 0339 public init(integerLiteral value: IntegerLiteralType) { 0340 self = .NumberValue(Double(value)) 0341 } 0342 } 0343 0344 extension JSON: FloatLiteralConvertible { 0345 public init(floatLiteral value: FloatLiteralType) { 0346 self = .NumberValue(Double(value)) 0347 } 0348 } 0349 0350 extension JSON: StringLiteralConvertible { 0351 public init(unicodeScalarLiteral value: String) { 0352 self = .StringValue(value) 0353 } 0354 0355 public init(extendedGraphemeClusterLiteral value: String) { 0356 self = .StringValue(value) 0357 } 0358 0359 public init(stringLiteral value: StringLiteralType) { 0360 self = .StringValue(value) 0361 } 0362 } 0363 0364 extension JSON: StringInterpolationConvertible { 0365 public init(stringInterpolation strings: JSON...) { 0366 var string = "" 0367 0368 for s in strings { 0369 string += s.string! 0370 } 0371 0372 self = .StringValue(String(string)) 0373 } 0374 0375 public init<T>(stringInterpolationSegment expr: T) { 0376 self = .StringValue(String(expr)) 0377 } 0378 } 0379 0380 extension JSON: ArrayLiteralConvertible { 0381 public init(arrayLiteral elements: JSON...) { 0382 self = .ArrayValue(elements) 0383 } 0384 } 0385 0386 extension JSON: DictionaryLiteralConvertible { 0387 public init(dictionaryLiteral elements: (String, JSON)...) { 0388 var dictionary = [String: JSON](minimumCapacity: elements.count) 0389 0390 for pair in elements { 0391 dictionary[pair.0] = pair.1 0392 } 0393 0394 self = .ObjectValue(dictionary) 0395 } 0396 } 0397 0398 extension JSON: CustomStringConvertible { 0399 public var description: String { 0400 return JSONSerializer().serializeToString(self) 0401 } 0402 } 0403 0404 extension JSON: CustomDebugStringConvertible { 0405 public var debugDescription: String { 0406 return PrettyJSONSerializer().serializeToString(self) 0407 } 0408 }