-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathJSONModelKit.swift
More file actions
186 lines (143 loc) · 7.37 KB
/
Copy pathJSONModelKit.swift
File metadata and controls
186 lines (143 loc) · 7.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
//
// JSONModelKit.swift
// JSONModelKit
//
// Created by Anton Doudarev on 6/22/15.
// Copyright © 2015 Ustwo. All rights reserved.
//
import Foundation
public struct JMConfig {
public enum JMMappingFormat: String {
case json = "json"
case plist = "plist"
}
static public var debugEnabled : Bool = false
static public var logLevel : JMLogLevel = .none
static public var fileFormat : JMMappingFormat = .json
}
public protocol JMInstantiatorProtocol {
func newInstance(ofType classname : String, withValue data : Dictionary<String, AnyObject>) -> AnyObject?
func transformerFromString(_ classString: String) -> JMTransformerProtocol?
}
public protocol JMTransformerProtocol {
func transformValues(_ inputValues : Dictionary<String, Any>?) -> Any?
}
public func typeCast<U>(_ object: Any?) -> U? {
if let typed = object as? U {
return typed
}
return nil
}
extension Array {
func containsValue<T>(_ obj: T) -> Bool where T : Equatable {
return self.filter({$0 as? T == obj}).count > 0
}
}
var propertyMappings : Dictionary<String, Dictionary<String, Dictionary<String, AnyObject>>> = Dictionary()
let JMMapperJSONKey = "key"
let JMMapperTypeKey = "type"
let JMMapperNonOptionalKey = "nonoptional"
let JMMapperDefaultKey = "default"
let JMMapperTransformerKey = "transformer"
let JMMapperCollectionSubTypeKey = "subtype"
let JMDataTypeString = "String"
let JMDataTypeInt = "Int"
let JMDataTypeDouble = "Double"
let JMDataTypeFloat = "Float"
let JMDataTypeBool = "Bool"
let JMDataTypeArray = "Array"
let JMDataTypeDictionary = "Dictionary"
let nativeDataTypes = [JMDataTypeString, JMDataTypeInt, JMDataTypeDouble, JMDataTypeFloat, JMDataTypeBool]
let collectionTypes = [JMDataTypeArray, JMDataTypeDictionary]
final public class JSONModelKit {
public class func mapValues(from dictionary : Dictionary<String, AnyObject>, forType classType : String , employing instantiator : JMInstantiatorProtocol, defaultsEnabled : Bool) -> Dictionary<String, Any>? {
if let mappingConfiguration = retrieveMappingConfiguration(classType) {
// Dictionary to store parsed values to be returned
var retrievedValueDictionary = Dictionary<String, Any>()
for (propertyKey, propertyMapping) in mappingConfiguration {
retrievedValueDictionary[propertyKey] = Parser.retrieveValue(from : dictionary, applying : propertyMapping, employing : instantiator, defaultsEnabled : defaultsEnabled)
}
if defaultsEnabled == false {
return retrievedValueDictionary
}
if Validator.validateResponse(forValues: retrievedValueDictionary, mappedTo : mappingConfiguration, forType : classType, withData : dictionary) {
return retrievedValueDictionary
}
}
return nil
}
class func retrieveMappingConfiguration(_ className : String) -> Dictionary<String, Dictionary<String, AnyObject>>? {
if let mappingconfiguration = propertyMappings[className] {
return mappingconfiguration
} else {
if JMConfig.fileFormat == .json {
if let mapping = self.loadMappingJSONInMainBundle(forClassName : className, ofType : "json") {
propertyMappings[className] = mapping
} else if let mapping = self.loadMappingJSONFromResourceBundle(forClassName : className, ofType : "json") {
propertyMappings[className] = mapping
}
} else if JMConfig.fileFormat == .plist {
if let mapping = self.loadMappingPLISTInMainBundle(forClassName : className, ofType : "plist") {
propertyMappings[className] = mapping
} else if let mapping = self.loadMappingPLISTFromResourceBundle(forClassName : className, ofType : "plist") {
propertyMappings[className] = mapping
}
}
}
return propertyMappings[className]
}
fileprivate class func loadMappingPLISTInMainBundle(forClassName className : String,
ofType type : String) -> Dictionary<String, Dictionary<String, AnyObject>>? {
if let mappingPath = Bundle.main.path(forResource: className, ofType: type),
let mappingDictionary = loadPLISTMapping(atPath: mappingPath) {
return mappingDictionary
}
return nil
}
fileprivate class func loadMappingPLISTFromResourceBundle(forClassName className : String,
ofType type : String) -> Dictionary<String, Dictionary<String, AnyObject>>? {
if let mappingPath = Bundle(for: self).path(forResource: className, ofType: type),
let mappingDictionary = loadPLISTMapping(atPath: mappingPath) {
return mappingDictionary
}
return nil
}
fileprivate class func loadMappingJSONInMainBundle(forClassName className : String,
ofType type : String) -> Dictionary<String, Dictionary<String, AnyObject>>? {
if let mappingPath = Bundle.main.path(forResource: className, ofType: type),
let mappingDictionary = loadJSONMapping(atPath: mappingPath) {
return mappingDictionary
}
return nil
}
fileprivate class func loadMappingJSONFromResourceBundle(forClassName className : String,
ofType type : String) -> Dictionary<String, Dictionary<String, AnyObject>>? {
if let mappingPath = Bundle(for: self).path(forResource: className, ofType: type),
let mappingDictionary = loadJSONMapping(atPath: mappingPath) {
return mappingDictionary
}
return nil
}
fileprivate class func loadJSONMapping(atPath mappingPath : String) -> Dictionary<String, Dictionary<String, AnyObject>>? {
do {
let jsonData = try Data(contentsOf: URL(fileURLWithPath: mappingPath), options: NSData.ReadingOptions.mappedIfSafe)
do {
let jsonMapping: NSDictionary = try JSONSerialization.jsonObject(with: jsonData, options: JSONSerialization.ReadingOptions.mutableContainers) as! NSDictionary
if let mapping = jsonMapping as? Dictionary<String, Dictionary<String, AnyObject>> {
if mapping.keys.count > 0 {
return mapping
}
}
return nil
} catch {}
} catch {}
return nil
}
fileprivate class func loadPLISTMapping(atPath mappingPath : String) -> Dictionary<String, Dictionary<String, AnyObject>>? {
guard let tempMapping = NSDictionary(contentsOfFile: mappingPath) as? Dictionary<String, Dictionary<String, AnyObject>>,
tempMapping.isEmpty == false else {
return nil
}
return tempMapping
}
}