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