Skip to content

Commit

Permalink
Modifies AFJSONObjectByRemovingKeysWithNullValues to remove NSArray n…
Browse files Browse the repository at this point in the history
…ull objects.
  • Loading branch information
ashfurrow committed Sep 18, 2017
1 parent dd59ffa commit 7576300
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
4 changes: 3 additions & 1 deletion AFNetworking/AFURLResponseSerialization.m
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ static id AFJSONObjectByRemovingKeysWithNullValues(id JSONObject, NSJSONReadingO
if ([JSONObject isKindOfClass:[NSArray class]]) {
NSMutableArray *mutableArray = [NSMutableArray arrayWithCapacity:[(NSArray *)JSONObject count]];
for (id value in (NSArray *)JSONObject) {
[mutableArray addObject:AFJSONObjectByRemovingKeysWithNullValues(value, readingOptions)];
if (![value isEqual:[NSNull null]]) {
[mutableArray addObject:AFJSONObjectByRemovingKeysWithNullValues(value, readingOptions)];
}
}

return (readingOptions & NSJSONReadingMutableContainers) ? mutableArray : [NSArray arrayWithArray:mutableArray];
Expand Down
3 changes: 2 additions & 1 deletion Tests/Tests/AFJSONSerializationTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ - (void)testThatJSONResponseSerializerReturnsNilObjectAndNilErrorForSingleSpace
- (void)testThatJSONRemovesKeysWithNullValues {
self.responseSerializer.removesKeysWithNullValues = YES;
NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] initWithURL:self.baseURL statusCode:200 HTTPVersion:@"1.1" headerFields:@{@"Content-Type":@"text/json"}];
NSData *data = [NSJSONSerialization dataWithJSONObject:@{@"key":@"value",@"nullkey":[NSNull null],@"array":@[@{@"subnullkey":[NSNull null]}]}
NSData *data = [NSJSONSerialization dataWithJSONObject:@{@"key":@"value",@"nullkey":[NSNull null],@"array":@[@{@"subnullkey":[NSNull null]}], @"arrayWithNulls": @[[NSNull null]]}
options:(NSJSONWritingOptions)0
error:nil];

Expand All @@ -189,6 +189,7 @@ - (void)testThatJSONRemovesKeysWithNullValues {
XCTAssertNotNil(responseObject[@"key"]);
XCTAssertNil(responseObject[@"nullkey"]);
XCTAssertNil(responseObject[@"array"][0][@"subnullkey"]);
XCTAssertEqualObjects(responseObject[@"arrayWithNulls"], @[]);
}

- (void)testThatJSONResponseSerializerCanBeCopied {
Expand Down

0 comments on commit 7576300

Please sign in to comment.