如何通过 YAML 加载基于抽象 class 的对象的数组列表

How to load array list of abstract class based objects via YAML

我正在尝试从 YAML 文件加载多态对象的 ArrayList,但收到此错误消息:

Exception in thread "main" Can't construct a java object for tag:yaml.org,2002:dirty.AbstractObj; exception=No suitable constructor with 2 arguments found for class dirty.AbstractObj in 'reader', line 2, column 1: - !obj1 ^

YAML 文件:

---
- !obj1
  abstractObjInt: 1222
  obj1Int: 333444
- !obj2
  abstractObjInt: 987
  obj2Int: 5555

Loader.java:

public class Loader {
    private static Yaml yaml;

    public Loader() {}

    public static void main(String[] args) {
        initAbstractObjs();
    }

    protected static ArrayList<AbstractObj> initAbstractObjs() {
        Constructor constructor = new Constructor(AbstractObj.class);
        constructor.addTypeDescription(new TypeDescription(Obj1.class, new Tag("!obj1")));
        constructor.addTypeDescription(new TypeDescription(Obj2.class, new Tag("!obj2")));
        yaml = new Yaml(constructor);
        InputStream input = loadFile();
        ArrayList<AbstractObj> abstractObjs = yaml.load(input);

        return abstractObjs;
    }
    
    private static InputStream loadFile() {
        File file = new File("path/to/file/file.yml");
        InputStream input = null;
        
        try {
           input = new FileInputStream(file);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return input;
    }
}

AbstractObj.java

public abstract class AbstractObj {
    protected int abstractObjInt;
    
    public AbstractObj() {}

    public int getAbstractObjInt() {
        return abstractObjInt;
    }

    public void setAbstractObjInt(int abstractObjInt) {
        this.abstractObjInt = abstractObjInt;
    }
}

Obj1.java

public class Obj1 extends AbstractObj {
    private int obj1Int;

    public Obj1() {}

    public void setObj1Int(int obj1Int) {
        this.obj1Int = obj1Int;
    }

    public int getObj1Int() {
        return obj1Int;
    }
}

Obj2.java

public class Obj2 extends AbstractObj {
    private int obj2Int;
    
    public Obj2() {}

    public void setObj2Int(int obj2Int) {
        this.obj2Int = obj2Int;
    }

    public int getObj2Int() {
        return obj2Int;
    }
}

谁能指出我做错了什么。

您是在告诉构造函数您的根类型是 AbstractObj,但实际上不是。

改为这样做:

Constructor constructor = new Constructor();

由于 SnakeYaml 默认情况下会从 YAML 序列构造 ArrayList,因此您无需提供明确的根类型。