Skip to content

Commit 0adca96

Browse files
niefyniefy
andauthored
🎨 #1659 Json解析统一优化
* 解决动态添加第一个公众号时由于configStorageMap为null报空指针异常 * Json解析统一优化 Co-authored-by: niefy <niefy@qq.com>
1 parent 8121a52 commit 0adca96

File tree

114 files changed

+229
-337
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

114 files changed

+229
-337
lines changed

weixin-graal/src/main/java/cn/binarywang/wx/graal/GraalProcessor.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cn.binarywang.wx.graal;
22

33
import lombok.Data;
4-
import lombok.extern.slf4j.Slf4j;
54

65
import javax.annotation.processing.AbstractProcessor;
76
import javax.annotation.processing.RoundEnvironment;

weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/apache/ApacheSimpleGetRequestExecutor.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package me.chanjar.weixin.common.util.http.apache;
22

33
import me.chanjar.weixin.common.WxType;
4-
import me.chanjar.weixin.common.error.WxError;
54
import me.chanjar.weixin.common.error.WxErrorException;
65
import me.chanjar.weixin.common.util.http.RequestHttp;
76
import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor;

weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/apache/ApacheSimplePostRequestExecutor.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package me.chanjar.weixin.common.util.http.apache;
22

33
import me.chanjar.weixin.common.WxType;
4-
import me.chanjar.weixin.common.error.WxError;
54
import me.chanjar.weixin.common.error.WxErrorException;
65
import me.chanjar.weixin.common.util.http.RequestHttp;
76
import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor;
@@ -12,7 +11,6 @@
1211
import org.apache.http.client.methods.HttpPost;
1312
import org.apache.http.entity.StringEntity;
1413
import org.apache.http.impl.client.CloseableHttpClient;
15-
import org.jetbrains.annotations.NotNull;
1614

1715
import java.io.IOException;
1816

weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/jodd/JoddHttpSimpleGetRequestExecutor.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import jodd.http.ProxyInfo;
77
import jodd.util.StringPool;
88
import me.chanjar.weixin.common.WxType;
9-
import me.chanjar.weixin.common.error.WxError;
109
import me.chanjar.weixin.common.error.WxErrorException;
1110
import me.chanjar.weixin.common.util.http.RequestHttp;
1211
import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor;

weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/okhttp/OkHttpSimpleGetRequestExecutor.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package me.chanjar.weixin.common.util.http.okhttp;
22

33
import me.chanjar.weixin.common.WxType;
4-
import me.chanjar.weixin.common.error.WxError;
54
import me.chanjar.weixin.common.error.WxErrorException;
65
import me.chanjar.weixin.common.util.http.RequestHttp;
76
import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package me.chanjar.weixin.common.util.json;
2+
3+
import com.google.gson.JsonObject;
4+
import com.google.gson.JsonParser;
5+
import com.google.gson.stream.JsonReader;
6+
7+
import java.io.Reader;
8+
9+
public class GsonParser {
10+
private static final JsonParser JSON_PARSER = new JsonParser();
11+
12+
public static JsonObject parse(String json){
13+
return JSON_PARSER.parse(json).getAsJsonObject();
14+
}
15+
16+
public static JsonObject parse(Reader json){
17+
return JSON_PARSER.parse(json).getAsJsonObject();
18+
}
19+
20+
public static JsonObject parse(JsonReader json){
21+
return JSON_PARSER.parse(json).getAsJsonObject();
22+
}
23+
}

weixin-java-common/src/test/java/me/chanjar/weixin/common/api/WxMessageInMemoryDuplicateCheckerTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package me.chanjar.weixin.common.api;
22

3-
import org.testng.Assert;
43
import org.testng.annotations.Test;
54

65
import java.util.concurrent.TimeUnit;

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpExternalContactService.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import me.chanjar.weixin.common.error.WxErrorException;
44
import me.chanjar.weixin.cp.bean.*;
55

6-
import java.util.Calendar;
76
import java.util.Date;
87
import java.util.List;
98

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/BaseWxCpServiceImpl.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
import com.google.common.base.Joiner;
44
import com.google.gson.JsonArray;
5-
import com.google.gson.JsonElement;
65
import com.google.gson.JsonObject;
7-
import com.google.gson.JsonParser;
86
import lombok.extern.slf4j.Slf4j;
97
import me.chanjar.weixin.common.WxType;
108
import me.chanjar.weixin.common.bean.WxJsapiSignature;
@@ -20,6 +18,7 @@
2018
import me.chanjar.weixin.common.util.http.RequestHttp;
2119
import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor;
2220
import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor;
21+
import me.chanjar.weixin.common.util.json.GsonParser;
2322
import me.chanjar.weixin.cp.api.*;
2423
import me.chanjar.weixin.cp.bean.WxCpMaJsCode2SessionResult;
2524
import me.chanjar.weixin.cp.bean.WxCpMessage;
@@ -110,7 +109,7 @@ public String getAgentJsapiTicket(boolean forceRefresh) throws WxErrorException
110109
synchronized (this.globalAgentJsapiTicketRefreshLock) {
111110
if (this.configStorage.isAgentJsapiTicketExpired()) {
112111
String responseContent = this.get(this.configStorage.getApiUrl(GET_AGENT_CONFIG_TICKET), null);
113-
JsonObject jsonObject = new JsonParser().parse(responseContent).getAsJsonObject();
112+
JsonObject jsonObject = GsonParser.parse(responseContent);
114113
this.configStorage.updateAgentJsapiTicket(jsonObject.get("ticket").getAsString(),
115114
jsonObject.get("expires_in").getAsInt());
116115
}
@@ -135,7 +134,7 @@ public String getJsapiTicket(boolean forceRefresh) throws WxErrorException {
135134
synchronized (this.globalJsapiTicketRefreshLock) {
136135
if (this.configStorage.isJsapiTicketExpired()) {
137136
String responseContent = this.get(this.configStorage.getApiUrl(GET_JSAPI_TICKET), null);
138-
JsonObject tmpJsonObject = new JsonParser().parse(responseContent).getAsJsonObject();
137+
JsonObject tmpJsonObject = GsonParser.parse(responseContent);
139138
this.configStorage.updateJsapiTicket(tmpJsonObject.get("ticket").getAsString(),
140139
tmpJsonObject.get("expires_in").getAsInt());
141140
}
@@ -191,8 +190,8 @@ public WxCpMaJsCode2SessionResult jsCode2Session(String jsCode) throws WxErrorEx
191190
@Override
192191
public String[] getCallbackIp() throws WxErrorException {
193192
String responseContent = get(this.configStorage.getApiUrl(GET_CALLBACK_IP), null);
194-
JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
195-
JsonArray jsonArray = tmpJsonElement.getAsJsonObject().get("ip_list").getAsJsonArray();
193+
JsonObject tmpJsonObject = GsonParser.parse(responseContent);
194+
JsonArray jsonArray = tmpJsonObject.get("ip_list").getAsJsonArray();
196195
String[] ips = new String[jsonArray.size()];
197196
for (int i = 0; i < jsonArray.size(); i++) {
198197
ips[i] = jsonArray.get(i).getAsString();

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/BaseWxCpTpServiceImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.google.common.base.Joiner;
44
import com.google.gson.JsonObject;
5-
import com.google.gson.JsonParser;
65
import lombok.SneakyThrows;
76
import lombok.extern.slf4j.Slf4j;
87
import me.chanjar.weixin.common.WxType;
@@ -15,6 +14,7 @@
1514
import me.chanjar.weixin.common.util.http.RequestHttp;
1615
import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor;
1716
import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor;
17+
import me.chanjar.weixin.common.util.json.GsonParser;
1818
import me.chanjar.weixin.cp.api.WxCpTpService;
1919
import me.chanjar.weixin.cp.bean.*;
2020
import me.chanjar.weixin.cp.config.WxCpTpConfigStorage;
@@ -119,7 +119,7 @@ public WxCpTpCorp getPermanentCode(String authCode) throws WxErrorException {
119119
jsonObject.addProperty("auth_code", authCode);
120120

121121
String result = post(configStorage.getApiUrl(GET_PERMANENT_CODE), jsonObject.toString());
122-
jsonObject = new JsonParser().parse(result).getAsJsonObject();
122+
jsonObject = GsonParser.parse(result);
123123
WxCpTpCorp wxCpTpCorp = WxCpTpCorp.fromJson(jsonObject.get("auth_corp_info").getAsJsonObject().toString());
124124
wxCpTpCorp.setPermanentCode(jsonObject.get("permanent_code").getAsString());
125125
return wxCpTpCorp;

0 commit comments

Comments
 (0)