JSONModel
public protocol JSONModel : AnyModel, _ExtendCustomModelType
通用JSON模型协议,默认未实现KeyMappable,使用方式同HandyJSON(不推荐,直接读写内存模式,不稳定也不安全); JSONModel可实现KeyMappable,并选择以下模式使用,推荐方式
KeyMappable模式一:MappedValue模式
- 支持JSONModel类型字段,使用方式:@MappedValue
- 支持多字段映射,使用方式:@MappedValue(“name1”, “name2”)
- 支持Any类型字段,使用方式:@MappedValue
- 未标记MappedValue的字段将自动忽略,也可代码忽略:@MappedValue(ignored: true)
KeyMappable模式二:MappedValueMacro模式(需引入FWPluginMacros子模块)
- 标记class或struct为自动映射存储属性宏,使用方式:@MappedValueMacro
- 可自定义字段映射规则,使用方式:@MappedValue(“name1”, “name2”)
- 以下划线开头或结尾的字段将自动忽略,也可代码忽略:@MappedValue(ignored: true)
KeyMappable模式三:自定义模式
- 需完整实现JSONModel协议的mappingValue(_:forKey:)协议方法
-
deserialize(from:
Extension methoddesignatedPath: ) Finds the internal dictionary in
dict
as thedesignatedPath
specified, and converts it to a ModeldesignatedPath
is a string likeresult.data.orderInfo
, which each element split by.
represents key of each layerDeclaration
Swift
public static func deserialize(from dict: [AnyHashable : Any]?, designatedPath: String? = nil) -> Self?
-
deserialize(from:
Extension methoddesignatedPath: ) Finds the internal JSON field in
json
as thedesignatedPath
specified, and converts it to a ModeldesignatedPath
is a string likeresult.data.orderInfo
, which each element split by.
represents key of each layerDeclaration
Swift
public static func deserialize(from json: String?, designatedPath: String? = nil) -> Self?
-
deserialize(from:
Extension methoddesignatedPath: ) Finds the internal JSON field in
array
as thedesignatedPath
specified, and converts it to a ModeldesignatedPath
is a string likeresult.data.orderInfo
, which each element split by.
represents key of each layerDeclaration
Swift
public static func deserialize(from array: [Any]?, designatedPath: String? = nil) -> Self?
-
deserializeAny(from:
Extension methoddesignatedPath: ) Finds the internal JSON field in
object
as thedesignatedPath
specified, and converts it to a ModeldesignatedPath
is a string likeresult.data.orderInfo
, which each element split by.
represents key of each layerDeclaration
Swift
public static func deserializeAny(from object: Any?, designatedPath: String? = nil) -> Self?
-
safeDeserialize(from:
Extension methoddesignatedPath: ) Finds the internal dictionary in
dict
as thedesignatedPath
specified, and safe converts it to a ModeldesignatedPath
is a string likeresult.data.orderInfo
, which each element split by.
represents key of each layerDeclaration
Swift
public static func safeDeserialize(from dict: [AnyHashable : Any]?, designatedPath: String? = nil) -> Self
-
safeDeserialize(from:
Extension methoddesignatedPath: ) Finds the internal JSON field in
json
as thedesignatedPath
specified, and safe converts it to a ModeldesignatedPath
is a string likeresult.data.orderInfo
, which each element split by.
represents key of each layerDeclaration
Swift
public static func safeDeserialize(from string: String?, designatedPath: String? = nil) -> Self
-
safeDeserialize(from:
Extension methoddesignatedPath: ) Finds the internal JSON field in
array
as thedesignatedPath
specified, and safe converts it to a ModeldesignatedPath
is a string likeresult.data.orderInfo
, which each element split by.
represents key of each layerDeclaration
Swift
public static func safeDeserialize(from array: [Any]?, designatedPath: String? = nil) -> Self
-
safeDeserializeAny(from:
Extension methoddesignatedPath: ) Finds the internal JSON field in
object
as thedesignatedPath
specified, and safe converts it to a ModeldesignatedPath
is a string likeresult.data.orderInfo
, which each element split by.
represents key of each layerDeclaration
Swift
public static func safeDeserializeAny(from object: Any?, designatedPath: String? = nil) -> Self
-
merge(from:
Extension methoddesignatedPath: ) Finds the internal dictionary in
dict
as thedesignatedPath
specified, and use it to reassign an exist modeldesignatedPath
is a string likeresult.data.orderInfo
, which each element split by.
represents key of each layer, or nilDeclaration
Swift
public mutating func merge(from dict: [AnyHashable : Any]?, designatedPath: String? = nil)
-
merge(from:
Extension methoddesignatedPath: ) Finds the internal JSON field in
json
as thedesignatedPath
specified, and use it to reassign an exist modeldesignatedPath
is a string likeresult.data.orderInfo
, which each element split by.
represents key of each layer, or nilDeclaration
Swift
public mutating func merge(from string: String?, designatedPath: String? = nil)
-
merge(from:
Extension methoddesignatedPath: ) Finds the internal JSON field in
array
as thedesignatedPath
specified, and use it to reassign an exist modeldesignatedPath
is a string likeresult.data.orderInfo
, which each element split by.
represents key of each layer, or nilDeclaration
Swift
public mutating func merge(from array: [Any]?, designatedPath: String? = nil)
-
mergeAny(from:
Extension methoddesignatedPath: ) Finds the internal JSON field in
object
as thedesignatedPath
specified, and use it to reassign an exist modeldesignatedPath
is a string likeresult.data.orderInfo
, which each element split by.
represents key of each layer, or nilDeclaration
Swift
public mutating func mergeAny(from object: Any?, designatedPath: String? = nil)
-
toJSON()
Extension methodUndocumented
Declaration
Swift
public func toJSON() -> [String : Any]?
-
toJSONString(prettyPrint:
Extension method) Undocumented
Declaration
Swift
public func toJSONString(prettyPrint: Bool = false) -> String?
-
hashString
Extension methodUndocumented
Declaration
Swift
public var hashString: String { get }