Files
yingmeng-ios-switf/yinmeng-ios/Base/Request/Deserialized.swift
2024-03-05 14:04:09 +08:00

38 lines
924 B
Swift

//
// Deserialized.swift
// yinmeng-ios
//
// Created by yinmeng on 2024/2/22.
//
import Foundation
@_exported import HandyJSON
/// Deserialized json converts to Model or Array.
public struct Deserialized<H> where H: HandyJSON {
public static func toModel(with element: Any?) -> H? {
if let string = element as? String, let model = H.deserialize(from: string) {
return model
}
if let dictionary = element as? Dictionary<String, Any>, let model = H.deserialize(from: dictionary) {
return model
}
if let dictionary = element as? [String : Any], let model = H.deserialize(from: dictionary) {
return model
}
return nil
}
public static func toArray(with element: Any?) -> [H]? {
if let string = element as? String, let array = [H].deserialize(from: string) as? [H] {
return array
}
if let array = [H].deserialize(from: element as? [Any]) as? [H] {
return array
}
return nil
}
}