Here is what I use to convert Core Data to JSON in Core Data.
-(void)deserializeFileAtPath:(NSString*)filePath { DLog(@"Deserialize file: %@",filePath); NSError* error = nil; NSString *stringJSON = [NSString stringWithContentsOfFile:filePath usedEncoding:nil error:&error]; if(error) { NSLog(@"Error reading from file: %@", filePath); }
Here's how to define a RestKit nested object model. When the JSON file of this structure is deserialized, it will automatically create all the nested relationships for you and even combine the contexts of managed objects!
+(void)setupCoreDataObjectMapping { RKObjectManager *objectManager = [RKObjectManager sharedManager ] ; // Setup our object mappings /*! Mapping by entity. Here we are configuring a mapping by targetting a Core Data entity with a specific name. This allows us to map back Twitter user objects directly onto NSManagedObject instances -- there is no backing model class! */ //******************************** RKManagedObjectMapping* nestedRelationshipMapping = [RKManagedObjectMapping mappingForEntityWithName:@"NestedRelationshipObject"]; //UUID determines which objects get updated and which ones get created during the mapping process nestedRelationshipMapping.primaryKeyAttribute = @"uuid"; [nestedRelationshipMapping mapKeyPathsToAttributes: @"IKeepTheseTheSame", @"IKeepTheseTheSame", @"AnotherValue",@"AnotherValue", //keep adding your attributes nil]; [objectManager.mappingProvider addObjectMapping:nestedRelationshipMapping]; //******************************** RKManagedObjectMapping* mainPayloadMapping = [RKManagedObjectMapping mappingForEntityWithName:@"RealCoreDataEntity"]; mainPayloadMapping.primaryKeyAttribute = @"uuid"; [mainPayloadMapping mapKeyPathsToAttributes: @"companyName",@"companyName", //keep adding your attributes nil]; //this is the main payload. I create all of it relationships before, and then add them to the mapping. [mainPayloadMapping mapRelationship:@"relationshipName" withMapping:nestedRelationshipMapping]; [objectManager.mappingProvider addObjectMapping:mainPayloadMapping]; [objectManager.mappingProvider setSerializationMapping:[mainPayloadMapping inverseMapping] forClass:[YourNSManagedObjectSubclass class]]; [objectManager.mappingProvider setMapping:nestedRelationshipMapping forKeyPath:@"mainPayloadToNestedDataRelationshipName"]; [objectManager.mappingProvider setMapping:mainPayloadMapping forKeyPath:@"wrapperToMainPayloadRelationshipName"]; //this is a top level JSON object. It name will not be identified within the object, but it relationshipName will be. The result of deserializing this object would be an object that is being wrapped. RKManagedObjectMapping* wrapperMapping = [RKManagedObjectMapping mappingForClass:[IconFileWrapper class]]; iconWrapperMapping.primaryKeyAttribute = @"uuid"; // keyPath and attribute names. must be even [iconWrapperMapping mapKeyPathsToAttributes:@"uuid",@"uuid",nil]; //keep adding your attributes [iconWrapperMapping mapRelationship:@"relationshipName" withMapping:mainPayloadMapping]; [objectManager.mappingProvider addObjectMapping:wrapperMapping]; [objectManager.mappingProvider setSerializationMapping:[wrapperMapping inverseMapping] forClass:[YourWrapperNSManagedObjectSubclass class]]; }
Alex stone
source share