Skip to content

Instantly share code, notes, and snippets.

@albertodebortoli
Created May 5, 2012 14:39
Show Gist options
  • Save albertodebortoli/2602966 to your computer and use it in GitHub Desktop.
Save albertodebortoli/2602966 to your computer and use it in GitHub Desktop.
JSON to NSManagedObject and viceversa (concept)
- (NSString*)jsonStructureFromManagedObjects:(NSArray*)managedObjects;
- (NSArray*)managedObjectsFromJSONStructure:(NSString*)json withManagedObjectContext:(NSManagedObjectContext*)moc;
- (NSDictionary*)dataStructureFromManagedObject:(NSManagedObject*)managedObject
{
NSDictionary *attributesByName = [[managedObject entity] attributesByName];
NSDictionary *relationshipsByName = [[managedObject entity] relationshipsByName];
NSMutableDictionary *valuesDictionary = [[managedObject dictionaryWithValuesForKeys:[attributesByName allKeys]] mutableCopy];
[valuesDictionary setObject:[[managedObject entity] name] forKey:@"ManagedObjectName"];
for (NSString *relationshipName in [relationshipsByName allKeys]) {
NSRelationshipDescription *description = [[[managedObject entity] relationshipsByName] objectForKey:relationshipName];
if (![description isToMany]) {
[valuesDictionary setValue:[self dataStructureForManagedObject:]];
continue;
}
NSSet *relationshipObjects = [managedObject objectForKey:relationshipName];
NSMutableArray *relationshipArray = [[NSMutableArray alloc] init];
for (NSManagedObject *relationshipObject in relationshipObjects) {
[relationshipArray addObject:[self dataStructureForManagedObject:relationshipObject]];
}
[valuesDictionary setObject:relationshipArray forKey:relationshipName];
}
return [valuesDictionary autorelease];
}
- (NSArray*)dataStructuresFromManagedObjects:(NSArray*)managedObjects
{
NSMutableArray *dataArray = [[NSArray alloc] init];
for (NSManagedObject *managedObject in managedObjects) {
[dataArray addObject:[self dataStructureForManagedObject:managedObject]];
}
return [dataArray autorelease];
}
- (NSString*)jsonStructureFromManagedObjects:(NSArray*)managedObjects
{
NSArray *objectsArray = [self dataStructuresFromManagedObjects:managedObjects];
NSString *jsonString = [[CJSONSerializer serializer] serializeArray:objectsArray];
return jsonString;
}
- (NSManagedObject*)managedObjectFromStructure:(NSDictionary*)structureDictionary withManagedObjectContext:(NSManagedObjectContext*)moc
{
NSString *objectName = [structureDictionary objectForKey:@"ManagedObjectName"];
NSManagedObject *managedObject = [NSEntityDescription insertNewObjectForEntityForName:objectName inManagedObjectContext:moc];
[managedObject setValuesForKeysWithDictionary:structureDictionary];
for (NSString *relationshipName in [[[managedObject entity] relationshipsByName] allKeys]) {
NSRelationshipDescription *description = [relationshipsByName objectForKey:relationshipName];
if (![description isToMany]) {
NSDictionary *childStructureDictionary = [structureDictionary objectForKey:relationshipName];
NSManagedObject *childObject = [self managedObjectFromStructure:childStructureDictionary withManagedObjectContext:moc];
[managedObject setObject:childObject forKey:relationshipName];
continue;
}
NSMutableSet *relationshipSet = [managedObject mutableSetForKey:relationshipName];
NSArray *relationshipArray = [structureDictionary objectForKey:relationshipName];
for (NSDictionary *childStructureDictionary in relationshipArray) {
NSManagedObject *childObject = [self managedObjectFromStructure:childStructureDictionary withManagedObjectContext:moc];
[relationshipSet addObject:childObject];
}
}
return managedObject;
}
- (NSArray*)managedObjectsFromJSONStructure:(NSString*)json withManagedObjectContext:(NSManagedObjectContext*)moc
{
NSError *error = nil;
NSArray *structureArray = [[CJSONDeserializer deserializer] deserializeAsArray:json error:&error];
NSAssert2(error == nil, @"Failed to deserialize\n%@\n%@", [error localizedDescription], json);
NSMutableArray *objectArray = [[NSMutableArray alloc] init];
for (NSDictionary *structureDictionary in structureArray) {
[objectArray addObject:[self managedObjectFromStructure:structureDictionary withManagedObjectContext:moc]];
}
return [objectArray autorelease];
}
@fatuhoku
Copy link

This doesn't seem to manage object references properly, does it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment