|
9 | 9 |
|
10 | 10 | package me.chanjar.weixin.cp.util.json;
|
11 | 11 |
|
12 |
| -import java.lang.reflect.Type; |
13 |
| - |
14 |
| -import com.google.gson.JsonArray; |
15 |
| -import com.google.gson.JsonDeserializationContext; |
16 |
| -import com.google.gson.JsonDeserializer; |
17 |
| -import com.google.gson.JsonElement; |
18 |
| -import com.google.gson.JsonObject; |
19 |
| -import com.google.gson.JsonParseException; |
20 |
| -import com.google.gson.JsonPrimitive; |
21 |
| -import com.google.gson.JsonSerializationContext; |
22 |
| -import com.google.gson.JsonSerializer; |
| 12 | +import com.google.gson.*; |
23 | 13 | import me.chanjar.weixin.common.util.json.GsonHelper;
|
24 | 14 | import me.chanjar.weixin.cp.bean.Gender;
|
25 | 15 | import me.chanjar.weixin.cp.bean.WxCpUser;
|
26 | 16 |
|
| 17 | +import java.lang.reflect.Type; |
| 18 | + |
27 | 19 | /**
|
28 | 20 | * cp user gson adapter.
|
29 | 21 | *
|
|
32 | 24 | public class WxCpUserGsonAdapter implements JsonDeserializer<WxCpUser>, JsonSerializer<WxCpUser> {
|
33 | 25 | private static final String EXTERNAL_PROFILE = "external_profile";
|
34 | 26 | private static final String EXTERNAL_ATTR = "external_attr";
|
35 |
| - private static final String EXTATTR = "extattr"; |
| 27 | + private static final String EXTRA_ATTR = "extattr"; |
| 28 | + private static final String EXTERNAL_POSITION = "external_position"; |
| 29 | + private static final String DEPARTMENT = "department"; |
| 30 | + private static final String EXTERNAL_CORP_NAME = "external_corp_name"; |
36 | 31 |
|
37 | 32 | @Override
|
38 | 33 | public WxCpUser deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
|
39 | 34 | JsonObject o = json.getAsJsonObject();
|
40 | 35 | WxCpUser user = new WxCpUser();
|
41 | 36 |
|
42 |
| - if (o.get("department") != null) { |
43 |
| - JsonArray departJsonArray = o.get("department").getAsJsonArray(); |
| 37 | + if (o.get(DEPARTMENT) != null) { |
| 38 | + JsonArray departJsonArray = o.get(DEPARTMENT).getAsJsonArray(); |
44 | 39 | Long[] departIds = new Long[departJsonArray.size()];
|
45 | 40 | int i = 0;
|
46 | 41 | for (JsonElement jsonElement : departJsonArray) {
|
@@ -80,25 +75,41 @@ public WxCpUser deserialize(JsonElement json, Type typeOfT, JsonDeserializationC
|
80 | 75 | user.setQrCode(GsonHelper.getString(o, "qr_code"));
|
81 | 76 | user.setToInvite(GsonHelper.getBoolean(o, "to_invite"));
|
82 | 77 |
|
83 |
| - if (GsonHelper.isNotNull(o.get(EXTATTR))) { |
| 78 | + if (GsonHelper.isNotNull(o.get(EXTRA_ATTR))) { |
84 | 79 | this.buildExtraAttrs(o, user);
|
85 | 80 | }
|
86 | 81 |
|
87 | 82 | if (GsonHelper.isNotNull(o.get(EXTERNAL_PROFILE))) {
|
| 83 | + user.setExternalCorpName(GsonHelper.getString(o.getAsJsonObject().get(EXTERNAL_PROFILE).getAsJsonObject(), EXTERNAL_CORP_NAME)); |
88 | 84 | this.buildExternalAttrs(o, user);
|
89 | 85 | }
|
90 | 86 |
|
| 87 | + user.setExternalPosition(GsonHelper.getString(o, EXTERNAL_POSITION)); |
| 88 | + |
91 | 89 | return user;
|
92 | 90 | }
|
93 | 91 |
|
94 | 92 | private void buildExtraAttrs(JsonObject o, WxCpUser user) {
|
95 |
| - JsonArray attrJsonElements = o.get(EXTATTR).getAsJsonObject().get("attrs").getAsJsonArray(); |
| 93 | + JsonArray attrJsonElements = o.get(EXTRA_ATTR).getAsJsonObject().get("attrs").getAsJsonArray(); |
96 | 94 | for (JsonElement attrJsonElement : attrJsonElements) {
|
97 |
| - WxCpUser.Attr attr = new WxCpUser.Attr( |
98 |
| - GsonHelper.getString(attrJsonElement.getAsJsonObject(), "name"), |
99 |
| - GsonHelper.getString(attrJsonElement.getAsJsonObject(), "value") |
100 |
| - ); |
| 95 | + final Integer type = GsonHelper.getInteger(attrJsonElement.getAsJsonObject(), "type"); |
| 96 | + final WxCpUser.Attr attr = new WxCpUser.Attr().setType(type) |
| 97 | + .setName(GsonHelper.getString(attrJsonElement.getAsJsonObject(), "name")); |
101 | 98 | user.getExtAttrs().add(attr);
|
| 99 | + |
| 100 | + switch (type) { |
| 101 | + case 0: { |
| 102 | + attr.setTextValue(GsonHelper.getString(attrJsonElement.getAsJsonObject().get("text").getAsJsonObject(), "value")); |
| 103 | + break; |
| 104 | + } |
| 105 | + case 1: { |
| 106 | + final JsonObject web = attrJsonElement.getAsJsonObject().get("web").getAsJsonObject(); |
| 107 | + attr.setWebTitle(GsonHelper.getString(web, "title")) |
| 108 | + .setWebUrl(GsonHelper.getString(web, "url")); |
| 109 | + break; |
| 110 | + } |
| 111 | + default://ignored |
| 112 | + } |
102 | 113 | }
|
103 | 114 | }
|
104 | 115 |
|
@@ -237,13 +248,39 @@ public JsonElement serialize(WxCpUser user, Type typeOfSrc, JsonSerializationCon
|
237 | 248 | JsonArray attrsJsonArray = new JsonArray();
|
238 | 249 | for (WxCpUser.Attr attr : user.getExtAttrs()) {
|
239 | 250 | JsonObject attrJson = new JsonObject();
|
240 |
| - attrJson.addProperty("name", attr.getName()); |
241 |
| - attrJson.addProperty("value", attr.getValue()); |
| 251 | + |
| 252 | + switch (attr.getType()) { |
| 253 | + case 0: { |
| 254 | + JsonObject text = new JsonObject(); |
| 255 | + text.addProperty("value", attr.getTextValue()); |
| 256 | + attrJson.add("text", text); |
| 257 | + break; |
| 258 | + } |
| 259 | + case 1: { |
| 260 | + JsonObject web = new JsonObject(); |
| 261 | + web.addProperty("url", attr.getWebUrl()); |
| 262 | + web.addProperty("title", attr.getWebTitle()); |
| 263 | + attrJson.add("web", web); |
| 264 | + break; |
| 265 | + } |
| 266 | + default: //ignored |
| 267 | + } |
242 | 268 | attrsJsonArray.add(attrJson);
|
243 | 269 | }
|
244 | 270 | JsonObject attrsJson = new JsonObject();
|
245 | 271 | attrsJson.add("attrs", attrsJsonArray);
|
246 |
| - o.add(EXTATTR, attrsJson); |
| 272 | + o.add(EXTRA_ATTR, attrsJson); |
| 273 | + } |
| 274 | + |
| 275 | + if (user.getExternalPosition() != null) { |
| 276 | + o.addProperty(EXTERNAL_POSITION, user.getExternalPosition()); |
| 277 | + } |
| 278 | + |
| 279 | + JsonObject attrsJson = new JsonObject(); |
| 280 | + o.add(EXTERNAL_PROFILE, attrsJson); |
| 281 | + |
| 282 | + if (user.getExternalCorpName() != null) { |
| 283 | + attrsJson.addProperty(EXTERNAL_CORP_NAME, user.getExternalCorpName()); |
247 | 284 | }
|
248 | 285 |
|
249 | 286 | if (user.getExternalAttrs().size() > 0) {
|
@@ -279,9 +316,7 @@ public JsonElement serialize(WxCpUser user, Type typeOfSrc, JsonSerializationCon
|
279 | 316 | attrsJsonArray.add(attrJson);
|
280 | 317 | }
|
281 | 318 |
|
282 |
| - JsonObject attrsJson = new JsonObject(); |
283 | 319 | attrsJson.add(EXTERNAL_ATTR, attrsJsonArray);
|
284 |
| - o.add(EXTERNAL_PROFILE, attrsJson); |
285 | 320 | }
|
286 | 321 |
|
287 | 322 | return o;
|
|
0 commit comments