Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for NSManagedObject subclasses #30

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions JTObjectMapping/Source/NSObject+JTObjectMapping.h
Expand Up @@ -32,6 +32,8 @@

+ (id)objectFromJSONObject:(id <JTValidJSONResponse>)object mapping:(NSDictionary *)mapping;

+ (id)objectFromJSONObject:(id<JTValidJSONResponse>)object mapping:(NSMutableDictionary *)mapping inContext:(NSManagedObjectContext *) context;

@end


Expand Down
32 changes: 26 additions & 6 deletions JTObjectMapping/Source/NSObject+JTObjectMapping.m
Expand Up @@ -146,30 +146,50 @@ - (void)setValueFromDictionary:(NSDictionary *)dict mapping:(NSDictionary *)mapp
[notMapped release];
}


/*
Instantiate and populate the properties of this class with the JTValidJSONResponse (NSDictionary).
If this is a dictionary or array, recurse into the json dict and create the corresponding child objects.
*/
+ (id)objectFromJSONObject:(id<JTValidJSONResponse>)object mapping:(NSMutableDictionary *)mapping {
id returnObject = nil;

return [[self class] objectFromJSONObject:object mapping:mapping inContext:nil];
}

/*
Instantiate and populate the properties of this class with the JTValidJSONResponse (NSDictionary).
If this is a dictionary or array, recurse into the json dict and create the corresponding child objects.

@param object JSON data to be mapped into apps objects
@param mapping mapping from JSON keys to object keys
@param context Optional. If target class is not a NSManagedObject subclass you can pass nil. Otherwise, pass context for object to be inserted in.
*/
+ (id)objectFromJSONObject:(id<JTValidJSONResponse>)object mapping:(NSMutableDictionary *)mapping inContext:(NSManagedObjectContext *) context {
id returnObject = nil;


if ([object isKindOfClass:[NSDictionary class]]) {
// the json object is a dict -- create a new dict with the objects we can map from its contents
returnObject = [[[[self class] alloc] init] autorelease];

// if target object is NSManagedObjectSubclass - create it with initWithEntity
if ([[self class] isSubclassOfClass:[NSManagedObject class]]) {
// NSManagedObject subsclasses can be instantiated only with context
if (!context) return nil;
NSEntityDescription *entity = [NSEntityDescription entityForName:[[self class] description] inManagedObjectContext:context];
returnObject = [[[[self class] alloc] initWithEntity:entity insertIntoManagedObjectContext:context] autorelease];
} else {
returnObject = [[[[self class] alloc] init] autorelease];
}
[returnObject setValueFromDictionary:(NSDictionary *)object mapping:mapping];
} else if ([object isKindOfClass:[NSArray class]]) {
// the json object is an array -- create a new array with the objects we can map from its contents
NSMutableArray *array = [NSMutableArray array];
for (NSObject *dict in (NSArray *)object) {
NSParameterAssert([dict isKindOfClass:[NSDictionary class]]);
NSObject *newObj = [[self class] objectFromJSONObject:(NSDictionary *)dict mapping:mapping];
NSObject *newObj = [[self class] objectFromJSONObject:(NSDictionary *)dict mapping:mapping inContext:context];
[array addObject:newObj];
}
returnObject = [NSArray arrayWithArray:array];
}

// let objects do post-mapping validation, etc
// (it's safe to call without checking respondsToSelector:, because we have an no-op method defined in this category)
[returnObject didMapObjectFromJSON:object];
Expand Down