@@ -70,6 +70,8 @@ public class ModelGenerator {
70
70
var declarations : String = " "
71
71
var stringConstants : String = " "
72
72
var initalizers : String = " "
73
+ var encoders : String = " "
74
+ var decoders : String = " "
73
75
74
76
let className = buildClassName ( className, prefix: self . prefix!)
75
77
if let object = parsedJSONObject. dictionary {
@@ -80,30 +82,35 @@ public class ModelGenerator {
80
82
let variableType : String = checkType ( subJson)
81
83
82
84
stringConstants = stringConstants. stringByAppendingFormat ( stringConstantDeclrationBuilder ( stringConstantName, key: key) )
85
+ encoders = encoders. stringByAppendingFormat ( " %@ \n " , encoderForVariable ( variableName, key: stringConstantName, type: variableType) )
83
86
84
87
if variableType == VariableType . kArrayType {
85
88
86
89
// If the array has objects, then take the first one and proces it to generate a model.
87
90
if subJson. arrayValue. count > 0 {
88
91
let subClassName = generateModelForClass ( subJson. arrayValue [ 0 ] , className: variableName)
89
92
declarations = declarations. stringByAppendingFormat ( variableDeclarationBuilder ( variableName, type: " [ \( subClassName) ] " ) )
90
- initalizers = initalizers. stringByAppendingFormat ( " %@ \n " , initalizerForObjectArray ( variableName, className: subClassName, key: key) )
93
+ initalizers = initalizers. stringByAppendingFormat ( " %@ \n " , initalizerForObjectArray ( variableName, className: subClassName, key: stringConstantName) )
94
+ decoders = decoders. stringByAppendingFormat ( " %@ \n " , decoderForVariable ( variableName, key: stringConstantName, type: " [ \( subClassName) ] " ) )
91
95
} else {
92
96
// if nothing is there make it a blank array.
93
97
declarations = declarations. stringByAppendingFormat ( variableDeclarationBuilder ( variableName, type: variableType) )
94
- initalizers = initalizers. stringByAppendingFormat ( " %@ \n " , initalizerForEmptyArray ( variableName, key: key) )
98
+ initalizers = initalizers. stringByAppendingFormat ( " %@ \n " , initalizerForEmptyArray ( variableName, key: stringConstantName) )
99
+
95
100
}
96
101
97
102
} else if variableType == VariableType . kObjectType {
98
103
99
104
let subClassName = generateModelForClass ( subJson, className: variableName)
100
105
declarations = declarations. stringByAppendingFormat ( variableDeclarationBuilder ( variableName, type: subClassName) )
101
- initalizers = initalizers. stringByAppendingFormat ( " %@ \n " , initalizerForObject ( variableName, className: subClassName, key: key) )
106
+ initalizers = initalizers. stringByAppendingFormat ( " %@ \n " , initalizerForObject ( variableName, className: subClassName, key: stringConstantName) )
107
+ decoders = decoders. stringByAppendingFormat ( " %@ \n " , decoderForVariable ( variableName, key: stringConstantName, type: subClassName) )
102
108
103
109
} else {
104
110
105
111
declarations = declarations. stringByAppendingFormat ( variableDeclarationBuilder ( variableName, type: variableType) )
106
- initalizers = initalizers. stringByAppendingFormat ( " %@ \n " , initalizerForVariable ( variableName, type: variableType, key: key) )
112
+ initalizers = initalizers. stringByAppendingFormat ( " %@ \n " , initalizerForVariable ( variableName, type: variableType, key: stringConstantName) )
113
+ decoders = decoders. stringByAppendingFormat ( " %@ \n " , decoderForVariable ( variableName, key: stringConstantName, type: variableType) )
107
114
108
115
}
109
116
@@ -126,6 +133,8 @@ public class ModelGenerator {
126
133
content = content. stringByReplacingOccurrencesOfString ( " {STRING_CONSTANT_BLOCK} " , withString: stringConstants)
127
134
content = content. stringByReplacingOccurrencesOfString ( " {PROPERTIES} " , withString: declarations)
128
135
content = content. stringByReplacingOccurrencesOfString ( " {INITALIZER} " , withString: initalizers)
136
+ content = content. stringByReplacingOccurrencesOfString ( " {ENCODERS} " , withString: encoders)
137
+ content = content. stringByReplacingOccurrencesOfString ( " {DECODERS} " , withString: decoders)
129
138
130
139
writeToFile ( className, content: content, path: filePath)
131
140
@@ -217,20 +226,38 @@ public class ModelGenerator {
217
226
218
227
219
228
internal func initalizerForVariable( variableName: String , var type: String , key: String ) -> String {
220
- type. replaceRange ( type. startIndex... type. startIndex, with: String ( type [ type. startIndex] ) . lowercaseString)
221
- return " \t \t if let value = json[ \" \( key) \" ]. \( type) { \n \t \t \t \( variableName) = value \n \t \t } "
229
+ if type == VariableType . kNumberType {
230
+ type = " number "
231
+ } else {
232
+ type. replaceRange ( type. startIndex... type. startIndex, with: String ( type [ type. startIndex] ) . lowercaseString)
233
+ }
234
+ return " \t \t if let value = json[ \( key) ]. \( type) { \n \t \t \t \( variableName) = value \n \t \t } "
222
235
}
223
236
224
237
internal func initalizerForObject( variableName: String , className: String , key: String ) -> String {
225
- return " \t \t \( variableName) = \( className) (json: json[ \" \ ( key) \" ]) "
238
+ return " \t \t \( variableName) = \( className) (json: json[ \( key) ]) "
226
239
}
227
240
228
241
internal func initalizerForEmptyArray( variableName: String , key: String ) -> String {
229
- return " \t \t if let value = json[ \" \ ( key) \" ].array { \n \t \t \t \( variableName) = value \n \t \t } "
242
+ return " \t \t if let value = json[ \( key) ].array { \n \t \t \t \( variableName) = value \n \t \t } "
230
243
}
231
244
232
245
internal func initalizerForObjectArray( variableName: String , className: String , key: String ) -> String {
233
- return " \t \t \( variableName) = [] \n \t \t if let items = json[ \" \( key) \" ].array { \n \t \t \t for item in items { \n \t \t \t \t \( variableName) ?.append( \( className) (json: item)) \n \t \t \t } \n \t \t } \n "
246
+ return " \t \t \( variableName) = [] \n \t \t if let items = json[ \( key) ].array { \n \t \t \t for item in items { \n \t \t \t \t \( variableName) ?.append( \( className) (json: item)) \n \t \t \t } \n \t \t } \n "
247
+ }
248
+
249
+ internal func encoderForVariable( variableName: String , key: String , type: String ) -> String {
250
+ if type == VariableType . kBoolType {
251
+ return " \t \t aCoder.encodeBool( \( variableName) , forKey: \( key) ) "
252
+ }
253
+ return " \t \t aCoder.encodeObject( \( variableName) , forKey: \( key) ) "
254
+ }
255
+
256
+ internal func decoderForVariable( variableName: String , key: String , type: String ) -> String {
257
+ if type == VariableType . kBoolType {
258
+ return " \t \t self. \( variableName) = aDecoder.decodeBoolForKey( \( key) ) "
259
+ }
260
+ return " \t \t self. \( variableName) = aDecoder.decodeObjectForKey( \( key) ) as? \( type) "
234
261
}
235
262
236
263
internal func todayDateString( ) -> String {
@@ -277,5 +304,15 @@ public class ModelGenerator {
277
304
}
278
305
}
279
306
307
+ /*
308
+ required init(coder aDecoder: NSCoder) {
309
+ self.bottlesArray = aDecoder.decodeObjectForKey("bottleArray") as NSMutableArray
310
+ }
311
+
312
+ func encodeWithCoder(aCoder: NSCoder) {
313
+ aCoder.encodeObject(bottlesArray, forKey: "bottleArray")
314
+ }
315
+ */
316
+
280
317
281
318
}
0 commit comments