🎨 解析响应的Gson构建类调整为单例

This commit is contained in:
DDERGOU
2022-01-11 12:44:50 +08:00
committed by GitHub
parent 4c781ee7ee
commit ee17a5ee6f
5 changed files with 183 additions and 139 deletions

View File

@ -1,32 +1,41 @@
package me.chanjar.weixin.common.util.json; package me.chanjar.weixin.common.util.json;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import me.chanjar.weixin.common.bean.WxAccessToken; import me.chanjar.weixin.common.bean.WxAccessToken;
import me.chanjar.weixin.common.bean.WxNetCheckResult; import me.chanjar.weixin.common.bean.WxNetCheckResult;
import me.chanjar.weixin.common.bean.menu.WxMenu; import me.chanjar.weixin.common.bean.menu.WxMenu;
import me.chanjar.weixin.common.error.WxError; import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult; import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
import java.util.Objects;
/**
* . /**
* @author chanjarster * .
*/ * @author chanjarster
public class WxGsonBuilder { */
private static final GsonBuilder INSTANCE = new GsonBuilder(); public class WxGsonBuilder {
private static final GsonBuilder INSTANCE = new GsonBuilder();
static { private static volatile Gson GSON_INSTANCE;
INSTANCE.disableHtmlEscaping();
INSTANCE.registerTypeAdapter(WxAccessToken.class, new WxAccessTokenAdapter()); static {
INSTANCE.registerTypeAdapter(WxError.class, new WxErrorAdapter()); INSTANCE.disableHtmlEscaping();
INSTANCE.registerTypeAdapter(WxMenu.class, new WxMenuGsonAdapter()); INSTANCE.registerTypeAdapter(WxAccessToken.class, new WxAccessTokenAdapter());
INSTANCE.registerTypeAdapter(WxMediaUploadResult.class, new WxMediaUploadResultAdapter()); INSTANCE.registerTypeAdapter(WxError.class, new WxErrorAdapter());
INSTANCE.registerTypeAdapter(WxNetCheckResult.class, new WxNetCheckResultGsonAdapter()); INSTANCE.registerTypeAdapter(WxMenu.class, new WxMenuGsonAdapter());
INSTANCE.registerTypeAdapter(WxMediaUploadResult.class, new WxMediaUploadResultAdapter());
} INSTANCE.registerTypeAdapter(WxNetCheckResult.class, new WxNetCheckResultGsonAdapter());
public static Gson create() { }
return INSTANCE.create();
} public static Gson create() {
if (Objects.isNull(GSON_INSTANCE)) {
} synchronized (GSON_INSTANCE) {
if (Objects.isNull(GSON_INSTANCE)) {
GSON_INSTANCE = INSTANCE.create();
}
}
}
return GSON_INSTANCE;
}
}

View File

@ -1,34 +1,43 @@
package me.chanjar.weixin.cp.util.json; package me.chanjar.weixin.cp.util.json;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import me.chanjar.weixin.common.bean.menu.WxMenu; import me.chanjar.weixin.common.bean.menu.WxMenu;
import me.chanjar.weixin.common.error.WxError; import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.util.json.WxErrorAdapter; import me.chanjar.weixin.common.util.json.WxErrorAdapter;
import me.chanjar.weixin.cp.bean.WxCpChat; import me.chanjar.weixin.cp.bean.WxCpChat;
import me.chanjar.weixin.cp.bean.WxCpDepart; import me.chanjar.weixin.cp.bean.WxCpDepart;
import me.chanjar.weixin.cp.bean.WxCpTag; import me.chanjar.weixin.cp.bean.WxCpTag;
import me.chanjar.weixin.cp.bean.WxCpUser; import me.chanjar.weixin.cp.bean.WxCpUser;
import java.util.Objects;
/**
* @author Daniel Qian /**
*/ * @author Daniel Qian
public class WxCpGsonBuilder { */
public class WxCpGsonBuilder {
private static final GsonBuilder INSTANCE = new GsonBuilder();
private static final GsonBuilder INSTANCE = new GsonBuilder();
static { private static volatile Gson GSON_INSTANCE;
INSTANCE.disableHtmlEscaping();
INSTANCE.registerTypeAdapter(WxCpChat.class, new WxCpChatGsonAdapter()); static {
INSTANCE.registerTypeAdapter(WxCpDepart.class, new WxCpDepartGsonAdapter()); INSTANCE.disableHtmlEscaping();
INSTANCE.registerTypeAdapter(WxCpUser.class, new WxCpUserGsonAdapter()); INSTANCE.registerTypeAdapter(WxCpChat.class, new WxCpChatGsonAdapter());
INSTANCE.registerTypeAdapter(WxError.class, new WxErrorAdapter()); INSTANCE.registerTypeAdapter(WxCpDepart.class, new WxCpDepartGsonAdapter());
INSTANCE.registerTypeAdapter(WxMenu.class, new WxCpMenuGsonAdapter()); INSTANCE.registerTypeAdapter(WxCpUser.class, new WxCpUserGsonAdapter());
INSTANCE.registerTypeAdapter(WxCpTag.class, new WxCpTagGsonAdapter()); INSTANCE.registerTypeAdapter(WxError.class, new WxErrorAdapter());
} INSTANCE.registerTypeAdapter(WxMenu.class, new WxCpMenuGsonAdapter());
INSTANCE.registerTypeAdapter(WxCpTag.class, new WxCpTagGsonAdapter());
public static Gson create() { }
return INSTANCE.create();
} public static Gson create() {
if (Objects.isNull(GSON_INSTANCE)) {
} synchronized (GSON_INSTANCE) {
if (Objects.isNull(GSON_INSTANCE)) {
GSON_INSTANCE = INSTANCE.create();
}
}
}
return GSON_INSTANCE;
}
}

