There are two JSON format that i came across:
Format A:
[
{"topic":
{"category":"testCategory","created_at":"2011-09-27T05:41:42Z",
"size":5,"title":"testTitle2", "id":1,
"posts":[
{"number":1,"created_at":"2011-09-27T05:41:42Z",
"text":"text2","id":1,"topic_id":1},
{"number":0,"created_at":"2011-09-27T05:41:42Z",
"text":"sentence1","id":2,"story_id":1}
]
}
}
]
Format B:
[
{"category":"testCategory","created_at":"2011-09-27T05:41:42Z",
"size":5,"title":"testTitle2", "id":1,
"posts":[
{"number":1,"created_at":"2011-09-27T05:41:42Z",
"text":"text2","id":1,"topic_id":1},
{"number":0,"created_at":"2011-09-27T05:41:42Z",
"text":"sentence1","id":2,"story_id":1}
]
}
]
When my restKit client gets format B she is pleased but when she gets format A i get the following error:
... Could not find an object mapping for keyPath: '' ...
My restKit configurations is at the bottom.
So i fixed it by making my rails 3.1 server send her a format B JSON.
But now, restKit sends the server a format A JSON and he is stump as he should be.
Why does restKit won't receive format A but sends formats A? Is there a way to make restKit send a format B JSON? Or, maybe is there a way to make rails send format B and receive format A?
my restKit configurations:
-(void)initRestKit{
RKObjectManager* objectManager = [RKObjectManager objectManagerWithBaseURL:@"http://localhost:3000/"];
objectManager.objectStore = [RKManagedObjectStore objectStoreWithStoreFilename:@"WTF.sqlite"];
// Setup our object mappings
RKManagedObjectMapping* topicMapping = [RKManagedObjectMapping mappingForClass:[Topic class]];
storyMapping.primaryKeyAttribute = @"topicID";
storyMapping.setDefaultValueForMissingAttributes = YES;
[storyMapping mapKeyPathsToAttributes:
@"id", @"topicID",
....
@"created_at", @"dateCreated", nil];
RKManagedObjectMapping* postMapping = [RKManagedObjectMapping mappingForClass:[Post class]];
sentencesMapping.primaryKeyAttribute = @"postID";
[sentencesMapping mapKeyPathsToAttributes:
@"id", @"postID",
...
@"text", @"text" , nil];
//setup relationships
[storyMapping mapRelationship:@"posts" withMapping:postMapping];//topic -> (posts) -> post
// Register our mappings with the provider
[objectManager.mappingProvider setMapping:topicMapping forKeyPath:@"topic"];
[objectManager.mappingProvider setMapping:postMapping forKeyPath:@"post"];
// Set Up Router
[objectManager.router routeClass:[Topic class] toResourcePath:@"/topics.json" forMethod:RKRequestMethodPOST];
[objectManager.router routeClass:[Topic class] toResourcePath:@"/topics/(topicID).json"];
[objectManager.router routeClass:[Post class] toResourcePath:@"/topics/(topic.topicID)/posts.json" forMethod:RKRequestMethodPOST];
[objectManager.router routeClass:[Post class] toResourcePath:@"/topics/(topic.topicID)/(post.postID).json"];
}
-(void)sendTopic{
RKObjectManager *manager =[RKObjectManager sharedManager];
[manager postObject:self.topic delegate:nil block:^(RKObjectLoader *loader) {
RKObjectMapping* sendTopicMapping = [RKManagedObjectMapping mappingForClass:[Topic class]];
[sendTopicMapping mapKeyPathsToAttributes:
@"id", @"topicID",
....
@"title", @"title", nil];
RKManagedObjectMapping* postPostMapping = [RKManagedObjectMapping mappingForClass:[Post class]];
[postPostMapping mapKeyPathsToAttributes:
@"id", @"postID",
....
@"text", @"text" , nil];
[sendTopicMapping mapRelationship:@"posts" withMapping:postPostMapping];
[manager.mappingProvider setMapping:sendTopicMapping forKeyPath:@"topic"];
loader.serializationMapping = [sendTopicMapping inverseMapping];
}];
}