I have a list of objects, which I would like to map to json as following:
"tags": {
"f6a34fea-5d06-4fb2-a2c7-127e2b58165a": true,
"39413ca6-b817-4ede-abd3-35822aec91b6": true
},
What I currently have:
RKObjectMapping *tagRequestMapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
[tagRequestMapping addAttributeMappingToKeyOfRepresentationFromAttribute:@"id"];
[tagRequestMapping addAttributeMappingsFromDictionary:@{@"hasTasks" : @"(id)"}];
tagRequestMapping.forceCollectionMapping = NO;
[taskRequestMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"tags"
toKeyPath:@"tags"
withMapping:tagRequestMapping]];
This results in the following json:
"tags": [
{
"f6a34fea-5d06-4fb2-a2c7-127e2b58165a": true
},
{
"39413ca6-b817-4ede-abd3-35822aec91b6": true
}
],
Unfortunately the webapp does not accept this as valid. I use the same rules to map the data from json to my core data objects, where it works just fine. But not for Core Data to json. How would I have to modify my mapping, to get one single object with all the values, just as specified?
Thanks to Wain I came up with a solution:
I added a new property to my Task class, that is readonly and has a custom getter. In that getter I create a dictionary from my NSSet of Tag objects. Then for the mapping part, I just added my new property as a normal attribute that should be mapped and it worked perfectly.
Custom Getter:
- (NSDictionary *)getTagDictionary {
NSMutableDictionary *tagDictionary = [NSMutableDictionary dictionary];
for (Tag *tag in self.tags) {
[tagDictionary setObject:tag.hasTasks forKey:tag.id];
}
return tagDictionary;
}
Mapping:
[taskRequestMapping addAttributeMappingsFromDictionary:@{
@"tagDictionary":@"tags"}];