How to change JSON before mapping by RESTKIT How to change JSON before mapping by RESTKIT json json

How to change JSON before mapping by RESTKIT


@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


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.


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)