Search code examples
objective-ciosrestkitintrospection

object mapping using introspection restkit


I'm trying to parse a SOAP response using RestKit. I'm able to successfully convert the SOAP XML response into objects if I define the mappings and relationships myself. But, I was wondering if it's possible to use introspection (or something else) to automatically convert the SOAP XML response into an Objective-C object.

Sample XML:

<return>
    <userId xsi:type="xsd:int">1113050187</userId>
    <location xsi:type="ns1:Location">
       <city xsi:type="xsd:string">San Francisco</city>
       <state xsi:type="xsd:string">California</state>
    </location>
</return>

I would like to convert this XML into this object:

@interface Return : NSObject

@property (strong, nonatomic) NSInteger userId; // attribute
@property (strong, nonatomic) Location *location; // relation

@end

The closest thing I could come up with is using introspection on the Return class to get all properties and using something like this for each attribute: [mapping addAttributeMapping:[RKObjectAttributeMapping mappingFromKeyPath:@"userId.text" toKeyPath:@"userId"]];

And for relations, I could again use introspection to find out all my class objects and use mapRelationship:withMapping: on each one


Solution

  • I ended up defining a method that recursively maps the properties to XML nodes if their names match. The naming convention and the data type of the property is important for this to work.

    I've tried my best to clean this up before I post it here, but let me know if you need any help.

    - (RKObjectMapping *)mapMe:(Class)class
    {
        RKObjectManager *objectManager = [RKObjectManager sharedManager];
        RKObjectMapping *mapping = [RKObjectMapping mappingForClass:class];
        id classType = objc_getClass([NSStringFromClass(class) UTF8String]);
        unsigned int outCount, i;
        objc_property_t *properties = class_copyPropertyList(classType, &outCount);
        for (i = 0; i < outCount; i++) {
            objc_property_t property = properties[i];
    //        fprintf(stdout, "%s\n", property_getName(property));
    
            const char *type = property_getAttributes(property);
            NSString *typeString = [NSString stringWithUTF8String:type];
            NSArray *attributes = [typeString componentsSeparatedByString:@","];
    //        NSLog(@"attributes = %@", attributes);
            NSString *typeAttribute = [attributes objectAtIndex:0];
    //        NSLog(@"typeAttribute = %@", typeAttribute);
            NSString *propertyType = [typeAttribute substringFromIndex:1];
    
            if ([propertyType hasPrefix:@"@"] && [propertyType length] > 1) {
                NSString * typeClassName = [propertyType substringWithRange:NSMakeRange(2, [propertyType length]-3)];  //turns @"NSDate" into NSDate
                Class typeClass = NSClassFromString(typeClassName);
                if (typeClass != nil && ![typeClassName hasPrefix:@"NS"]) {
    //            my custom class detected.
                    RKObjectMapping *subMapping = [self mapMe:typeClass forObjectManager:objectManager];
                    [mapping mapRelationship:[NSString stringWithUTF8String:property_getName(property)] withMapping:subMapping];
                    [objectManager.mappingProvider setMapping:subMapping forKeyPath:[NSString stringWithUTF8String:property_getName(property)]];
                } else {
                    [mapping addAttributeMapping:[RKObjectAttributeMapping mappingFromKeyPath:[NSString stringWithFormat:@"%@.text", [NSString stringWithUTF8String:property_getName(property)]] toKeyPath:[NSString stringWithUTF8String:property_getName(property)]]];
                }
            }
        }
        free(properties);
        return mapping;
    }
    

    And then to map it automatically call it as:

    [self mapMe:[Myclass class]];