gson反序列化多态类型组装问题

问题描述 投票:0回答:1

这是pojo

class Mother {
    //..
}

@Data
@AllArgsConstructor
class Son extends Mother {
    private int age;
}

@Data
@AllArgsConstructor
class Daughter extends Mother {
    private int age;
}

这是主要的

public static void main(String[] args) {
    List<Son> sons = new ArrayList<>();
        sons.add(new Son(12));
        sons.add(new Son(13));

    List<Daughter> daughters = new ArrayList<>();
        daughters.add(new Daughter(5));
        daughters.add(new Daughter(6));

    Map<String,List<? extends Mother>> map = new HashMap<>();
        map.put("ex-husband",sons);
        map.put("husband",daughters);

    Gson gson = new Gson();
    String json = gson.toJson(map);

    System.out.println(json);
    //ok

    Gson gson1 = new Gson();
    Type type = new TypeToken<Map<String,List<? extends Mother>>>() {}.getType();

    Map<String,List<? extends Mother>> map1 = gson1.fromJson(json,type);

    List<Son> sons1 = (List<Son>) map1.get("ex-husband");
    System.out.println(sons1.get(0).getAge());
    //error
}

这是错误

java.lang.ClassCastException: Mother cannot be cast to Son

这是格式化后的json

{
    "ex-husband": [
        {
            "age": 12
        },
        {
            "age": 13
        }
    ],
    "husband": [
        {
            "age": 5
        },
        {
            "age": 6
        }
    ]
}

它的结构如下。 地图>

gson 可以正常序列化为 json,但是反过来它无法识别具体的 Son 和 Daughter 类型,因此无法读取具体属性的值

我的期望是反序列化后可以读取子类属性,但是报错了,大佬们,这个怎么改啊,有什么好的办法吗?

java json gson deserialization
1个回答
0
投票

好的,我明白了,哈哈

@Setter
@Getter
class Mother {
    private String className;//---------------------
    //..
}

@Data
@AllArgsConstructor
@Builder
class Son extends Mother {//------------------------
    private int age;
}

@Data
@AllArgsConstructor
class Daughter extends Mother {
    private int age;
}

public class Test2 {
    public static void main(String[] args) {
        List<Son> sons = new ArrayList<>();
        Son son = new Son(11);
        Son son2 = new Son(12);
        son.setClassName("Son");//----------------------
        son.setClassName("Son");
        sons.add(son);
        sons.add(son2);

        List<Daughter> daughters = new ArrayList<>();
        Daughter daughter = new Daughter(5);
        Daughter daughter1 = new Daughter(6);
        daughter.setClassName("Daughter");//-----------------------
        daughter1.setClassName("Daughter");
        daughters.add(daughter);
        daughters.add(daughter1);

        Map<String,List<? extends Mother>> map = new HashMap<>();
        map.put("ex-husband",sons);
        map.put("husband",daughters);

        Gson gson =  new GsonBuilder().setPrettyPrinting().create();
        String json = gson.toJson(map);

        System.out.println(json);

        Type type = new TypeToken<Map<String,List<Son>>>() {}.getType();//----------------------
        Map<String,List<Son>> map1 = gson.fromJson(json,type);

        List<Son> sons1 = map1.get("ex-husband");
        for (Son son1 : sons1) {
            if (son1 instanceof Son)
                System.out.println(son1.getAge());
        }

        Type type2 = new TypeToken<Map<String,List<Daughter>>>() {}.getType();//--------------------
        Map<String,List<Daughter>> map2 = gson.fromJson(json,type2);

        List<Daughter> daughters1 = map2.get("husband");
        for (Daughter d1 : daughters1) {
            if (d1 instanceof Daughter)
                System.out.println(d1.getAge());
        }
    }
}

结果:

{
  "ex-husband": [
    {
      "age": 11,
      "className": "Son"
    },
    {
      "age": 12
    }
  ],
  "husband": [
    {
      "age": 5,
      "className": "Daughter"
    },
    {
      "age": 6,
      "className": "Daughter"
    }
  ]
}
11
12
5
6
© www.soinside.com 2019 - 2024. All rights reserved.