Gson自定义转换器转换成不同的子类

public static void main(String[] args) {
        String personStr1 = "{\"id\":\"123456\",\"product_category\":\"101\",\"name\":\"xiaofeifei\",\"age\":\"25\",\"ranking\":\"0\"}";
        String personStr2 = "{\"id\":\"123456\",\"product_category\":\"201\",\"name\":\"xiaofeifei\",\"age\":\"25\",\"ranking\":\"1\"}";
        Gson gson = new GsonBuilder().registerTypeAdapter(RANKING.class, new JsonDeserializer<RANKING>() {
                    @Override
                    public RANKING deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
                        int ranking = json.getAsJsonPrimitive().getAsInt();
                        return RANKING.values()[ranking];
                    }
                }).registerTypeAdapter(BasePerson.class, new JsonDeserializer<BasePerson>() {
            @Override
            public BasePerson deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
                String type = json.getAsJsonObject().get("product_category").getAsString();
                return context.deserialize(json, PersonType.getByProductCategory(type).getType());
            }
        }).create();
        BasePerson person1 = gson.fromJson(personStr1,BasePerson.class);
        BasePerson person2 = gson.fromJson(personStr2,BasePerson.class);
    }
class BasePerson{
    private Long id;
    private String product_category;
    private RANKING ranking;

    public RANKING getRanking() {
        return ranking;
    }

    public void setRanking(RANKING ranking) {
        this.ranking = ranking;
    }

    public String getProduct_category() {
        return product_category;
    }

    public void setProduct_category(String product_category) {
        this.product_category = product_category;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
}

class PersonWithName extends BasePerson {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

class PersonWithAge extends BasePerson{
    private Integer age;

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

enum PersonType {
    WITHNAME("名称", PersonWithName.class, "101"),
    WITHAGE("年龄", PersonWithAge.class, "201");

    private String displayName;
    private Class<? extends BasePerson> type;
    private String product_category;

    public String getDisplayName() {
        return displayName;
    }

    public void setDisplayName(String displayName) {
        this.displayName = displayName;
    }

    public Class<? extends BasePerson> getType() {
        return type;
    }

    public void setType(Class<? extends BasePerson> type) {
        this.type = type;
    }

    public String getProduct_category() {
        return product_category;
    }

    public void setProduct_category(String product_category) {
        this.product_category = product_category;
    }

    private PersonType(String displayName, Class<? extends BasePerson> type , String product_category) {
        this.displayName = displayName;
        this.type = type;
        this.product_category = product_category;
    }
    public static PersonType getByProductCategory(String category){
        for(PersonType t : values()){
            if(t.product_category.equals(category)){
                return t;
            }
        }
        return null;
    }
}

enum RANKING {
    TOP, BOTTOM;
}




                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值