From 613f325e4836f252e8ad3b5da60cd973dd9c7ce4 Mon Sep 17 00:00:00 2001 From: YoloMao Date: Wed, 27 Sep 2023 16:23:07 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20swift=20protobuf=20=E4=B8=AD=20toJson=20?= =?UTF-8?q?=E5=90=8E=20timestamp=20=E5=BA=94=E4=B8=BA=20Number=20=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Services/SwiftProtobuf/SwiftProtobuf.swift | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/Services/SwiftProtobuf/SwiftProtobuf.swift b/Services/SwiftProtobuf/SwiftProtobuf.swift index 20343f6ee..3e2b6fd97 100644 --- a/Services/SwiftProtobuf/SwiftProtobuf.swift +++ b/Services/SwiftProtobuf/SwiftProtobuf.swift @@ -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 {