行业相关api

This commit is contained in:
miller
2016-06-19 10:13:31 +08:00
parent 7a816cf7a9
commit af2f12267e
6 changed files with 161 additions and 0 deletions

View File

@@ -42,6 +42,7 @@ public class WxMpGsonBuilder {
INSTANCE.registerTypeAdapter(WxMpCard.class, new WxMpCardGsonAdapter());
INSTANCE.registerTypeAdapter(WxMpMassPreviewMessage.class, new WxMpMassPreviewMessageGsonAdapter());
INSTANCE.registerTypeAdapter(WxMediaImgUploadResult.class, new WxMediaImgUploadResultGsonAdapter());
INSTANCE.registerTypeAdapter(WxMpIndustry.class, new WxMpIndustryGsonAdapter());
}
public static Gson create() {

View File

@@ -0,0 +1,44 @@
package me.chanjar.weixin.mp.util.json;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import me.chanjar.weixin.common.util.json.GsonHelper;
import me.chanjar.weixin.mp.bean.Industry;
import me.chanjar.weixin.mp.bean.WxMpIndustry;
import java.lang.reflect.Type;
/**
* @author miller
*/
public class WxMpIndustryGsonAdapter implements JsonSerializer<WxMpIndustry>, JsonDeserializer<WxMpIndustry> {
@Override
public JsonElement serialize(WxMpIndustry wxMpIndustry, Type type, JsonSerializationContext jsonSerializationContext) {
JsonObject json = new JsonObject();
json.addProperty("industry_id1", wxMpIndustry.getPrimaryIndustry().getId());
json.addProperty("industry_id2", wxMpIndustry.getSecondIndustry().getId());
return json;
}
@Override
public WxMpIndustry deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
WxMpIndustry wxMpIndustry = new WxMpIndustry();
JsonObject primaryIndustry = jsonElement.getAsJsonObject().get("primary_industry").getAsJsonObject();
wxMpIndustry.setPrimaryIndustry(convertFromJson(primaryIndustry));
JsonObject secondaryIndustry = jsonElement.getAsJsonObject().get("secondary_industry").getAsJsonObject();
wxMpIndustry.setSecondIndustry(convertFromJson(secondaryIndustry));
return wxMpIndustry;
}
private Industry convertFromJson(JsonObject json) {
Industry industry = new Industry();
industry.setFirstClass(GsonHelper.getString(json, "first_class"));
industry.setSecondClass(GsonHelper.getString(json, "second_class"));
return industry;
}
}