介紹
Retrofit 默認(rèn)提供了gson和Jackson對(duì)應(yīng)的converter,但是并沒有提供fastjson轉(zhuǎn)換。在參考了源碼的基礎(chǔ)之上對(duì)fastjson的使用進(jìn)行簡(jiǎn)單介紹厦章。
源碼分析
在此穆壕,我選擇了gson轉(zhuǎn)換的三個(gè)類GsonConverterFactory
,GsonRequestBodyConverter
,GsonResponseBodyConverter
苹祟。
- 首先
GsonConverterFactory
類:
該類主要是將GsonRequestBodyConverter
,GsonResponseBodyConverter
整合到一起趟庄,后面使用的時(shí)候直接調(diào)用該類即可泪喊。
public final class GsonConverterFactory extends Converter.Factory {
public static GsonConverterFactory create() {
return create(new Gson());
}
@SuppressWarnings("ConstantConditions")
public static GsonConverterFactory create(Gson gson) {
if (gson == null) throw new NullPointerException("gson == null");
return new GsonConverterFactory(gson);
}
private final Gson gson;
private GsonConverterFactory(Gson gson) {
this.gson = gson;
}
@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations,
Retrofit retrofit) {
TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));
return new GsonResponseBodyConverter<>(gson, adapter);
}
@Override
public Converter<?, RequestBody> requestBodyConverter(Type type,
Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));
return new GsonRequestBodyConverter<>(gson, adapter);
}
}
- 參考得到
FastJsonConverterFactory
:這里也是整合作用窿克,不同于gson,fastjson需要以序列化的方式轉(zhuǎn)換骏庸。gson也可以使用序列化毛甲,這里使用TypeAdapter
適配器可以提高效率。
public class FastJsonConverterFactory extends Converter.Factory {
private ParserConfig mParserConfig = ParserConfig.getGlobalInstance();
private int featureValues = JSON.DEFAULT_PARSER_FEATURE;
private Feature[] features;
private SerializeConfig serializeConfig;
private SerializerFeature[] serializerFeatures = {SerializerFeature.WriteNullListAsEmpty,
SerializerFeature.SkipTransientField,
SerializerFeature.DisableCircularReferenceDetect};
public static FastJsonConverterFactory create() {
return new FastJsonConverterFactory();
}
private FastJsonConverterFactory() {
}
@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations,
Retrofit retrofit) {
return new FastJsonResponseBodyConverter<>(type, mParserConfig, featureValues, features);
}
@Override
public Converter<?, RequestBody> requestBodyConverter(Type type,
Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
return new FastJsonRequestBodyConverter<>(serializeConfig, serializerFeatures);
}
//get,set方法
...
}
+FastJsonRequestBodyConverter
類具被,對(duì)請(qǐng)求轉(zhuǎn)化:
final class FastJsonRequestBodyConverter<T> implements Converter<T, RequestBody> {
private static final MediaType MEDIA_TYPE = MediaType.parse("application/json; charset=UTF-8");
private SerializeConfig serializeConfig;
private SerializerFeature[] serializerFeatures;
FastJsonRequestBodyConverter(SerializeConfig config, SerializerFeature... features) {
serializeConfig = config;
serializerFeatures = features;
}
@Override
public RequestBody convert(T value) throws IOException {
byte[] content;
if (serializeConfig != null) {
if (serializerFeatures != null) {
content = JSON.toJSONBytes(value, serializeConfig, serializerFeatures);
} else {
content = JSON.toJSONBytes(value, serializeConfig);
}
} else {
if (serializerFeatures != null) {
content = JSON.toJSONBytes(value, serializerFeatures);
} else {
content = JSON.toJSONBytes(value);
}
}
return RequestBody.create(MEDIA_TYPE, content);
}
}
+FastJsonResponseBodyConverter
類玻募,對(duì)響應(yīng)轉(zhuǎn)化:
final class FastJsonResponseBodyConverter<T> implements Converter<ResponseBody, T> {
private static final Feature[] EMPTY_SERIALIZER_FEATURES = new Feature[0];
private Type mType;
private ParserConfig config;
private int featureValues;
private Feature[] features;
FastJsonResponseBodyConverter(Type type, ParserConfig config, int featureValues,
Feature... features) {
mType = type;
this.config = config;
this.featureValues = featureValues;
this.features = features;
}
@Override
@SuppressWarnings("unchecked")
public T convert(ResponseBody value) throws IOException {
try {
if (mType == String.class) {
return (T) value.string();
}
return JSON.parseObject(value.string(), mType, config, featureValues,
features != null ? features : EMPTY_SERIALIZER_FEATURES);
} finally {
value.close();
}
}
}