// // KMLightMemberToken.swift // PDF Master // // Created by lizhe on 2023/2/27. // import Cocoa class KMLightMemberToken: NSObject, Codable{ var access_token: String = "" var expires_in: String = "" var refresh_token: String = "" var token_type: String = "" // var scope: String = "" } extension KMLightMemberToken { static public func parseData(data: NSDictionary, completion:(_ data: KMLightMemberToken) -> Void) -> Void { if (data.count != 0) { KMLightMemberCache.cache.saveData(data: data, type: .token) } var result: KMLightMemberToken = KMLightMemberToken() let jsonString: String = DictionaryToJson.dictionary(toJson: data)! let jsonData: Data = jsonString.data(using: .utf8)! let decoder = JSONDecoder() // decoder.dataDecodingStrategy = .base64 // decoder.keyDecodingStrategy = .convertFromSnakeCase //带下划线命名 decoder.nonConformingFloatDecodingStrategy = .convertFromString(positiveInfinity: "+∞", negativeInfinity: "-∞", nan: "NaN") #if DEBUG //MARK: 测试使用 var lightMemberToken = try! decoder.decode(KMLightMemberToken.self, from: jsonData) if (lightMemberToken != nil) { result = lightMemberToken } #else // MARK: 此处try 使用 ? ,如果数据出问题 advertisementModel将无参数, 测试时使用! var lightMemberToken = try? decoder.decode(KMLightMemberToken.self, from: jsonData) if (lightMemberToken != nil) { result = lightMemberToken! } #endif completion(result) } } public class DictionaryToJson: NSObject { public static func dictionary(toJson dic: AnyObject?) -> String? { var jsonData: Data? = nil do { if let dic = dic { jsonData = try JSONSerialization.data(withJSONObject: dic, options: .prettyPrinted) } } catch let parseError { print("(parseError.localizedDescription)") } if let jsonData = jsonData { return String(data: jsonData, encoding: .utf8) } return nil } }