1. 程式人生 > >AFNetworking3.1.0原始碼分析(十二)AFURLResponseSerialization

AFNetworking3.1.0原始碼分析(十二)AFURLResponseSerialization

一:AFURLResponseSerialization 和 AFURLRequestSerialization 是一對出現在網路處理中,AFURLResponseSerialization主要負責對網路請求完成之後返回的結果做解析處理。

分析AFURLResponseSerialization.h  可得到,相關類的結構如下圖所示:


二:提供json解析是去除空值功能:

static id AFJSONObjectByRemovingKeysWithNullValues(id JSONObject, NSJSONReadingOptions readingOptions) {
    if ([JSONObject isKindOfClass:[NSArray class]]) {
        NSMutableArray *mutableArray = [NSMutableArray arrayWithCapacity:[(NSArray *)JSONObject count]];
        for (id value in (NSArray *)JSONObject) {
            [mutableArray addObject:AFJSONObjectByRemovingKeysWithNullValues(value, readingOptions)];
        }

        return (readingOptions & NSJSONReadingMutableContainers) ? mutableArray : [NSArray arrayWithArray:mutableArray];
    } else if ([JSONObject isKindOfClass:[NSDictionary class]]) {
        NSMutableDictionary *mutableDictionary = [NSMutableDictionary dictionaryWithDictionary:JSONObject];
        for (id <NSCopying> key in [(NSDictionary *)JSONObject allKeys]) {
            id value = (NSDictionary *)JSONObject[key];
            if (!value || [value isEqual:[NSNull null]]) {
                [mutableDictionary removeObjectForKey:key];
            } else if ([value isKindOfClass:[NSArray class]] || [value isKindOfClass:[NSDictionary class]]) {
                mutableDictionary[key] = AFJSONObjectByRemovingKeysWithNullValues(value, readingOptions);
            }
        }

        return (readingOptions & NSJSONReadingMutableContainers) ? mutableDictionary : [NSDictionary dictionaryWithDictionary:mutableDictionary];
    }

    return JSONObject;
}
其中的:NSJSONReadingOptions含義如下:
/*
     NSJSONReadingMutableContainers:返回可變容器
     NSJSONReadingMutableLeaves:返回的JSON物件中字串的值為NSMutableString
     NSJSONReadingAllowFragments:允許JSON字串最外層既不是NSArray也不是NSDictionary,但必須是有效的JSON Fragment。例如使用這個選項可以解析 @“123” 這樣的字串
     
     */
二:

AFCompoundResponseSerializer 是個符合型別的,可以設定多種解析方式處理方式如下程式碼:

+ (instancetype)compoundSerializerWithResponseSerializers:(NSArray *)responseSerializers {
    //設定可以接收的解析型別物件
    AFCompoundResponseSerializer *serializer = [[self alloc] init];
    serializer.responseSerializers = responseSerializers;

    return serializer;
}

#pragma mark - AFURLResponseSerialization

- (id)responseObjectForResponse:(NSURLResponse *)response
                           data:(NSData *)data
                          error:(NSError *__autoreleasing *)error
{
    //迴圈解析,遇見可以解析的類解析資料,如果解析資料為空,繼續執行其它的解析方式
    for (id <AFURLResponseSerialization> serializer in self.responseSerializers) {
        if (![serializer isKindOfClass:[AFHTTPResponseSerializer class]]) {
            continue;
        }

        NSError *serializerError = nil;
        id responseObject = [serializer responseObjectForResponse:response data:data error:&serializerError];
        if (responseObject) {
            if (error) {
                *error = AFErrorWithUnderlyingError(serializerError, *error);
            }

            return responseObject;
        }
    }

    return [super responseObjectForResponse:response data:data error:error];
}