RestKit: mapping to NSDictionary with values of specific type

1k Views Asked by At

I've got the following JSON coming from my webservice:

  "GasPrices":{
     "Ai92":{
        "Price":30.1000,
        "LastDate":"\/Date(1385337600000)\/",
        "Votes":0
     },
     "Ai95":{
        "Price":33.2000,
        "LastDate":"\/Date(1385337600000)\/",
        "Votes":0
     }

I want to map it to NSDictionary, whose keys would be NSStrings and values would be of my custom class, say, PriceInfo.

What I got now with default setup is NSDictionary whose values are also NSDictionaries.

How can I achieve the desired mapping?

UPD. Here's my full setup for now.

@interface FillingStation : NSObject
@property (nonatomic, strong) NSNumber *uid;
@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSDictionary *fuelPrices;
@end

@interface PriceInfo : NSObject
@property (nonatomic, strong) NSNumber *price;
@property (nonatomic, strong) NSDate *lastDate;
@property (nonatomic, strong) NSNumber *votes;
@end

Configuring the mapping:

RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[FillingStation class]];
[mapping addAttributeMappingsFromDictionary:@{
        @"Id" : @"uid",
        @"Title" : @"title",
        @"GasPrices" : @"fuelPrices"
}];

This results in fuelPrices is NSDictionary with the structure:

NSString -> NSDictionary,
NSString -> NSDictionary,
...

And I want it to be:

NSString -> PriceInfo,
NSString -> PriceInfo,
...

And I don't want an intermediate dictionary in FillingStation which I can then manually map.

2

There are 2 best solutions below

0
On BEST ANSWER

Okay, seems like I found the answer. Not exactly what I wanted, yet acceptable for me: https://github.com/RestKit/RestKit/wiki/Object-Mapping#handling-dynamic-nesting-attributes

I had to add another property to PriceInfo class and change fuelPrices from NSDictionary to NSSet (NSArray would also work, but I don't need ordering), so it became:

@interface FillingStation : NSObject
@property (nonatomic, strong) NSNumber *uid;
@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSSet *fuelPrices;
@end

@interface PriceInfo : NSObject
@property (nonatomic, strong) NSString *fuelType;
@property (nonatomic, strong) NSNumber *price;
@property (nonatomic, strong) NSDate *updateDate;
@property (nonatomic, assign) NSUInteger votes;
@end

and my mapping now looks like this:

RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[FillingStation class]];
[mapping addAttributeMappingsFromDictionary:@{
        @"Id" : @"uid",
        @"Title" : @"title"
}];

RKObjectMapping *priceInfoMapping = [RKObjectMapping mappingForClass:[PriceInfo class]];
[priceInfoMapping setForceCollectionMapping:YES];
[priceInfoMapping addAttributeMappingFromKeyOfRepresentationToAttribute:@"fuelType"];
[priceInfoMapping addAttributeMappingsFromDictionary:@{
        @"(fuelType).Price": @"price",
        @"(fuelType).LastDate": @"updateDate",
        @"(fuelType).Votes": @"votes"
}];

[mapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"GasPrices"
                                                                        toKeyPath:@"fuelPrices"
                                                                      withMapping:priceInfoMapping]];
1
On

Jastor May be what you're looking for. Nested objects directly mapped to known classes.