Skip to content

Commit

Permalink
Merge pull request #160 from github/nil-dictionary-error
Browse files Browse the repository at this point in the history
JSON dictionary error handling redux
  • Loading branch information
Keith Duncan committed Oct 10, 2013
2 parents 6cbdbeb + c82064b commit 7eecee4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
6 changes: 5 additions & 1 deletion Mantle/MTLJSONAdapter.h
Expand Up @@ -60,6 +60,9 @@ extern NSString * const MTLJSONAdapterErrorDomain;
// +classForParsingJSONDictionary: returned nil for the given dictionary.
extern const NSInteger MTLJSONAdapterErrorNoClassFound;

// The provided JSONDictionary is not valid.
extern const NSInteger MTLJSONAdapterErrorInvalidJSONDictionary;

// Converts a MTLModel object to and from a JSON dictionary.
@interface MTLJSONAdapter : NSObject

Expand Down Expand Up @@ -95,7 +98,8 @@ extern const NSInteger MTLJSONAdapterErrorNoClassFound;
//
// JSONDictionary - A dictionary representing JSON data. This should match the
// format returned by NSJSONSerialization. If this argument is
// nil, the method returns nil.
// nil, the method returns nil and an error with code
// MTLJSONAdapterErrorInvalidJSONDictionary.
// modelClass - The MTLModel subclass to attempt to parse from the JSON.
// This class must conform to <MTLJSONSerializing>. This
// argument must not be nil.
Expand Down
13 changes: 12 additions & 1 deletion Mantle/MTLJSONAdapter.m
Expand Up @@ -12,6 +12,7 @@

NSString * const MTLJSONAdapterErrorDomain = @"MTLJSONAdapterErrorDomain";
const NSInteger MTLJSONAdapterErrorNoClassFound = 2;
const NSInteger MTLJSONAdapterErrorInvalidJSONDictionary = 3;

// An exception was thrown and caught.
static const NSInteger MTLJSONAdapterErrorExceptionThrown = 1;
Expand Down Expand Up @@ -71,7 +72,17 @@ - (id)initWithJSONDictionary:(NSDictionary *)JSONDictionary modelClass:(Class)mo
NSParameterAssert([modelClass isSubclassOfClass:MTLModel.class]);
NSParameterAssert([modelClass conformsToProtocol:@protocol(MTLJSONSerializing)]);

if (JSONDictionary == nil) return nil;

if (JSONDictionary == nil) {
if (error != NULL) {
NSDictionary *userInfo = @{
NSLocalizedDescriptionKey: NSLocalizedString(@"Invalid JSONDictionary", @""),
NSLocalizedFailureReasonErrorKey: NSLocalizedString(@"The JSONDictionary parameter is invalid.", @"")
};
*error = [NSError errorWithDomain:MTLJSONAdapterErrorDomain code:MTLJSONAdapterErrorInvalidJSONDictionary userInfo:userInfo];
}
return nil;
}

if ([modelClass respondsToSelector:@selector(classForParsingJSONDictionary:)]) {
modelClass = [modelClass classForParsingJSONDictionary:JSONDictionary];
Expand Down
6 changes: 4 additions & 2 deletions MantleTests/MTLJSONAdapterSpec.m
Expand Up @@ -73,11 +73,13 @@
expect([MTLJSONAdapter JSONDictionaryFromModel:model]).to.equal(values);
});

it(@"should return nil with a nil JSON dictionary, but no error", ^{
it(@"should return nil and an error with a nil JSON dictionary", ^{
NSError *error = nil;
MTLJSONAdapter *adapter = [[MTLJSONAdapter alloc] initWithJSONDictionary:nil modelClass:MTLTestModel.class error:&error];
expect(adapter).to.beNil();
expect(error).to.beNil();
expect(error).notTo.beNil();
expect(error.domain).to.equal(MTLJSONAdapterErrorDomain);
expect(error.code).to.equal(MTLJSONAdapterErrorInvalidJSONDictionary);
});

it(@"should ignore unrecognized JSON keys", ^{
Expand Down

0 comments on commit 7eecee4

Please sign in to comment.