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

Ability to specify a subset of properties for JSON representation. #264

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Mantle/MTLJSONAdapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@
// to abort parsing (e.g., if the data is invalid).
+ (Class)classForParsingJSONDictionary:(NSDictionary *)JSONDictionary;

// Specifies the subset of properties that will be included in the JSON
// representation.
//
// Subclasses overriding this method should combine their values with those of
// `super`.
//
// Returns an array of property keys.
+ (NSArray *)propertyKeysForJSONRepresentation;

@end

// The domain for errors originating from MTLJSONAdapter.
Expand Down
19 changes: 15 additions & 4 deletions Mantle/MTLJSONAdapter.m
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,23 @@ - (NSString *)JSONKeyPathForPropertyKey:(NSString *)key {

id JSONKeyPath = self.JSONKeyPathsByPropertyKey[key];
if ([JSONKeyPath isEqual:NSNull.null]) return nil;

if (JSONKeyPath == nil) {
return key;
} else {
return JSONKeyPath;
JSONKeyPath = key;
}

if ([self.modelClass respondsToSelector:@selector(propertyKeysForJSONRepresentation)]) {
NSSet *propertyKeys = [NSSet setWithArray:[self.modelClass propertyKeysForJSONRepresentation]];

if ([propertyKeys containsObject:key]) {
return JSONKeyPath;
}
else {
return nil;
}
}

return JSONKeyPath;
}

@end
Expand Down
15 changes: 15 additions & 0 deletions MantleTests/MTLJSONAdapterSpec.m
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,19 @@
expect(error.code).to.equal(MTLJSONAdapterErrorNoClassFound);
});

it(@"should serialize a subset of properties to JSON", ^{
MTLPropertySubsetModel *model = [MTLPropertySubsetModel modelWithDictionary:@{
@"name": @"foobar",
@"count": @5
} error:NULL];

MTLJSONAdapter *adapter = [[MTLJSONAdapter alloc] initWithModel:model];

NSDictionary *JSONDictionary = @{
@"username": @"foobar"
};

expect(adapter.JSONDictionary).to.equal(JSONDictionary);
});

SpecEnd
5 changes: 5 additions & 0 deletions MantleTests/MTLTestModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,8 @@ extern const NSInteger MTLTestModelNameMissing;
// Returns a default name of 'foobar' when validateName:error: is invoked
@interface MTLSelfValidatingModel : MTLValidationModel
@end

// Specifies a subset of property keys that should be stored in JSON. The subset
// only contains a single property: "name".
@interface MTLPropertySubsetModel : MTLTestModel
@end
8 changes: 8 additions & 0 deletions MantleTests/MTLTestModel.m
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,11 @@ - (BOOL)validateName:(NSString **)name error:(NSError **)error {
}

@end

@implementation MTLPropertySubsetModel

+ (NSArray *)propertyKeysForJSONRepresentation {
return @[ @"name" ];
}

@end