38 lines
924 B
Swift
38 lines
924 B
Swift
![]() |
//
|
||
|
// Deserialized.swift
|
||
|
// yinmeng-ios
|
||
|
//
|
||
|
// Created by MaiMang 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
|
||
|
}
|
||
|
}
|