Mappable.swift 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //
  2. // Mappable.swift
  3. // ObjectMapper
  4. //
  5. // Created by Scott Hoyt on 10/25/15.
  6. // Copyright © 2015 hearst. All rights reserved.
  7. //
  8. import Foundation
  9. public protocol Mappable {
  10. init?(_ map: Map)
  11. mutating func mapping(map: Map)
  12. }
  13. public protocol MappableCluster: Mappable {
  14. static func objectForMapping(map: Map) -> Mappable?
  15. }
  16. public extension Mappable {
  17. /// Initializes object from a JSON String
  18. public init?(JSONString: String) {
  19. if let obj: Self = Mapper().map(JSONString) {
  20. self = obj
  21. } else {
  22. return nil
  23. }
  24. }
  25. /// Initializes object from a JSON Dictionary
  26. public init?(JSON: [String : AnyObject]) {
  27. if let obj: Self = Mapper().map(JSON) {
  28. self = obj
  29. } else {
  30. return nil
  31. }
  32. }
  33. /// Returns the JSON Dictionary for the object
  34. public func toJSON() -> [String: AnyObject] {
  35. return Mapper().toJSON(self)
  36. }
  37. /// Returns the JSON String for the object
  38. public func toJSONString(prettyPrint: Bool = false) -> String? {
  39. return Mapper().toJSONString(self, prettyPrint: prettyPrint)
  40. }
  41. }
  42. public extension Array where Element: Mappable {
  43. /// Initialize Array from a JSON String
  44. public init?(JSONString: String) {
  45. if let obj: [Element] = Mapper().mapArray(JSONString) {
  46. self = obj
  47. } else {
  48. return nil
  49. }
  50. }
  51. /// Initialize Array from a JSON Array
  52. public init?(JSONArray: [[String : AnyObject]]) {
  53. if let obj: [Element] = Mapper().mapArray(JSONArray) {
  54. self = obj
  55. } else {
  56. return nil
  57. }
  58. }
  59. /// Returns the JSON Array
  60. public func toJSON() -> [[String : AnyObject]] {
  61. return Mapper().toJSONArray(self)
  62. }
  63. /// Returns the JSON String for the object
  64. public func toJSONString(prettyPrint: Bool = false) -> String? {
  65. return Mapper().toJSONString(self, prettyPrint: prettyPrint)
  66. }
  67. }
  68. public extension Set where Element: Mappable {
  69. /// Initializes a set from a JSON String
  70. public init?(JSONString: String) {
  71. if let obj: Set<Element> = Mapper().mapSet(JSONString) {
  72. self = obj
  73. } else {
  74. return nil
  75. }
  76. }
  77. /// Initializes a set from JSON
  78. public init?(JSONArray: [[String : AnyObject]]) {
  79. if let obj: Set<Element> = Mapper().mapSet(JSONArray) {
  80. self = obj
  81. } else {
  82. return nil
  83. }
  84. }
  85. /// Returns the JSON Set
  86. public func toJSON() -> [[String : AnyObject]] {
  87. return Mapper().toJSONSet(self)
  88. }
  89. /// Returns the JSON String for the object
  90. public func toJSONString(prettyPrint: Bool = false) -> String? {
  91. return Mapper().toJSONString(self, prettyPrint: prettyPrint)
  92. }
  93. }