You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

202 lines
7.8 KiB

  1. //
  2. // FromJSON.swift
  3. // ObjectMapper
  4. //
  5. // Created by Tristan Himmelman on 2014-10-09.
  6. //
  7. // The MIT License (MIT)
  8. //
  9. // Copyright (c) 2014-2016 Tristan Himmelman
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining a copy
  12. // of this software and associated documentation files (the "Software"), to deal
  13. // in the Software without restriction, including without limitation the rights
  14. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. // copies of the Software, and to permit persons to whom the Software is
  16. // furnished to do so, subject to the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be included in
  19. // all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. // THE SOFTWARE.
  28. internal final class FromJSON {
  29. /// Basic type
  30. class func basicType<FieldType>(_ field: inout FieldType, object: FieldType?) {
  31. if let value = object {
  32. field = value
  33. }
  34. }
  35. /// optional basic type
  36. class func optionalBasicType<FieldType>(_ field: inout FieldType?, object: FieldType?) {
  37. field = object
  38. }
  39. // Code targeting the Swift 4.1 compiler and below.
  40. #if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0)))
  41. /// Implicitly unwrapped optional basic type
  42. class func optionalBasicType<FieldType>(_ field: inout FieldType!, object: FieldType?) {
  43. field = object
  44. }
  45. #endif
  46. /// Mappable object
  47. class func object<N: BaseMappable>(_ field: inout N, map: Map) {
  48. if map.toObject {
  49. field = Mapper(context: map.context).map(JSONObject: map.currentValue, toObject: field)
  50. } else if let value: N = Mapper(context: map.context).map(JSONObject: map.currentValue) {
  51. field = value
  52. }
  53. }
  54. /// Optional Mappable Object
  55. class func optionalObject<N: BaseMappable>(_ field: inout N?, map: Map) {
  56. if let f = field , map.toObject && map.currentValue != nil {
  57. field = Mapper(context: map.context).map(JSONObject: map.currentValue, toObject: f)
  58. } else {
  59. field = Mapper(context: map.context).map(JSONObject: map.currentValue)
  60. }
  61. }
  62. // Code targeting the Swift 4.1 compiler and below.
  63. #if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0)))
  64. /// Implicitly unwrapped Optional Mappable Object
  65. class func optionalObject<N: BaseMappable>(_ field: inout N!, map: Map) {
  66. if let f = field , map.toObject && map.currentValue != nil {
  67. field = Mapper(context: map.context).map(JSONObject: map.currentValue, toObject: f)
  68. } else {
  69. field = Mapper(context: map.context).map(JSONObject: map.currentValue)
  70. }
  71. }
  72. #endif
  73. /// mappable object array
  74. class func objectArray<N: BaseMappable>(_ field: inout Array<N>, map: Map) {
  75. if let objects = Mapper<N>(context: map.context).mapArray(JSONObject: map.currentValue) {
  76. field = objects
  77. }
  78. }
  79. /// optional mappable object array
  80. class func optionalObjectArray<N: BaseMappable>(_ field: inout Array<N>?, map: Map) {
  81. if let objects: Array<N> = Mapper(context: map.context).mapArray(JSONObject: map.currentValue) {
  82. field = objects
  83. } else {
  84. field = nil
  85. }
  86. }
  87. // Code targeting the Swift 4.1 compiler and below.
  88. #if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0)))
  89. /// Implicitly unwrapped optional mappable object array
  90. class func optionalObjectArray<N: BaseMappable>(_ field: inout Array<N>!, map: Map) {
  91. if let objects: Array<N> = Mapper(context: map.context).mapArray(JSONObject: map.currentValue) {
  92. field = objects
  93. } else {
  94. field = nil
  95. }
  96. }
  97. #endif
  98. /// mappable object array
  99. class func twoDimensionalObjectArray<N: BaseMappable>(_ field: inout Array<Array<N>>, map: Map) {
  100. if let objects = Mapper<N>(context: map.context).mapArrayOfArrays(JSONObject: map.currentValue) {
  101. field = objects
  102. }
  103. }
  104. /// optional mappable 2 dimentional object array
  105. class func optionalTwoDimensionalObjectArray<N: BaseMappable>(_ field: inout Array<Array<N>>?, map: Map) {
  106. field = Mapper(context: map.context).mapArrayOfArrays(JSONObject: map.currentValue)
  107. }
  108. // Code targeting the Swift 4.1 compiler and below.
  109. #if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0)))
  110. /// Implicitly unwrapped optional 2 dimentional mappable object array
  111. class func optionalTwoDimensionalObjectArray<N: BaseMappable>(_ field: inout Array<Array<N>>!, map: Map) {
  112. field = Mapper(context: map.context).mapArrayOfArrays(JSONObject: map.currentValue)
  113. }
  114. #endif
  115. /// Dctionary containing Mappable objects
  116. class func objectDictionary<N: BaseMappable>(_ field: inout Dictionary<String, N>, map: Map) {
  117. if map.toObject {
  118. field = Mapper<N>(context: map.context).mapDictionary(JSONObject: map.currentValue, toDictionary: field)
  119. } else {
  120. if let objects = Mapper<N>(context: map.context).mapDictionary(JSONObject: map.currentValue) {
  121. field = objects
  122. }
  123. }
  124. }
  125. /// Optional dictionary containing Mappable objects
  126. class func optionalObjectDictionary<N: BaseMappable>(_ field: inout Dictionary<String, N>?, map: Map) {
  127. if let f = field , map.toObject && map.currentValue != nil {
  128. field = Mapper(context: map.context).mapDictionary(JSONObject: map.currentValue, toDictionary: f)
  129. } else {
  130. field = Mapper(context: map.context).mapDictionary(JSONObject: map.currentValue)
  131. }
  132. }
  133. // Code targeting the Swift 4.1 compiler and below.
  134. #if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0)))
  135. /// Implicitly unwrapped Dictionary containing Mappable objects
  136. class func optionalObjectDictionary<N: BaseMappable>(_ field: inout Dictionary<String, N>!, map: Map) {
  137. if let f = field , map.toObject && map.currentValue != nil {
  138. field = Mapper(context: map.context).mapDictionary(JSONObject: map.currentValue, toDictionary: f)
  139. } else {
  140. field = Mapper(context: map.context).mapDictionary(JSONObject: map.currentValue)
  141. }
  142. }
  143. #endif
  144. /// Dictionary containing Array of Mappable objects
  145. class func objectDictionaryOfArrays<N: BaseMappable>(_ field: inout Dictionary<String, [N]>, map: Map) {
  146. if let objects = Mapper<N>(context: map.context).mapDictionaryOfArrays(JSONObject: map.currentValue) {
  147. field = objects
  148. }
  149. }
  150. /// Optional Dictionary containing Array of Mappable objects
  151. class func optionalObjectDictionaryOfArrays<N: BaseMappable>(_ field: inout Dictionary<String, [N]>?, map: Map) {
  152. field = Mapper<N>(context: map.context).mapDictionaryOfArrays(JSONObject: map.currentValue)
  153. }
  154. // Code targeting the Swift 4.1 compiler and below.
  155. #if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0)))
  156. /// Implicitly unwrapped Dictionary containing Array of Mappable objects
  157. class func optionalObjectDictionaryOfArrays<N: BaseMappable>(_ field: inout Dictionary<String, [N]>!, map: Map) {
  158. field = Mapper<N>(context: map.context).mapDictionaryOfArrays(JSONObject: map.currentValue)
  159. }
  160. #endif
  161. /// mappable object Set
  162. class func objectSet<N: BaseMappable>(_ field: inout Set<N>, map: Map) {
  163. if let objects = Mapper<N>(context: map.context).mapSet(JSONObject: map.currentValue) {
  164. field = objects
  165. }
  166. }
  167. /// optional mappable object array
  168. class func optionalObjectSet<N: BaseMappable>(_ field: inout Set<N>?, map: Map) {
  169. field = Mapper(context: map.context).mapSet(JSONObject: map.currentValue)
  170. }
  171. // Code targeting the Swift 4.1 compiler and below.
  172. #if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0)))
  173. /// Implicitly unwrapped optional mappable object array
  174. class func optionalObjectSet<N: BaseMappable>(_ field: inout Set<N>!, map: Map) {
  175. field = Mapper(context: map.context).mapSet(JSONObject: map.currentValue)
  176. }
  177. #endif
  178. }