Merge pull request #63 from ZhangYu0418/4.0-dev

fix: 修复接口返回包含空字符串对象引起的反序列化失败
This commit is contained in:
HuanCheng65 2021-09-18 12:17:23 +08:00 committed by GitHub
commit 4912e6366d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 0 deletions

View File

@ -0,0 +1,27 @@
package com.huanchengfly.tieba.post.api.adapters;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.huanchengfly.tieba.post.api.models.ThreadContentBean;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
public class ContentMsgAdapter implements JsonDeserializer<List<ThreadContentBean.ContentBean>> {
@Override
public List<ThreadContentBean.ContentBean> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
List<ThreadContentBean.ContentBean> list = new ArrayList<>();
if(json.isJsonArray()){
json.getAsJsonArray().forEach(element->{
if(element.isJsonObject()){
list.add(context.deserialize(element,ThreadContentBean.ContentBean.class));
}
});
}
return list;
}
}

View File

@ -2,6 +2,7 @@ package com.huanchengfly.tieba.post.api.models
import com.google.gson.annotations.JsonAdapter
import com.google.gson.annotations.SerializedName
import com.huanchengfly.tieba.post.api.adapters.ContentMsgAdapter
import com.huanchengfly.tieba.post.api.adapters.PortraitAdapter
import com.huanchengfly.tieba.post.api.adapters.SubPostListAdapter
import com.huanchengfly.tieba.post.models.BaseBean
@ -158,6 +159,7 @@ class ThreadContentBean : BaseBean() {
class OriginThreadInfo {
val title: String? = null
@JsonAdapter(ContentMsgAdapter::class)
val content: List<ContentBean>? = null
}
@ -166,6 +168,7 @@ class ThreadContentBean : BaseBean() {
val title: String? = null
val floor: String? = null
val time: String? = null
@JsonAdapter(ContentMsgAdapter::class)
val content: List<ContentBean>? = null
val agree: AgreeBean? = null