Visitar URL original
Develop by niefy · Pull Request #1659 · binarywang/WxJava · GitHub
Skip to content
Merged

Develop #1659

Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Json解析统一优化
  • Loading branch information
niefy
niefy committed Jul 3, 2020
commit b34ea5f7ddaad66320f96e27c73303a9b2f2ae4c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import me.chanjar.weixin.common.WxType;
import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.json.GsonParser;
import me.chanjar.weixin.cp.api.WxCpAgentService;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.WxCpAgent;
Expand All @@ -27,7 +28,7 @@
*/
@RequiredArgsConstructor
public class WxCpAgentServiceImpl implements WxCpAgentService {
private static final JsonParser JSON_PARSER = new JsonParser();


private final WxCpService mainService;

Expand All @@ -45,7 +46,7 @@ public WxCpAgent get(Integer agentId) throws WxErrorException {
public void set(WxCpAgent agentInfo) throws WxErrorException {
String url = this.mainService.getWxCpConfigStorage().getApiUrl(AGENT_SET);
String responseContent = this.mainService.post(url, agentInfo.toJson());
JsonObject jsonObject = JSON_PARSER.parse(responseContent).getAsJsonObject();
JsonObject jsonObject = GsonParser.parse(responseContent);
if (jsonObject.get("errcode").getAsInt() != 0) {
throw new WxErrorException(WxError.fromJson(responseContent, WxType.CP));
}
Expand All @@ -55,7 +56,7 @@ public void set(WxCpAgent agentInfo) throws WxErrorException {
public List<WxCpAgent> list() throws WxErrorException {
String url = this.mainService.getWxCpConfigStorage().getApiUrl(AGENT_LIST);
String responseContent = this.mainService.get(url, null);
JsonObject jsonObject = JSON_PARSER.parse(responseContent).getAsJsonObject();
JsonObject jsonObject = GsonParser.parse(responseContent);
if (jsonObject.get("errcode").getAsInt() != 0) {
throw new WxErrorException(WxError.fromJson(responseContent, WxType.CP));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.gson.JsonParser;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.json.GsonParser;
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
import me.chanjar.weixin.cp.api.WxCpChatService;
import me.chanjar.weixin.cp.api.WxCpService;
Expand All @@ -25,7 +26,7 @@
*/
@RequiredArgsConstructor
public class WxCpChatServiceImpl implements WxCpChatService {
private static final JsonParser JSON_PARSER = new JsonParser();

private final WxCpService cpService;

@Override
Expand Down Expand Up @@ -87,7 +88,7 @@ public void update(String chatId, String name, String owner, List<String> usersT
public WxCpChat chatGet(String chatId) throws WxErrorException {
final String url = this.cpService.getWxCpConfigStorage().getApiUrl(APPCHAT_GET_CHATID + chatId);
String result = this.cpService.get(url, null);
final String chatInfo = JSON_PARSER.parse(result).getAsJsonObject().getAsJsonObject("chat_info").toString();
final String chatInfo = GsonParser.parse(result).getAsJsonObject("chat_info").toString();
return WxCpGsonBuilder.create().fromJson(chatInfo, WxCpChat.class);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.google.gson.reflect.TypeToken;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.json.GsonParser;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.api.WxCpTagService;
import me.chanjar.weixin.cp.bean.WxCpTag;
Expand Down Expand Up @@ -49,8 +50,8 @@ public String create(String tagName) throws WxErrorException {
private String create(JsonObject param) throws WxErrorException {
String url = this.mainService.getWxCpConfigStorage().getApiUrl(TAG_CREATE);
String responseContent = this.mainService.post(url, param.toString());
JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
return tmpJsonElement.getAsJsonObject().get("tagid").getAsString();
JsonObject jsonObject = GsonParser.parse(responseContent);
return jsonObject.get("tagid").getAsString();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.google.gson.reflect.TypeToken;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.json.GsonParser;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.api.WxCpUserService;
import me.chanjar.weixin.cp.bean.WxCpInviteResult;
Expand Down Expand Up @@ -84,9 +85,9 @@ public List<WxCpUser> listByDepartment(Long departId, Boolean fetchChild, Intege

String url = this.mainService.getWxCpConfigStorage().getApiUrl(USER_LIST + departId);
String responseContent = this.mainService.get(url, params);
JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
JsonObject jsonObject = GsonParser.parse(responseContent);
return WxCpGsonBuilder.create()
.fromJson(tmpJsonElement.getAsJsonObject().get("userlist"),
.fromJson(jsonObject.get("userlist"),
new TypeToken<List<WxCpUser>>() {
}.getType()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import me.chanjar.weixin.common.util.http.RequestHttp;
import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor;
import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor;
import me.chanjar.weixin.common.util.json.GsonParser;
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
import org.apache.commons.lang3.StringUtils;

Expand All @@ -36,7 +37,7 @@
*/
@Slf4j
public abstract class BaseWxMaServiceImpl<H, P> implements WxMaService, RequestHttp<H, P> {
private static final JsonParser JSON_PARSER = new JsonParser();

private WxMaConfig wxMaConfig;

private final WxMaMsgService kefuService = new WxMaMsgServiceImpl(this);
Expand Down Expand Up @@ -91,7 +92,7 @@ public String getPaidUnionId(String openid, String transactionId, String mchId,
throw new WxErrorException(error);
}

return JSON_PARSER.parse(responseContent).getAsJsonObject().get("unionid").getAsString();
return GsonParser.parse(responseContent).get("unionid").getAsString();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.google.gson.reflect.TypeToken;
import lombok.AllArgsConstructor;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.json.GsonParser;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.lang.reflect.Type;
Expand All @@ -26,7 +27,7 @@
*/
@AllArgsConstructor
public class WxMaAnalysisServiceImpl implements WxMaAnalysisService {
private static final JsonParser JSON_PARSER = new JsonParser();

private WxMaService wxMaService;

@Override
Expand Down Expand Up @@ -106,7 +107,7 @@ private WxMaRetainInfo getRetainInfo(Date beginDate, Date endDate, String url) t
*/
private <T> List<T> getAnalysisResultAsList(String url, Date beginDate, Date endDate, Type returnType) throws WxErrorException {
String responseContent = this.wxMaService.post(url, toJson(beginDate, endDate));
JsonObject response = JSON_PARSER.parse(responseContent).getAsJsonObject();
JsonObject response = GsonParser.parse(responseContent);
boolean hasList = response.has("list");
if (hasList) {
return WxMaGsonBuilder.create().fromJson(response.getAsJsonArray("list"), returnType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.json.GsonParser;
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
import org.apache.commons.lang3.StringUtils;

Expand All @@ -31,7 +32,7 @@
@Slf4j
@RequiredArgsConstructor
public class WxMaCloudServiceImpl implements WxMaCloudService {
private static final JsonParser JSON_PARSER = new JsonParser();

private final WxMaService wxMaService;

@Override
Expand All @@ -44,7 +45,7 @@ public String invokeCloudFunction(String name, String body) throws WxErrorExcept
public String invokeCloudFunction(String env, String name, String body) throws WxErrorException {
String cloudEnv = this.wxMaService.getWxMaConfig().getCloudEnv();
final String response = this.wxMaService.post(String.format(INVOKE_CLOUD_FUNCTION_URL, cloudEnv, name), body);
return JSON_PARSER.parse(response).getAsJsonObject().get("resp_data").getAsString();
return GsonParser.parse(response).get("resp_data").getAsString();
}

@Override
Expand All @@ -59,8 +60,7 @@ public List<String> add(String collection, List list) throws WxErrorException {
params.addProperty("query", query);

String responseContent = wxMaService.post(DATABASE_ADD_URL, params.toString());
JsonElement tmpJsonElement = JSON_PARSER.parse(responseContent);
JsonObject jsonObject = tmpJsonElement.getAsJsonObject();
JsonObject jsonObject = GsonParser.parse(responseContent);
if (jsonObject.get(WxMaConstants.ERRCODE).getAsInt() != 0) {
throw new WxErrorException(WxError.fromJson(responseContent));
}
Expand All @@ -84,8 +84,7 @@ public String add(String collection, Object obj) throws WxErrorException {
params.addProperty("query", query);

String responseContent = wxMaService.post(DATABASE_ADD_URL, params.toString());
JsonElement tmpJsonElement = JSON_PARSER.parse(responseContent);
JsonObject jsonObject = tmpJsonElement.getAsJsonObject();
JsonObject jsonObject = GsonParser.parse(responseContent);
if (jsonObject.get(WxMaConstants.ERRCODE).getAsInt() != 0) {
throw new WxErrorException(WxError.fromJson(responseContent));
}
Expand All @@ -102,7 +101,7 @@ public JsonArray databaseAdd(String query) throws WxErrorException {
@Override
public JsonArray databaseAdd(String env, String query) throws WxErrorException {
String response = this.wxMaService.post(DATABASE_ADD_URL, ImmutableMap.of("env", env, "query", query));
return JSON_PARSER.parse(response).getAsJsonObject().get("id_list").getAsJsonArray();
return GsonParser.parse(response).get("id_list").getAsJsonArray();
}

@Override
Expand All @@ -116,8 +115,7 @@ public Integer delete(String collection, String whereJson) throws WxErrorExcepti
params.addProperty("query", query);

String responseContent = wxMaService.post(DATABASE_DELETE_URL, params.toString());
JsonElement tmpJsonElement = JSON_PARSER.parse(responseContent);
JsonObject jsonObject = tmpJsonElement.getAsJsonObject();
JsonObject jsonObject = GsonParser.parse(responseContent);
if (jsonObject.get(WxMaConstants.ERRCODE).getAsInt() != 0) {
throw new WxErrorException(WxError.fromJson(responseContent));
}
Expand All @@ -133,7 +131,7 @@ public int databaseDelete(String query) throws WxErrorException {
@Override
public int databaseDelete(String env, String query) throws WxErrorException {
String response = this.wxMaService.post(DATABASE_DELETE_URL, ImmutableMap.of("env", env, "query", query));
return JSON_PARSER.parse(response).getAsJsonObject().get("deleted").getAsInt();
return GsonParser.parse(response).get("deleted").getAsInt();
}

@Override
Expand Down Expand Up @@ -214,7 +212,7 @@ public JsonArray databaseAggregate(String query) throws WxErrorException {
@Override
public JsonArray databaseAggregate(String env, String query) throws WxErrorException {
String response = this.wxMaService.post(DATABASE_AGGREGATE_URL, ImmutableMap.of("env", env, "query", query));
return JSON_PARSER.parse(response).getAsJsonObject().get("data").getAsJsonArray();
return GsonParser.parse(response).get("data").getAsJsonArray();
}

@Override
Expand All @@ -228,7 +226,7 @@ public Long count(String collection, String whereJson) throws WxErrorException {
params.addProperty("query", query);

String responseContent = wxMaService.post(DATABASE_COUNT_URL, params.toString());
return JSON_PARSER.parse(responseContent).getAsJsonObject().get("count").getAsLong();
return GsonParser.parse(responseContent).get("count").getAsLong();
}

@Override
Expand All @@ -240,7 +238,7 @@ public Long databaseCount(String query) throws WxErrorException {
@Override
public Long databaseCount(String env, String query) throws WxErrorException {
String response = this.wxMaService.post(DATABASE_COUNT_URL, ImmutableMap.of("env", env, "query", query));
return JSON_PARSER.parse(response).getAsJsonObject().get("count").getAsLong();
return GsonParser.parse(response).get("count").getAsLong();
}

@Override
Expand Down Expand Up @@ -283,7 +281,7 @@ public Long databaseMigrateImport(String env, String collectionName, String file
params.addProperty("conflict_mode", conflictMode);

String response = this.wxMaService.post(DATABASE_MIGRATE_IMPORT_URL, params.toString());
return JSON_PARSER.parse(response).getAsJsonObject().get("job_id").getAsLong();
return GsonParser.parse(response).get("job_id").getAsLong();
}

@Override
Expand All @@ -301,7 +299,7 @@ public Long databaseMigrateExport(String env, String filePath, int fileType, Str
params.addProperty("query", query);

String response = this.wxMaService.post(DATABASE_MIGRATE_EXPORT_URL, params.toString());
return JSON_PARSER.parse(response).getAsJsonObject().get("job_id").getAsLong();
return GsonParser.parse(response).get("job_id").getAsLong();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.List;

import lombok.AllArgsConstructor;
import me.chanjar.weixin.common.util.json.GsonParser;
import org.apache.commons.lang3.StringUtils;

import cn.binarywang.wx.miniapp.api.WxMaCodeService;
Expand All @@ -34,7 +35,7 @@
*/
@AllArgsConstructor
public class WxMaCodeServiceImpl implements WxMaCodeService {
private static final JsonParser JSON_PARSER = new JsonParser();

private WxMaService wxMaService;

@Override
Expand Down Expand Up @@ -73,7 +74,7 @@ public byte[] getQrCode(String path) throws WxErrorException {
@Override
public List<WxMaCategory> getCategory() throws WxErrorException {
String responseContent = this.wxMaService.get(GET_CATEGORY_URL, null);
JsonObject jsonObject = JSON_PARSER.parse(responseContent).getAsJsonObject();
JsonObject jsonObject = GsonParser.parse(responseContent);
boolean hasCategoryList = jsonObject.has("category_list");
if (hasCategoryList) {
return WxMaGsonBuilder.create().fromJson(jsonObject.getAsJsonArray("category_list"),
Expand All @@ -87,7 +88,7 @@ public List<WxMaCategory> getCategory() throws WxErrorException {
@Override
public List<String> getPage() throws WxErrorException {
String responseContent = this.wxMaService.get(GET_PAGE_URL, null);
JsonObject jsonObject = JSON_PARSER.parse(responseContent).getAsJsonObject();
JsonObject jsonObject = GsonParser.parse(responseContent);
boolean hasPageList = jsonObject.has("page_list");
if (hasPageList) {
return WxMaGsonBuilder.create().fromJson(jsonObject.getAsJsonArray("page_list"),
Expand All @@ -101,7 +102,7 @@ public List<String> getPage() throws WxErrorException {
@Override
public long submitAudit(WxMaCodeSubmitAuditRequest auditRequest) throws WxErrorException {
String responseContent = this.wxMaService.post(SUBMIT_AUDIT_URL, auditRequest.toJson());
JsonObject jsonObject = JSON_PARSER.parse(responseContent).getAsJsonObject();
JsonObject jsonObject = GsonParser.parse(responseContent);
return GsonHelper.getLong(jsonObject, "auditid");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.RandomUtils;
import me.chanjar.weixin.common.util.crypto.SHA1;
import me.chanjar.weixin.common.util.json.GsonParser;

import java.util.concurrent.locks.Lock;

Expand All @@ -22,7 +23,7 @@
*/
@AllArgsConstructor
public class WxMaJsapiServiceImpl implements WxMaJsapiService {
private static final JsonParser JSON_PARSER = new JsonParser();


private WxMaService wxMaService;

Expand All @@ -42,8 +43,7 @@ public String getCardApiTicket(boolean forceRefresh) throws WxErrorException {

if (this.wxMaService.getWxMaConfig().isCardApiTicketExpired()) {
String responseContent = this.wxMaService.get(GET_JSAPI_TICKET_URL + "?type=wx_card", null);
JsonElement tmpJsonElement = JSON_PARSER.parse(responseContent);
JsonObject tmpJsonObject = tmpJsonElement.getAsJsonObject();
JsonObject tmpJsonObject = GsonParser.parse(responseContent);
String jsapiTicket = tmpJsonObject.get("ticket").getAsString();
int expiresInSeconds = tmpJsonObject.get("expires_in").getAsInt();
this.wxMaService.getWxMaConfig().updateCardApiTicket(jsapiTicket, expiresInSeconds);
Expand All @@ -70,8 +70,7 @@ public String getJsapiTicket(boolean forceRefresh) throws WxErrorException {

if (this.wxMaService.getWxMaConfig().isJsapiTicketExpired()) {
String responseContent = this.wxMaService.get(GET_JSAPI_TICKET_URL + "?type=jsapi", null);
JsonElement tmpJsonElement = JSON_PARSER.parse(responseContent);
JsonObject tmpJsonObject = tmpJsonElement.getAsJsonObject();
JsonObject tmpJsonObject = GsonParser.parse(responseContent);
String jsapiTicket = tmpJsonObject.get("ticket").getAsString();
int expiresInSeconds = tmpJsonObject.get("expires_in").getAsInt();
this.wxMaService.getWxMaConfig().updateJsapiTicket(jsapiTicket, expiresInSeconds);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import me.chanjar.weixin.common.WxType;
import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.json.GsonParser;

import java.util.ArrayList;
import java.util.HashMap;
Expand All @@ -25,7 +26,7 @@
*/
@AllArgsConstructor
public class WxMaLiveServiceImpl implements WxMaLiveService {
private static final JsonParser JSON_PARSER = new JsonParser();

private WxMaService service;

@Override
Expand Down Expand Up @@ -91,7 +92,7 @@ private JsonObject getJsonObject(Integer start, Integer limit, Map<String, Objec
map.put("start", start);
map.put("limit", limit);
String responseContent = service.post(GET_LIVE_INFO, WxMaGsonBuilder.create().toJson(map));
JsonObject jsonObject = JSON_PARSER.parse(responseContent).getAsJsonObject();
JsonObject jsonObject = GsonParser.parse(responseContent);
if (jsonObject.get("errcode").getAsInt() != 0) {
throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp));
}
Expand Down
Loading