View File

@ -11,12 +11,14 @@ import cn.binarywang.wx.miniapp.bean.code.WxMaCodeVersionDistribution;
import cn.binarywang.wx.miniapp.json.adaptor.*; import cn.binarywang.wx.miniapp.json.adaptor.*;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import java.util.Objects;
/** /**
* @author <a href="https://github.com/binarywang">Binary Wang</a> * @author <a href="https://github.com/binarywang">Binary Wang</a>
*/ */
public class WxMaGsonBuilder { public class WxMaGsonBuilder {
private static final GsonBuilder INSTANCE = new GsonBuilder(); private static final GsonBuilder INSTANCE = new GsonBuilder();
private static volatile Gson GSON_INSTANCE;
static { static {
INSTANCE.disableHtmlEscaping(); INSTANCE.disableHtmlEscaping();
@ -31,7 +33,14 @@ public class WxMaGsonBuilder {
} }
public static Gson create() { public static Gson create() {
return INSTANCE.create(); if (Objects.isNull(GSON_INSTANCE)) {
synchronized (GSON_INSTANCE) {
if (Objects.isNull(GSON_INSTANCE)) {
GSON_INSTANCE = INSTANCE.create();
}
}
}
return GSON_INSTANCE;
} }
} }

View File

@ -1,70 +1,79 @@
package me.chanjar.weixin.mp.util.json; package me.chanjar.weixin.mp.util.json;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import me.chanjar.weixin.mp.bean.*; import me.chanjar.weixin.mp.bean.*;
import me.chanjar.weixin.mp.bean.card.WxMpCard; import me.chanjar.weixin.mp.bean.card.WxMpCard;
import me.chanjar.weixin.mp.bean.card.WxMpCardResult; import me.chanjar.weixin.mp.bean.card.WxMpCardResult;
import me.chanjar.weixin.mp.bean.datacube.WxDataCubeUserCumulate; import me.chanjar.weixin.mp.bean.datacube.WxDataCubeUserCumulate;
import me.chanjar.weixin.mp.bean.datacube.WxDataCubeUserSummary; import me.chanjar.weixin.mp.bean.datacube.WxDataCubeUserSummary;
import me.chanjar.weixin.mp.bean.kefu.WxMpKefuMessage; import me.chanjar.weixin.mp.bean.kefu.WxMpKefuMessage;
import me.chanjar.weixin.mp.bean.material.*; import me.chanjar.weixin.mp.bean.material.*;
import me.chanjar.weixin.mp.bean.card.membercard.WxMpMemberCardActivateTempInfoResult; import me.chanjar.weixin.mp.bean.card.membercard.WxMpMemberCardActivateTempInfoResult;
import me.chanjar.weixin.mp.bean.card.membercard.WxMpMemberCardUpdateResult; import me.chanjar.weixin.mp.bean.card.membercard.WxMpMemberCardUpdateResult;
import me.chanjar.weixin.mp.bean.card.membercard.WxMpMemberCardUserInfoResult; import me.chanjar.weixin.mp.bean.card.membercard.WxMpMemberCardUserInfoResult;
import me.chanjar.weixin.mp.bean.result.*; import me.chanjar.weixin.mp.bean.result.*;
import me.chanjar.weixin.mp.bean.subscribe.WxMpSubscribeMessage; import me.chanjar.weixin.mp.bean.subscribe.WxMpSubscribeMessage;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateIndustry; import me.chanjar.weixin.mp.bean.template.WxMpTemplateIndustry;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage; import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import java.util.Objects;
/**
* @author someone /**
*/ * @author someone
public class WxMpGsonBuilder { */
public class WxMpGsonBuilder {
private static final GsonBuilder INSTANCE = new GsonBuilder();
private static final GsonBuilder INSTANCE = new GsonBuilder();
static { private static volatile Gson GSON_INSTANCE;
INSTANCE.disableHtmlEscaping();
INSTANCE.registerTypeAdapter(WxMpKefuMessage.class, new WxMpKefuMessageGsonAdapter()); static {
INSTANCE.registerTypeAdapter(WxMpMassNews.class, new WxMpMassNewsGsonAdapter()); INSTANCE.disableHtmlEscaping();
INSTANCE.registerTypeAdapter(WxMpMassTagMessage.class, new WxMpMassTagMessageGsonAdapter()); INSTANCE.registerTypeAdapter(WxMpKefuMessage.class, new WxMpKefuMessageGsonAdapter());
INSTANCE.registerTypeAdapter(WxMpMassOpenIdsMessage.class, new WxMpMassOpenIdsMessageGsonAdapter()); INSTANCE.registerTypeAdapter(WxMpMassNews.class, new WxMpMassNewsGsonAdapter());
INSTANCE.registerTypeAdapter(WxMpUser.class, new WxMpUserGsonAdapter()); INSTANCE.registerTypeAdapter(WxMpMassTagMessage.class, new WxMpMassTagMessageGsonAdapter());
INSTANCE.registerTypeAdapter(WxMpChangeOpenid.class, new WxMpChangeOpenidGsonAdapter()); INSTANCE.registerTypeAdapter(WxMpMassOpenIdsMessage.class, new WxMpMassOpenIdsMessageGsonAdapter());
INSTANCE.registerTypeAdapter(WxMpUserList.class, new WxUserListGsonAdapter()); INSTANCE.registerTypeAdapter(WxMpUser.class, new WxMpUserGsonAdapter());
INSTANCE.registerTypeAdapter(WxMpMassVideo.class, new WxMpMassVideoAdapter()); INSTANCE.registerTypeAdapter(WxMpChangeOpenid.class, new WxMpChangeOpenidGsonAdapter());
INSTANCE.registerTypeAdapter(WxMpMassSendResult.class, new WxMpMassSendResultAdapter()); INSTANCE.registerTypeAdapter(WxMpUserList.class, new WxUserListGsonAdapter());
INSTANCE.registerTypeAdapter(WxMpMassUploadResult.class, new WxMpMassUploadResultAdapter()); INSTANCE.registerTypeAdapter(WxMpMassVideo.class, new WxMpMassVideoAdapter());
INSTANCE.registerTypeAdapter(WxMpQrCodeTicket.class, new WxQrCodeTicketAdapter()); INSTANCE.registerTypeAdapter(WxMpMassSendResult.class, new WxMpMassSendResultAdapter());
INSTANCE.registerTypeAdapter(WxMpTemplateMessage.class, new WxMpTemplateMessageGsonAdapter()); INSTANCE.registerTypeAdapter(WxMpMassUploadResult.class, new WxMpMassUploadResultAdapter());
INSTANCE.registerTypeAdapter(WxMpSubscribeMessage.class, new WxMpSubscribeMessageGsonAdapter()); INSTANCE.registerTypeAdapter(WxMpQrCodeTicket.class, new WxQrCodeTicketAdapter());
INSTANCE.registerTypeAdapter(WxMpSemanticQueryResult.class, new WxMpSemanticQueryResultAdapter()); INSTANCE.registerTypeAdapter(WxMpTemplateMessage.class, new WxMpTemplateMessageGsonAdapter());
INSTANCE.registerTypeAdapter(WxDataCubeUserSummary.class, new WxMpUserSummaryGsonAdapter()); INSTANCE.registerTypeAdapter(WxMpSubscribeMessage.class, new WxMpSubscribeMessageGsonAdapter());
INSTANCE.registerTypeAdapter(WxDataCubeUserCumulate.class, new WxMpUserCumulateGsonAdapter()); INSTANCE.registerTypeAdapter(WxMpSemanticQueryResult.class, new WxMpSemanticQueryResultAdapter());
INSTANCE.registerTypeAdapter(WxMpMaterialUploadResult.class, new WxMpMaterialUploadResultAdapter()); INSTANCE.registerTypeAdapter(WxDataCubeUserSummary.class, new WxMpUserSummaryGsonAdapter());
INSTANCE.registerTypeAdapter(WxMpMaterialVideoInfoResult.class, new WxMpMaterialVideoInfoResultAdapter()); INSTANCE.registerTypeAdapter(WxDataCubeUserCumulate.class, new WxMpUserCumulateGsonAdapter());
INSTANCE.registerTypeAdapter(WxMpMaterialArticleUpdate.class, new WxMpMaterialArticleUpdateGsonAdapter()); INSTANCE.registerTypeAdapter(WxMpMaterialUploadResult.class, new WxMpMaterialUploadResultAdapter());
INSTANCE.registerTypeAdapter(WxMpMaterialCountResult.class, new WxMpMaterialCountResultAdapter()); INSTANCE.registerTypeAdapter(WxMpMaterialVideoInfoResult.class, new WxMpMaterialVideoInfoResultAdapter());
INSTANCE.registerTypeAdapter(WxMpMaterialNews.class, new WxMpMaterialNewsGsonAdapter()); INSTANCE.registerTypeAdapter(WxMpMaterialArticleUpdate.class, new WxMpMaterialArticleUpdateGsonAdapter());
INSTANCE.registerTypeAdapter(WxMpNewsArticle.class, new WxMpNewsArticleGsonAdapter()); INSTANCE.registerTypeAdapter(WxMpMaterialCountResult.class, new WxMpMaterialCountResultAdapter());
INSTANCE.registerTypeAdapter(WxMpMaterialNewsBatchGetResult.class, new WxMpMaterialNewsBatchGetGsonAdapter()); INSTANCE.registerTypeAdapter(WxMpMaterialNews.class, new WxMpMaterialNewsGsonAdapter());
INSTANCE.registerTypeAdapter(WxMpMaterialNewsBatchGetResult.WxMaterialNewsBatchGetNewsItem.class, new WxMpMaterialNewsBatchGetGsonItemAdapter()); INSTANCE.registerTypeAdapter(WxMpNewsArticle.class, new WxMpNewsArticleGsonAdapter());
INSTANCE.registerTypeAdapter(WxMpMaterialFileBatchGetResult.class, new WxMpMaterialFileBatchGetGsonAdapter()); INSTANCE.registerTypeAdapter(WxMpMaterialNewsBatchGetResult.class, new WxMpMaterialNewsBatchGetGsonAdapter());
INSTANCE.registerTypeAdapter(WxMpMaterialFileBatchGetResult.WxMaterialFileBatchGetNewsItem.class, new WxMpMaterialFileBatchGetGsonItemAdapter()); INSTANCE.registerTypeAdapter(WxMpMaterialNewsBatchGetResult.WxMaterialNewsBatchGetNewsItem.class, new WxMpMaterialNewsBatchGetGsonItemAdapter());
INSTANCE.registerTypeAdapter(WxMpCardResult.class, new WxMpCardResultGsonAdapter()); INSTANCE.registerTypeAdapter(WxMpMaterialFileBatchGetResult.class, new WxMpMaterialFileBatchGetGsonAdapter());
INSTANCE.registerTypeAdapter(WxMpCard.class, new WxMpCardGsonAdapter()); INSTANCE.registerTypeAdapter(WxMpMaterialFileBatchGetResult.WxMaterialFileBatchGetNewsItem.class, new WxMpMaterialFileBatchGetGsonItemAdapter());
INSTANCE.registerTypeAdapter(WxMpMassPreviewMessage.class, new WxMpMassPreviewMessageGsonAdapter()); INSTANCE.registerTypeAdapter(WxMpCardResult.class, new WxMpCardResultGsonAdapter());
INSTANCE.registerTypeAdapter(WxMediaImgUploadResult.class, new WxMediaImgUploadResultGsonAdapter()); INSTANCE.registerTypeAdapter(WxMpCard.class, new WxMpCardGsonAdapter());
INSTANCE.registerTypeAdapter(WxMpTemplateIndustry.class, new WxMpIndustryGsonAdapter()); INSTANCE.registerTypeAdapter(WxMpMassPreviewMessage.class, new WxMpMassPreviewMessageGsonAdapter());
INSTANCE.registerTypeAdapter(WxMpUserBlacklistGetResult.class, new WxUserBlacklistGetResultGsonAdapter()); INSTANCE.registerTypeAdapter(WxMediaImgUploadResult.class, new WxMediaImgUploadResultGsonAdapter());
INSTANCE.registerTypeAdapter(WxMpMemberCardUserInfoResult.class, new WxMpMemberCardUserInfoResultGsonAdapter()); INSTANCE.registerTypeAdapter(WxMpTemplateIndustry.class, new WxMpIndustryGsonAdapter());
INSTANCE.registerTypeAdapter(WxMpMemberCardUpdateResult.class, new WxMpMemberCardUpdateResultGsonAdapter()); INSTANCE.registerTypeAdapter(WxMpUserBlacklistGetResult.class, new WxUserBlacklistGetResultGsonAdapter());
INSTANCE.registerTypeAdapter(WxMpMemberCardActivateTempInfoResult.class, new WxMpMemberCardActivateTempInfoResultGsonAdapter()); INSTANCE.registerTypeAdapter(WxMpMemberCardUserInfoResult.class, new WxMpMemberCardUserInfoResultGsonAdapter());
} INSTANCE.registerTypeAdapter(WxMpMemberCardUpdateResult.class, new WxMpMemberCardUpdateResultGsonAdapter());
INSTANCE.registerTypeAdapter(WxMpMemberCardActivateTempInfoResult.class, new WxMpMemberCardActivateTempInfoResultGsonAdapter());
public static Gson create() { }
return INSTANCE.create();
} public static Gson create() {
if (Objects.isNull(GSON_INSTANCE)) {
} synchronized (GSON_INSTANCE) {
if (Objects.isNull(GSON_INSTANCE)) {
GSON_INSTANCE = INSTANCE.create();
}
}
}
return GSON_INSTANCE;
}
}

View File

@ -7,6 +7,7 @@ import me.chanjar.weixin.open.bean.WxOpenComponentAccessToken;
import me.chanjar.weixin.open.bean.auth.WxOpenAuthorizationInfo; import me.chanjar.weixin.open.bean.auth.WxOpenAuthorizationInfo;
import me.chanjar.weixin.open.bean.auth.WxOpenAuthorizerInfo; import me.chanjar.weixin.open.bean.auth.WxOpenAuthorizerInfo;
import me.chanjar.weixin.open.bean.result.*; import me.chanjar.weixin.open.bean.result.*;
import java.util.Objects;
/** /**
* @author <a href="https://github.com/007gzs">007</a> * @author <a href="https://github.com/007gzs">007</a>
@ -14,6 +15,7 @@ import me.chanjar.weixin.open.bean.result.*;
public class WxOpenGsonBuilder { public class WxOpenGsonBuilder {
private static final GsonBuilder INSTANCE = new GsonBuilder(); private static final GsonBuilder INSTANCE = new GsonBuilder();
private static volatile Gson GSON_INSTANCE;
static { static {
INSTANCE.disableHtmlEscaping(); INSTANCE.disableHtmlEscaping();
@ -26,11 +28,17 @@ public class WxOpenGsonBuilder {
INSTANCE.registerTypeAdapter(WxOpenAuthorizerOptionResult.class, new WxOpenAuthorizerOptionResultGsonAdapter()); INSTANCE.registerTypeAdapter(WxOpenAuthorizerOptionResult.class, new WxOpenAuthorizerOptionResultGsonAdapter());
INSTANCE.registerTypeAdapter(WxFastMaAccountBasicInfoResult.class, new WxFastMaAccountBasicInfoGsonAdapter()); INSTANCE.registerTypeAdapter(WxFastMaAccountBasicInfoResult.class, new WxFastMaAccountBasicInfoGsonAdapter());
INSTANCE.registerTypeAdapter(WxOpenAuthorizerListResult.class, new WxOpenAuthorizerListResultGsonAdapter()); INSTANCE.registerTypeAdapter(WxOpenAuthorizerListResult.class, new WxOpenAuthorizerListResultGsonAdapter());
} }
public static Gson create() { public static Gson create() {
return INSTANCE.create(); if (Objects.isNull(GSON_INSTANCE)) {
synchronized (GSON_INSTANCE) {
if (Objects.isNull(GSON_INSTANCE)) {
GSON_INSTANCE = INSTANCE.create();
}
}
}
return GSON_INSTANCE;
} }
} }