From 6b8e6ae970123eeb2eb0af65a8d14564803dbd0e Mon Sep 17 00:00:00 2001 From: cwh Date: Thu, 11 Mar 2021 19:22:56 +0800 Subject: [PATCH 1/3] =?UTF-8?q?fix:=E9=80=82=E9=85=8D=E5=9F=BA=E6=9C=AC?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E7=B1=BB=E5=9E=8B=E6=95=B0=E7=BB=84=E7=9A=84?= =?UTF-8?q?=E8=A7=A3=E7=A0=81=E9=97=AE=E9=A2=98=EF=BC=8C=E5=B8=83=E5=B1=80?= =?UTF-8?q?=E9=99=90=E4=BA=8E=E5=AF=B9=E8=B1=A1;=20=E4=BE=8B=E5=A6=82?= =?UTF-8?q?=EF=BC=9A=20let=20json2:=20String=20=3D=20"[1,=202,=203]"=20let?= =?UTF-8?q?=20list2=20=3D=20[Int].deserialize(from:=20json2)=20as=3F=20[In?= =?UTF-8?q?t]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Source/Deserializer.swift | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Source/Deserializer.swift b/Source/Deserializer.swift index 150c929..ad8bc8d 100644 --- a/Source/Deserializer.swift +++ b/Source/Deserializer.swift @@ -125,7 +125,7 @@ public class JSONDeserializer { } } - /// if the JSON field found by `designatedPath` in `json` is representing a array, such as `[{...}, {...}, {...}]`, + /// if the JSON field found by `designatedPath` in `json` is representing a array, such as `[{...}, {...}, {...}]`, `[1, 2, 3]` , `["1", "2", "3"]` /// this method converts it to a Models array public static func deserializeModelArrayFrom(json: String?, designatedPath: String? = nil) -> [T?]? { guard let _json = json else { @@ -135,7 +135,11 @@ public class JSONDeserializer { let jsonObject = try JSONSerialization.jsonObject(with: _json.data(using: String.Encoding.utf8)!, options: .allowFragments) if let jsonArray = getInnerObject(inside: jsonObject, by: designatedPath) as? [Any] { return jsonArray.map({ (item) -> T? in - return self.deserializeFrom(dict: item as? [String: Any]) + if let dic = item as? [String: Any] { + return self.deserializeFrom(dict: dic) + }else{ + return item as? T + } }) } } catch let error { From 0cd0355f18ca9033d9a937c0bde7489ce38feffa Mon Sep 17 00:00:00 2001 From: cwh Date: Thu, 18 Mar 2021 17:29:34 +0800 Subject: [PATCH 2/3] =?UTF-8?q?fix:=20=E9=80=82=E9=85=8D=E5=9F=BA=E6=9C=AC?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E6=95=B0=E7=BB=84=E7=BC=96=E7=A0=81=E8=BF=94?= =?UTF-8?q?=E5=9B=9Enull=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Source/Serializer.swift | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Source/Serializer.swift b/Source/Serializer.swift index e05f563..aaf85af 100644 --- a/Source/Serializer.swift +++ b/Source/Serializer.swift @@ -57,8 +57,14 @@ public extension HandyJSON { public extension Collection where Iterator.Element: HandyJSON { - func toJSON() -> [[String: Any]?] { - return self.map{ $0.toJSON() } + func toJSON() -> [Any?] { // a array, such as `[{...}, {...}, {...}]`, `[1, 2, 3]` , `["1", "2", "3"]` + return self.map{ + if let _ = $0 as? [String: Any] { + return $0.toJSON() + }else{ + return $0 + } + } } func toJSONString(prettyPrint: Bool = false) -> String? { From f212b2054b06447d4096d7e0ebce46c384004b79 Mon Sep 17 00:00:00 2001 From: cwh Date: Tue, 23 Mar 2021 18:42:20 +0800 Subject: [PATCH 3/3] =?UTF-8?q?fix:=E6=94=AF=E6=8C=81=E5=9F=BA=E6=9C=AC?= =?UTF-8?q?=E6=95=B0=E7=BB=84=E7=B1=BB=E5=9E=8Bjson=E5=BA=8F=E5=88=97?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Source/Serializer.swift | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Source/Serializer.swift b/Source/Serializer.swift index aaf85af..803be5b 100644 --- a/Source/Serializer.swift +++ b/Source/Serializer.swift @@ -59,10 +59,12 @@ public extension Collection where Iterator.Element: HandyJSON { func toJSON() -> [Any?] { // a array, such as `[{...}, {...}, {...}]`, `[1, 2, 3]` , `["1", "2", "3"]` return self.map{ - if let _ = $0 as? [String: Any] { - return $0.toJSON() - }else{ + if let _ = $0 as? String { + return $0 + }else if let _ = $0 as? Int { return $0 + }else{ + return $0.toJSON() } } }