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.

181 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-2016 Hearst
  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. if components.isEmpty {
  42. return
  43. }
  44. let head = components.first!
  45. if components.count == 1 {
  46. dictionary[String(head)] = value
  47. } else {
  48. var child = dictionary[String(head)] as? [String : Any]
  49. if child == nil {
  50. child = [:]
  51. }
  52. let tail = components.dropFirst()
  53. setValue(value, forKeyPathComponents: tail, dictionary: &child!)
  54. dictionary[String(head)] = child
  55. }
  56. }
  57. internal final class ToJSON {
  58. class func basicType<N>(_ field: N, map: Map) {
  59. if let x = field as Any? , false
  60. || x is NSNumber // Basic types
  61. || x is Bool
  62. || x is Int
  63. || x is Double
  64. || x is Float
  65. || x is String
  66. || x is NSNull
  67. || x is Array<NSNumber> // Arrays
  68. || x is Array<Bool>
  69. || x is Array<Int>
  70. || x is Array<Double>
  71. || x is Array<Float>
  72. || x is Array<String>
  73. || x is Array<Any>
  74. || x is Array<Dictionary<String, Any>>
  75. || x is Dictionary<String, NSNumber> // Dictionaries
  76. || x is Dictionary<String, Bool>
  77. || x is Dictionary<String, Int>
  78. || x is Dictionary<String, Double>
  79. || x is Dictionary<String, Float>
  80. || x is Dictionary<String, String>
  81. || x is Dictionary<String, Any>
  82. {
  83. setValue(x, map: map)
  84. }
  85. }
  86. class func optionalBasicType<N>(_ field: N?, map: Map) {
  87. if let field = field {
  88. basicType(field, map: map)
  89. } else if map.shouldIncludeNilValues {
  90. basicType(NSNull(), map: map) //If BasicType is nil, emit NSNull into the JSON output
  91. }
  92. }
  93. class func object<N: BaseMappable>(_ field: N, map: Map) {
  94. if let result = Mapper(context: map.context, shouldIncludeNilValues: map.shouldIncludeNilValues).toJSON(field) as Any? {
  95. setValue(result, map: map)
  96. }
  97. }
  98. class func optionalObject<N: BaseMappable>(_ field: N?, map: Map) {
  99. if let field = field {
  100. object(field, map: map)
  101. } else if map.shouldIncludeNilValues {
  102. basicType(NSNull(), map: map) //If field is nil, emit NSNull into the JSON output
  103. }
  104. }
  105. class func objectArray<N: BaseMappable>(_ field: Array<N>, map: Map) {
  106. let JSONObjects = Mapper(context: map.context, shouldIncludeNilValues: map.shouldIncludeNilValues).toJSONArray(field)
  107. setValue(JSONObjects, map: map)
  108. }
  109. class func optionalObjectArray<N: BaseMappable>(_ field: Array<N>?, map: Map) {
  110. if let field = field {
  111. objectArray(field, map: map)
  112. }
  113. }
  114. class func twoDimensionalObjectArray<N: BaseMappable>(_ field: Array<Array<N>>, map: Map) {
  115. var array = [[[String: Any]]]()
  116. for innerArray in field {
  117. let JSONObjects = Mapper(context: map.context, shouldIncludeNilValues: map.shouldIncludeNilValues).toJSONArray(innerArray)
  118. array.append(JSONObjects)
  119. }
  120. setValue(array, map: map)
  121. }
  122. class func optionalTwoDimensionalObjectArray<N: BaseMappable>(_ field: Array<Array<N>>?, map: Map) {
  123. if let field = field {
  124. twoDimensionalObjectArray(field, map: map)
  125. }
  126. }
  127. class func objectSet<N: BaseMappable>(_ field: Set<N>, map: Map) {
  128. let JSONObjects = Mapper(context: map.context, shouldIncludeNilValues: map.shouldIncludeNilValues).toJSONSet(field)
  129. setValue(JSONObjects, map: map)
  130. }
  131. class func optionalObjectSet<N: BaseMappable>(_ field: Set<N>?, map: Map) {
  132. if let field = field {
  133. objectSet(field, map: map)
  134. }
  135. }
  136. class func objectDictionary<N: BaseMappable>(_ field: Dictionary<String, N>, map: Map) {
  137. let JSONObjects = Mapper(context: map.context, shouldIncludeNilValues: map.shouldIncludeNilValues).toJSONDictionary(field)
  138. setValue(JSONObjects, map: map)
  139. }
  140. class func optionalObjectDictionary<N: BaseMappable>(_ field: Dictionary<String, N>?, map: Map) {
  141. if let field = field {
  142. objectDictionary(field, map: map)
  143. }
  144. }
  145. class func objectDictionaryOfArrays<N: BaseMappable>(_ field: Dictionary<String, [N]>, map: Map) {
  146. let JSONObjects = Mapper(context: map.context, shouldIncludeNilValues: map.shouldIncludeNilValues).toJSONDictionaryOfArrays(field)
  147. setValue(JSONObjects, map: map)
  148. }
  149. class func optionalObjectDictionaryOfArrays<N: BaseMappable>(_ field: Dictionary<String, [N]>?, map: Map) {
  150. if let field = field {
  151. objectDictionaryOfArrays(field, map: map)
  152. }
  153. }
  154. }