Implemented nested mapping for structures similar to the BuildBot JSON structure. fixes #112

This commit is contained in:
Blake Watters
2011-06-17 11:55:57 -04:00
parent e9e4c83630
commit 3bf4b7bc0f
18 changed files with 3342 additions and 77 deletions

View File

@@ -37,6 +37,7 @@ relationship. Relationships are processed using an object mapping as well.
NSString* _rootKeyPath;
BOOL _setNilForMissingAttributes;
BOOL _setNilForMissingRelationships;
BOOL _forceCollectionMapping;
}
/**
@@ -86,6 +87,30 @@ relationship. Relationships are processed using an object mapping as well.
*/
@property (nonatomic, assign) BOOL setNilForMissingRelationships;
/**
Forces the mapper to treat the mapped keyPath as a collection even if it does not
return an array or a set of objects. This permits mapping where a dictionary identifies
a collection of objects.
When enabled, each key/value pair in the resolved dictionary will be mapped as a separate
entity. This is useful when you have a JSON structure similar to:
{ "users":
{
"blake": { "id": 1234, "email": "blake@restkit.org" },
"rachit": { "id": 5678", "email": "rachit@restkit.org" }
}
}
By enabling forceCollectionMapping, RestKit will map "blake" => attributes and
"rachit" => attributes as independent objects. This can be combined with
mapKeyOfNestedDictionaryToAttribute: to properly map these sorts of structures.
@default NO
@see mapKeyOfNestedDictionaryToAttribute
*/
@property (nonatomic, assign) BOOL forceCollectionMapping;
/**
An array of date format strings to apply when mapping a
String attribute to a NSDate property. Each format string will be applied
@@ -240,6 +265,31 @@ relationship. Relationships are processed using an object mapping as well.
*/
- (void)mapKeyPathsToAttributes:(NSString*)sourceKeyPath, ... NS_REQUIRES_NIL_TERMINATION;
/**
Configures a sub-key mapping for cases where JSON has been nested underneath a key named after an attribute.
For example, consider the following JSON:
{ "users":
{
"blake": { "id": 1234, "email": "blake@restkit.org" },
"rachit": { "id": 5678", "email": "rachit@restkit.org" }
}
}
We can configure our mappings to handle this in the following form:
RKObjectMapping* mapping = [RKObjectMapping mappingForClass:[User class]];
mapping.forceCollectionMapping = YES; // RestKit cannot infer this is a collection, so we force it
[mapping mapKeyOfNestedDictionaryToAttribute:@"firstName"];
[mapping mapFromKeyPath:@"(firstName).id" toAttribute:"userID"];
[mapping mapFromKeyPath:@"(firstName).email" toAttribute:"email"];
[[RKObjectManager sharedManager].mappingProvider setMapping:mapping forKeyPath:@"users"];
*/
- (void)mapKeyOfNestedDictionaryToAttribute:(NSString*)attributeName;
/**
Removes all currently configured attribute and relationship mappings from the object mapping
*/