|
| 1 | +package me.chanjar.weixin.mp.util.json; |
| 2 | + |
| 3 | +import com.google.gson.JsonElement; |
| 4 | +import com.google.gson.JsonObject; |
| 5 | +import com.google.gson.JsonSerializationContext; |
| 6 | +import com.google.gson.JsonSerializer; |
| 7 | +import me.chanjar.weixin.mp.bean.subscribe.WxMpSubscribeMessage; |
| 8 | + |
| 9 | +import java.lang.reflect.Type; |
| 10 | + |
| 11 | +/** |
| 12 | + * @author Mklaus |
| 13 | + * @date 2018-01-22 下午12:31 |
| 14 | + */ |
| 15 | +public class WxMpSubscribeMessageGsonAdapter implements JsonSerializer<WxMpSubscribeMessage> { |
| 16 | + |
| 17 | + @Override |
| 18 | + public JsonElement serialize(WxMpSubscribeMessage message, Type type, JsonSerializationContext jsonSerializationContext) { |
| 19 | + JsonObject messageJson = new JsonObject(); |
| 20 | + messageJson.addProperty("touser", message.getToUser()); |
| 21 | + messageJson.addProperty("template_id", message.getTemplateId()); |
| 22 | + |
| 23 | + if (message.getUrl() != null) { |
| 24 | + messageJson.addProperty("url", message.getUrl()); |
| 25 | + } |
| 26 | + |
| 27 | + if (message.getMiniProgram() != null) { |
| 28 | + JsonObject miniProgramJson = new JsonObject(); |
| 29 | + miniProgramJson.addProperty("appid", message.getMiniProgram().getAppid()); |
| 30 | + miniProgramJson.addProperty("pagepath", message.getMiniProgram().getPagePath()); |
| 31 | + messageJson.add("miniprogram", miniProgramJson); |
| 32 | + } |
| 33 | + |
| 34 | + messageJson.addProperty("scene", message.getScene()); |
| 35 | + messageJson.addProperty("title", message.getTitle()); |
| 36 | + |
| 37 | + JsonObject data = new JsonObject(); |
| 38 | + messageJson.add("data", data); |
| 39 | + |
| 40 | + JsonObject content = new JsonObject(); |
| 41 | + data.add("content", content); |
| 42 | + |
| 43 | + if (message.getContentValue() != null) { |
| 44 | + content.addProperty("value", message.getContentValue()); |
| 45 | + } |
| 46 | + |
| 47 | + if (message.getContentColor() != null) { |
| 48 | + content.addProperty("color", message.getContentColor()); |
| 49 | + } |
| 50 | + |
| 51 | + return messageJson; |
| 52 | + |
| 53 | + } |
| 54 | +} |
0 commit comments