5
votes

I'm working on Objective C project, that uses RestKit framework for parsing JSON responses. Now I need some help with object mapping settings for following case: JSON response:

    {
   "data": {
       "SOME.API.Auth": {
           "maxVersion": 2,
           "minVersion": 1,
           "path": "auth.cgi"
       },
       "SOME.Station": {
           "maxVersion": 1,
           "minVersion": 1,
           "path": "Station/task.cgi"
       }
   },
   "success": true
}

and following objects:

@interface Response : NSObject
@property (strong, nonatomic) NSArray *data;
@property (assign, nonatomic) BOOL success;
@end


@interface SomeAPIInfo : NSObject
@property (strong, nonatomic) NSString *name;
@property (strong, nonatomic) NSString *path;
@property (strong, nonatomic) NSString *minVersion;
@property (strong, nonatomic) NSString *maxVersion;
@end

And here is my mapping settings:

RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[Response class]];
[responseMapping addAttributeMappingsFromDictionary:@{@"success": @"success"}];

RKObjectMapping *dataObjectMapping =  [RKObjectMapping mappingForClass:[SomeAPIInfo class]];
dataObjectMapping.forceCollectionMapping = YES;
[dataObjectMapping addAttributeMappingFromKeyOfRepresentationToAttribute:@"name"];
[dataObjectMapping addAttributeMappingsFromDictionary:@{
@"(name).path": @"path",
@"(name).minVersion": @"minVersion",
@"(name).maxVersion": @"maxVersion"}];


[responseMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"data"
                                                                               toKeyPath:@"data"
                                                                             withMapping:dataObjectMapping]];

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:responseMapping
                                                                                  pathPattern:nil
                                                                                      keyPath:nil
                                                                                  statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];


[_objectManager addResponseDescriptor:responseDescriptor];

Problem is that "data" object is not properly mapped: NSArray *data; is filled with 2 "SomeAPIInfo" objects. name is properly filled, but other values(path,maxVersion,minVersion) are empty.

What am I doing wrong ? Is there another way to map "data" object ? Maybe directly into NSDictionary, so "Some.API.Auth" would be key and "SomeAPIInfo" would be object (without "name" property).

Thanks for help!

Edit: I think that the mapping doesn't work because of the dots in key ("SOME.API.Auth).

RKMappingOperation.m:
- (NSArray *)simpleAttributeMappings
{
    NSMutableArray *mappings = [NSMutableArray array];
    for (RKAttributeMapping *mapping in self.nestedAttributeMappings) {
        if ([mapping.sourceKeyPath rangeOfString:@"."].location == NSNotFound) {
            [mappings addObject:mapping];
        }
    }
1

1 Answers

0
votes

I ran into similar issues that was caused by a bug in RestKit https://github.com/RestKit/RestKit/issues/1532 . I'd try to eliminate the possible dot issue by creating a fake JSON response with the same structure but without the dots and see if the problem is still there.