|
| 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 | +package me.chanjar.weixin.mp.util.json; |
| 10 | + |
| 11 | +import com.google.gson.*; |
| 12 | +import me.chanjar.weixin.common.api.WxConsts; |
| 13 | +import me.chanjar.weixin.mp.bean.WxMpCustomMessage; |
| 14 | +import me.chanjar.weixin.mp.bean.WxMpTemplateData; |
| 15 | +import me.chanjar.weixin.mp.bean.WxMpTemplateMessage; |
| 16 | + |
| 17 | +import java.lang.reflect.Type; |
| 18 | + |
| 19 | +/** |
| 20 | + * @author qianjia |
| 21 | + */ |
| 22 | +public class WxMpTemplateMessageGsonAdapter implements JsonSerializer<WxMpTemplateMessage> { |
| 23 | + |
| 24 | + public JsonElement serialize(WxMpTemplateMessage message, Type typeOfSrc, JsonSerializationContext context) { |
| 25 | + JsonObject messageJson = new JsonObject(); |
| 26 | + messageJson.addProperty("touser", message.getToUser()); |
| 27 | + messageJson.addProperty("template_id", message.getTemplateId()); |
| 28 | + if (message.getUrl() != null) { |
| 29 | + messageJson.addProperty("url", message.getUrl()); |
| 30 | + } |
| 31 | + if (message.getTopColor() != null) { |
| 32 | + messageJson.addProperty("topcolor", message.getTopColor()); |
| 33 | + } |
| 34 | + |
| 35 | + JsonObject datas = new JsonObject(); |
| 36 | + messageJson.add("data", datas); |
| 37 | + |
| 38 | + for (WxMpTemplateData data : message.getDatas()) { |
| 39 | + JsonObject dataJson = new JsonObject(); |
| 40 | + dataJson.addProperty("value", data.getValue()); |
| 41 | + if (data.getColor() != null) { |
| 42 | + dataJson.addProperty("color", data.getColor()); |
| 43 | + } |
| 44 | + datas.add(data.getName(), dataJson); |
| 45 | + } |
| 46 | + |
| 47 | + return messageJson; |
| 48 | + } |
| 49 | + |
| 50 | +} |
0 commit comments