|
- /*
- * KINGSTAR MEDIA SOLUTIONS Co.,LTD. Copyright c 2005-2013. All rights reserved.
- *
- * This source code is the property of KINGSTAR MEDIA SOLUTIONS LTD. It is intended
- * only for the use of KINGSTAR MEDIA application development. Reengineering, reproduction
- * arose from modification of the original source, or other redistribution of this source
- * is not permitted without written permission of the KINGSTAR MEDIA SOLUTIONS LTD.
- */
- package chanjarster.weixin.util;
-
- import java.lang.reflect.Type;
-
- import chanjarster.weixin.out.WxCustomMessage;
- import chanjarster.weixin.out.WxCustomMessage.WxArticle;
- import chanjarster.weixin.service.WxMsgType;
-
- import com.google.gson.JsonArray;
- import com.google.gson.JsonElement;
- import com.google.gson.JsonObject;
- import com.google.gson.JsonSerializationContext;
- import com.google.gson.JsonSerializer;
-
- /**
- *
- * @author qianjia
- *
- */
- public class WxCustomMessageGsonAdapter implements JsonSerializer<WxCustomMessage> {
-
- public JsonElement serialize(WxCustomMessage message, Type typeOfSrc, JsonSerializationContext context) {
- JsonObject messageJson = new JsonObject();
- messageJson.addProperty("touser", message.getTouser());
- messageJson.addProperty("msgtype", message.getMsgtype());
-
- if (WxMsgType.TEXT.equals(message.getMsgtype())) {
- JsonObject text = new JsonObject();
- text.addProperty("content", message.getContent());
- messageJson.add("text", text);
- }
-
- if (WxMsgType.IMAGE.equals(message.getMsgtype())) {
- JsonObject image = new JsonObject();
- image.addProperty("media_id", message.getMedia_id());
- messageJson.add("image", image);
- }
-
- if (WxMsgType.VOICE.equals(message.getMsgtype())) {
- JsonObject voice = new JsonObject();
- voice.addProperty("media_id", message.getMedia_id());
- messageJson.add("voice", voice);
- }
-
- if (WxMsgType.VIDEO.equals(message.getMsgtype())) {
- JsonObject video = new JsonObject();
- video.addProperty("media_id", message.getMedia_id());
- video.addProperty("thumb_media_id", message.getThumb_media_id());
- video.addProperty("title", message.getTitle());
- video.addProperty("description", message.getDescription());
- messageJson.add("video", video);
- }
-
- if (WxMsgType.MUSIC.equals(message.getMsgtype())) {
- JsonObject music = new JsonObject();
- music.addProperty("title", message.getTitle());
- music.addProperty("description", message.getDescription());
- music.addProperty("thumb_media_id", message.getThumb_media_id());
- music.addProperty("musicurl", message.getMusicurl());
- music.addProperty("hqmusicurl", message.getHqmusicurl());
- messageJson.add("music", music);
- }
-
- if (WxMsgType.NEWS.equals(message.getMsgtype())) {
- JsonArray articleJsonArray = new JsonArray();
- for (WxArticle article : message.getArticles()) {
- JsonObject articleJson = new JsonObject();
- articleJson.addProperty("title", article.getTitle());
- articleJson.addProperty("description", article.getDescription());
- articleJson.addProperty("url", article.getUrl());
- articleJson.addProperty("picurl", article.getPicurl());
- articleJsonArray.add(articleJson);
- }
- messageJson.add("articles", articleJsonArray);
- }
-
- return messageJson;
- }
-
- }
|