diff --git a/Services/SwiftProtobuf/SwiftProtobuf.swift b/Services/SwiftProtobuf/SwiftProtobuf.swift index 9ead8a891..3e2b6fd97 100644 --- a/Services/SwiftProtobuf/SwiftProtobuf.swift +++ b/Services/SwiftProtobuf/SwiftProtobuf.swift @@ -88,7 +88,7 @@ public class SwiftProtobufWrapper: NSObject { dto.referralPage = jsonObject["referralPage"] as? String ?? "" dto.protocolType = jsonObject["protocolType"] as? String ?? "" dto.eventName = jsonObject["eventName"] as? String ?? "" - dto.timezoneOffset = (jsonObject["timezoneOffset"] as? NSNumber)?.int32Value ?? 0 + dto.timezoneOffset = jsonObject["timezoneOffset"] as? String ?? "" return SwiftProtobufWrapper(dto) } @@ -97,7 +97,15 @@ public class SwiftProtobufWrapper: NSObject { public func toJsonObject() -> [String: AnyObject]? { do { let data = try unbox.jsonUTF8Data() - let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: AnyObject] + guard var json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: AnyObject] else { + return nil + } + // Int64 类型在 proto3 JSON Mapping 默认为 String 类型,apple/swift-protobuf 有将 Int64 转为 Number 的提案,但 + // 截止 SwiftProtobuf 1.23.0,该提案尚未合并到 1_x_release_branch 分支上,仅合并入 main 分支 + // 见:https://github.com/growingio/growingio-sdk-ios-autotracker/pull/248#issuecomment-1515913736 + if let timestamp = json["timestamp"] as? String, let tm = Int64(timestamp) { + json["timestamp"] = tm as AnyObject + } return json } catch { return nil @@ -161,10 +169,16 @@ public extension SwiftProtobufWrapper { var array = [[String: AnyObject]]() for dto in list.values { let jsonData = try dto.jsonUTF8Data() - let dic = try JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers) as? [String: AnyObject] - if let dic = dic { - array.append(dic) + guard var json = try JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers) as? [String: AnyObject] else { + continue + } + // Int64 类型在 proto3 JSON Mapping 默认为 String 类型,apple/swift-protobuf 有将 Int64 转为 Number 的提案,但 + // 截止 SwiftProtobuf 1.23.0,该提案尚未合并到 1_x_release_branch 分支上,仅合并入 main 分支 + // 见:https://github.com/growingio/growingio-sdk-ios-autotracker/pull/248#issuecomment-1515913736 + if let timestamp = json["timestamp"] as? String, let tm = Int64(timestamp) { + json["timestamp"] = tm as AnyObject } + array.append(json) } return array } catch { @@ -201,7 +215,7 @@ public extension GrowingBaseEvent { dto.longitude = longitude dto.sdkVersion = sdkVersion dto.userKey = userKey ?? "" - dto.timezoneOffset = Int32(timezoneOffset) + dto.timezoneOffset = timezoneOffset dto.eventType = SwiftProtobufWrapper.eventType(eventType) dto.idfa = idfa()