昨天被提了一个需求,最近后台传来的json数据,部分类型和javabean所需要的类型有出入,导致app整个崩溃,希望对网络模块能对转换过程进行控制
虽然这是一个后台的锅,但是被甩给网络模块并甩给了我。表示无奈。。。我只是来实习的啊
为了解决这个问题,需要更深入的了解Gson,所以先用retrofit写了一个demo
retrofit @H_301_8@= new @H_301_8@Retrofit.Builder()
.baseUrl("https://api.douban.com/v2/"@H_301_8@)
.addConverterFactory(GsonConverterFactory.create@H_301_8@(gson))
.build();
@H_301_8@service@H_301_8@= retrofit@H_301_8@.create(RetrofitService.class@H_301_8@);@H_301_8@
不难看出,retrofit在此处添加了一个转换工厂,addConverterFactory(GsonConverterFactory.create(gson)),转换任务应当都交给了此处的GsonConverterFactory
所以跟进查看GsonConverterFactory
@Override
@H_301_8@public @H_301_8@Converter<ResponseBody,@H_301_8@?> responseBodyConverter@H_301_8@(Type type,@H_301_8@Annotation[] annotations,@H_301_8@ @H_301_8@Retrofit retrofit) {
TypeAdapter<?> adapter = gson@H_301_8@.getAdapter(TypeToken.get@H_301_8@(type));
@H_301_8@ return new @H_301_8@GsonResponseBodyConverter<>(gson@H_301_8@,@H_301_8@adapter);
@H_301_8@}
@Override
@H_301_8@public @H_301_8@Converter<?,@H_301_8@RequestBody> requestBodyConverter@H_301_8@(Type type,@H_301_8@ @H_301_8@Annotation[] parameterAnnotations,@H_301_8@Annotation[] methodAnnotations,@H_301_8@Retrofit retrofit) {
TypeAdapter<?> adapter = gson@H_301_8@.getAdapter(TypeToken.get@H_301_8@(type));
@H_301_8@ return new @H_301_8@GsonRequestBodyConverter<>(gson@H_301_8@,@H_301_8@adapter);
@H_301_8@}
然后在这个工厂中,一眼就可以看出最重要的就是这个请求和收到的回复的转换是通过上面两个函数
我们需要处理的是收到的回复,所以,主要看responseBodyConverter方法
第一句获得了一个TypeAdapter,稍微找点博客就可以知道,这里的TypeAdapter就是用来处理中间层,推荐博文点击打开链接
这一句的大意应该是首先获得了传入的JavaBean类的typeToken,然后根据TypeToken,gson对象动态生成了一个对应的TypeAdapter
虽然懂了,但是对于这种动态生成的过程,表示还是非常的恐惧,本来想求助一下,并没有找到相关的资料
@SuppressWarnings@H_301_8@("unchecked"@H_301_8@)
public @H_301_8@<T@H_301_8@> TypeAdapter<T@H_301_8@> getAdapter@H_301_8@(TypeToken<T@H_301_8@> type) {
TypeAdapter<?> cached = typeTokenCache@H_301_8@.get(type);
@H_301_8@ if @H_301_8@(cached != null@H_301_8@) {
return @H_301_8@(TypeAdapter<T@H_301_8@>) cached;
@H_301_8@ @H_301_8@}
Map<TypeToken<?>,@H_301_8@FutureTypeAdapter<?>> threadCalls = calls@H_301_8@.get();
@H_301_8@ boolean @H_301_8@requiresThreadLocalCleanup = false;
@H_301_8@ if @H_301_8@(threadCalls == null@H_301_8@) {
threadCalls = new @H_301_8@HashMap<TypeToken<?>,@H_301_8@FutureTypeAdapter<?>>();
@H_301_8@ @H_301_8@calls@H_301_8@.set(threadCalls);
@H_301_8@ @H_301_8@requiresThreadLocalCleanup = true;
@H_301_8@ @H_301_8@}
// the key and value type parameters always agree
@H_301_8@ @H_301_8@FutureTypeAdapter<T@H_301_8@> ongoingCall = (FutureTypeAdapter<T@H_301_8@>) threadCalls.get(type);
@H_301_8@ if @H_301_8@(ongoingCall != null@H_301_8@) {
return @H_301_8@ongoingCall;
@H_301_8@ @H_301_8@}
try @H_301_8@{
FutureTypeAdapter<T@H_301_8@> call = new @H_301_8@FutureTypeAdapter<T@H_301_8@>();
@H_301_8@ @H_301_8@threadCalls.put(type,@H_301_8@call);
@H_301_8@
@H_301_8@ for @H_301_8@(TypeAdapterFactory factory : factories@H_301_8@) {
TypeAdapter<T@H_301_8@> candidate = factory.create(this,@H_301_8@type);
@H_301_8@ if @H_301_8@(candidate != null@H_301_8@) {
call.setDelegate(candidate);
@H_301_8@ @H_301_8@typeTokenCache@H_301_8@.put(type,@H_301_8@candidate);
@H_301_8@ return @H_301_8@candidate;
@H_301_8@ @H_301_8@}
}
throw new @H_301_8@IllegalArgumentException("GSON cannot handle " @H_301_8@+ type);
@H_301_8@ @H_301_8@} finally @H_301_8@{
threadCalls.remove(type);
@H_301_8@
@H_301_8@ if @H_301_8@(requiresThreadLocalCleanup) {
calls@H_301_8@.remove();
@H_301_8@ @H_301_8@}
}
}
上面就是获取typeAdapter的方法,可以看到最重要的下面这句
for @H_301_8@(TypeAdapterFactory factory : factories@H_301_8@) {
TypeAdapter<T@H_301_8@> candidate = factory.create(this,@H_301_8@type);
@H_301_8@ if @H_301_8@(candidate != null@H_301_8@) {
call.setDelegate(candidate);
@H_301_8@ @H_301_8@typeTokenCache@H_301_8@.put(type,@H_301_8@candidate);
@H_301_8@ return @H_301_8@candidate;
@H_301_8@ @H_301_8@}
}
可以看出,该方法便利typeAdpterFactory集合,生成了TypeAdapter,那么这个factories到底是什么
// type adapters for basic platform types
@H_301_8@factories.add(TypeAdapters.STRING_FACTORY@H_301_8@);
@H_301_8@factories.add(TypeAdapters.INTEGER_FACTORY@H_301_8@);
@H_301_8@factories.add(TypeAdapters.BOOLEAN_FACTORY@H_301_8@);
@H_301_8@factories.add(TypeAdapters.BYTE_FACTORY@H_301_8@);
@H_301_8@factories.add(TypeAdapters.SHORT_FACTORY@H_301_8@);
@H_301_8@TypeAdapter<Number> longAdapter = longAdapter@H_301_8@(longSerializationPolicy);
@H_301_8@factories.add(TypeAdapters.newFactory@H_301_8@(long@H_301_8@.class,@H_301_8@Long.class,@H_301_8@longAdapter));
@H_301_8@factories.add(TypeAdapters.newFactory@H_301_8@(double@H_301_8@.class,@H_301_8@Double.class,@H_301_8@ @H_301_8@doubleAdapter(serializeSpecialFloatingPointValues)));
@H_301_8@factories.add(TypeAdapters.newFactory@H_301_8@(float@H_301_8@.class,@H_301_8@Float.class,@H_301_8@ @H_301_8@floatAdapter(serializeSpecialFloatingPointValues)));
@H_301_8@factories.add(TypeAdapters.NUMBER_FACTORY@H_301_8@);
@H_301_8@factories.add(TypeAdapters.ATOMIC_INTEGER_FACTORY@H_301_8@);
@H_301_8@factories.add(TypeAdapters.ATOMIC_BOOLEAN_FACTORY@H_301_8@);
@H_301_8@factories.add(TypeAdapters.newFactory@H_301_8@(AtomicLong.class,@H_301_8@atomicLongAdapter@H_301_8@(longAdapter)));
@H_301_8@factories.add(TypeAdapters.newFactory@H_301_8@(AtomicLongArray.class,@H_301_8@atomicLongArrayAdapter@H_301_8@(longAdapter)));
@H_301_8@factories.add(TypeAdapters.ATOMIC_INTEGER_ARRAY_FACTORY@H_301_8@);
@H_301_8@factories.add(TypeAdapters.CHARACTER_FACTORY@H_301_8@);
@H_301_8@factories.add(TypeAdapters.STRING_BUILDER_FACTORY@H_301_8@);
@H_301_8@factories.add(TypeAdapters.STRING_BUFFER_FACTORY@H_301_8@);
@H_301_8@factories.add(TypeAdapters.newFactory@H_301_8@(BigDecimal.class,@H_301_8@TypeAdapters.BIG_DECIMAL@H_301_8@));
@H_301_8@factories.add(TypeAdapters.newFactory@H_301_8@(BigInteger.class,@H_301_8@TypeAdapters.BIG_INTEGER@H_301_8@));
@H_301_8@factories.add(TypeAdapters.URL_FACTORY@H_301_8@);
@H_301_8@factories.add(TypeAdapters.URI_FACTORY@H_301_8@);
@H_301_8@factories.add(TypeAdapters.UUID_FACTORY@H_301_8@);
@H_301_8@factories.add(TypeAdapters.CURRENCY_FACTORY@H_301_8@);
@H_301_8@factories.add(TypeAdapters.LOCALE_FACTORY@H_301_8@);
@H_301_8@factories.add(TypeAdapters.INET_ADDRESS_FACTORY@H_301_8@);
@H_301_8@factories.add(TypeAdapters.BIT_SET_FACTORY@H_301_8@);
@H_301_8@factories.add(DateTypeAdapter.FACTORY@H_301_8@);
@H_301_8@factories.add(TypeAdapters.CALENDAR_FACTORY@H_301_8@);
@H_301_8@factories.add(TimeTypeAdapter.FACTORY@H_301_8@);
@H_301_8@factories.add(sqlDateTypeAdapter.FACTORY@H_301_8@);
@H_301_8@factories.add(TypeAdapters.TIMESTAMP_FACTORY@H_301_8@);
@H_301_8@factories.add(ArrayTypeAdapter.FACTORY@H_301_8@);
@H_301_8@factories.add(TypeAdapters.CLASS_FACTORY@H_301_8@);
@H_301_8@
@H_301_8@// type adapters for composite and user-defined types
@H_301_8@factories.add(new @H_301_8@CollectionTypeAdapterFactory(constructorConstructor@H_301_8@));
@H_301_8@factories.add(new @H_301_8@MapTypeAdapterFactory(constructorConstructor@H_301_8@,@H_301_8@complexMapKeySerialization));
@H_301_8@factories.add(new @H_301_8@JsonAdapterAnnotationTypeAdapterFactory(constructorConstructor@H_301_8@));
@H_301_8@factories.add(TypeAdapters.ENUM_FACTORY@H_301_8@);
@H_301_8@factories.add(new @H_301_8@ReflectiveTypeAdapterFactory(
constructorConstructor@H_301_8@,@H_301_8@fieldNamingPolicy,@H_301_8@excluder));@H_301_8@
原来在Gson生成时,就自定义添加了大量的factory,具体找一个String类型的Factory看一下
public static final @H_301_8@TypeAdapterFactory STRING_FACTORY@H_301_8@ = newFactory@H_301_8@(String.class,@H_301_8@STRING@H_301_8@);@H_301_8@
public static final @H_301_8@TypeAdapter<String> STRING@H_301_8@ = new @H_301_8@TypeAdapter<String>() {
@Override
@H_301_8@ @H_301_8@public @H_301_8@String read@H_301_8@(JsonReader in) throws @H_301_8@IOException {
JsonToken peek = in.peek();
@H_301_8@ if @H_301_8@(peek == JsonToken.NULL@H_301_8@) {
in.nextNull();
@H_301_8@ return null;
@H_301_8@ @H_301_8@}
/* coerce booleans to strings for backwards compatibility */
@H_301_8@ @H_301_8@if @H_301_8@(peek == JsonToken.BOOLEAN@H_301_8@) {
return @H_301_8@Boolean.toString@H_301_8@(in.nextBoolean());
@H_301_8@ @H_301_8@}
return @H_301_8@in.nextString();
@H_301_8@ @H_301_8@}
@Override
@H_301_8@ @H_301_8@public void @H_301_8@write@H_301_8@(JsonWriter out,@H_301_8@String value) throws @H_301_8@IOException {
out.value(value);
@H_301_8@ @H_301_8@}
};@H_301_8@
这里我猜想是通过TypeAdapter返回的类型,递归的实现对应类的TypeAdapter。而且在这个typeAdapter中是可以对输入的Typetoken进行判断的,所以如果我能够干涉这个factory集合,就可以实现所需要的功能。
索性,再Gson builder中,作者预留了这个接口
public @H_301_8@GsonBuilder registerTypeAdapterFactory@H_301_8@(TypeAdapterFactory factory) {
factories@H_301_8@.add(factory);
@H_301_8@ return this;
@H_301_8@}
而且,再GsonBuilder生成Gson的过程中,是先把自定义的TypeAdapterFactory添加进去的,而遍历TypeAdapterFactory集合的时候,一旦有了符合的对象,就会返回TypeAdapter的。这就意味着,自定义的TypeAdapterFactory是有更高的优先级。所以直接添加自定义的TypeAdapterFactory就可以实现需求
例如,我需要对String类型预先判断,那么就可以这么做
1.先自定义一个TypeAdapters和对应的TypeAdapterFactory
public class @H_301_8@TypeAdapters {
public static final @H_301_8@TypeAdapter<String> STRING @H_301_8@= new @H_301_8@TypeAdapter<String>() {
@Override
@H_301_8@ @H_301_8@public @H_301_8@String read@H_301_8@(JsonReader in) throws @H_301_8@IOException {
JsonToken peek = in.peek();
@H_301_8@ if @H_301_8@(peek == JsonToken.NULL@H_301_8@) {
in.nextNull();
@H_301_8@ return null;
@H_301_8@ @H_301_8@}
/* coerce booleans to strings for backwards compatibility */
@H_301_8@
@H_301_8@ @H_301_8@if @H_301_8@(peek == JsonToken.BOOLEAN@H_301_8@) {
return @H_301_8@Boolean.toString@H_301_8@(in.nextBoolean());
@H_301_8@ @H_301_8@}
if@H_301_8@(peek!=JsonToken.STRING@H_301_8@){
in.skipValue();
@H_301_8@ return @H_301_8@""@H_301_8@;
@H_301_8@ @H_301_8@}
return @H_301_8@in.nextString();
@H_301_8@ @H_301_8@}
@Override
@H_301_8@ @H_301_8@public void @H_301_8@write@H_301_8@(JsonWriter out,@H_301_8@String value) throws @H_301_8@IOException {
out.value(value);
@H_301_8@ @H_301_8@}
};
@H_301_8@ public static final @H_301_8@TypeAdapterFactory STRING_FACTORY @H_301_8@= newFactory@H_301_8@(String.class,@H_301_8@STRING@H_301_8@);
@H_301_8@}
然后在构造Gson中
GsonBuilder gsonBuild=new @H_301_8@GsonBuilder();
@H_301_8@Gson gson=gsonBuild.registerTypeAdapterFactory(TypeAdapters.STRING_FACTORY@H_301_8@).create();@H_301_8@
即可 原文链接:https://www.f2er.com/json/288796.html