JSON format inconsistency
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];
}];
}
Since you seem to control both ends of the process, you also have control over what gets sents. The two JSON snippets you provide are actually two DIFFERENT data structures. (A) is an array which contains two objects, each containing a number of member elements. (B) is an array which contains two objects which have a 'patient' key and a value which is another object containing a multitude of different key/value pairs.
e.g. your system is generating two different data structures, and your service is expecting only one of those structures - and rightfully has no idea how to deal with the 'bad' one, even though it appears to contain the proper data.
I solved it if anyone is interested:
in rails i changed the modle so it will send the restKit client nested json(Format A) like this:
class Topic < ActiveRecord::Base
has_many :posts
accepts_nested_attributes_for :posts
self.include_root_in_json = true
end
I made the post method attributes nested like this:
-(void)sendTopic{
RKObjectManager *manager =[RKObjectManager sharedManager];
[manager postObject:self.topic delegate:nil block:^(RKObjectLoader *loader) {
RKObjectMapping* sendTopicMapping = [RKManagedObjectMapping mappingForClass:[Topic class]];
[sendTopicMapping mapKeyPathsToAttributes:
@"topic[id]", @"topicID",
....
@"topic[title]", @"title", nil];
RKManagedObjectMapping* postPostMapping = [RKManagedObjectMapping mappingForClass:[Post class]];
[postPostMapping mapKeyPathsToAttributes:
@"post[id]", @"postID",
....
@"post[text]", @"text" , nil];
[sendTopicMapping mapRelationship:@"posts" withMapping:postPostMapping];
[manager.mappingProvider setMapping:sendTopicMapping forKeyPath:@"topic"];
loader.serializationMapping = [sendTopicMapping inverseMapping];
}];
}
精彩评论