2
votes

Using the development branch of restkit (0.20), is there a way to perform mapping on a JSON string into the core data object store?

I know in 0.10, the method described at the bottom of this page worked but how is this operation performed in restkit 0.20? Any guidance appreciated! Thanks!

id<RKParser> parser = [[RKParserRegistry sharedRegistry] parserForMIMEType:MIMEType];
id parsedData = [parser objectFromString:JSONString error:&error];

RKObjectMappingProvider* mappingProvider = [RKObjectManager sharedManager].mappingProvider;
RKObjectMapper* mapper = [RKObjectMapper mapperWithObject:parsedData mappingProvider:mappingProvider];
RKObjectMappingResult* result = [mapper performMapping];
3

3 Answers

6
votes

I installed RESTKit v0.20.0pre4 yesterday and had the same need for one particular case.

Here is an example of the JSON String I want to map:

{"info":"the sun is shining","detail":"in Bordeaux ~ 29 °C"}

This is how I perform the mapping.

- (InfoRESTMapped *)mapInfoFromJSONString:(NSString *) JSONString{

NSString* MIMEType = @"application/json";
NSError* error;

NSData *data = [JSONString dataUsingEncoding:NSUTF8StringEncoding];
id parsedData = [RKMIMETypeSerialization objectFromData:data MIMEType:MIMEType error:&error];
if (parsedData == nil && error) {
    //deal with error
}

RKObjectMapping *infoMapping = [RKObjectMapping requestMapping];
[infoMapping addAttributeMappingsFromDictionary:@{
 @"info": @"myInfo",
 @"detail": @"myDetail",
 }];


InfoRESTMapped *infoMapped = [[InfoRESTMapped alloc] init];
RKMappingOperation* mapper = [[RKMappingOperation alloc] initWithSourceObject:parsedData destinationObject:infoMapped mapping:infoMapping];
[mapper performMapping:&error];

return infoMapped;
}

These posts led me to the solution I have suggested:

I hope it helps. I guess some other will give better solutions when the final version will be released (with better documentation too...).

1
votes

For Restkit 0.22, You can use this code. This returns an RKMappingResult wherein you can enumerate the objects after mapping using the property .array.

- (RKMappingResult *)mapJSONStringWithString:(NSString *)jsonString
{
     RKMappingResult *result = nil;

     NSError* error;
     NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
     id parsedData = [RKMIMETypeSerialization objectFromData:data MIMEType:RKMIMETypeJSON error:&error];
     if (parsedData == nil && error) {
        NSLog(@"json mapping error");
     }

     NSDictionary *mappingsDictionary = @{@"":[CustomMappingClass getMappingForUsers]};

     ObjectClass *obj = [ObjectClass new];
     RKMapperOperation *mapper = [[RKMapperOperation alloc] initWithRepresentation:parsedData mappingsDictionary:mappingsDictionary];
     NSError *mappingError = nil;
     mapper.targetObject = obj;
     BOOL isMapped = [mapper execute:&mappingError];
     if (isMapped && !mappingError) {
         result = [mapper mappingResult];
     }

    return result;
}
1
votes

The most straightforward way to import JSON into your Core Data store using RestKit is documented in the Read Me "Generate a Seed Database"

Here is what I wrote in Swift:

if let persistentStore = RKManagedObjectStore.default().persistentStoreCoordinator.persistentStores.first {
    if let objectImporter = RKManagedObjectImporter.init(persistentStore: persistentStore) {
        let path = Bundle.main.path(forResource: jsonFile, ofType: "json")
        let mapping = SLTour.responseMapping()
        var error:NSError?
        objectImporter.importObjectsFromItem(atPath: path, with: mapping, keyPath: "data", error: &error)
        try objectImporter.finishImporting()
        objectImporter.logSeedingInfo()
    }
}

Hopefully, this will save somebody from several hours of trying to solve this in the future.