|
@@ -8,11 +8,22 @@
|
|
*/
|
|
*/
|
|
package me.chanjar.weixin.common.util.json;
|
|
package me.chanjar.weixin.common.util.json;
|
|
|
|
|
|
-import com.google.gson.*;
|
|
|
|
-import me.chanjar.weixin.common.bean.WxMenu;
|
|
|
|
-
|
|
|
|
import java.lang.reflect.Type;
|
|
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
|
|
* @author Daniel Qian
|
|
*/
|
|
*/
|
|
@@ -22,7 +33,7 @@ public class WxMenuGsonAdapter implements JsonSerializer<WxMenu>, JsonDeserializ
|
|
JsonObject json = new JsonObject();
|
|
JsonObject json = new JsonObject();
|
|
|
|
|
|
JsonArray buttonArray = new JsonArray();
|
|
JsonArray buttonArray = new JsonArray();
|
|
- for (WxMenu.WxMenuButton button : menu.getButtons()) {
|
|
|
|
|
|
+ for (WxMenuButton button : menu.getButtons()) {
|
|
JsonObject buttonJson = convertToJson(button);
|
|
JsonObject buttonJson = convertToJson(button);
|
|
buttonArray.add(buttonJson);
|
|
buttonArray.add(buttonJson);
|
|
}
|
|
}
|
|
@@ -35,7 +46,7 @@ public class WxMenuGsonAdapter implements JsonSerializer<WxMenu>, JsonDeserializ
|
|
return json;
|
|
return json;
|
|
}
|
|
}
|
|
|
|
|
|
- protected JsonObject convertToJson(WxMenu.WxMenuButton button) {
|
|
|
|
|
|
+ protected JsonObject convertToJson(WxMenuButton button) {
|
|
JsonObject buttonJson = new JsonObject();
|
|
JsonObject buttonJson = new JsonObject();
|
|
buttonJson.addProperty("type", button.getType());
|
|
buttonJson.addProperty("type", button.getType());
|
|
buttonJson.addProperty("name", button.getName());
|
|
buttonJson.addProperty("name", button.getName());
|
|
@@ -43,7 +54,7 @@ public class WxMenuGsonAdapter implements JsonSerializer<WxMenu>, JsonDeserializ
|
|
buttonJson.addProperty("url", button.getUrl());
|
|
buttonJson.addProperty("url", button.getUrl());
|
|
if (button.getSubButtons() != null && button.getSubButtons().size() > 0) {
|
|
if (button.getSubButtons() != null && button.getSubButtons().size() > 0) {
|
|
JsonArray buttonArray = new JsonArray();
|
|
JsonArray buttonArray = new JsonArray();
|
|
- for (WxMenu.WxMenuButton sub_button : button.getSubButtons()) {
|
|
|
|
|
|
+ for (WxMenuButton sub_button : button.getSubButtons()) {
|
|
buttonArray.add(convertToJson(sub_button));
|
|
buttonArray.add(convertToJson(sub_button));
|
|
}
|
|
}
|
|
buttonJson.add("sub_button", buttonArray);
|
|
buttonJson.add("sub_button", buttonArray);
|
|
@@ -51,7 +62,7 @@ public class WxMenuGsonAdapter implements JsonSerializer<WxMenu>, JsonDeserializ
|
|
return buttonJson;
|
|
return buttonJson;
|
|
}
|
|
}
|
|
|
|
|
|
- protected JsonObject convertToJson(WxMenu.WxMenuRule menuRule) {
|
|
|
|
|
|
+ protected JsonObject convertToJson(WxMenuRule menuRule) {
|
|
JsonObject matchRule = new JsonObject();
|
|
JsonObject matchRule = new JsonObject();
|
|
matchRule.addProperty("tag_id", menuRule.getTagId());
|
|
matchRule.addProperty("tag_id", menuRule.getTagId());
|
|
matchRule.addProperty("sex", menuRule.getSex());
|
|
matchRule.addProperty("sex", menuRule.getSex());
|
|
@@ -74,7 +85,7 @@ public class WxMenuGsonAdapter implements JsonSerializer<WxMenu>, JsonDeserializ
|
|
JsonArray buttonsJson = menuJson.get("button").getAsJsonArray();
|
|
JsonArray buttonsJson = menuJson.get("button").getAsJsonArray();
|
|
for (int i = 0; i < buttonsJson.size(); i++) {
|
|
for (int i = 0; i < buttonsJson.size(); i++) {
|
|
JsonObject buttonJson = buttonsJson.get(i).getAsJsonObject();
|
|
JsonObject buttonJson = buttonsJson.get(i).getAsJsonObject();
|
|
- WxMenu.WxMenuButton button = convertFromJson(buttonJson);
|
|
|
|
|
|
+ WxMenuButton button = convertFromJson(buttonJson);
|
|
menu.getButtons().add(button);
|
|
menu.getButtons().add(button);
|
|
if (buttonJson.get("sub_button") == null || buttonJson.get("sub_button").isJsonNull()) {
|
|
if (buttonJson.get("sub_button") == null || buttonJson.get("sub_button").isJsonNull()) {
|
|
continue;
|
|
continue;
|
|
@@ -88,8 +99,8 @@ public class WxMenuGsonAdapter implements JsonSerializer<WxMenu>, JsonDeserializ
|
|
return menu;
|
|
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.setName(GsonHelper.getString(json, "name"));
|
|
button.setKey(GsonHelper.getString(json, "key"));
|
|
button.setKey(GsonHelper.getString(json, "key"));
|
|
button.setUrl(GsonHelper.getString(json, "url"));
|
|
button.setUrl(GsonHelper.getString(json, "url"));
|