diff --git a/AFNetworking/AFURLResponseSerialization.m b/AFNetworking/AFURLResponseSerialization.m index b7be3d403c..bb378faa48 100755 --- a/AFNetworking/AFURLResponseSerialization.m +++ b/AFNetworking/AFURLResponseSerialization.m @@ -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]; diff --git a/Tests/Tests/AFJSONSerializationTests.m b/Tests/Tests/AFJSONSerializationTests.m index d1b15e97a5..8be5bfeccd 100644 --- a/Tests/Tests/AFJSONSerializationTests.m +++ b/Tests/Tests/AFJSONSerializationTests.m @@ -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]; @@ -189,6 +189,7 @@ - (void)testThatJSONRemovesKeysWithNullValues { XCTAssertNotNil(responseObject[@"key"]); XCTAssertNil(responseObject[@"nullkey"]); XCTAssertNil(responseObject[@"array"][0][@"subnullkey"]); + XCTAssertEqualObjects(responseObject[@"arrayWithNulls"], @[]); } - (void)testThatJSONResponseSerializerCanBeCopied {