0001
0023 import Foundation
0024
0025 public class Model| Model.swift:88 | public class func modelForJSON(json: JSON) -> Model { |
| Model.swift:89 | return Model(json: json) |
| Model.swift:92 | public class func modelForStrictJSON(json: JSON) -> Model? { |
| Model.swift:93 | return Model(strictJSON: json) |
: NSObject, NSCoding {
0026
0027 private var JSONMappings| Model.swift:53 | JSONMappings.forEach { $0.fromJSON(json) } |
| Model.swift:54 | JSONMappings.forEach { $0.initPostProcess() } |
| Model.swift:64 | for map in JSONMappings { |
| Model.swift:85 | JSONMappings.forEach { $0.initPostProcess() } |
| Model.swift:115 | for map in JSONMappings { |
| Model.swift:146 | JSONMappings.forEach { $0.decode(aDecoder) } |
| Model.swift:150 | JSONMappings.forEach { $0.encode(aCoder) } |
: [PropertyDescription] {
0028 let mirror = Mirror(reflecting: self)
0029 return inspect(mirror)
0030 }
0031
0032 private func inspect| Model.swift:29 | return inspect(mirror) |
| Model.swift:37 | mappings += inspect(parentMirror, mappings) |
(mirror: Mirror, _ mappings: [PropertyDescription] = []) -> [PropertyDescription] {
0033
0034 var mappings = mappings
0035
0036 if let parentMirror = mirror.superclassMirror() where parentMirror.children.count > 0 {
0037 mappings += inspect(parentMirror, mappings)
0038 }
0039
0040 mappings += mirror.children.flatMap { $0.value as? PropertyDescription }
0041
0042 return mappings
0043 }
0044
0045
0047 public required override init| Model.swift:145 | self.init() |
| Model.swift:159 | return self.dynamicType.init() |
() {
0048 super.init()
0049 }
0050
0051 public required init| Model.swift:89 | return Model(json: json) |
| Model.swift:157 | return self.dynamicType.init(json: json) |
(json: JSON) {
0052 super.init()
0053 JSONMappings.forEach { $0.fromJSON(json) }
0054 JSONMappings.forEach { $0.initPostProcess() }
0055 }
0056
0057 public required init| JSONTransformable.swift:43 | return Self.init(strictJSON: json) |
| Model.swift:93 | return Model(strictJSON: json) |
?(strictJSON: JSON) {
0058 super.init()
0059
0060 var valid = true
0061
0062 var debugString = "Initializing object of type: \(self.dynamicType)"
0063
0064 for map in JSONMappings {
0065 let validObject = map.fromJSON(strictJSON)
0066
0067 if map.required && !validObject {
0068 valid = false
0069
0070 #if DEBUG
0071 debugString += "\n\tProperty failed for key: \(map.key), type: \(map.type)"
0072 #else
0073 break
0074 #endif
0075 }
0076 }
0077
0078 guard valid else {
0079 #if DEBUG
0080 print(debugString)
0081 #endif
0082 return nil
0083 }
0084
0085 JSONMappings.forEach { $0.initPostProcess() }
0086 }
0087
0088 public class func modelForJSON(json: JSON) -> Model {
0089 return Model(json: json)
0090 }
0091
0092 public class func modelForStrictJSON(json: JSON) -> Model? {
0093 return Model(strictJSON: json)
0094 }
0095
0096
0098 private func subKeyPathDictionary| Model.swift:107 | return subKeyPathDictionary(value: [keys[index] : value], keys: keys, index: index-1, previousDictionary: previousDictionary) |
| Model.swift:123 | let subDictionary = subKeyPathDictionary(value: value, keys: subKeyPaths, index: subKeyPaths.count-1, previousDictionary: previousDictionary) |
(value value: AnyObject, keys: [String], index: Int, previousDictionary: AnyObject?) -> [String : AnyObject] {
0099
0100 if index == 0 {
0101 let key = keys[index]
0102 guard let previousDictionary = previousDictionary as? [String : AnyObject] else { return [key : value] }
0103
0104 return mergeDictionaries(previousDictionary, [key : value])
0105 }
0106
0107 return subKeyPathDictionary(value: [keys[index] : value], keys: keys, index: index-1, previousDictionary: previousDictionary)
0108 }
0109
0110 public func json| JSONTransformable.swift:46 | return json().dictionary |
| Model.swift:156 | if let json = json().json { |
() -> (dictionary: [String : AnyObject], json: JSON?, data: NSData?) {
0112
0113 var dictionary: [String : AnyObject] = [:]
0114
0115 for map in JSONMappings {
0116 let components = map.key.componentsSeparatedByString(".")
0117 if components.count > 1 {
0118 if let value: AnyObject = map.toJSON() {
0119
0120 let firstKeyPath = components[0]
0121 let subKeyPaths = Array(components[1..<components.count])
0122 let previousDictionary: AnyObject? = dictionary[firstKeyPath]
0123 let subDictionary = subKeyPathDictionary(value: value, keys: subKeyPaths, index: subKeyPaths.count-1, previousDictionary: previousDictionary)
0124
0125 dictionary[firstKeyPath] = subDictionary
0126 }
0127 }
0128 else if let value: AnyObject = map.toJSON() {
0129 dictionary[map.key] = value
0130 }
0131 }
0132
0133 if let jsonData = try? NSJSONSerialization.dataWithJSONObject(dictionary, options: .PrettyPrinted) {
0134 let json = JSON(data: jsonData)
0135
0136 return (dictionary: dictionary, json: json, data: jsonData)
0137 }
0138
0139 return (dictionary: dictionary, json: nil, data: nil)
0140 }
0141
0142
0144 public required convenience init?(coder aDecoder: NSCoder) {
0145 self.init()
0146 JSONMappings.forEach { $0.decode(aDecoder) }
0147 }
0148
0149 public func encodeWithCoder(aCoder: NSCoder) {
0150 JSONMappings.forEach { $0.encode(aCoder) }
0151 }
0152
0153
0155 public override func copy() -> AnyObject {
0156 if let json = json().json {
0157 return self.dynamicType.init(json: json)
0158 }
0159 return self.dynamicType.init()
0160 }
0161
0162
0164 private func mergeDictionaries| Model.swift:104 | return mergeDictionaries(previousDictionary, [key : value]) |
| Model.swift:174 | let replacement = mergeDictionaries(previousValue, value) |
(left: [String : AnyObject], _ right: [String : AnyObject]) -> [String : AnyObject] {
0165
0166 var map: [String : AnyObject] = [:]
0167 for (k, v) in left {
0168 map[k] = v
0169 }
0170
0171 for (k, v) in right {
0172 if let previousValue = map[k] as? [String : AnyObject] {
0173 if let value = v as? [String : AnyObject] {
0174 let replacement = mergeDictionaries(previousValue, value)
0175 map[k] = replacement
0176 continue
0177 }
0178 }
0179 map[k] = v
0180 }
0181
0182 return map
0183 }
0184 }
0185