Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #199 from github/keypath-hook

Expose -JSONKeyPathForKey:
  • Loading branch information...
commit 439be82a87e449f26abb2171df1d32c0c15f9e72 2 parents 7a47437 + f9f70db
@jspahrsummers jspahrsummers authored
Showing with 14 additions and 11 deletions.
  1. +11 −0 Mantle/MTLJSONAdapter.h
  2. +3 −11 Mantle/MTLJSONAdapter.m
View
11 Mantle/MTLJSONAdapter.h
@@ -121,6 +121,17 @@ extern const NSInteger MTLJSONAdapterErrorInvalidJSONDictionary;
// Returns a JSON dictionary, or nil if a serialization error occurred.
- (NSDictionary *)JSONDictionary;
+// Looks up the JSON key path in the model's +propertyKeys.
+//
+// Subclasses may override this method to customize the adapter's seralizing
+// behavior. You should not call this method directly.
+//
+// key - The property key to retrieve the corresponding JSON key path for. This
+// argument must not be nil.
+//
+// Returns a key path to use, or nil to omit the property from JSON.
+- (NSString *)JSONKeyPathForPropertyKey:(NSString *)key;
+
@end
@interface MTLJSONAdapter (Deprecated)
View
14 Mantle/MTLJSONAdapter.m
@@ -36,14 +36,6 @@ @interface MTLJSONAdapter ()
// Returns a transformer to use, or nil to not transform the property.
- (NSValueTransformer *)JSONTransformerForKey:(NSString *)key;
-// Looks up the JSON key path that corresponds to the given key.
-//
-// key - The property key to retrieve the corresponding JSON key path for. This
-// argument must not be nil.
-//
-// Returns a key path to use, or nil to omit the property from JSON.
-- (NSString *)JSONKeyPathForKey:(NSString *)key;
-
@end
@implementation MTLJSONAdapter
@@ -111,7 +103,7 @@ - (id)initWithJSONDictionary:(NSDictionary *)JSONDictionary modelClass:(Class)mo
NSMutableDictionary *dictionaryValue = [[NSMutableDictionary alloc] initWithCapacity:JSONDictionary.count];
for (NSString *propertyKey in [self.modelClass propertyKeys]) {
- NSString *JSONKeyPath = [self JSONKeyPathForKey:propertyKey];
+ NSString *JSONKeyPath = [self JSONKeyPathForPropertyKey:propertyKey];
if (JSONKeyPath == nil) continue;
id value = [JSONDictionary valueForKeyPath:JSONKeyPath];
@@ -175,7 +167,7 @@ - (NSDictionary *)JSONDictionary {
NSMutableDictionary *JSONDictionary = [[NSMutableDictionary alloc] initWithCapacity:dictionaryValue.count];
[dictionaryValue enumerateKeysAndObjectsUsingBlock:^(NSString *propertyKey, id value, BOOL *stop) {
- NSString *JSONKeyPath = [self JSONKeyPathForKey:propertyKey];
+ NSString *JSONKeyPath = [self JSONKeyPathForPropertyKey:propertyKey];
if (JSONKeyPath == nil) return;
NSValueTransformer *transformer = [self JSONTransformerForKey:propertyKey];
@@ -228,7 +220,7 @@ - (NSValueTransformer *)JSONTransformerForKey:(NSString *)key {
return nil;
}
-- (NSString *)JSONKeyPathForKey:(NSString *)key {
+- (NSString *)JSONKeyPathForPropertyKey:(NSString *)key {
NSParameterAssert(key != nil);
id JSONKeyPath = self.JSONKeyPathsByPropertyKey[key];
Please sign in to comment.
Something went wrong with that request. Please try again.