123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- import Foundation
- public protocol BaseMappable {
-
- mutating func mapping(map: Map)
- }
- public protocol Mappable: BaseMappable {
-
- init?(map: Map)
- }
- public protocol StaticMappable: BaseMappable {
-
-
-
- static func objectForMapping(map: Map) -> BaseMappable?
- }
- public extension Mappable {
-
-
- init?(JSONString: String, context: MapContext? = nil) {
- if let obj: Self = Mapper(context: context).map(JSONString: JSONString) {
- self = obj
- } else {
- return nil
- }
- }
-
-
- init?(JSON: [String: Any], context: MapContext? = nil) {
- if let obj: Self = Mapper(context: context).map(JSON: JSON) {
- self = obj
- } else {
- return nil
- }
- }
- }
- public extension BaseMappable {
-
- func toJSON() -> [String: Any] {
- return Mapper().toJSON(self)
- }
-
- func toJSONString(prettyPrint: Bool = false) -> String? {
- return Mapper().toJSONString(self, prettyPrint: prettyPrint)
- }
- }
- public extension Array where Element: BaseMappable {
-
-
- init?(JSONString: String, context: MapContext? = nil) {
- if let obj: [Element] = Mapper(context: context).mapArray(JSONString: JSONString) {
- self = obj
- } else {
- return nil
- }
- }
-
-
- init(JSONArray: [[String: Any]], context: MapContext? = nil) {
- let obj: [Element] = Mapper(context: context).mapArray(JSONArray: JSONArray)
- self = obj
- }
-
-
- func toJSON() -> [[String: Any]] {
- return Mapper().toJSONArray(self)
- }
-
-
- func toJSONString(prettyPrint: Bool = false) -> String? {
- return Mapper().toJSONString(self, prettyPrint: prettyPrint)
- }
- }
- public extension Set where Element: BaseMappable {
-
-
- init?(JSONString: String, context: MapContext? = nil) {
- if let obj: Set<Element> = Mapper(context: context).mapSet(JSONString: JSONString) {
- self = obj
- } else {
- return nil
- }
- }
-
-
- init?(JSONArray: [[String: Any]], context: MapContext? = nil) {
- guard let obj = Mapper(context: context).mapSet(JSONArray: JSONArray) as Set<Element>? else {
- return nil
- }
- self = obj
- }
-
-
- func toJSON() -> [[String: Any]] {
- return Mapper().toJSONSet(self)
- }
-
-
- func toJSONString(prettyPrint: Bool = false) -> String? {
- return Mapper().toJSONString(self, prettyPrint: prettyPrint)
- }
- }
|