0001 // InterchangeData.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 @_exported import Data 0026 0027 public enum InterchangeDataError: ErrorType { 0028 case IncompatibleType
InterchangeData.swift:208 throw InterchangeDataError.IncompatibleTypeInterchangeData.swift:215 throw InterchangeDataError.IncompatibleTypeInterchangeData.swift:222 throw InterchangeDataError.IncompatibleTypeInterchangeData.swift:229 throw InterchangeDataError.IncompatibleTypeInterchangeData.swift:236 throw InterchangeDataError.IncompatibleTypeInterchangeData.swift:243 throw InterchangeDataError.IncompatibleTypeInterchangeData.swift:250 throw InterchangeDataError.IncompatibleTypeInterchangeData.swift:257 throw InterchangeDataError.IncompatibleType0029 } 0030 0031 public protocol InterchangeDataParser
InterchangeData.swift:208 throw InterchangeDataError.IncompatibleTypeInterchangeData.swift:215 throw InterchangeDataError.IncompatibleTypeInterchangeData.swift:222 throw InterchangeDataError.IncompatibleTypeInterchangeData.swift:229 throw InterchangeDataError.IncompatibleTypeInterchangeData.swift:236 throw InterchangeDataError.IncompatibleTypeInterchangeData.swift:243 throw InterchangeDataError.IncompatibleTypeInterchangeData.swift:250 throw InterchangeDataError.IncompatibleTypeInterchangeData.swift:257 throw InterchangeDataError.IncompatibleType{ 0032 func parse(data: Data) throws -> InterchangeData 0033 } 0034 0035 public extension InterchangeDataParser { 0036 public func parse(convertible: DataConvertible) throws -> InterchangeData { 0037 return try parse(convertible.data) 0038 } 0039 } 0040 0041 public protocol InterchangeDataSerializer { 0042 func serialize(interchangeData: InterchangeData) throws -> Data 0043 } 0044 0045 public enum InterchangeData
InterchangeData.swift:35 public extension InterchangeDataParser {{ 0046 case NullValue
InterchangeData.swift:32 func parse(data: Data) throws -> InterchangeDataInterchangeData.swift:36 public func parse(convertible: DataConvertible) throws -> InterchangeData {InterchangeData.swift:42 func serialize(interchangeData: InterchangeData) throws -> DataInterchangeData.swift:50 case ArrayValue([InterchangeData])InterchangeData.swift:51 case ObjectValue([String: InterchangeData])InterchangeData.swift:53 public static func from(value: Bool) -> InterchangeData {InterchangeData.swift:57 public static func from(value: Double) -> InterchangeData {InterchangeData.swift:61 public static func from(value: Int) -> InterchangeData {InterchangeData.swift:65 public static func from(value: String) -> InterchangeData {InterchangeData.swift:69 public static func from(value: [InterchangeData]) -> InterchangeData {InterchangeData.swift:69 public static func from(value: [InterchangeData]) -> InterchangeData {InterchangeData.swift:73 public static func from(value: [String: InterchangeData]) -> InterchangeData {InterchangeData.swift:73 public static func from(value: [String: InterchangeData]) -> InterchangeData {InterchangeData.swift:147 public var array: [InterchangeData]? {InterchangeData.swift:154 public var dictionary: [String: InterchangeData]? {InterchangeData.swift:246 public func asArray() throws -> [InterchangeData] {InterchangeData.swift:253 public func asDictionary() throws -> [String: InterchangeData] {InterchangeData.swift:305 extension InterchangeData: Equatable {}InterchangeData.swift:307 public func ==(lhs: InterchangeData, rhs: InterchangeData) -> Bool {InterchangeData.swift:307 public func ==(lhs: InterchangeData, rhs: InterchangeData) -> Bool {InterchangeData.swift:342 extension InterchangeData: NilLiteralConvertible {InterchangeData.swift:348 extension InterchangeData: BooleanLiteralConvertible {InterchangeData.swift:354 extension InterchangeData: IntegerLiteralConvertible {InterchangeData.swift:360 extension InterchangeData: FloatLiteralConvertible {InterchangeData.swift:366 extension InterchangeData: StringLiteralConvertible {InterchangeData.swift:380 extension InterchangeData: StringInterpolationConvertible {InterchangeData.swift:381 public init(stringInterpolation strings: InterchangeData...) {InterchangeData.swift:396 extension InterchangeData: ArrayLiteralConvertible {InterchangeData.swift:397 public init(arrayLiteral elements: InterchangeData...) {InterchangeData.swift:402 extension InterchangeData: DictionaryLiteralConvertible {InterchangeData.swift:403 public init(dictionaryLiteral elements: (String, InterchangeData)...) {InterchangeData.swift:404 var dictionary = [String: InterchangeData](minimumCapacity: elements.count)InterchangeData.swift:414 extension InterchangeData: CustomStringConvertible {InterchangeData.swift:418 func serialize(data: InterchangeData) -> String {InterchangeData.swift:437 func serializeArray(a: [InterchangeData]) -> String {InterchangeData.swift:455 func serializeObject(o: [String: InterchangeData]) -> String {0047 case BooleanValue
InterchangeData.swift:163 case NullValue:InterchangeData.swift:269 array[index] = .NullValueInterchangeData.swift:309 case .NullValue:InterchangeData.swift:311 case .NullValue: return trueInterchangeData.swift:344 self = .NullValueInterchangeData.swift:420 case .NullValue: return "null"(Bool) 0048 case NumberValue
InterchangeData.swift:54 return .BooleanValue(value)InterchangeData.swift:79 case .BooleanValue: return trueInterchangeData.swift:114 case .BooleanValue(let b): return bInterchangeData.swift:165 case BooleanValue(let bool):InterchangeData.swift:180 case BooleanValue(let bool):InterchangeData.swift:314 case .BooleanValue(let lhsValue):InterchangeData.swift:316 case .BooleanValue(let rhsValue): return lhsValue == rhsValueInterchangeData.swift:350 self = .BooleanValue(value)InterchangeData.swift:421 case .BooleanValue(let b): return b ? "true" : "false"(Double) 0049 case StringValue
InterchangeData.swift:58 return .NumberValue(value)InterchangeData.swift:62 return .NumberValue(Double(value))InterchangeData.swift:86 case .NumberValue: return trueInterchangeData.swift:121 case .NumberValue(let n): return nInterchangeData.swift:167 case NumberValue(let number):InterchangeData.swift:185 case NumberValue(let number):InterchangeData.swift:324 case .NumberValue(let lhsValue):InterchangeData.swift:326 case .NumberValue(let rhsValue): return lhsValue == rhsValueInterchangeData.swift:356 self = .NumberValue(Double(value))InterchangeData.swift:362 self = .NumberValue(Double(value))InterchangeData.swift:422 case .NumberValue(let n): return serializeNumber(n)(String) 0050 case ArrayValue
InterchangeData.swift:66 return .StringValue(value)InterchangeData.swift:93 case .StringValue: return trueInterchangeData.swift:142 case .StringValue(let s): return sInterchangeData.swift:169 case StringValue(let string):InterchangeData.swift:190 case StringValue(let string):InterchangeData.swift:319 case .StringValue(let lhsValue):InterchangeData.swift:321 case .StringValue(let rhsValue): return lhsValue == rhsValueInterchangeData.swift:368 self = .StringValue(value)InterchangeData.swift:372 self = .StringValue(value)InterchangeData.swift:376 self = .StringValue(value)InterchangeData.swift:388 self = .StringValue(String(string))InterchangeData.swift:392 self = .StringValue(String(expr))InterchangeData.swift:423 case .StringValue(let s): return escape(s)([InterchangeData]) 0051 case ObjectValue
InterchangeData.swift:70 return .ArrayValue(value)InterchangeData.swift:100 case .ArrayValue: return trueInterchangeData.swift:149 case .ArrayValue(let array): return arrayInterchangeData.swift:171 case ArrayValue(let array):InterchangeData.swift:195 case ArrayValue(let array):InterchangeData.swift:278 case .ArrayValue(let array):InterchangeData.swift:263 case .ArrayValue(let array):InterchangeData.swift:271 self = .ArrayValue(array)InterchangeData.swift:329 case .ArrayValue(let lhsValue):InterchangeData.swift:331 case .ArrayValue(let rhsValue): return lhsValue == rhsValueInterchangeData.swift:398 self = .ArrayValue(elements)InterchangeData.swift:424 case .ArrayValue(let a): return serializeArray(a)([String: InterchangeData]) 0052 0053 public static func from(value: Bool) -> InterchangeData { 0054 return .BooleanValue(value) 0055 } 0056 0057 public static func from(value: Double) -> InterchangeData { 0058 return .NumberValue(value) 0059 } 0060 0061 public static func from(value: Int) -> InterchangeData { 0062 return .NumberValue(Double(value)) 0063 } 0064 0065 public static func from(value: String) -> InterchangeData { 0066 return .StringValue(value) 0067 } 0068 0069 public static func from(value: [InterchangeData]) -> InterchangeData { 0070 return .ArrayValue(value) 0071 } 0072 0073 public static func from(value: [String: InterchangeData]) -> InterchangeData { 0074 return .ObjectValue(value) 0075 } 0076 0077 public var isBoolean: Bool { 0078 switch self { 0079 case .BooleanValue: return true 0080 default: return false 0081 } 0082 } 0083 0084 public var isNumber: Bool { 0085 switch self { 0086 case .NumberValue: return true 0087 default: return false 0088 } 0089 } 0090 0091 public var isString: Bool { 0092 switch self { 0093 case .StringValue: return true 0094 default: return false 0095 } 0096 } 0097 0098 public var isArray: Bool { 0099 switch self { 0100 case .ArrayValue: return true 0101 default: return false 0102 } 0103 } 0104 0105 public var isObject: Bool { 0106 switch self { 0107 case .ObjectValue: return true 0108 default: return false 0109 } 0110 } 0111 0112 public var bool
InterchangeData.swift:74 return .ObjectValue(value)InterchangeData.swift:107 case .ObjectValue: return trueInterchangeData.swift:156 case .ObjectValue(let dictionary): return dictionaryInterchangeData.swift:173 case ObjectValue(let object):InterchangeData.swift:200 case ObjectValue(let object):InterchangeData.swift:297 case .ObjectValue(let object):InterchangeData.swift:288 case .ObjectValue(let object):InterchangeData.swift:291 self = .ObjectValue(object)InterchangeData.swift:334 case .ObjectValue(let lhsValue):InterchangeData.swift:336 case .ObjectValue(let rhsValue): return lhsValue == rhsValueInterchangeData.swift:410 self = .ObjectValue(dictionary)InterchangeData.swift:425 case .ObjectValue(let o): return serializeObject(o): Bool? { 0113 switch self { 0114 case .BooleanValue(let b): return b 0115 default: return nil 0116 } 0117 } 0118 0119 public var double
InterchangeData.swift:212 if let v = bool {: Double? { 0120 switch self { 0121 case .NumberValue(let n): return n 0122 default: return nil 0123 } 0124 } 0125 0126 public var int
InterchangeData.swift:127 if let v = double {InterchangeData.swift:134 if let v = double {InterchangeData.swift:219 if let v = double {: Int? { 0127 if let v = double { 0128 return Int(v) 0129 } 0130 return nil 0131 } 0132 0133 public var uint
InterchangeData.swift:226 if let v = int {: UInt? { 0134 if let v = double { 0135 return UInt(v) 0136 } 0137 return nil 0138 } 0139 0140 public var string
InterchangeData.swift:233 if let v = uint {: String? { 0141 switch self { 0142 case .StringValue(let s): return s 0143 default: return nil 0144 } 0145 } 0146 0147 public var array
InterchangeData.swift:240 if let v = string {InterchangeData.swift:385 string += s.string!: [InterchangeData]? { 0148 switch self { 0149 case .ArrayValue(let array): return array 0150 default: return nil 0151 } 0152 } 0153 0154 public var dictionary
InterchangeData.swift:247 if let v = array {: [String: InterchangeData]? { 0155 switch self { 0156 case .ObjectValue(let dictionary): return dictionary 0157 default: return nil 0158 } 0159 } 0160 0161 public func get<T>() -> T? { 0162 switch self { 0163 case NullValue: 0164 return nil 0165 case BooleanValue(let bool): 0166 return bool as? T 0167 case NumberValue(let number): 0168 return number as? T 0169 case StringValue(let string): 0170 return string as? T 0171 case ArrayValue(let array): 0172 return array as? T 0173 case ObjectValue(let object): 0174 return object as? T 0175 } 0176 } 0177 0178 public func get<T>() throws -> T { 0179 switch self { 0180 case BooleanValue(let bool): 0181 if let value = bool as? T { 0182 return value 0183 } 0184 0185 case NumberValue(let number): 0186 if let value = number as? T { 0187 return value 0188 } 0189 0190 case StringValue(let string): 0191 if let value = string as? T { 0192 return value 0193 } 0194 0195 case ArrayValue(let array): 0196 if let value = array as? T { 0197 return value 0198 } 0199 0200 case ObjectValue(let object): 0201 if let value = object as? T { 0202 return value 0203 } 0204 0205 default: break 0206 } 0207 0208 throw InterchangeDataError.IncompatibleType 0209 } 0210 0211 public func asBool() throws -> Bool { 0212 if let v = bool { 0213 return v 0214 } 0215 throw InterchangeDataError.IncompatibleType 0216 } 0217 0218 public func asDouble() throws -> Double { 0219 if let v = double { 0220 return v 0221 } 0222 throw InterchangeDataError.IncompatibleType 0223 } 0224 0225 public func asInt() throws -> Int { 0226 if let v = int { 0227 return v 0228 } 0229 throw InterchangeDataError.IncompatibleType 0230 } 0231 0232 public func asUInt() throws -> UInt { 0233 if let v = uint { 0234 return UInt(v) 0235 } 0236 throw InterchangeDataError.IncompatibleType 0237 } 0238 0239 public func asString() throws -> String { 0240 if let v = string { 0241 return v 0242 } 0243 throw InterchangeDataError.IncompatibleType 0244 } 0245 0246 public func asArray() throws -> [InterchangeData] { 0247 if let v = array { 0248 return v 0249 } 0250 throw InterchangeDataError.IncompatibleType 0251 } 0252 0253 public func asDictionary() throws -> [String: InterchangeData] { 0254 if let v = dictionary { 0255 return v 0256 } 0257 throw InterchangeDataError.IncompatibleType 0258 } 0259 0260 public subscript(index: Int) -> InterchangeData? { 0261 set { 0262 switch self { 0263 case .ArrayValue(let array): 0264 var array = array 0265 if index < array.count { 0266 if let json = newValue { 0267 array[index] = json 0268 } else { 0269 array[index] = .NullValue 0270 } 0271 self = .ArrayValue(array) 0272 } 0273 default: break 0274 } 0275 } 0276 get { 0277 switch self { 0278 case .ArrayValue(let array): 0279 return index < array.count ? array[index] : nil 0280 default: return nil 0281 } 0282 } 0283 } 0284 0285 public subscript(key: String) -> InterchangeData? { 0286 set { 0287 switch self { 0288 case .ObjectValue(let object): 0289 var object = object 0290 object[key] = newValue 0291 self = .ObjectValue(object) 0292 default: break 0293 } 0294 } 0295 get { 0296 switch self { 0297 case .ObjectValue(let object): 0298 return object[key] 0299 default: return nil 0300 } 0301 } 0302 } 0303 } 0304 0305 extension InterchangeData: Equatable {} 0306 0307 public func ==(lhs: InterchangeData, rhs: InterchangeData) -> Bool { 0308 switch lhs { 0309 case .NullValue: 0310 switch rhs { 0311 case .NullValue: return true 0312 default: return false 0313 } 0314 case .BooleanValue(let lhsValue): 0315 switch rhs { 0316 case .BooleanValue(let rhsValue): return lhsValue == rhsValue 0317 default: return false 0318 } 0319 case .StringValue(let lhsValue): 0320 switch rhs { 0321 case .StringValue(let rhsValue): return lhsValue == rhsValue 0322 default: return false 0323 } 0324 case .NumberValue(let lhsValue): 0325 switch rhs { 0326 case .NumberValue(let rhsValue): return lhsValue == rhsValue 0327 default: return false 0328 } 0329 case .ArrayValue(let lhsValue): 0330 switch rhs { 0331 case .ArrayValue(let rhsValue): return lhsValue == rhsValue 0332 default: return false 0333 } 0334 case .ObjectValue(let lhsValue): 0335 switch rhs { 0336 case .ObjectValue(let rhsValue): return lhsValue == rhsValue 0337 default: return false 0338 } 0339 } 0340 } 0341 0342 extension InterchangeData: NilLiteralConvertible { 0343 public init(nilLiteral value: Void) { 0344 self = .NullValue 0345 } 0346 } 0347 0348 extension InterchangeData: BooleanLiteralConvertible { 0349 public init(booleanLiteral value: BooleanLiteralType) { 0350 self = .BooleanValue(value) 0351 } 0352 } 0353 0354 extension InterchangeData: IntegerLiteralConvertible { 0355 public init(integerLiteral value: IntegerLiteralType) { 0356 self = .NumberValue(Double(value)) 0357 } 0358 } 0359 0360 extension InterchangeData: FloatLiteralConvertible { 0361 public init(floatLiteral value: FloatLiteralType) { 0362 self = .NumberValue(Double(value)) 0363 } 0364 } 0365 0366 extension InterchangeData: StringLiteralConvertible { 0367 public init(unicodeScalarLiteral value: String) { 0368 self = .StringValue(value) 0369 } 0370 0371 public init(extendedGraphemeClusterLiteral value: String) { 0372 self = .StringValue(value) 0373 } 0374 0375 public init(stringLiteral value: StringLiteralType) { 0376 self = .StringValue(value) 0377 } 0378 } 0379 0380 extension InterchangeData: StringInterpolationConvertible { 0381 public init(stringInterpolation strings: InterchangeData...) { 0382 var string = "" 0383 0384 for s in strings { 0385 string += s.string! 0386 } 0387 0388 self = .StringValue(String(string)) 0389 } 0390 0391 public init<T>(stringInterpolationSegment expr: T) { 0392 self = .StringValue(String(expr)) 0393 } 0394 } 0395 0396 extension InterchangeData: ArrayLiteralConvertible { 0397 public init(arrayLiteral elements: InterchangeData...) { 0398 self = .ArrayValue(elements) 0399 } 0400 } 0401 0402 extension InterchangeData: DictionaryLiteralConvertible { 0403 public init(dictionaryLiteral elements: (String, InterchangeData)...) { 0404 var dictionary = [String: InterchangeData](minimumCapacity: elements.count) 0405 0406 for pair in elements { 0407 dictionary[pair.0] = pair.1 0408 } 0409 0410 self = .ObjectValue(dictionary) 0411 } 0412 } 0413 0414 extension InterchangeData: CustomStringConvertible { 0415 public var description: String { 0416 var indentLevel = 0 0417 0418 func serialize(data: InterchangeData) -> String { 0419 switch data { 0420 case .NullValue: return "null" 0421 case .BooleanValue(let b): return b ? "true" : "false" 0422 case .NumberValue(let n): return serializeNumber(n) 0423 case .StringValue(let s): return escape(s) 0424 case .ArrayValue(let a): return serializeArray(a) 0425 case .ObjectValue(let o): return serializeObject(o) 0426 } 0427 } 0428 0429 func serializeNumber(n: Double) -> String { 0430 if n == Double(Int64(n)) { 0431 return Int64(n).description 0432 } else { 0433 return n.description 0434 } 0435 } 0436 0437 func serializeArray(a: [InterchangeData]) -> String { 0438 var s = "[" 0439 indentLevel += 1 0440 0441 for i in 0 ..< a.count { 0442 s += "\n" 0443 s += indent() 0444 s += serialize(a[i]) 0445 0446 if i != (a.count - 1) { 0447 s += "," 0448 } 0449 } 0450 0451 indentLevel -= 1 0452 return s + "\n" + indent() + "]" 0453 } 0454 0455 func serializeObject(o: [String: InterchangeData]) -> String { 0456 var s = "{" 0457 indentLevel += 1 0458 var i = 0 0459 0460 for (key, value) in o { 0461 s += "\n" 0462 s += indent() 0463 s += "\(escape(key)): \(serialize(value))" 0464 0465 if i != (o.count - 1) { 0466 s += "," 0467 } 0468 i += 1 0469 } 0470 0471 indentLevel -= 1 0472 return s + "\n" + indent() + "}" 0473 } 0474 0475 func indent() -> String { 0476 var s = "" 0477 0478 for _ in 0 ..< indentLevel { 0479 s += " " 0480 } 0481 0482 return s 0483 } 0484 0485 return serialize(self) 0486 } 0487 } 0488 0489 func escape
InterchangeData.swift:254 if let v = dictionary {(source: String) -> String { 0490 var s = "\"" 0491 0492 for c in source.characters { 0493 if let escapedSymbol = escapeMapping[c] { 0494 s.appendContentsOf(escapedSymbol) 0495 } else { 0496 s.append(c) 0497 } 0498 } 0499 0500 s.appendContentsOf("\"") 0501 0502 return s 0503 } 0504 0505 let escapeMapping
InterchangeData.swift:423 case .StringValue(let s): return escape(s)InterchangeData.swift:463 s += "\(escape(key)): \(serialize(value))": [Character: String] = [ 0506 "\r": "\\r", 0507 "\n": "\\n", 0508 "\t": "\\t", 0509 "\\": "\\\\", 0510 "\"": "\\\"", 0511 0512 "\u{2028}": "\\u2028", 0513 "\u{2029}": "\\u2029", 0514 0515 "\r\n": "\\r\\n" 0516 ]
InterchangeData.swift:493 if let escapedSymbol = escapeMapping[c] {