objective c - Mapping Based on Value of JSON Response in RestKit -


the rest service consuming has 2 different modes of response.

{ success: true, data: { //all data goes here} } 

or

{ success: false, error: "error", message: "some message" } 

i'm trying configure restkit return error object i've created store error response , configured class (based on url) success=true case.

i'm trying use rkdynamicmapping accomplish based on value of "success".

   - (rkdynamicmapping *)createcompatiblemapping:(nsdictionary *)successdictionary  withclass:(class) class{         nsmutabledictionary *withsuccessdictionary = [nsmutabledictionary dictionarywithdictionary:successdictionary];         [withsuccessdictionary addentriesfromdictionary:@{@"success" : @"success"}];      rkobjectmapping *successmapping = [rkobjectmapping mappingforclass:class];     [successmapping addattributemappingsfromdictionary:withsuccessdictionary];      rkobjectmapping *resterrormapping = [rkobjectmapping mappingforclass:[resterrorobject class]];     [resterrormapping addattributemappingsfromdictionary:@{@"success" : @"success", @"error" : @"errorstring", @"message" : @"message"}];      rkdynamicmapping *dynamicmapping = [rkdynamicmapping new];       [dynamicmapping addmatcher:[rkobjectmappingmatcher matcherwithkeypath:@"success" expectedvalue:[nsnumber numberwithbool:yes ] objectmapping:successmapping]];     [dynamicmapping addmatcher:[rkobjectmappingmatcher matcherwithkeypath:@"success" expectedvalue:[nsnumber numberwithbool:no ] objectmapping:resterrormapping]];     return dynamicmapping; } 

i add response descriptor such:

rkresponsedescriptor * carddetailsresponsedescriptor2  = [rkresponsedescriptor responsedescriptorwithmapping:carddetailsresponsemapping method:rkrequestmethodget                                                                                                     pathpattern:@"services/details/:cardnumber" keypath:@"data"                                                                                                     statuscodes:rkstatuscodeindexsetforclass(rkstatuscodeclasssuccessful)];  [_objectmanager.router.routeset addroute:[rkroute routewithname:@"getcarddetailsroute" pathpattern:@"services/details/:cardnumber" method:rkrequestmethodget]]; 

i call

[self.objectmanager getobjectsatpathforroutenamed:@"getcarddetailsroute" object:card parameters:@{@"token" : authtoken} success:^(rkobjectrequestoperation *operation, rkmappingresult *mappingresult) { 

i know big post , may hard understand. appreciate patience :d confused on should do.

changing rest api isn't option.

try setting expected value @"true" @"false". think values strings @ time dynamic mapping run , converted bools during mapping , if destination type bool.


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -