// // FromJSON.swift // ObjectMapper // // Created by Tristan Himmelman on 2014-10-09. // // The MIT License (MIT) // // Copyright (c) 2014-2016 Hearst // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. internal final class FromJSON { /// Basic type class func basicType(_ field: inout FieldType, object: FieldType?) { if let value = object { field = value } } /// optional basic type class func optionalBasicType(_ field: inout FieldType?, object: FieldType?) { field = object } // Code targeting the Swift 4.1 compiler and below. #if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0))) /// Implicitly unwrapped optional basic type class func optionalBasicType(_ field: inout FieldType!, object: FieldType?) { field = object } #endif /// Mappable object class func object(_ field: inout N, map: Map) { if map.toObject { field = Mapper(context: map.context).map(JSONObject: map.currentValue, toObject: field) } else if let value: N = Mapper(context: map.context).map(JSONObject: map.currentValue) { field = value } } /// Optional Mappable Object class func optionalObject(_ field: inout N?, map: Map) { if let f = field , map.toObject && map.currentValue != nil { field = Mapper(context: map.context).map(JSONObject: map.currentValue, toObject: f) } else { field = Mapper(context: map.context).map(JSONObject: map.currentValue) } } // Code targeting the Swift 4.1 compiler and below. #if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0))) /// Implicitly unwrapped Optional Mappable Object class func optionalObject(_ field: inout N!, map: Map) { if let f = field , map.toObject && map.currentValue != nil { field = Mapper(context: map.context).map(JSONObject: map.currentValue, toObject: f) } else { field = Mapper(context: map.context).map(JSONObject: map.currentValue) } } #endif /// mappable object array class func objectArray(_ field: inout Array, map: Map) { if let objects = Mapper(context: map.context).mapArray(JSONObject: map.currentValue) { field = objects } } /// optional mappable object array class func optionalObjectArray(_ field: inout Array?, map: Map) { if let objects: Array = Mapper(context: map.context).mapArray(JSONObject: map.currentValue) { field = objects } else { field = nil } } // Code targeting the Swift 4.1 compiler and below. #if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0))) /// Implicitly unwrapped optional mappable object array class func optionalObjectArray(_ field: inout Array!, map: Map) { if let objects: Array = Mapper(context: map.context).mapArray(JSONObject: map.currentValue) { field = objects } else { field = nil } } #endif /// mappable object array class func twoDimensionalObjectArray(_ field: inout Array>, map: Map) { if let objects = Mapper(context: map.context).mapArrayOfArrays(JSONObject: map.currentValue) { field = objects } } /// optional mappable 2 dimentional object array class func optionalTwoDimensionalObjectArray(_ field: inout Array>?, map: Map) { field = Mapper(context: map.context).mapArrayOfArrays(JSONObject: map.currentValue) } // Code targeting the Swift 4.1 compiler and below. #if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0))) /// Implicitly unwrapped optional 2 dimentional mappable object array class func optionalTwoDimensionalObjectArray(_ field: inout Array>!, map: Map) { field = Mapper(context: map.context).mapArrayOfArrays(JSONObject: map.currentValue) } #endif /// Dctionary containing Mappable objects class func objectDictionary(_ field: inout Dictionary, map: Map) { if map.toObject { field = Mapper(context: map.context).mapDictionary(JSONObject: map.currentValue, toDictionary: field) } else { if let objects = Mapper(context: map.context).mapDictionary(JSONObject: map.currentValue) { field = objects } } } /// Optional dictionary containing Mappable objects class func optionalObjectDictionary(_ field: inout Dictionary?, map: Map) { if let f = field , map.toObject && map.currentValue != nil { field = Mapper(context: map.context).mapDictionary(JSONObject: map.currentValue, toDictionary: f) } else { field = Mapper(context: map.context).mapDictionary(JSONObject: map.currentValue) } } // Code targeting the Swift 4.1 compiler and below. #if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0))) /// Implicitly unwrapped Dictionary containing Mappable objects class func optionalObjectDictionary(_ field: inout Dictionary!, map: Map) { if let f = field , map.toObject && map.currentValue != nil { field = Mapper(context: map.context).mapDictionary(JSONObject: map.currentValue, toDictionary: f) } else { field = Mapper(context: map.context).mapDictionary(JSONObject: map.currentValue) } } #endif /// Dictionary containing Array of Mappable objects class func objectDictionaryOfArrays(_ field: inout Dictionary, map: Map) { if let objects = Mapper(context: map.context).mapDictionaryOfArrays(JSONObject: map.currentValue) { field = objects } } /// Optional Dictionary containing Array of Mappable objects class func optionalObjectDictionaryOfArrays(_ field: inout Dictionary?, map: Map) { field = Mapper(context: map.context).mapDictionaryOfArrays(JSONObject: map.currentValue) } // Code targeting the Swift 4.1 compiler and below. #if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0))) /// Implicitly unwrapped Dictionary containing Array of Mappable objects class func optionalObjectDictionaryOfArrays(_ field: inout Dictionary!, map: Map) { field = Mapper(context: map.context).mapDictionaryOfArrays(JSONObject: map.currentValue) } #endif /// mappable object Set class func objectSet(_ field: inout Set, map: Map) { if let objects = Mapper(context: map.context).mapSet(JSONObject: map.currentValue) { field = objects } } /// optional mappable object array class func optionalObjectSet(_ field: inout Set?, map: Map) { field = Mapper(context: map.context).mapSet(JSONObject: map.currentValue) } // Code targeting the Swift 4.1 compiler and below. #if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0))) /// Implicitly unwrapped optional mappable object array class func optionalObjectSet(_ field: inout Set!, map: Map) { field = Mapper(context: map.context).mapSet(JSONObject: map.currentValue) } #endif }