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 { diff --git a/Source/Serializer.swift b/Source/Serializer.swift index e05f563..803be5b 100644 --- a/Source/Serializer.swift +++ b/Source/Serializer.swift @@ -57,8 +57,16 @@ 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 { + return $0 + }else if let _ = $0 as? Int { + return $0 + }else{ + return $0.toJSON() + } + } } func toJSONString(prettyPrint: Bool = false) -> String? {