I am using an API that prepends )]}'
to every JSON response, apparently as a safety measure. e.g.
response.body=)]}',
[{"id":13,"name":"Demo Company","total_amount_due":15714.2}]
RestKit can't map that, so I need to strip the first item from the response before mapping.
What's the best way of doing this?
Solved this by making a MyJsonSerialization
class:
@interface MyJsonSerialization : NSObject <RKSerialization>
@end
@implementation MyJsonSerialization
+ (id)objectFromData:(NSData *)data error:(NSError **)error
{
NSString* responseStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSData *refinedData = data;
if ([responseStr hasPrefix:@")]}',"]){
NSRange range = NSMakeRange(5, data.length - 5);
refinedData = [data subdataWithRange:range];
}
id result = [NSJSONSerialization JSONObjectWithData:refinedData options:0 error:error];
return result;
}
+ (NSData *)dataFromObject:(id)object error:(NSError **)error
{
return [NSJSONSerialization dataWithJSONObject:object options:0 error:error];
}
@end
And using it like this:
[RKMIMETypeSerialization registerClass:[MyJsonSerialization class] forMIMEType:@"application/json"];