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.

139 lines
4.4 KiB

2 years ago
2 years ago
2 years ago
  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 Mappable {
  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. }
  62. public extension BaseMappable {
  63. /// Returns the JSON Dictionary for the object
  64. func toJSON() -> [String: Any] {
  65. return Mapper().toJSON(self)
  66. }
  67. /// Returns the JSON String for the object
  68. func toJSONString(prettyPrint: Bool = false) -> String? {
  69. return Mapper().toJSONString(self, prettyPrint: prettyPrint)
  70. }
  71. }
  72. public extension Array where Element: BaseMappable {
  73. /// Initialize Array from a JSON String
  74. init?(JSONString: String, context: MapContext? = nil) {
  75. if let obj: [Element] = Mapper(context: context).mapArray(JSONString: JSONString) {
  76. self = obj
  77. } else {
  78. return nil
  79. }
  80. }
  81. /// Initialize Array from a JSON Array
  82. init(JSONArray: [[String: Any]], context: MapContext? = nil) {
  83. let obj: [Element] = Mapper(context: context).mapArray(JSONArray: JSONArray)
  84. self = obj
  85. }
  86. /// Returns the JSON Array
  87. func toJSON() -> [[String: Any]] {
  88. return Mapper().toJSONArray(self)
  89. }
  90. /// Returns the JSON String for the object
  91. func toJSONString(prettyPrint: Bool = false) -> String? {
  92. return Mapper().toJSONString(self, prettyPrint: prettyPrint)
  93. }
  94. }
  95. public extension Set where Element: BaseMappable {
  96. /// Initializes a set from a JSON String
  97. init?(JSONString: String, context: MapContext? = nil) {
  98. if let obj: Set<Element> = Mapper(context: context).mapSet(JSONString: JSONString) {
  99. self = obj
  100. } else {
  101. return nil
  102. }
  103. }
  104. /// Initializes a set from JSON
  105. init?(JSONArray: [[String: Any]], context: MapContext? = nil) {
  106. guard let obj = Mapper(context: context).mapSet(JSONArray: JSONArray) as Set<Element>? else {
  107. return nil
  108. }
  109. self = obj
  110. }
  111. /// Returns the JSON Set
  112. func toJSON() -> [[String: Any]] {
  113. return Mapper().toJSONSet(self)
  114. }
  115. /// Returns the JSON String for the object
  116. func toJSONString(prettyPrint: Bool = false) -> String? {
  117. return Mapper().toJSONString(self, prettyPrint: prettyPrint)
  118. }
  119. }