@Test public void testSerialize() { WxCpUser user = new WxCpUser(); user.setOrders(new Integer[]{1, 2}); user.addExternalAttr(WxCpUser.ExternalAttribute.builder() .type(0) .name("文本名称") .value("文本") .build()); user.addExternalAttr(WxCpUser.ExternalAttribute.builder() .type(1) .name("网页名称") .url("http: .title("标题") .build()); user.addExternalAttr(WxCpUser.ExternalAttribute.builder() .type(2) .name("测试app") .appid("wx8bd80126147df384") .pagePath("/index") .title("my miniprogram") .build()); assertThat(user.toJson()).isEqualTo("{\"order\":[1,2],\"external_profile\":{\"external_attr\":" + "[{\"type\":0,\"name\":\"文本名称\",\"text\":{\"value\":\"文本\"}}," + "{\"type\":1,\"name\":\"网页名称\",\"web\":{\"url\":\"http: "{\"type\":2,\"name\":\"测试app\"," + "\"miniprogram\":{\"appid\":\"wx8bd80126147df384\",\"pagepath\":\"/index\",\"title\":\"my miniprogram\"}}]}}"); }
@Override public JsonElement serialize(WxCpUser user, Type typeOfSrc, JsonSerializationContext context) { JsonObject o = new JsonObject(); if (user.getUserId() != null) { o.addProperty("userid", user.getUserId()); } if (user.getName() != null) { o.addProperty("name", user.getName()); } if (user.getDepartIds() != null) { JsonArray jsonArray = new JsonArray(); for (Long departId : user.getDepartIds()) { jsonArray.add(new JsonPrimitive(departId)); } o.add("department", jsonArray); } if (user.getOrders() != null) { JsonArray jsonArray = new JsonArray(); for (Integer order : user.getOrders()) { jsonArray.add(new JsonPrimitive(order)); } o.add("order", jsonArray); } if (user.getPosition() != null) { o.addProperty("position", user.getPosition()); } if (user.getMobile() != null) { o.addProperty("mobile", user.getMobile()); } if (user.getGender() != null) { o.addProperty("gender", user.getGender().getCode()); } if (user.getEmail() != null) { o.addProperty("email", user.getEmail()); } if (user.getAvatar() != null) { o.addProperty("avatar", user.getAvatar()); } if (user.getAvatarMediaId() != null) { o.addProperty("avatar_mediaid", user.getAvatarMediaId()); } if (user.getStatus() != null) { o.addProperty("status", user.getStatus()); } if (user.getEnable() != null) { o.addProperty("enable", user.getEnable()); } if (user.getIsLeader() != null) { o.addProperty("isleader", user.getIsLeader()); } if (user.getHideMobile() != null) { o.addProperty("hide_mobile", user.getHideMobile()); } if (user.getEnglishName() != null) { o.addProperty("english_name", user.getEnglishName()); } if (user.getTelephone() != null) { o.addProperty("telephone", user.getTelephone()); } if (user.getQrCode() != null) { o.addProperty("qr_code", user.getQrCode()); } if (user.getToInvite() != null) { o.addProperty("to_invite", user.getToInvite()); } if (user.getExtAttrs().size() > 0) { JsonArray attrsJsonArray = new JsonArray(); for (WxCpUser.Attr attr : user.getExtAttrs()) { JsonObject attrJson = new JsonObject(); attrJson.addProperty("name", attr.getName()); attrJson.addProperty("value", attr.getValue()); attrsJsonArray.add(attrJson); } JsonObject attrsJson = new JsonObject(); attrsJson.add("attrs", attrsJsonArray); o.add(EXTATTR, attrsJson); } if (user.getExternalAttrs().size() > 0) { JsonArray attrsJsonArray = new JsonArray(); for (WxCpUser.ExternalAttribute attr : user.getExternalAttrs()) { JsonObject attrJson = new JsonObject(); attrJson.addProperty("type", attr.getType()); attrJson.addProperty("name", attr.getName()); switch (attr.getType()) { case 0: { JsonObject text = new JsonObject(); text.addProperty("value", attr.getValue()); attrJson.add("text", text); break; } case 1: { JsonObject web = new JsonObject(); web.addProperty("url", attr.getUrl()); web.addProperty("title", attr.getTitle()); attrJson.add("web", web); break; } case 2: { JsonObject miniprogram = new JsonObject(); miniprogram.addProperty("appid", attr.getAppid()); miniprogram.addProperty("pagepath", attr.getPagePath()); miniprogram.addProperty("title", attr.getTitle()); attrJson.add("miniprogram", miniprogram); break; } default: } attrsJsonArray.add(attrJson); } JsonObject attrsJson = new JsonObject(); attrsJson.add(EXTERNAL_ATTR, attrsJsonArray); o.add(EXTERNAL_PROFILE, attrsJson); } return o; }
WxCpUserGsonAdapter implements JsonDeserializer<WxCpUser>, JsonSerializer<WxCpUser> { @Override public JsonElement serialize(WxCpUser user, Type typeOfSrc, JsonSerializationContext context) { JsonObject o = new JsonObject(); if (user.getUserId() != null) { o.addProperty("userid", user.getUserId()); } if (user.getName() != null) { o.addProperty("name", user.getName()); } if (user.getDepartIds() != null) { JsonArray jsonArray = new JsonArray(); for (Long departId : user.getDepartIds()) { jsonArray.add(new JsonPrimitive(departId)); } o.add("department", jsonArray); } if (user.getOrders() != null) { JsonArray jsonArray = new JsonArray(); for (Integer order : user.getOrders()) { jsonArray.add(new JsonPrimitive(order)); } o.add("order", jsonArray); } if (user.getPosition() != null) { o.addProperty("position", user.getPosition()); } if (user.getMobile() != null) { o.addProperty("mobile", user.getMobile()); } if (user.getGender() != null) { o.addProperty("gender", user.getGender().getCode()); } if (user.getEmail() != null) { o.addProperty("email", user.getEmail()); } if (user.getAvatar() != null) { o.addProperty("avatar", user.getAvatar()); } if (user.getAvatarMediaId() != null) { o.addProperty("avatar_mediaid", user.getAvatarMediaId()); } if (user.getStatus() != null) { o.addProperty("status", user.getStatus()); } if (user.getEnable() != null) { o.addProperty("enable", user.getEnable()); } if (user.getIsLeader() != null) { o.addProperty("isleader", user.getIsLeader()); } if (user.getHideMobile() != null) { o.addProperty("hide_mobile", user.getHideMobile()); } if (user.getEnglishName() != null) { o.addProperty("english_name", user.getEnglishName()); } if (user.getTelephone() != null) { o.addProperty("telephone", user.getTelephone()); } if (user.getQrCode() != null) { o.addProperty("qr_code", user.getQrCode()); } if (user.getToInvite() != null) { o.addProperty("to_invite", user.getToInvite()); } if (user.getExtAttrs().size() > 0) { JsonArray attrsJsonArray = new JsonArray(); for (WxCpUser.Attr attr : user.getExtAttrs()) { JsonObject attrJson = new JsonObject(); attrJson.addProperty("name", attr.getName()); attrJson.addProperty("value", attr.getValue()); attrsJsonArray.add(attrJson); } JsonObject attrsJson = new JsonObject(); attrsJson.add("attrs", attrsJsonArray); o.add(EXTATTR, attrsJson); } if (user.getExternalAttrs().size() > 0) { JsonArray attrsJsonArray = new JsonArray(); for (WxCpUser.ExternalAttribute attr : user.getExternalAttrs()) { JsonObject attrJson = new JsonObject(); attrJson.addProperty("type", attr.getType()); attrJson.addProperty("name", attr.getName()); switch (attr.getType()) { case 0: { JsonObject text = new JsonObject(); text.addProperty("value", attr.getValue()); attrJson.add("text", text); break; } case 1: { JsonObject web = new JsonObject(); web.addProperty("url", attr.getUrl()); web.addProperty("title", attr.getTitle()); attrJson.add("web", web); break; } case 2: { JsonObject miniprogram = new JsonObject(); miniprogram.addProperty("appid", attr.getAppid()); miniprogram.addProperty("pagepath", attr.getPagePath()); miniprogram.addProperty("title", attr.getTitle()); attrJson.add("miniprogram", miniprogram); break; } default: } attrsJsonArray.add(attrJson); } JsonObject attrsJson = new JsonObject(); attrsJson.add(EXTERNAL_ATTR, attrsJsonArray); o.add(EXTERNAL_PROFILE, attrsJson); } return o; } }
WxCpUserGsonAdapter implements JsonDeserializer<WxCpUser>, JsonSerializer<WxCpUser> { @Override public JsonElement serialize(WxCpUser user, Type typeOfSrc, JsonSerializationContext context) { JsonObject o = new JsonObject(); if (user.getUserId() != null) { o.addProperty("userid", user.getUserId()); } if (user.getName() != null) { o.addProperty("name", user.getName()); } if (user.getDepartIds() != null) { JsonArray jsonArray = new JsonArray(); for (Long departId : user.getDepartIds()) { jsonArray.add(new JsonPrimitive(departId)); } o.add("department", jsonArray); } if (user.getOrders() != null) { JsonArray jsonArray = new JsonArray(); for (Integer order : user.getOrders()) { jsonArray.add(new JsonPrimitive(order)); } o.add("order", jsonArray); } if (user.getPosition() != null) { o.addProperty("position", user.getPosition()); } if (user.getMobile() != null) { o.addProperty("mobile", user.getMobile()); } if (user.getGender() != null) { o.addProperty("gender", user.getGender().getCode()); } if (user.getEmail() != null) { o.addProperty("email", user.getEmail()); } if (user.getAvatar() != null) { o.addProperty("avatar", user.getAvatar()); } if (user.getAvatarMediaId() != null) { o.addProperty("avatar_mediaid", user.getAvatarMediaId()); } if (user.getStatus() != null) { o.addProperty("status", user.getStatus()); } if (user.getEnable() != null) { o.addProperty("enable", user.getEnable()); } if (user.getIsLeader() != null) { o.addProperty("isleader", user.getIsLeader()); } if (user.getHideMobile() != null) { o.addProperty("hide_mobile", user.getHideMobile()); } if (user.getEnglishName() != null) { o.addProperty("english_name", user.getEnglishName()); } if (user.getTelephone() != null) { o.addProperty("telephone", user.getTelephone()); } if (user.getQrCode() != null) { o.addProperty("qr_code", user.getQrCode()); } if (user.getToInvite() != null) { o.addProperty("to_invite", user.getToInvite()); } if (user.getExtAttrs().size() > 0) { JsonArray attrsJsonArray = new JsonArray(); for (WxCpUser.Attr attr : user.getExtAttrs()) { JsonObject attrJson = new JsonObject(); attrJson.addProperty("name", attr.getName()); attrJson.addProperty("value", attr.getValue()); attrsJsonArray.add(attrJson); } JsonObject attrsJson = new JsonObject(); attrsJson.add("attrs", attrsJsonArray); o.add(EXTATTR, attrsJson); } if (user.getExternalAttrs().size() > 0) { JsonArray attrsJsonArray = new JsonArray(); for (WxCpUser.ExternalAttribute attr : user.getExternalAttrs()) { JsonObject attrJson = new JsonObject(); attrJson.addProperty("type", attr.getType()); attrJson.addProperty("name", attr.getName()); switch (attr.getType()) { case 0: { JsonObject text = new JsonObject(); text.addProperty("value", attr.getValue()); attrJson.add("text", text); break; } case 1: { JsonObject web = new JsonObject(); web.addProperty("url", attr.getUrl()); web.addProperty("title", attr.getTitle()); attrJson.add("web", web); break; } case 2: { JsonObject miniprogram = new JsonObject(); miniprogram.addProperty("appid", attr.getAppid()); miniprogram.addProperty("pagepath", attr.getPagePath()); miniprogram.addProperty("title", attr.getTitle()); attrJson.add("miniprogram", miniprogram); break; } default: } attrsJsonArray.add(attrJson); } JsonObject attrsJson = new JsonObject(); attrsJson.add(EXTERNAL_ATTR, attrsJsonArray); o.add(EXTERNAL_PROFILE, attrsJson); } return o; }  }
WxCpUserGsonAdapter implements JsonDeserializer<WxCpUser>, JsonSerializer<WxCpUser> { @Override public JsonElement serialize(WxCpUser user, Type typeOfSrc, JsonSerializationContext context) { JsonObject o = new JsonObject(); if (user.getUserId() != null) { o.addProperty("userid", user.getUserId()); } if (user.getName() != null) { o.addProperty("name", user.getName()); } if (user.getDepartIds() != null) { JsonArray jsonArray = new JsonArray(); for (Long departId : user.getDepartIds()) { jsonArray.add(new JsonPrimitive(departId)); } o.add("department", jsonArray); } if (user.getOrders() != null) { JsonArray jsonArray = new JsonArray(); for (Integer order : user.getOrders()) { jsonArray.add(new JsonPrimitive(order)); } o.add("order", jsonArray); } if (user.getPosition() != null) { o.addProperty("position", user.getPosition()); } if (user.getMobile() != null) { o.addProperty("mobile", user.getMobile()); } if (user.getGender() != null) { o.addProperty("gender", user.getGender().getCode()); } if (user.getEmail() != null) { o.addProperty("email", user.getEmail()); } if (user.getAvatar() != null) { o.addProperty("avatar", user.getAvatar()); } if (user.getAvatarMediaId() != null) { o.addProperty("avatar_mediaid", user.getAvatarMediaId()); } if (user.getStatus() != null) { o.addProperty("status", user.getStatus()); } if (user.getEnable() != null) { o.addProperty("enable", user.getEnable()); } if (user.getIsLeader() != null) { o.addProperty("isleader", user.getIsLeader()); } if (user.getHideMobile() != null) { o.addProperty("hide_mobile", user.getHideMobile()); } if (user.getEnglishName() != null) { o.addProperty("english_name", user.getEnglishName()); } if (user.getTelephone() != null) { o.addProperty("telephone", user.getTelephone()); } if (user.getQrCode() != null) { o.addProperty("qr_code", user.getQrCode()); } if (user.getToInvite() != null) { o.addProperty("to_invite", user.getToInvite()); } if (user.getExtAttrs().size() > 0) { JsonArray attrsJsonArray = new JsonArray(); for (WxCpUser.Attr attr : user.getExtAttrs()) { JsonObject attrJson = new JsonObject(); attrJson.addProperty("name", attr.getName()); attrJson.addProperty("value", attr.getValue()); attrsJsonArray.add(attrJson); } JsonObject attrsJson = new JsonObject(); attrsJson.add("attrs", attrsJsonArray); o.add(EXTATTR, attrsJson); } if (user.getExternalAttrs().size() > 0) { JsonArray attrsJsonArray = new JsonArray(); for (WxCpUser.ExternalAttribute attr : user.getExternalAttrs()) { JsonObject attrJson = new JsonObject(); attrJson.addProperty("type", attr.getType()); attrJson.addProperty("name", attr.getName()); switch (attr.getType()) { case 0: { JsonObject text = new JsonObject(); text.addProperty("value", attr.getValue()); attrJson.add("text", text); break; } case 1: { JsonObject web = new JsonObject(); web.addProperty("url", attr.getUrl()); web.addProperty("title", attr.getTitle()); attrJson.add("web", web); break; } case 2: { JsonObject miniprogram = new JsonObject(); miniprogram.addProperty("appid", attr.getAppid()); miniprogram.addProperty("pagepath", attr.getPagePath()); miniprogram.addProperty("title", attr.getTitle()); attrJson.add("miniprogram", miniprogram); break; } default: } attrsJsonArray.add(attrJson); } JsonObject attrsJson = new JsonObject(); attrsJson.add(EXTERNAL_ATTR, attrsJsonArray); o.add(EXTERNAL_PROFILE, attrsJson); } return o; }  @Override WxCpUser deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context); @Override JsonElement serialize(WxCpUser user, Type typeOfSrc, JsonSerializationContext context); }
WxCpUserGsonAdapter implements JsonDeserializer<WxCpUser>, JsonSerializer<WxCpUser> { @Override public JsonElement serialize(WxCpUser user, Type typeOfSrc, JsonSerializationContext context) { JsonObject o = new JsonObject(); if (user.getUserId() != null) { o.addProperty("userid", user.getUserId()); } if (user.getName() != null) { o.addProperty("name", user.getName()); } if (user.getDepartIds() != null) { JsonArray jsonArray = new JsonArray(); for (Long departId : user.getDepartIds()) { jsonArray.add(new JsonPrimitive(departId)); } o.add("department", jsonArray); } if (user.getOrders() != null) { JsonArray jsonArray = new JsonArray(); for (Integer order : user.getOrders()) { jsonArray.add(new JsonPrimitive(order)); } o.add("order", jsonArray); } if (user.getPosition() != null) { o.addProperty("position", user.getPosition()); } if (user.getMobile() != null) { o.addProperty("mobile", user.getMobile()); } if (user.getGender() != null) { o.addProperty("gender", user.getGender().getCode()); } if (user.getEmail() != null) { o.addProperty("email", user.getEmail()); } if (user.getAvatar() != null) { o.addProperty("avatar", user.getAvatar()); } if (user.getAvatarMediaId() != null) { o.addProperty("avatar_mediaid", user.getAvatarMediaId()); } if (user.getStatus() != null) { o.addProperty("status", user.getStatus()); } if (user.getEnable() != null) { o.addProperty("enable", user.getEnable()); } if (user.getIsLeader() != null) { o.addProperty("isleader", user.getIsLeader()); } if (user.getHideMobile() != null) { o.addProperty("hide_mobile", user.getHideMobile()); } if (user.getEnglishName() != null) { o.addProperty("english_name", user.getEnglishName()); } if (user.getTelephone() != null) { o.addProperty("telephone", user.getTelephone()); } if (user.getQrCode() != null) { o.addProperty("qr_code", user.getQrCode()); } if (user.getToInvite() != null) { o.addProperty("to_invite", user.getToInvite()); } if (user.getExtAttrs().size() > 0) { JsonArray attrsJsonArray = new JsonArray(); for (WxCpUser.Attr attr : user.getExtAttrs()) { JsonObject attrJson = new JsonObject(); attrJson.addProperty("name", attr.getName()); attrJson.addProperty("value", attr.getValue()); attrsJsonArray.add(attrJson); } JsonObject attrsJson = new JsonObject(); attrsJson.add("attrs", attrsJsonArray); o.add(EXTATTR, attrsJson); } if (user.getExternalAttrs().size() > 0) { JsonArray attrsJsonArray = new JsonArray(); for (WxCpUser.ExternalAttribute attr : user.getExternalAttrs()) { JsonObject attrJson = new JsonObject(); attrJson.addProperty("type", attr.getType()); attrJson.addProperty("name", attr.getName()); switch (attr.getType()) { case 0: { JsonObject text = new JsonObject(); text.addProperty("value", attr.getValue()); attrJson.add("text", text); break; } case 1: { JsonObject web = new JsonObject(); web.addProperty("url", attr.getUrl()); web.addProperty("title", attr.getTitle()); attrJson.add("web", web); break; } case 2: { JsonObject miniprogram = new JsonObject(); miniprogram.addProperty("appid", attr.getAppid()); miniprogram.addProperty("pagepath", attr.getPagePath()); miniprogram.addProperty("title", attr.getTitle()); attrJson.add("miniprogram", miniprogram); break; } default: } attrsJsonArray.add(attrJson); } JsonObject attrsJson = new JsonObject(); attrsJson.add(EXTERNAL_ATTR, attrsJsonArray); o.add(EXTERNAL_PROFILE, attrsJson); } return o; }  @Override WxCpUser deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context); @Override JsonElement serialize(WxCpUser user, Type typeOfSrc, JsonSerializationContext context);  }