Skip to content

Instantly share code, notes, and snippets.

@tlivings
Created May 7, 2011 16:04
Show Gist options
  • Select an option

  • Save tlivings/960603 to your computer and use it in GitHub Desktop.

Select an option

Save tlivings/960603 to your computer and use it in GitHub Desktop.
Quick and Dirty proxyForJson Implementation on NSManagedObject
@implementation NSManagedObject (NSManagedObject_JSON)
/*
*
* This method is implemented to help NSObject+JSON with transforming to JSON as per SBJSON's JSONValue helper method.
*
*/
-(NSDictionary*) proxyForJson {
NSDictionary * attributes = [[self entity] attributesByName];
NSDictionary * relationships = [[self entity] relationshipsByName];
NSMutableDictionary * results = [NSMutableDictionary dictionary];
for (NSString * relationship in [relationships keyEnumerator]) {
id value = [self valueForKey:relationship];
if(value) {
if([value isKindOfClass:[NSSet class]]) {
[results setObject:[(NSSet*)value allObjects] forKey:relationship];
}
else {
[results setObject:value forKey:relationship];
}
}
}
for (NSString * attribute in [attributes keyEnumerator]) {
id value = [self valueForKey:attribute];
if(value) {
[results setObject:value forKey:attribute];
}
}
return results;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment