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

This commit is contained in:
DDERGOU 2022-01-11 12:44:50 +08:00 committed by GitHub
parent 4c781ee7ee
commit ee17a5ee6f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 183 additions and 139 deletions

View File

@ -7,6 +7,7 @@ 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;
/** /**
* . * .
@ -14,6 +15,7 @@ import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
*/ */
public class WxGsonBuilder { public class WxGsonBuilder {
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,7 +28,14 @@ public class WxGsonBuilder {
} }
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

@ -9,6 +9,7 @@ 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
@ -16,6 +17,7 @@ import me.chanjar.weixin.cp.bean.WxCpUser;
public class WxCpGsonBuilder { public class WxCpGsonBuilder {
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();
@ -28,7 +30,14 @@ public class WxCpGsonBuilder {
} }
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

@ -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

@ -16,6 +16,7 @@ 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
@ -23,6 +24,7 @@ import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
public class WxMpGsonBuilder { public class WxMpGsonBuilder {
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();
@ -64,7 +66,14 @@ public class WxMpGsonBuilder {
} }
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

@ -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;
} }
} }