@Test public void testDeserialize() { final String userJson = "{\n" + " \"errcode\": 0,\n" + " \"errmsg\": \"ok\",\n" + " \"userid\": \"zhangsan\",\n" + " \"name\": \"李四\",\n" + " \"department\": [1, 2],\n" + " \"order\": [1, 2],\n" + " \"position\": \"后台工程师\",\n" + " \"mobile\": \"15913215421\",\n" + " \"gender\": \"1\",\n" + " \"email\": \"zhangsan@gzdev.com\",\n" + " \"isleader\": 1,\n" + " \"avatar\": \"http: " \"telephone\": \"020-123456\",\n" + " \"enable\": 1,\n" + " \"alias\": \"jackzhang\",\n" + " \"extattr\": {\n" + " \"attrs\": [{\n" + " \"name\": \"爱好\",\n" + " \"value\": \"旅游\"\n" + " }, {\n" + " \"name\": \"卡号\",\n" + " \"value\": \"1234567234\"\n" + " }]\n" + " },\n" + " \"status\": 1,\n" + " \"qr_code\": \"https: " \"external_profile\": {\n" + " \"external_attr\": [{\n" + " \"type\": 0,\n" + " \"name\": \"文本名称\",\n" + " \"text\": {\n" + " \"value\": \"文本\"\n" + " }\n" + " },\n" + " {\n" + " \"type\": 1,\n" + " \"name\": \"网页名称\",\n" + " \"web\": {\n" + " \"url\": \"http: " \"title\": \"标题\"\n" + " }\n" + " },\n" + " {\n" + " \"type\": 2,\n" + " \"name\": \"测试app\",\n" + " \"miniprogram\": {\n" + " \"appid\": \"wx8bd80126147df384\",\n" + " \"pagepath\": \"/index\",\n" + " \"title\": \"my miniprogram\"\n" + " }\n" + " }\n" + " ]\n" + " }\n" + "}"; final WxCpUser user = WxCpUser.fromJson(userJson); assertThat(user).isNotNull(); assertThat(user.getOrders()).isNotEmpty(); assertThat(user.getOrders().length).isEqualTo(2); assertThat(user.getOrders()[0]).isEqualTo(1); assertThat(user.getOrders()[1]).isEqualTo(2); assertThat(user.getExternalAttrs()).isNotEmpty(); final WxCpUser.ExternalAttribute externalAttr1 = user.getExternalAttrs().get(0); assertThat(externalAttr1.getType()).isEqualTo(0); assertThat(externalAttr1.getName()).isEqualTo("文本名称"); assertThat(externalAttr1.getValue()).isEqualTo("文本"); final WxCpUser.ExternalAttribute externalAttr2 = user.getExternalAttrs().get(1); assertThat(externalAttr2.getType()).isEqualTo(1); assertThat(externalAttr2.getName()).isEqualTo("网页名称"); assertThat(externalAttr2.getUrl()).isEqualTo("http: assertThat(externalAttr2.getTitle()).isEqualTo("标题"); final WxCpUser.ExternalAttribute externalAttr3 = user.getExternalAttrs().get(2); assertThat(externalAttr3.getType()).isEqualTo(2); assertThat(externalAttr3.getName()).isEqualTo("测试app"); assertThat(externalAttr3.getAppid()).isEqualTo("wx8bd80126147df384"); assertThat(externalAttr3.getPagePath()).isEqualTo("/index"); assertThat(externalAttr3.getTitle()).isEqualTo("my miniprogram"); }
@Override public WxCpUser deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonObject o = json.getAsJsonObject(); WxCpUser user = new WxCpUser(); if (o.get("department") != null) { JsonArray departJsonArray = o.get("department").getAsJsonArray(); Long[] departIds = new Long[departJsonArray.size()]; int i = 0; for (JsonElement jsonElement : departJsonArray) { departIds[i++] = jsonElement.getAsLong(); } user.setDepartIds(departIds); } if (o.get("order") != null) { JsonArray departJsonArray = o.get("order").getAsJsonArray(); Integer[] orders = new Integer[departJsonArray.size()]; int i = 0; for (JsonElement jsonElement : departJsonArray) { orders[i++] = jsonElement.getAsInt(); } user.setOrders(orders); } user.setUserId(GsonHelper.getString(o, "userid")); user.setName(GsonHelper.getString(o, "name")); user.setPosition(GsonHelper.getString(o, "position")); user.setMobile(GsonHelper.getString(o, "mobile")); user.setGender(Gender.fromCode(GsonHelper.getString(o, "gender"))); user.setEmail(GsonHelper.getString(o, "email")); user.setAvatar(GsonHelper.getString(o, "avatar")); user.setAvatarMediaId(GsonHelper.getString(o, "avatar_mediaid")); user.setStatus(GsonHelper.getInteger(o, "status")); user.setEnable(GsonHelper.getInteger(o, "enable")); user.setIsLeader(GsonHelper.getInteger(o, "isleader")); user.setHideMobile(GsonHelper.getInteger(o, "hide_mobile")); user.setEnglishName(GsonHelper.getString(o, "english_name")); user.setTelephone(GsonHelper.getString(o, "telephone")); user.setQrCode(GsonHelper.getString(o, "qr_code")); user.setToInvite(GsonHelper.getBoolean(o, "to_invite")); if (GsonHelper.isNotNull(o.get(EXTATTR))) { this.buildExtraAttrs(o, user); } if (GsonHelper.isNotNull(o.get(EXTERNAL_PROFILE))) { this.buildExternalAttrs(o, user); } return user; }
WxCpUserGsonAdapter implements JsonDeserializer<WxCpUser>, JsonSerializer<WxCpUser> { @Override public WxCpUser deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonObject o = json.getAsJsonObject(); WxCpUser user = new WxCpUser(); if (o.get("department") != null) { JsonArray departJsonArray = o.get("department").getAsJsonArray(); Long[] departIds = new Long[departJsonArray.size()]; int i = 0; for (JsonElement jsonElement : departJsonArray) { departIds[i++] = jsonElement.getAsLong(); } user.setDepartIds(departIds); } if (o.get("order") != null) { JsonArray departJsonArray = o.get("order").getAsJsonArray(); Integer[] orders = new Integer[departJsonArray.size()]; int i = 0; for (JsonElement jsonElement : departJsonArray) { orders[i++] = jsonElement.getAsInt(); } user.setOrders(orders); } user.setUserId(GsonHelper.getString(o, "userid")); user.setName(GsonHelper.getString(o, "name")); user.setPosition(GsonHelper.getString(o, "position")); user.setMobile(GsonHelper.getString(o, "mobile")); user.setGender(Gender.fromCode(GsonHelper.getString(o, "gender"))); user.setEmail(GsonHelper.getString(o, "email")); user.setAvatar(GsonHelper.getString(o, "avatar")); user.setAvatarMediaId(GsonHelper.getString(o, "avatar_mediaid")); user.setStatus(GsonHelper.getInteger(o, "status")); user.setEnable(GsonHelper.getInteger(o, "enable")); user.setIsLeader(GsonHelper.getInteger(o, "isleader")); user.setHideMobile(GsonHelper.getInteger(o, "hide_mobile")); user.setEnglishName(GsonHelper.getString(o, "english_name")); user.setTelephone(GsonHelper.getString(o, "telephone")); user.setQrCode(GsonHelper.getString(o, "qr_code")); user.setToInvite(GsonHelper.getBoolean(o, "to_invite")); if (GsonHelper.isNotNull(o.get(EXTATTR))) { this.buildExtraAttrs(o, user); } if (GsonHelper.isNotNull(o.get(EXTERNAL_PROFILE))) { this.buildExternalAttrs(o, user); } return user; } }
WxCpUserGsonAdapter implements JsonDeserializer<WxCpUser>, JsonSerializer<WxCpUser> { @Override public WxCpUser deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonObject o = json.getAsJsonObject(); WxCpUser user = new WxCpUser(); if (o.get("department") != null) { JsonArray departJsonArray = o.get("department").getAsJsonArray(); Long[] departIds = new Long[departJsonArray.size()]; int i = 0; for (JsonElement jsonElement : departJsonArray) { departIds[i++] = jsonElement.getAsLong(); } user.setDepartIds(departIds); } if (o.get("order") != null) { JsonArray departJsonArray = o.get("order").getAsJsonArray(); Integer[] orders = new Integer[departJsonArray.size()]; int i = 0; for (JsonElement jsonElement : departJsonArray) { orders[i++] = jsonElement.getAsInt(); } user.setOrders(orders); } user.setUserId(GsonHelper.getString(o, "userid")); user.setName(GsonHelper.getString(o, "name")); user.setPosition(GsonHelper.getString(o, "position")); user.setMobile(GsonHelper.getString(o, "mobile")); user.setGender(Gender.fromCode(GsonHelper.getString(o, "gender"))); user.setEmail(GsonHelper.getString(o, "email")); user.setAvatar(GsonHelper.getString(o, "avatar")); user.setAvatarMediaId(GsonHelper.getString(o, "avatar_mediaid")); user.setStatus(GsonHelper.getInteger(o, "status")); user.setEnable(GsonHelper.getInteger(o, "enable")); user.setIsLeader(GsonHelper.getInteger(o, "isleader")); user.setHideMobile(GsonHelper.getInteger(o, "hide_mobile")); user.setEnglishName(GsonHelper.getString(o, "english_name")); user.setTelephone(GsonHelper.getString(o, "telephone")); user.setQrCode(GsonHelper.getString(o, "qr_code")); user.setToInvite(GsonHelper.getBoolean(o, "to_invite")); if (GsonHelper.isNotNull(o.get(EXTATTR))) { this.buildExtraAttrs(o, user); } if (GsonHelper.isNotNull(o.get(EXTERNAL_PROFILE))) { this.buildExternalAttrs(o, user); } return user; }  }
WxCpUserGsonAdapter implements JsonDeserializer<WxCpUser>, JsonSerializer<WxCpUser> { @Override public WxCpUser deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonObject o = json.getAsJsonObject(); WxCpUser user = new WxCpUser(); if (o.get("department") != null) { JsonArray departJsonArray = o.get("department").getAsJsonArray(); Long[] departIds = new Long[departJsonArray.size()]; int i = 0; for (JsonElement jsonElement : departJsonArray) { departIds[i++] = jsonElement.getAsLong(); } user.setDepartIds(departIds); } if (o.get("order") != null) { JsonArray departJsonArray = o.get("order").getAsJsonArray(); Integer[] orders = new Integer[departJsonArray.size()]; int i = 0; for (JsonElement jsonElement : departJsonArray) { orders[i++] = jsonElement.getAsInt(); } user.setOrders(orders); } user.setUserId(GsonHelper.getString(o, "userid")); user.setName(GsonHelper.getString(o, "name")); user.setPosition(GsonHelper.getString(o, "position")); user.setMobile(GsonHelper.getString(o, "mobile")); user.setGender(Gender.fromCode(GsonHelper.getString(o, "gender"))); user.setEmail(GsonHelper.getString(o, "email")); user.setAvatar(GsonHelper.getString(o, "avatar")); user.setAvatarMediaId(GsonHelper.getString(o, "avatar_mediaid")); user.setStatus(GsonHelper.getInteger(o, "status")); user.setEnable(GsonHelper.getInteger(o, "enable")); user.setIsLeader(GsonHelper.getInteger(o, "isleader")); user.setHideMobile(GsonHelper.getInteger(o, "hide_mobile")); user.setEnglishName(GsonHelper.getString(o, "english_name")); user.setTelephone(GsonHelper.getString(o, "telephone")); user.setQrCode(GsonHelper.getString(o, "qr_code")); user.setToInvite(GsonHelper.getBoolean(o, "to_invite")); if (GsonHelper.isNotNull(o.get(EXTATTR))) { this.buildExtraAttrs(o, user); } if (GsonHelper.isNotNull(o.get(EXTERNAL_PROFILE))) { this.buildExternalAttrs(o, user); } return user; }  @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 WxCpUser deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonObject o = json.getAsJsonObject(); WxCpUser user = new WxCpUser(); if (o.get("department") != null) { JsonArray departJsonArray = o.get("department").getAsJsonArray(); Long[] departIds = new Long[departJsonArray.size()]; int i = 0; for (JsonElement jsonElement : departJsonArray) { departIds[i++] = jsonElement.getAsLong(); } user.setDepartIds(departIds); } if (o.get("order") != null) { JsonArray departJsonArray = o.get("order").getAsJsonArray(); Integer[] orders = new Integer[departJsonArray.size()]; int i = 0; for (JsonElement jsonElement : departJsonArray) { orders[i++] = jsonElement.getAsInt(); } user.setOrders(orders); } user.setUserId(GsonHelper.getString(o, "userid")); user.setName(GsonHelper.getString(o, "name")); user.setPosition(GsonHelper.getString(o, "position")); user.setMobile(GsonHelper.getString(o, "mobile")); user.setGender(Gender.fromCode(GsonHelper.getString(o, "gender"))); user.setEmail(GsonHelper.getString(o, "email")); user.setAvatar(GsonHelper.getString(o, "avatar")); user.setAvatarMediaId(GsonHelper.getString(o, "avatar_mediaid")); user.setStatus(GsonHelper.getInteger(o, "status")); user.setEnable(GsonHelper.getInteger(o, "enable")); user.setIsLeader(GsonHelper.getInteger(o, "isleader")); user.setHideMobile(GsonHelper.getInteger(o, "hide_mobile")); user.setEnglishName(GsonHelper.getString(o, "english_name")); user.setTelephone(GsonHelper.getString(o, "telephone")); user.setQrCode(GsonHelper.getString(o, "qr_code")); user.setToInvite(GsonHelper.getBoolean(o, "to_invite")); if (GsonHelper.isNotNull(o.get(EXTATTR))) { this.buildExtraAttrs(o, user); } if (GsonHelper.isNotNull(o.get(EXTERNAL_PROFILE))) { this.buildExternalAttrs(o, user); } return user; }  @Override WxCpUser deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context); @Override JsonElement serialize(WxCpUser user, Type typeOfSrc, JsonSerializationContext context);  }