|
1 |
| -/* |
2 |
| - * KINGSTAR MEDIA SOLUTIONS Co.,LTD. Copyright c 2005-2013. All rights reserved. |
3 |
| - * |
4 |
| - * This source code is the property of KINGSTAR MEDIA SOLUTIONS LTD. It is intended |
5 |
| - * only for the use of KINGSTAR MEDIA application development. Reengineering, reproduction |
6 |
| - * arose from modification of the original source, or other redistribution of this source |
7 |
| - * is not permitted without written permission of the KINGSTAR MEDIA SOLUTIONS LTD. |
8 |
| - */ |
9 | 1 | package me.chanjar.weixin.common.util.json;
|
10 | 2 |
|
11 | 3 | import com.google.gson.*;
|
|
14 | 6 | import me.chanjar.weixin.common.bean.menu.WxMenuRule;
|
15 | 7 |
|
16 | 8 | import java.lang.reflect.Type;
|
| 9 | +import java.util.Optional; |
17 | 10 |
|
18 | 11 |
|
19 | 12 | /**
|
20 | 13 | * @author Daniel Qian
|
21 | 14 | */
|
22 | 15 | public class WxMenuGsonAdapter implements JsonSerializer<WxMenu>, JsonDeserializer<WxMenu> {
|
23 | 16 |
|
| 17 | + // JSON字段常量定义 |
| 18 | + private static final String FIELD_BUTTON = "button"; |
| 19 | + private static final String FIELD_MATCH_RULE = "matchrule"; |
| 20 | + private static final String FIELD_SUB_BUTTON = "sub_button"; |
| 21 | + private static final String FIELD_MENU = "menu"; |
| 22 | + |
| 23 | + // 菜单按钮字段常量 |
| 24 | + private static final String FIELD_TYPE = "type"; |
| 25 | + private static final String FIELD_NAME = "name"; |
| 26 | + private static final String FIELD_KEY = "key"; |
| 27 | + private static final String FIELD_URL = "url"; |
| 28 | + private static final String FIELD_MEDIA_ID = "media_id"; |
| 29 | + private static final String FIELD_ARTICLE_ID = "article_id"; |
| 30 | + private static final String FIELD_APP_ID = "appid"; |
| 31 | + private static final String FIELD_PAGE_PATH = "pagepath"; |
| 32 | + |
| 33 | + // 菜单规则字段常量 |
| 34 | + private static final String FIELD_TAG_ID = "tag_id"; |
| 35 | + private static final String FIELD_SEX = "sex"; |
| 36 | + private static final String FIELD_COUNTRY = "country"; |
| 37 | + private static final String FIELD_PROVINCE = "province"; |
| 38 | + private static final String FIELD_CITY = "city"; |
| 39 | + private static final String FIELD_CLIENT_PLATFORM_TYPE = "client_platform_type"; |
| 40 | + private static final String FIELD_LANGUAGE = "language"; |
| 41 | + |
24 | 42 | @Override
|
25 | 43 | public JsonElement serialize(WxMenu menu, Type typeOfSrc, JsonSerializationContext context) {
|
26 | 44 | JsonObject json = new JsonObject();
|
27 |
| - |
28 | 45 | JsonArray buttonArray = new JsonArray();
|
29 |
| - for (WxMenuButton button : menu.getButtons()) { |
30 |
| - JsonObject buttonJson = convertToJson(button); |
31 |
| - buttonArray.add(buttonJson); |
32 |
| - } |
33 |
| - json.add("button", buttonArray); |
34 |
| - |
| 46 | + Optional.ofNullable(menu.getButtons()) |
| 47 | + .ifPresent(buttons -> buttons.stream() |
| 48 | + .map(this::convertToJson) |
| 49 | + .forEach(buttonArray::add)); |
| 50 | + json.add(FIELD_BUTTON, buttonArray); |
35 | 51 | if (menu.getMatchRule() != null) {
|
36 |
| - json.add("matchrule", convertToJson(menu.getMatchRule())); |
| 52 | + json.add(FIELD_MATCH_RULE, convertToJson(menu.getMatchRule())); |
37 | 53 | }
|
38 |
| - |
39 | 54 | return json;
|
40 | 55 | }
|
41 | 56 |
|
42 | 57 | protected JsonObject convertToJson(WxMenuButton button) {
|
43 | 58 | JsonObject buttonJson = new JsonObject();
|
44 |
| - buttonJson.addProperty("type", button.getType()); |
45 |
| - buttonJson.addProperty("name", button.getName()); |
46 |
| - buttonJson.addProperty("key", button.getKey()); |
47 |
| - buttonJson.addProperty("url", button.getUrl()); |
48 |
| - buttonJson.addProperty("media_id", button.getMediaId()); |
49 |
| - buttonJson.addProperty("article_id", button.getArticleId()); |
50 |
| - buttonJson.addProperty("appid", button.getAppId()); |
51 |
| - buttonJson.addProperty("pagepath", button.getPagePath()); |
| 59 | + addPropertyIfNotNull(buttonJson, FIELD_TYPE, button.getType()); |
| 60 | + addPropertyIfNotNull(buttonJson, FIELD_NAME, button.getName()); |
| 61 | + addPropertyIfNotNull(buttonJson, FIELD_KEY, button.getKey()); |
| 62 | + addPropertyIfNotNull(buttonJson, FIELD_URL, button.getUrl()); |
| 63 | + addPropertyIfNotNull(buttonJson, FIELD_MEDIA_ID, button.getMediaId()); |
| 64 | + addPropertyIfNotNull(buttonJson, FIELD_ARTICLE_ID, button.getArticleId()); |
| 65 | + addPropertyIfNotNull(buttonJson, FIELD_APP_ID, button.getAppId()); |
| 66 | + addPropertyIfNotNull(buttonJson, FIELD_PAGE_PATH, button.getPagePath()); |
52 | 67 | if (button.getSubButtons() != null && !button.getSubButtons().isEmpty()) {
|
53 | 68 | JsonArray buttonArray = new JsonArray();
|
54 |
| - for (WxMenuButton sub_button : button.getSubButtons()) { |
55 |
| - buttonArray.add(convertToJson(sub_button)); |
56 |
| - } |
57 |
| - buttonJson.add("sub_button", buttonArray); |
| 69 | + button.getSubButtons().stream() |
| 70 | + .map(this::convertToJson) |
| 71 | + .forEach(buttonArray::add); |
| 72 | + buttonJson.add(FIELD_SUB_BUTTON, buttonArray); |
58 | 73 | }
|
59 | 74 | return buttonJson;
|
60 | 75 | }
|
61 | 76 |
|
62 | 77 | protected JsonObject convertToJson(WxMenuRule menuRule) {
|
63 | 78 | JsonObject matchRule = new JsonObject();
|
64 |
| - matchRule.addProperty("tag_id", menuRule.getTagId()); |
65 |
| - matchRule.addProperty("sex", menuRule.getSex()); |
66 |
| - matchRule.addProperty("country", menuRule.getCountry()); |
67 |
| - matchRule.addProperty("province", menuRule.getProvince()); |
68 |
| - matchRule.addProperty("city", menuRule.getCity()); |
69 |
| - matchRule.addProperty("client_platform_type", menuRule.getClientPlatformType()); |
70 |
| - matchRule.addProperty("language", menuRule.getLanguage()); |
| 79 | + addPropertyIfNotNull(matchRule, FIELD_TAG_ID, menuRule.getTagId()); |
| 80 | + addPropertyIfNotNull(matchRule, FIELD_SEX, menuRule.getSex()); |
| 81 | + addPropertyIfNotNull(matchRule, FIELD_COUNTRY, menuRule.getCountry()); |
| 82 | + addPropertyIfNotNull(matchRule, FIELD_PROVINCE, menuRule.getProvince()); |
| 83 | + addPropertyIfNotNull(matchRule, FIELD_CITY, menuRule.getCity()); |
| 84 | + addPropertyIfNotNull(matchRule, FIELD_CLIENT_PLATFORM_TYPE, menuRule.getClientPlatformType()); |
| 85 | + addPropertyIfNotNull(matchRule, FIELD_LANGUAGE, menuRule.getLanguage()); |
71 | 86 | return matchRule;
|
72 | 87 | }
|
73 | 88 |
|
74 |
| - @Deprecated |
75 |
| - private WxMenuRule convertToRule(JsonObject json) { |
76 |
| - WxMenuRule menuRule = new WxMenuRule(); |
77 |
| - //变态的微信接口,这里居然反人类的使用和序列化时不一样的名字 |
78 |
| - //menuRule.setTagId(GsonHelper.getString(json,"tag_id")); |
79 |
| - menuRule.setTagId(GsonHelper.getString(json, "group_id")); |
80 |
| - menuRule.setSex(GsonHelper.getString(json, "sex")); |
81 |
| - menuRule.setCountry(GsonHelper.getString(json, "country")); |
82 |
| - menuRule.setProvince(GsonHelper.getString(json, "province")); |
83 |
| - menuRule.setCity(GsonHelper.getString(json, "city")); |
84 |
| - menuRule.setClientPlatformType(GsonHelper.getString(json, "client_platform_type")); |
85 |
| - menuRule.setLanguage(GsonHelper.getString(json, "language")); |
86 |
| - return menuRule; |
| 89 | + private void addPropertyIfNotNull(JsonObject obj, String key, String value) { |
| 90 | + if (value != null) { |
| 91 | + obj.addProperty(key, value); |
| 92 | + } |
87 | 93 | }
|
88 | 94 |
|
89 | 95 | @Override
|
90 | 96 | public WxMenu deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
|
91 |
| - /* |
92 |
| - * 操蛋的微信 |
93 |
| - * 创建菜单时是 { button : ... } |
94 |
| - * 查询菜单时是 { menu : { button : ... } } |
95 |
| - * 现在企业号升级为企业微信后,没有此问题,因此需要单独处理 |
96 |
| - */ |
97 |
| - JsonArray buttonsJson = json.getAsJsonObject().get("menu").getAsJsonObject().get("button").getAsJsonArray(); |
98 |
| - return this.buildMenuFromJson(buttonsJson); |
| 97 | + JsonObject root = json.getAsJsonObject(); |
| 98 | + JsonArray buttonsJson = null; |
| 99 | + if (root.has(FIELD_MENU)) { |
| 100 | + JsonObject menuObj = root.getAsJsonObject(FIELD_MENU); |
| 101 | + buttonsJson = menuObj.getAsJsonArray(FIELD_BUTTON); |
| 102 | + } else if (root.has(FIELD_BUTTON)) { |
| 103 | + buttonsJson = root.getAsJsonArray(FIELD_BUTTON); |
| 104 | + } |
| 105 | + if (buttonsJson == null) { |
| 106 | + throw new JsonParseException("No button array found in menu JSON"); |
| 107 | + } |
| 108 | + return buildMenuFromJson(buttonsJson); |
99 | 109 | }
|
100 | 110 |
|
101 | 111 | protected WxMenu buildMenuFromJson(JsonArray buttonsJson) {
|
102 | 112 | WxMenu menu = new WxMenu();
|
103 |
| - for (int i = 0; i < buttonsJson.size(); i++) { |
104 |
| - JsonObject buttonJson = buttonsJson.get(i).getAsJsonObject(); |
| 113 | + for (JsonElement btnElem : buttonsJson) { |
| 114 | + JsonObject buttonJson = btnElem.getAsJsonObject(); |
105 | 115 | WxMenuButton button = convertFromJson(buttonJson);
|
106 | 116 | menu.getButtons().add(button);
|
107 |
| - if (buttonJson.get("sub_button") == null || buttonJson.get("sub_button").isJsonNull()) { |
108 |
| - continue; |
109 |
| - } |
110 |
| - JsonArray sub_buttonsJson = buttonJson.get("sub_button").getAsJsonArray(); |
111 |
| - for (int j = 0; j < sub_buttonsJson.size(); j++) { |
112 |
| - JsonObject sub_buttonJson = sub_buttonsJson.get(j).getAsJsonObject(); |
113 |
| - button.getSubButtons().add(convertFromJson(sub_buttonJson)); |
| 117 | + if (buttonJson.has(FIELD_SUB_BUTTON) && buttonJson.get(FIELD_SUB_BUTTON).isJsonArray()) { |
| 118 | + JsonArray sub_buttonsJson = buttonJson.getAsJsonArray(FIELD_SUB_BUTTON); |
| 119 | + for (JsonElement subBtnElem : sub_buttonsJson) { |
| 120 | + button.getSubButtons().add(convertFromJson(subBtnElem.getAsJsonObject())); |
| 121 | + } |
114 | 122 | }
|
115 | 123 | }
|
116 | 124 | return menu;
|
117 | 125 | }
|
118 | 126 |
|
119 | 127 | protected WxMenuButton convertFromJson(JsonObject json) {
|
120 | 128 | WxMenuButton button = new WxMenuButton();
|
121 |
| - button.setName(GsonHelper.getString(json, "name")); |
122 |
| - button.setKey(GsonHelper.getString(json, "key")); |
123 |
| - button.setUrl(GsonHelper.getString(json, "url")); |
124 |
| - button.setType(GsonHelper.getString(json, "type")); |
125 |
| - button.setMediaId(GsonHelper.getString(json, "media_id")); |
126 |
| - button.setArticleId(GsonHelper.getString(json, "article_id")); |
127 |
| - button.setAppId(GsonHelper.getString(json, "appid")); |
128 |
| - button.setPagePath(GsonHelper.getString(json, "pagepath")); |
| 129 | + button.setName(GsonHelper.getString(json, FIELD_NAME)); |
| 130 | + button.setKey(GsonHelper.getString(json, FIELD_KEY)); |
| 131 | + button.setUrl(GsonHelper.getString(json, FIELD_URL)); |
| 132 | + button.setType(GsonHelper.getString(json, FIELD_TYPE)); |
| 133 | + button.setMediaId(GsonHelper.getString(json, FIELD_MEDIA_ID)); |
| 134 | + button.setArticleId(GsonHelper.getString(json, FIELD_ARTICLE_ID)); |
| 135 | + button.setAppId(GsonHelper.getString(json, FIELD_APP_ID)); |
| 136 | + button.setPagePath(GsonHelper.getString(json, FIELD_PAGE_PATH)); |
129 | 137 | return button;
|
130 | 138 | }
|
131 | 139 |
|
|
0 commit comments