I’m working with a RestKit framework for one of my projects. It includes an example of how to convert JSON objects into a core data graph using object mapping. I’m wandering if the reverse is possible – can a Core Data entity using RestKit be converted back to a JSON representation?
I found RKObjectSerializer class, but I can’t seem to make it work – the serialized object that I get is nil
-(void)doCoreDataToJSONConversion
{
Article* article = [_articles objectAtIndex:0];
RKManagedObjectMapping* articleMapping = [RKManagedObjectMapping mappingForClass:[Article class]];
NSAssert(articleMapping!=nil,@"article mapping is nil!");
NSLog(@"%@",[article description]);
RKObjectSerializer* serializer =[RKObjectSerializer serializerWithObject:article mapping:articleMapping];
NSError* error = nil;
NSMutableDictionary* serializedObject = [serializer serializedObject:&error];
if(error!=nil)
{
NSLog(@"!!!!! Error: %@",[error localizedDescription]);
}
//prints nil
NSLog(@"Serialized Object: %@", [serializedObject description]);
}
Thank you for your input!
I ended up using these methods to convert core data objects into JSON. To deserialize these objects I need to define a different class of mapping: RKManagedObjectMapping, which is slightly different from the regular object mapping.