Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change JSON before mapping by RESTKIT

I am using RESTKIT to map the JSON returned from server.

The JSON result obtained from server is as follows

{"term":"Zh","results":{"users":[{"id":{"$oid":"4ebe59970a614e0019000055"},"term":"some text","score":1}]}

How can I convert the above JSON result to the below:

{"results":{"users":[{"uid":"4ebe59970a614e0019000055","text":"some text"}]}

Also, where can I do this so that the RESTKIT mapping will use the converted JSON instead of the initial one?

Below is the loader class that I am using to manage the JSON and mappings

-(void)getObjects
{
    RKObjectManager *sharedManager = [RKObjectManager sharedManager];
    [sharedManager loadObjectsAtResourcePath:self.resourcePath delegate:self];
}

- (void)request:(RKRequest*)request didLoadResponse:(RKResponse*)response {
    NSLog(@"Loaded PAYLOAD successfully for %@, with response %@", self.resourcePath , [response bodyAsString]  );
}


- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects 
{
}


+ (void)setManagerAndMappings
{
    RKObjectManager* manager = [RKObjectManager objectManagerWithBaseURL:SERVER_URL];
    RKObjectMappingProvider* provider = [[RKObjectMappingProvider new] autorelease];

    //User Object Mapping
    RKObjectMapping* userMapping = [RKObjectMapping mappingForClass:[User class]];
    [userMapping mapKeyPath:@"_id" toAttribute:@"uid"];

    [userMapping mapAttributes:@"avatar_url",@"created_at",@"email",@"name",@"nickname",@"follower_count",@"following_count",@"answer_count",@"new_notification_count",@"new_questions_count",@"is_following",@"facebook_configured",@"twitter_configured",@"description",@"approved",@"type", nil];
    [provider setMapping:userMapping forKeyPath:@"user"];

}
like image 989
Zhen Avatar asked Jan 01 '12 07:01

Zhen


4 Answers

@Shocks answer is appealing, unfortunately it is valid a for version before 0.20.

Anyway, a similar solution is available for 0.20 too, using an implementation of RKSerialization:

@interface ORRKJsonSerialization : NSObject <RKSerialization>
@end

and implementing

@implementation ORRKJsonSerialization

+ (id)objectFromData:(NSData *)data error:(NSError **)error
{
    id result = [NSJSONSerialization JSONObjectWithData:data options:0 error:error];
    // change your data before mapping
    return result;
}

+ (NSData *)dataFromObject:(id)object error:(NSError **)error
{
    return [NSJSONSerialization dataWithJSONObject:object options:0 error:error];
}

@end

then during the setup:

[RKMIMETypeSerialization registerClass:[ORRKJsonSerialization class] forMIMEType:@"application/json"];

HTH

like image 77
Giordano Scalzo Avatar answered Nov 12 '22 06:11

Giordano Scalzo


There is a willMapData: selector in the RKObkectLoaderDelegate that is invoked just after parsing has completed. The mappableData argumet is mutable, so i guess you can change the data just before the object mapping will take place.

like image 34
mja Avatar answered Nov 12 '22 06:11

mja


use RKObjectRequestOperation.setWillMapDeserializedResponseBlock:.

In swift:

    let request = RKObjectManager.sharedManager().requestWith...
    let operation = RKObjectManager.sharedManager().managedObjectRequestOperationWithRequest(request, managedObjectContext: context, success: { operation, result in
        // success
    }, failure: { operation, error in
        // failure
    })
    operation.setWillMapDeserializedResponseBlock { deserializedResponse -> AnyObject! in
        // Here to transform response
        return transformResponse(deserializedResponse)
    }
    RKObjectManager.sharedManager().enqueueObjectRequestOperation(operation)
like image 3
mmtootmm Avatar answered Nov 12 '22 05:11

mmtootmm


For me the only solution is to modify the returned object at the server level. If you can't,just map that returns the server.

like image 1
Beber Avatar answered Nov 12 '22 05:11

Beber