Skip to content

Commit 5a17661

Browse files
committed
#1 added support for nested resources for infinite depth
1 parent 4a3cf6b commit 5a17661

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

SwiftyJSONAccelerator/SJModelGenerator.swift

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public class ModelGenerator {
141141

142142
// If the type is an object, generate a new model and also create appropriate initalizers, declarations and decoders.
143143
if subClassType == VariableType.kObjectType {
144-
let subClassName = generateModelForClass(jsonValue.arrayValue[0], className: variableName, isSubModule:true)
144+
let subClassName = generateModelForClass(mergeArrayToSingleObject(jsonValue.arrayValue), className: variableName, isSubModule:true)
145145
declarations = declarations.stringByAppendingFormat(variableDeclarationBuilder(variableName, type: "[\(subClassName)]"))
146146
initalizers = initalizers.stringByAppendingFormat("%@\n", initalizerForObjectArray(variableName, className: subClassName, key: stringConstantName))
147147
decoders = decoders.stringByAppendingFormat("%@\n", decoderForVariable(variableName,key: stringConstantName, type: "[\(subClassName)]"))
@@ -219,9 +219,10 @@ public class ModelGenerator {
219219

220220
// If the type is an object then make it the base class and generate stuff.
221221
if subClassType == VariableType.kObjectType {
222-
self.generateModelForClass(object[0], className: className, isSubModule: false)
222+
return self.generateModelForClass(mergeArrayToSingleObject(object), className: className, isSubModule: false)
223+
} else {
224+
return ""
223225
}
224-
return ""
225226
}
226227

227228
return className
@@ -322,10 +323,10 @@ public class ModelGenerator {
322323

323324
if let _ = js.string {
324325
type = VariableType.kStringType
325-
} else if let _ = js.number {
326-
type = VariableType.kNumberType
327326
} else if let _ = js.bool {
328327
type = VariableType.kBoolType
328+
} else if let _ = js.number {
329+
type = VariableType.kNumberType
329330
} else if let _ = js.array {
330331
type = VariableType.kArrayType
331332
}
@@ -548,7 +549,7 @@ public class ModelGenerator {
548549
*/
549550
internal func replaceInternalKeywordsForVariableName(currentName: String) -> String {
550551

551-
let currentReservedName = ["id":"internalIdentifier","description":"descriptionValue","_id":"internalIdentifier"]
552+
let currentReservedName = ["id" : "internalIdentifier", "description" : "descriptionValue","_id" : "internalIdentifier","class" : "classProperty", "struct" : "structProperty", "internal" : "internalProperty"]
552553
for (key, value) in currentReservedName {
553554
if key == currentName {
554555
return value
@@ -557,5 +558,21 @@ public class ModelGenerator {
557558
return currentName
558559

559560
}
561+
562+
internal func mergeArrayToSingleObject(items: [JSON]) -> JSON {
563+
var finalObject: JSON = JSON([ : ])
564+
for item in items {
565+
for (key, jsonValue) in item {
566+
if finalObject[key] == nil {
567+
finalObject[key] = jsonValue
568+
} else if let newValue = jsonValue.dictionary {
569+
finalObject[key] = mergeArrayToSingleObject([JSON(newValue), finalObject[key]])
570+
} else if let newValue = jsonValue.array {
571+
finalObject[key] = mergeArrayToSingleObject(newValue + finalObject[key].arrayValue)
572+
}
573+
}
574+
}
575+
return finalObject
576+
}
560577

561578
}

0 commit comments

Comments
 (0)