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 // This file has been modified from its original project Swift-JsonSerializer 0026 0027 public enum JSON{ 0028 case NullValue
JSON.swift:32 case ArrayValue([JSON])JSON.swift:33 case ObjectValue([String: JSON])JSON.swift:35 public static func from(value: Bool) -> JSON {JSON.swift:39 public static func from(value: Double) -> JSON {JSON.swift:43 public static func from(value: String) -> JSON {JSON.swift:47 public static func from(value: [JSON]) -> JSON {JSON.swift:47 public static func from(value: [JSON]) -> JSON {JSON.swift:51 public static func from(value: [String: JSON]) -> JSON {JSON.swift:51 public static func from(value: [String: JSON]) -> JSON {JSON.swift:56 public static func from(values: [Any]) -> JSON {JSON.swift:57 var jsonArray: [JSON] = []JSON.swift:60 jsonArray.append(JSON.from(value))JSON.swift:63 jsonArray.append(JSON.from(value))JSON.swift:66 jsonArray.append(JSON.from(value))JSON.swift:69 jsonArray.append(JSON.from(value))JSON.swift:72 jsonArray.append(JSON.from(value))JSON.swift:75 return JSON.from(jsonArray)JSON.swift:79 public static func from(value: [String: Any]) -> JSON {JSON.swift:80 var jsonDictionary: [String: JSON] = [:]JSON.swift:83 jsonDictionary[key] = JSON.from(value)JSON.swift:86 jsonDictionary[key] = JSON.from(value)JSON.swift:89 jsonDictionary[key] = JSON.from(value)JSON.swift:92 jsonDictionary[key] = JSON.from(value)JSON.swift:95 jsonDictionary[key] = JSON.from(value)JSON.swift:99 return JSON.from(jsonDictionary)JSON.swift:172 public var arrayValue: [JSON]? {JSON.swift:179 public var dictionaryValue: [String: JSON]? {JSON.swift:235 extension JSON: CustomStringConvertible {JSON.swift:241 extension JSON: CustomDebugStringConvertible {JSON.swift:247 extension JSON: Equatable {}JSON.swift:249 public func ==(lhs: JSON, rhs: JSON) -> Bool {JSON.swift:249 public func ==(lhs: JSON, rhs: JSON) -> Bool {JSON.swift:284 extension JSON: NilLiteralConvertible {JSON.swift:290 extension JSON: BooleanLiteralConvertible {JSON.swift:296 extension JSON: IntegerLiteralConvertible {JSON.swift:302 extension JSON: FloatLiteralConvertible {JSON.swift:308 extension JSON: StringLiteralConvertible {JSON.swift:326 extension JSON: ArrayLiteralConvertible {JSON.swift:327 public init(arrayLiteral elements: JSON...) {JSON.swift:332 extension JSON: DictionaryLiteralConvertible {JSON.swift:333 public init(dictionaryLiteral elements: (String, JSON)...) {JSON.swift:334 var dictionary = [String: JSON](minimumCapacity: elements.count)JSONParser.swift:34 public static func parse(source: String) throws -> JSON {JSONParser.swift:38 public static func parse(source: [UInt8]) throws -> JSON {JSONParser.swift:42 public static func parse(source: [Int8]) throws -> JSON {JSONParser.swift:64 public func parse() throws -> JSON {JSONParser.swift:82 private func parseValue() throws -> JSON {JSONParser.swift:93 case Char(ascii: "n"): return try parseSymbol("null", JSON.NullValue)JSONParser.swift:94 case Char(ascii: "t"): return try parseSymbol("true", JSON.BooleanValue(true))JSONParser.swift:95 case Char(ascii: "f"): return try parseSymbol("false", JSON.BooleanValue(false))JSONParser.swift:120 private func parseSymbol(target: StaticString, @autoclosure _ iftrue: Void -> JSON) throws -> JSON {JSONParser.swift:120 private func parseSymbol(target: StaticString, @autoclosure _ iftrue: Void -> JSON) throws -> JSON {JSONParser.swift:132 private func parseString() throws -> JSON {JSONParser.swift:208 private func parseNumber() throws -> JSON {JSONParser.swift:301 private func parseObject() throws -> JSON {JSONParser.swift:305 var object: [String: JSON] = [:]JSONParser.swift:350 private func parseArray() throws -> JSON {JSONParser.swift:355 var array: [JSON] = []JSONSerializer.swift:28 func serialize(JSONValue: JSON) -> StringJSONSerializer.swift:34 public func serialize(JSONValue: JSON) -> String {JSONSerializer.swift:53 func serializeArray(a: [JSON]) -> String {JSONSerializer.swift:67 func serializeObject(o: [String: JSON]) -> String {JSONSerializer.swift:85 override public func serializeArray(a: [JSON]) -> String {JSONSerializer.swift:103 override public func serializeObject(o: [String: JSON]) -> String {0029 case BooleanValue
JSON.swift:195 a[Int(index)] = .NullValueJSON.swift:251 case .NullValue:JSON.swift:253 case .NullValue: return trueJSON.swift:286 self = .NullValueJSONParser.swift:93 case Char(ascii: "n"): return try parseSymbol("null", JSON.NullValue)JSONSerializer.swift:36 case .NullValue: return "null"(Bool) 0030 case NumberValue
JSON.swift:36 return .BooleanValue(value)JSON.swift:104 case .BooleanValue: return trueJSON.swift:139 case .BooleanValue(let b): return bJSON.swift:256 case .BooleanValue(let lhsValue):JSON.swift:258 case .BooleanValue(let rhsValue): return lhsValue == rhsValueJSON.swift:292 self = .BooleanValue(value)JSONParser.swift:94 case Char(ascii: "t"): return try parseSymbol("true", JSON.BooleanValue(true))JSONParser.swift:95 case Char(ascii: "f"): return try parseSymbol("false", JSON.BooleanValue(false))JSONSerializer.swift:37 case .BooleanValue(let b): return b ? "true" : "false"(Double) 0031 case StringValue
JSON.swift:40 return .NumberValue(value)JSON.swift:111 case .NumberValue: return trueJSON.swift:146 case .NumberValue(let n): return nJSON.swift:266 case .NumberValue(let lhsValue):JSON.swift:268 case .NumberValue(let rhsValue): return lhsValue == rhsValueJSON.swift:298 self = .NumberValue(Double(value))JSON.swift:304 self = .NumberValue(Double(value))JSONParser.swift:298 return .NumberValue(sign * (Double(integer) + fraction) * pow(10, Double(exponent)))JSONSerializer.swift:38 case .NumberValue(let n): return serializeNumber(n)(String) 0032 case ArrayValue
JSON.swift:44 return .StringValue(value)JSON.swift:118 case .StringValue: return trueJSON.swift:167 case .StringValue(let s): return sJSON.swift:261 case .StringValue(let lhsValue):JSON.swift:263 case .StringValue(let rhsValue): return lhsValue == rhsValueJSON.swift:312 self = .StringValue(value)JSON.swift:318 self = .StringValue(value)JSON.swift:322 self = .StringValue(value)JSONParser.swift:176 return .StringValue(s)JSONParser.swift:311 case .StringValue(let key):JSONSerializer.swift:39 case .StringValue(let s): return escapeAsJSONString(s)([JSON]) 0033 case ObjectValue
JSON.swift:48 return .ArrayValue(value)JSON.swift:125 case .ArrayValue: return trueJSON.swift:174 case .ArrayValue(let array): return arrayJSON.swift:204 case .ArrayValue(let a):JSON.swift:189 case .ArrayValue(let a):JSON.swift:197 self = .ArrayValue(a)JSON.swift:271 case .ArrayValue(let lhsValue):JSON.swift:273 case .ArrayValue(let rhsValue): return lhsValue == rhsValueJSON.swift:328 self = .ArrayValue(elements)JSONParser.swift:375 return .ArrayValue(array)JSONSerializer.swift:40 case .ArrayValue(let a): return serializeArray(a)([String: JSON]) 0034 0035 public static func from
JSON.swift:52 return .ObjectValue(value)JSON.swift:132 case .ObjectValue: return trueJSON.swift:181 case .ObjectValue(let dictionary): return dictionaryJSON.swift:223 case .ObjectValue(let o):JSON.swift:214 case .ObjectValue(let o):JSON.swift:217 self = .ObjectValue(o)JSON.swift:276 case .ObjectValue(let lhsValue):JSON.swift:278 case .ObjectValue(let rhsValue): return lhsValue == rhsValueJSON.swift:340 self = .ObjectValue(dictionary)JSONParser.swift:347 return .ObjectValue(object)JSONSerializer.swift:41 case .ObjectValue(let o): return serializeObject(o)(value: Bool) -> JSON { 0036 return .BooleanValue(value) 0037 } 0038 0039 public static func from
JSON.swift:60 jsonArray.append(JSON.from(value))JSON.swift:83 jsonDictionary[key] = JSON.from(value)(value: Double) -> JSON { 0040 return .NumberValue(value) 0041 } 0042 0043 public static func from
JSON.swift:63 jsonArray.append(JSON.from(value))JSON.swift:86 jsonDictionary[key] = JSON.from(value)(value: String) -> JSON { 0044 return .StringValue(value) 0045 } 0046 0047 public static func from
JSON.swift:66 jsonArray.append(JSON.from(value))JSON.swift:89 jsonDictionary[key] = JSON.from(value)(value: [JSON]) -> JSON { 0048 return .ArrayValue(value) 0049 } 0050 0051 public static func from
JSON.swift:75 return JSON.from(jsonArray)(value: [String: JSON]) -> JSON { 0052 return .ObjectValue(value) 0053 } 0054 0055 // TODO: decide what to do if Any is not a JSON value 0056 public static func from
JSON.swift:99 return JSON.from(jsonDictionary)(values: [Any]) -> JSON { 0057 var jsonArray: [JSON] = [] 0058 for value in values { 0059 if let value = value as? Bool { 0060 jsonArray.append(JSON.from(value)) 0061 } 0062 if let value = value as? Double { 0063 jsonArray.append(JSON.from(value)) 0064 } 0065 if let value = value as? String { 0066 jsonArray.append(JSON.from(value)) 0067 } 0068 if let value = value as? [Any] { 0069 jsonArray.append(JSON.from(value)) 0070 } 0071 if let value = value as? [String: Any] { 0072 jsonArray.append(JSON.from(value)) 0073 } 0074 } 0075 return JSON.from(jsonArray) 0076 } 0077 0078 // TODO: decide what to do if Any is not a JSON value 0079 public static func from
JSON.swift:69 jsonArray.append(JSON.from(value))JSON.swift:92 jsonDictionary[key] = JSON.from(value)(value: [String: Any]) -> JSON { 0080 var jsonDictionary: [String: JSON] = [:] 0081 for (key, value) in value { 0082 if let value = value as? Bool { 0083 jsonDictionary[key] = JSON.from(value) 0084 } 0085 if let value = value as? Double { 0086 jsonDictionary[key] = JSON.from(value) 0087 } 0088 if let value = value as? String { 0089 jsonDictionary[key] = JSON.from(value) 0090 } 0091 if let value = value as? [Any] { 0092 jsonDictionary[key] = JSON.from(value) 0093 } 0094 if let value = value as? [String: Any] { 0095 jsonDictionary[key] = JSON.from(value) 0096 } 0097 } 0098 0099 return JSON.from(jsonDictionary) 0100 } 0101 0102 public var isBoolean: Bool { 0103 switch self { 0104 case .BooleanValue: return true 0105 default: return false 0106 } 0107 } 0108 0109 public var isNumber: Bool { 0110 switch self { 0111 case .NumberValue: return true 0112 default: return false 0113 } 0114 } 0115 0116 public var isString: Bool { 0117 switch self { 0118 case .StringValue: return true 0119 default: return false 0120 } 0121 } 0122 0123 public var isArray: Bool { 0124 switch self { 0125 case .ArrayValue: return true 0126 default: return false 0127 } 0128 } 0129 0130 public var isObject: Bool { 0131 switch self { 0132 case .ObjectValue: return true 0133 default: return false 0134 } 0135 } 0136 0137 public var boolValue: Bool? { 0138 switch self { 0139 case .BooleanValue(let b): return b 0140 default: return nil 0141 } 0142 } 0143 0144 public var doubleValue
JSON.swift:72 jsonArray.append(JSON.from(value))JSON.swift:95 jsonDictionary[key] = JSON.from(value): Double? { 0145 switch self { 0146 case .NumberValue(let n): return n 0147 default: return nil 0148 } 0149 } 0150 0151 public var intValue: Int? { 0152 if let v = doubleValue { 0153 return Int(v) 0154 } 0155 return nil 0156 } 0157 0158 public var uintValue: UInt? { 0159 if let v = doubleValue { 0160 return UInt(v) 0161 } 0162 return nil 0163 } 0164 0165 public var stringValue: String? { 0166 switch self { 0167 case .StringValue(let s): return s 0168 default: return nil 0169 } 0170 } 0171 0172 public var arrayValue: [JSON]? { 0173 switch self { 0174 case .ArrayValue(let array): return array 0175 default: return nil 0176 } 0177 } 0178 0179 public var dictionaryValue: [String: JSON]? { 0180 switch self { 0181 case .ObjectValue(let dictionary): return dictionary 0182 default: return nil 0183 } 0184 } 0185 0186 public subscript(index: UInt) -> JSON? { 0187 set { 0188 switch self { 0189 case .ArrayValue(let a): 0190 var a = a 0191 if Int(index) < a.count { 0192 if let json = newValue { 0193 a[Int(index)] = json 0194 } else { 0195 a[Int(index)] = .NullValue 0196 } 0197 self = .ArrayValue(a) 0198 } 0199 default: break 0200 } 0201 } 0202 get { 0203 switch self { 0204 case .ArrayValue(let a): 0205 return Int(index) < a.count ? a[Int(index)] : nil 0206 default: return nil 0207 } 0208 } 0209 } 0210 0211 public subscript(key: String) -> JSON? { 0212 set { 0213 switch self { 0214 case .ObjectValue(let o): 0215 var o = o 0216 o[key] = newValue 0217 self = .ObjectValue(o) 0218 default: break 0219 } 0220 } 0221 get { 0222 switch self { 0223 case .ObjectValue(let o): 0224 return o[key] 0225 default: return nil 0226 } 0227 } 0228 } 0229 0230 public func serialize
JSON.swift:152 if let v = doubleValue {JSON.swift:159 if let v = doubleValue {(serializer: JSONSerializer) -> String { 0231 return serializer.serialize(self) 0232 } 0233 } 0234 0235 extension JSON: CustomStringConvertible { 0236 public var description: String { 0237 return serialize(DefaultJSONSerializer()) 0238 } 0239 } 0240 0241 extension JSON: CustomDebugStringConvertible { 0242 public var debugDescription: String { 0243 return serialize(PrettyJSONSerializer()) 0244 } 0245 } 0246 0247 extension JSON: Equatable {} 0248 0249 public func ==(lhs: JSON, rhs: JSON) -> Bool { 0250 switch lhs { 0251 case .NullValue: 0252 switch rhs { 0253 case .NullValue: return true 0254 default: return false 0255 } 0256 case .BooleanValue(let lhsValue): 0257 switch rhs { 0258 case .BooleanValue(let rhsValue): return lhsValue == rhsValue 0259 default: return false 0260 } 0261 case .StringValue(let lhsValue): 0262 switch rhs { 0263 case .StringValue(let rhsValue): return lhsValue == rhsValue 0264 default: return false 0265 } 0266 case .NumberValue(let lhsValue): 0267 switch rhs { 0268 case .NumberValue(let rhsValue): return lhsValue == rhsValue 0269 default: return false 0270 } 0271 case .ArrayValue(let lhsValue): 0272 switch rhs { 0273 case .ArrayValue(let rhsValue): return lhsValue == rhsValue 0274 default: return false 0275 } 0276 case .ObjectValue(let lhsValue): 0277 switch rhs { 0278 case .ObjectValue(let rhsValue): return lhsValue == rhsValue 0279 default: return false 0280 } 0281 } 0282 } 0283 0284 extension JSON: NilLiteralConvertible { 0285 public init(nilLiteral value: Void) { 0286 self = .NullValue 0287 } 0288 } 0289 0290 extension JSON: BooleanLiteralConvertible { 0291 public init(booleanLiteral value: BooleanLiteralType) { 0292 self = .BooleanValue(value) 0293 } 0294 } 0295 0296 extension JSON: IntegerLiteralConvertible { 0297 public init(integerLiteral value: IntegerLiteralType) { 0298 self = .NumberValue(Double(value)) 0299 } 0300 } 0301 0302 extension JSON: FloatLiteralConvertible { 0303 public init(floatLiteral value: FloatLiteralType) { 0304 self = .NumberValue(Double(value)) 0305 } 0306 } 0307 0308 extension JSON: StringLiteralConvertible { 0309 public typealias UnicodeScalarLiteralType
JSON.swift:237 return serialize(DefaultJSONSerializer())JSON.swift:243 return serialize(PrettyJSONSerializer())JSONSerializer.swift:57 s += a[i].serialize(self)JSONSerializer.swift:72 s += "\(escapeAsJSONString(entry.0)):\(entry.1.serialize(self))"JSONSerializer.swift:92 s += a[i].serialize(self)JSONSerializer.swift:114 s += "\(escapeAsJSONString(key)): \(o[key]!.serialize(self))"= String 0310 0311 public init(unicodeScalarLiteral value: UnicodeScalarLiteralType) { 0312 self = .StringValue(value) 0313 } 0314 0315 public typealias ExtendedGraphemeClusterLiteralType = String 0316 0317 public init(extendedGraphemeClusterLiteral value: ExtendedGraphemeClusterType) { 0318 self = .StringValue(value) 0319 } 0320 0321 public init(stringLiteral value: StringLiteralType) { 0322 self = .StringValue(value) 0323 } 0324 } 0325 0326 extension JSON: ArrayLiteralConvertible { 0327 public init(arrayLiteral elements: JSON...) { 0328 self = .ArrayValue(elements) 0329 } 0330 } 0331 0332 extension JSON: DictionaryLiteralConvertible { 0333 public init(dictionaryLiteral elements: (String, JSON)...) { 0334 var dictionary = [String: JSON](minimumCapacity: elements.count) 0335 0336 for pair in elements { 0337 dictionary[pair.0] = pair.1 0338 } 0339 0340 self = .ObjectValue(dictionary) 0341 } 0342 }
JSON.swift:311 public init(unicodeScalarLiteral value: UnicodeScalarLiteralType) {