mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2026-03-10 00:13:40 +08:00
将菜单相关的类转移到单独的包里方便管理,以便于某些特殊需求
This commit is contained in:
@@ -2,8 +2,9 @@ package me.chanjar.weixin.common.util.json;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
|
||||
import me.chanjar.weixin.common.bean.WxAccessToken;
|
||||
import me.chanjar.weixin.common.bean.WxMenu;
|
||||
import me.chanjar.weixin.common.bean.menu.WxMenu;
|
||||
import me.chanjar.weixin.common.bean.result.WxError;
|
||||
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
|
||||
|
||||
|
||||
@@ -8,11 +8,22 @@
|
||||
*/
|
||||
package me.chanjar.weixin.common.util.json;
|
||||
|
||||
import com.google.gson.*;
|
||||
import me.chanjar.weixin.common.bean.WxMenu;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonDeserializationContext;
|
||||
import com.google.gson.JsonDeserializer;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParseException;
|
||||
import com.google.gson.JsonSerializationContext;
|
||||
import com.google.gson.JsonSerializer;
|
||||
|
||||
import me.chanjar.weixin.common.bean.menu.WxMenu;
|
||||
import me.chanjar.weixin.common.bean.menu.WxMenuButton;
|
||||
import me.chanjar.weixin.common.bean.menu.WxMenuRule;
|
||||
|
||||
|
||||
/**
|
||||
* @author Daniel Qian
|
||||
*/
|
||||
@@ -22,7 +33,7 @@ public class WxMenuGsonAdapter implements JsonSerializer<WxMenu>, JsonDeserializ
|
||||
JsonObject json = new JsonObject();
|
||||
|
||||
JsonArray buttonArray = new JsonArray();
|
||||
for (WxMenu.WxMenuButton button : menu.getButtons()) {
|
||||
for (WxMenuButton button : menu.getButtons()) {
|
||||
JsonObject buttonJson = convertToJson(button);
|
||||
buttonArray.add(buttonJson);
|
||||
}
|
||||
@@ -35,7 +46,7 @@ public class WxMenuGsonAdapter implements JsonSerializer<WxMenu>, JsonDeserializ
|
||||
return json;
|
||||
}
|
||||
|
||||
protected JsonObject convertToJson(WxMenu.WxMenuButton button) {
|
||||
protected JsonObject convertToJson(WxMenuButton button) {
|
||||
JsonObject buttonJson = new JsonObject();
|
||||
buttonJson.addProperty("type", button.getType());
|
||||
buttonJson.addProperty("name", button.getName());
|
||||
@@ -43,7 +54,7 @@ public class WxMenuGsonAdapter implements JsonSerializer<WxMenu>, JsonDeserializ
|
||||
buttonJson.addProperty("url", button.getUrl());
|
||||
if (button.getSubButtons() != null && button.getSubButtons().size() > 0) {
|
||||
JsonArray buttonArray = new JsonArray();
|
||||
for (WxMenu.WxMenuButton sub_button : button.getSubButtons()) {
|
||||
for (WxMenuButton sub_button : button.getSubButtons()) {
|
||||
buttonArray.add(convertToJson(sub_button));
|
||||
}
|
||||
buttonJson.add("sub_button", buttonArray);
|
||||
@@ -51,7 +62,7 @@ public class WxMenuGsonAdapter implements JsonSerializer<WxMenu>, JsonDeserializ
|
||||
return buttonJson;
|
||||
}
|
||||
|
||||
protected JsonObject convertToJson(WxMenu.WxMenuRule menuRule) {
|
||||
protected JsonObject convertToJson(WxMenuRule menuRule) {
|
||||
JsonObject matchRule = new JsonObject();
|
||||
matchRule.addProperty("tag_id", menuRule.getTagId());
|
||||
matchRule.addProperty("sex", menuRule.getSex());
|
||||
@@ -74,7 +85,7 @@ public class WxMenuGsonAdapter implements JsonSerializer<WxMenu>, JsonDeserializ
|
||||
JsonArray buttonsJson = menuJson.get("button").getAsJsonArray();
|
||||
for (int i = 0; i < buttonsJson.size(); i++) {
|
||||
JsonObject buttonJson = buttonsJson.get(i).getAsJsonObject();
|
||||
WxMenu.WxMenuButton button = convertFromJson(buttonJson);
|
||||
WxMenuButton button = convertFromJson(buttonJson);
|
||||
menu.getButtons().add(button);
|
||||
if (buttonJson.get("sub_button") == null || buttonJson.get("sub_button").isJsonNull()) {
|
||||
continue;
|
||||
@@ -88,8 +99,8 @@ public class WxMenuGsonAdapter implements JsonSerializer<WxMenu>, JsonDeserializ
|
||||
return menu;
|
||||
}
|
||||
|
||||
protected WxMenu.WxMenuButton convertFromJson(JsonObject json) {
|
||||
WxMenu.WxMenuButton button = new WxMenu.WxMenuButton();
|
||||
protected WxMenuButton convertFromJson(JsonObject json) {
|
||||
WxMenuButton button = new WxMenuButton();
|
||||
button.setName(GsonHelper.getString(json, "name"));
|
||||
button.setKey(GsonHelper.getString(json, "key"));
|
||||
button.setUrl(GsonHelper.getString(json, "url"));
|
||||
|
||||
Reference in New Issue
Block a user