Skip to content

Commit f1f1c1d

Browse files
committed
+ Added handling of structs as model properties
1 parent 77837e3 commit f1f1c1d

3 files changed

Lines changed: 31 additions & 7 deletions

File tree

‎JSONModel/JSONModel/JSONModel.m‎

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,16 @@ -(id)initWithDictionary:(NSDictionary*)dict error:(NSError**)err
251251

252252
if (property) {
253253

254+
// 0.0) handle structs
255+
if (property.structName) {
256+
NSLog(@"struct found: %@", property.structName);
257+
258+
//continue to next property
259+
//continue;
260+
}
261+
254262
// 0) handle primitives
255-
if (property.type == nil) {
263+
if (property.type == nil && property.structName==nil) {
256264

257265
//just copy the value
258266
[self setValue:jsonValue forKey: property.name];
@@ -326,7 +334,9 @@ -(id)initWithDictionary:(NSDictionary*)dict error:(NSError**)err
326334
//JMLog(@"to type: [%@] from type: [%@] transformer: [%@]", p.type, sourceClass, selectorName);
327335

328336
//build a method selector for the property and json object classes
329-
NSString* selectorName = [NSString stringWithFormat:@"%@From%@:", property.type, sourceClass];
337+
NSString* selectorName = [NSString stringWithFormat:@"%@From%@:",
338+
(property.structName? property.structName : property.type), //target name
339+
sourceClass]; //source name
330340
SEL selector = NSSelectorFromString(selectorName);
331341

332342
//check if there's a transformer with that name
@@ -468,7 +478,18 @@ -(void)__restrospectProperties
468478
[scanner scanString:@">" intoString:NULL];
469479
}
470480

471-
} else {
481+
}
482+
//check if the property is a structure
483+
else if ([scanner scanString:@"{" intoString: &propertyType]) {
484+
[scanner scanCharactersFromSet:[NSCharacterSet alphanumericCharacterSet]
485+
intoString:&propertyType];
486+
487+
p.isStandardJSONType = NO;
488+
p.structName = propertyType;
489+
490+
}
491+
//the property must be a primitive
492+
else {
472493

473494
//the property contains a primitive data type
474495
[scanner scanUpToCharactersFromSet:[NSCharacterSet characterSetWithCharactersInString:@","]
@@ -664,7 +685,7 @@ -(NSDictionary*)toDictionary
664685
}
665686

666687
// 2) check for standard types OR 2.1) primitives
667-
if (p.isStandardJSONType || p.type==nil) {
688+
if (p.structName==nil && (p.isStandardJSONType || p.type==nil)) {
668689
[tempDictionary setValue:value forKeyPath: keyPath];
669690
continue;
670691
}
@@ -673,7 +694,7 @@ -(NSDictionary*)toDictionary
673694
if (YES) {
674695

675696
//create selector from the property's class name
676-
NSString* selectorName = [NSString stringWithFormat:@"%@From%@:", @"JSONObject", p.type];
697+
NSString* selectorName = [NSString stringWithFormat:@"%@From%@:", @"JSONObject", p.type?p.type:p.structName];
677698
SEL selector = NSSelectorFromString(selectorName);
678699

679700
//check if there's a transformer declared

‎JSONModel/JSONModel/JSONModelClassProperty.h‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,12 @@
2929
/** The name of the declared property (not the ivar name) */
3030
@property (copy, nonatomic) NSString* name;
3131

32-
/** A primitive type name ("float", "short", etc) or a class name */
32+
/** A property class type */
3333
@property (assign, nonatomic) Class type;
3434

35+
/** Struct name if a struct */
36+
@property (strong, nonatomic) NSString* structName;
37+
3538
/** The name of the protocol the property conforms to (or nil) */
3639
@property (copy, nonatomic) NSString* protocol;
3740

‎JSONModel/JSONModel/JSONModelClassProperty.m‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ @implementation JSONModelClassProperty
2020

2121
-(NSString*)description
2222
{
23-
return [NSString stringWithFormat:@"%@[@%@]<%@> %@ %@ %@ %@", self.name, self.type?self.type:@"primitive", self.protocol?self.protocol:@"", self.isOptional?@"Optional,":@"",self.isMutable?@"Mutable,":@"",self.convertsOnDemand?@"ConvertOnDemand,":@"",self.isStandardJSONType?@"JSONType":@""];
23+
return [NSString stringWithFormat:@"%@[@%@]<%@> %@ %@ %@ %@", self.name, self.type?self.type:(self.structName?self.structName:@"primitive"), self.protocol?self.protocol:@"", self.isOptional?@"Optional,":@"",self.isMutable?@"Mutable,":@"",self.convertsOnDemand?@"ConvertOnDemand,":@"",self.isStandardJSONType?@"JSONType":@""];
2424
}
2525

2626
@end

0 commit comments

Comments
 (0)