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.

136 lines
4.4 KiB

  1. //
  2. // Mappable.swift
  3. // ObjectMapper
  4. //
  5. // Created by Scott Hoyt on 10/25/15.
  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. /// BaseMappable should not be implemented directly. Mappable or StaticMappable should be used instead
  30. public protocol BaseMappable {
  31. /// This function is where all variable mappings should occur. It is executed by Mapper during the mapping (serialization and deserialization) process.
  32. mutating func mapping(map: Map)
  33. }
  34. public protocol Mappable: BaseMappable {
  35. /// This function can be used to validate JSON prior to mapping. Return nil to cancel mapping at this point
  36. init?(map: Map)
  37. }
  38. public protocol StaticMappable: BaseMappable {
  39. /// This is function that can be used to:
  40. /// 1) provide an existing cached object to be used for mapping
  41. /// 2) return an object of another class (which conforms to BaseMappable) to be used for mapping. For instance, you may inspect the JSON to infer the type of object that should be used for any given mapping
  42. static func objectForMapping(map: Map) -> BaseMappable?
  43. }
  44. public extension BaseMappable {
  45. /// Initializes object from a JSON String
  46. init?(JSONString: String, context: MapContext? = nil) {
  47. if let obj: Self = Mapper(context: context).map(JSONString: JSONString) {
  48. self = obj
  49. } else {
  50. return nil
  51. }
  52. }
  53. /// Initializes object from a JSON Dictionary
  54. init?(JSON: [String: Any], context: MapContext? = nil) {
  55. if let obj: Self = Mapper(context: context).map(JSON: JSON) {
  56. self = obj
  57. } else {
  58. return nil
  59. }
  60. }
  61. /// Returns the JSON Dictionary for the object
  62. func toJSON() -> [String: Any] {
  63. return Mapper().toJSON(self)
  64. }
  65. /// Returns the JSON String for the object
  66. func toJSONString(prettyPrint: Bool = false) -> String? {
  67. return Mapper().toJSONString(self, prettyPrint: prettyPrint)
  68. }
  69. }
  70. public extension Array where Element: BaseMappable {
  71. /// Initialize Array from a JSON String
  72. init?(JSONString: String, context: MapContext? = nil) {
  73. if let obj: [Element] = Mapper(context: context).mapArray(JSONString: JSONString) {
  74. self = obj
  75. } else {
  76. return nil
  77. }
  78. }
  79. /// Initialize Array from a JSON Array
  80. init(JSONArray: [[String: Any]], context: MapContext? = nil) {
  81. let obj: [Element] = Mapper(context: context).mapArray(JSONArray: JSONArray)
  82. self = obj
  83. }
  84. /// Returns the JSON Array
  85. func toJSON() -> [[String: Any]] {
  86. return Mapper().toJSONArray(self)
  87. }
  88. /// Returns the JSON String for the object
  89. func toJSONString(prettyPrint: Bool = false) -> String? {
  90. return Mapper().toJSONString(self, prettyPrint: prettyPrint)
  91. }
  92. }
  93. public extension Set where Element: BaseMappable {
  94. /// Initializes a set from a JSON String
  95. init?(JSONString: String, context: MapContext? = nil) {
  96. if let obj: Set<Element> = Mapper(context: context).mapSet(JSONString: JSONString) {
  97. self = obj
  98. } else {
  99. return nil
  100. }
  101. }
  102. /// Initializes a set from JSON
  103. init?(JSONArray: [[String: Any]], context: MapContext? = nil) {
  104. guard let obj = Mapper(context: context).mapSet(JSONArray: JSONArray) as Set<Element>? else {
  105. return nil
  106. }
  107. self = obj
  108. }
  109. /// Returns the JSON Set
  110. func toJSON() -> [[String: Any]] {
  111. return Mapper().toJSONSet(self)
  112. }
  113. /// Returns the JSON String for the object
  114. func toJSONString(prettyPrint: Bool = false) -> String? {
  115. return Mapper().toJSONString(self, prettyPrint: prettyPrint)
  116. }
  117. }