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.

177 lines
5.9 KiB

  1. //
  2. // ToJSON.swift
  3. // ObjectMapper
  4. //
  5. // Created by Tristan Himmelman on 2014-10-13.
  6. //
  7. // The MIT License (MIT)
  8. //
  9. // Copyright (c) 2014-2018 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. import Foundation
  29. private func setValue(_ value: Any, map: Map) {
  30. setValue(value, key: map.currentKey!, checkForNestedKeys: map.keyIsNested, delimiter: map.nestedKeyDelimiter, dictionary: &map.JSON)
  31. }
  32. private func setValue(_ value: Any, key: String, checkForNestedKeys: Bool, delimiter: String, dictionary: inout [String : Any]) {
  33. if checkForNestedKeys {
  34. let keyComponents = ArraySlice(key.components(separatedBy: delimiter).filter { !$0.isEmpty }.map { $0 })
  35. setValue(value, forKeyPathComponents: keyComponents, dictionary: &dictionary)
  36. } else {
  37. dictionary[key] = value
  38. }
  39. }
  40. private func setValue(_ value: Any, forKeyPathComponents components: ArraySlice<String>, dictionary: inout [String : Any]) {
  41. guard let head = components.first else {
  42. return
  43. }
  44. let headAsString = String(head)
  45. if components.count == 1 {
  46. dictionary[headAsString] = value
  47. } else {
  48. var child = dictionary[headAsString] as? [String : Any] ?? [:]
  49. let tail = components.dropFirst()
  50. setValue(value, forKeyPathComponents: tail, dictionary: &child)
  51. dictionary[headAsString] = child
  52. }
  53. }
  54. internal final class ToJSON {
  55. class func basicType<N>(_ field: N, map: Map) {
  56. if let x = field as Any? , false
  57. || x is NSNumber // Basic types
  58. || x is Bool
  59. || x is Int
  60. || x is Double
  61. || x is Float
  62. || x is String
  63. || x is NSNull
  64. || x is Array<NSNumber> // Arrays
  65. || x is Array<Bool>
  66. || x is Array<Int>
  67. || x is Array<Double>
  68. || x is Array<Float>
  69. || x is Array<String>
  70. || x is Array<Any>
  71. || x is Array<Dictionary<String, Any>>
  72. || x is Dictionary<String, NSNumber> // Dictionaries
  73. || x is Dictionary<String, Bool>
  74. || x is Dictionary<String, Int>
  75. || x is Dictionary<String, Double>
  76. || x is Dictionary<String, Float>
  77. || x is Dictionary<String, String>
  78. || x is Dictionary<String, Any>
  79. {
  80. setValue(x, map: map)
  81. }
  82. }
  83. class func optionalBasicType<N>(_ field: N?, map: Map) {
  84. if let field = field {
  85. basicType(field, map: map)
  86. } else if map.shouldIncludeNilValues {
  87. basicType(NSNull(), map: map) //If BasicType is nil, emit NSNull into the JSON output
  88. }
  89. }
  90. class func object<N: BaseMappable>(_ field: N, map: Map) {
  91. if let result = Mapper(context: map.context, shouldIncludeNilValues: map.shouldIncludeNilValues).toJSON(field) as Any? {
  92. setValue(result, map: map)
  93. }
  94. }
  95. class func optionalObject<N: BaseMappable>(_ field: N?, map: Map) {
  96. if let field = field {
  97. object(field, map: map)
  98. } else if map.shouldIncludeNilValues {
  99. basicType(NSNull(), map: map) //If field is nil, emit NSNull into the JSON output
  100. }
  101. }
  102. class func objectArray<N: BaseMappable>(_ field: Array<N>, map: Map) {
  103. let JSONObjects = Mapper(context: map.context, shouldIncludeNilValues: map.shouldIncludeNilValues).toJSONArray(field)
  104. setValue(JSONObjects, map: map)
  105. }
  106. class func optionalObjectArray<N: BaseMappable>(_ field: Array<N>?, map: Map) {
  107. if let field = field {
  108. objectArray(field, map: map)
  109. }
  110. }
  111. class func twoDimensionalObjectArray<N: BaseMappable>(_ field: Array<Array<N>>, map: Map) {
  112. var array = [[[String: Any]]]()
  113. for innerArray in field {
  114. let JSONObjects = Mapper(context: map.context, shouldIncludeNilValues: map.shouldIncludeNilValues).toJSONArray(innerArray)
  115. array.append(JSONObjects)
  116. }
  117. setValue(array, map: map)
  118. }
  119. class func optionalTwoDimensionalObjectArray<N: BaseMappable>(_ field: Array<Array<N>>?, map: Map) {
  120. if let field = field {
  121. twoDimensionalObjectArray(field, map: map)
  122. }
  123. }
  124. class func objectSet<N: BaseMappable>(_ field: Set<N>, map: Map) {
  125. let JSONObjects = Mapper(context: map.context, shouldIncludeNilValues: map.shouldIncludeNilValues).toJSONSet(field)
  126. setValue(JSONObjects, map: map)
  127. }
  128. class func optionalObjectSet<N: BaseMappable>(_ field: Set<N>?, map: Map) {
  129. if let field = field {
  130. objectSet(field, map: map)
  131. }
  132. }
  133. class func objectDictionary<N: BaseMappable>(_ field: Dictionary<String, N>, map: Map) {
  134. let JSONObjects = Mapper(context: map.context, shouldIncludeNilValues: map.shouldIncludeNilValues).toJSONDictionary(field)
  135. setValue(JSONObjects, map: map)
  136. }
  137. class func optionalObjectDictionary<N: BaseMappable>(_ field: Dictionary<String, N>?, map: Map) {
  138. if let field = field {
  139. objectDictionary(field, map: map)
  140. }
  141. }
  142. class func objectDictionaryOfArrays<N: BaseMappable>(_ field: Dictionary<String, [N]>, map: Map) {
  143. let JSONObjects = Mapper(context: map.context, shouldIncludeNilValues: map.shouldIncludeNilValues).toJSONDictionaryOfArrays(field)
  144. setValue(JSONObjects, map: map)
  145. }
  146. class func optionalObjectDictionaryOfArrays<N: BaseMappable>(_ field: Dictionary<String, [N]>?, map: Map) {
  147. if let field = field {
  148. objectDictionaryOfArrays(field, map: map)
  149. }
  150. }
  151. }