Closed Moouna closed 1 year ago
经测试,是测试用例的问题.如果存在继承的必须明确指出使用哪个类型.否则将以指定的类型进行反序列化 以下是修改后的代码,可以通过
public static class ElementContent {
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public static class VarElement extends ElementContent {
private ElementContent defineName;
private List<List<CaseWhenElement>> children;
public ElementContent getDefineName() {
return defineName;
}
public void setDefineName(ElementContent defineName) {
this.defineName = defineName;
}
public List<List<CaseWhenElement>> getChildren() {
return children;
}
public void setChildren(List<List<CaseWhenElement>> children) {
this.children = children;
}
}
public static class CaseWhenElement extends VarElement {
private List<CaseWhen> caseWhens;
private List<ElementContent> theElse;
public List<CaseWhen> getCaseWhens() {
return caseWhens;
}
public void setCaseWhens(List<CaseWhen> caseWhens) {
this.caseWhens = caseWhens;
}
public List<ElementContent> getTheElse() {
return theElse;
}
public void setTheElse(List<ElementContent> theElse) {
this.theElse = theElse;
}
}
public static class CaseWhen {
private List<ElementContent> theCase = new ArrayList<ElementContent>();
private List<ElementContent> theWhen = new ArrayList<ElementContent>();
public List<ElementContent> getTheCase() {
return theCase;
}
public void setTheCase(List<ElementContent> theCase) {
this.theCase = theCase;
}
public List<ElementContent> getTheWhen() {
return theWhen;
}
public void setTheWhen(List<ElementContent> theWhen) {
this.theWhen = theWhen;
}
}
@Test
public void test() {
VarElement varElement = new VarElement();
varElement.setChildren(new ArrayList<List<CaseWhenElement>>() {
{
this.add(new ArrayList<CaseWhenElement>() {
{
this.add(new CaseWhenElement() {
{
this.setCaseWhens(new ArrayList<CaseWhen>() {
{
this.add(new CaseWhen() {
{
this.setTheCase(new ArrayList<ElementContent>() {
{
this.add(new ElementContent() {
{
this.setId("a");
this.setName("保费");
}
});
this.add(new ElementContent() {
{
this.setId(">=");
this.setName(">=");
}
});
this.add(new ElementContent() {
{
this.setId("b");
this.setName("b");
}
});
}
});
this.setTheWhen(new ArrayList<ElementContent>() {
{
this.add(new ElementContent() {
{
this.setId("c");
this.setName("c");
}
});
this.add(new ElementContent() {
{
this.setId("*");
this.setName("*");
}
});
this.add(new ElementContent() {
{
this.setId("10");
this.setName("10");
}
});
}
});
}
});
}
});
this.setTheElse(new ArrayList<ElementContent>() {
{
this.add(new ElementContent() {
{
this.setId("d");
this.setName("d");
}
});
this.add(new ElementContent() {
{
this.setId("-");
this.setName("-");
}
});
this.add(new ElementContent() {
{
this.setId("3");
this.setName("3");
}
});
}
});
}
});
}
});
}
});
String json = JSON.toJSONString(varElement);
VarElement element = JSON.parseObject(json, VarElement.class);
Assert.assertEquals(json, JSON.toJSONString(element));
}
泛型类反序列化失败 `import java.util.ArrayList; import java.util.List;
import org.junit.Assert; import org.junit.Test;
import com.alibaba.fastjson.JSON;
public class ElementJsonTest {
} `