|
| 1 | +package me.chanjar.weixin.mp.util.json; |
| 2 | + |
| 3 | +import com.google.gson.JsonDeserializationContext; |
| 4 | +import com.google.gson.JsonDeserializer; |
| 5 | +import com.google.gson.JsonElement; |
| 6 | +import com.google.gson.JsonObject; |
| 7 | +import com.google.gson.JsonParseException; |
| 8 | +import com.google.gson.JsonSerializationContext; |
| 9 | +import com.google.gson.JsonSerializer; |
| 10 | +import me.chanjar.weixin.common.util.json.GsonHelper; |
| 11 | +import me.chanjar.weixin.mp.bean.Industry; |
| 12 | +import me.chanjar.weixin.mp.bean.WxMpIndustry; |
| 13 | + |
| 14 | +import java.lang.reflect.Type; |
| 15 | + |
| 16 | +/** |
| 17 | + * @author miller |
| 18 | + */ |
| 19 | +public class WxMpIndustryGsonAdapter implements JsonSerializer<WxMpIndustry>, JsonDeserializer<WxMpIndustry> { |
| 20 | + @Override |
| 21 | + public JsonElement serialize(WxMpIndustry wxMpIndustry, Type type, JsonSerializationContext jsonSerializationContext) { |
| 22 | + JsonObject json = new JsonObject(); |
| 23 | + json.addProperty("industry_id1", wxMpIndustry.getPrimaryIndustry().getId()); |
| 24 | + json.addProperty("industry_id2", wxMpIndustry.getSecondIndustry().getId()); |
| 25 | + return json; |
| 26 | + } |
| 27 | + |
| 28 | + @Override |
| 29 | + public WxMpIndustry deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { |
| 30 | + WxMpIndustry wxMpIndustry = new WxMpIndustry(); |
| 31 | + JsonObject primaryIndustry = jsonElement.getAsJsonObject().get("primary_industry").getAsJsonObject(); |
| 32 | + wxMpIndustry.setPrimaryIndustry(convertFromJson(primaryIndustry)); |
| 33 | + JsonObject secondaryIndustry = jsonElement.getAsJsonObject().get("secondary_industry").getAsJsonObject(); |
| 34 | + wxMpIndustry.setSecondIndustry(convertFromJson(secondaryIndustry)); |
| 35 | + return wxMpIndustry; |
| 36 | + } |
| 37 | + |
| 38 | + private Industry convertFromJson(JsonObject json) { |
| 39 | + Industry industry = new Industry(); |
| 40 | + industry.setFirstClass(GsonHelper.getString(json, "first_class")); |
| 41 | + industry.setSecondClass(GsonHelper.getString(json, "second_class")); |
| 42 | + return industry; |
| 43 | + } |
| 44 | +} |
0 commit comments