如何使用 SnakeYaml 读取字符串列表

How to read a list of strings using SnakeYaml

我想使用 SnakeYaml 读取一个简单的 YAML 文件

#My project 
  name: glog
  dependencies: 
    bling
    blong

使用这个简单的 Java class:

public class Project {
    private String name;

    private ArrayList<String> dependencies = new ArrayList<String>();


    private final IvyManager ivyManager = new IvyManager();

    public Project() {

    }

    public String getName() {
        return name;
    }

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

    public ArrayList<String> getDependencies() {
        return dependencies;
    }

    public void setDependencies(ArrayList<String> dependencies) {
        this.dependencies = dependencies;
    }
}

为了能够读取文件,我需要准备 Yaml 解析器,因为 dependencies 集合是这样的:

    Constructor constructor = new Constructor(Project.class);
    TypeDescription projectDesc = new TypeDescription(Project.class);
    projectDesc.putListPropertyType("dependencies", String.class);
    constructor.addTypeDescription(projectDesc);

    Yaml yaml = new Yaml(constructor);
    Project result = yaml.loadAs(FileUtils.readFileToString(f),
            Project.class);

不幸的是,这不起作用。我有以下错误:

Cannot create property=dependencies for JavaBean=ninja.core.Project@26653222
 in 'string', line 2, column 3:
      name: glog
      ^
Can't construct a java object for scalar tag:yaml.org,2002:str; No String constructor found. Exception=java.util.ArrayList.<init>(java.lang.String)
 in 'string', line 4, column 5:
        bling
        ^

    at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.constructJavaBean2ndStep(Constructor.java:308)
    at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.construct(Constructor.java:189)
    at org.yaml.snakeyaml.constructor.Constructor$ConstructYamlObject.construct(Constructor.java:341)
    at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:182)
    at org.yaml.snakeyaml.constructor.BaseConstructor.constructDocument(BaseConstructor.java:141)
    at org.yaml.snakeyaml.constructor.BaseConstructor.getSingleData(BaseConstructor.java:127)
    at org.yaml.snakeyaml.Yaml.loadFromReader(Yaml.java:450)
    at org.yaml.snakeyaml.Yaml.loadAs(Yaml.java:427)
    at ninja.core.ProjectLoader.load(ProjectLoader.java:20)
    at ninja.command.Command.loadCurrentProject(Command.java:44)
    at ninja.command.EclipseCommand.execute(EclipseCommand.java:18)
    at ninja.cmd.Main.execute(Main.java:39)
    at ninja.cmd.Main.main(Main.java:15)
Caused by: org.yaml.snakeyaml.error.YAMLException: Can't construct a java object for scalar tag:yaml.org,2002:str; No String constructor found. Exception=java.util.ArrayList.<init>(java.lang.String)
    at org.yaml.snakeyaml.constructor.Constructor$ConstructScalar.construct(Constructor.java:406)
    at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:182)
    at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.constructJavaBean2ndStep(Constructor.java:297)
    ... 12 more
Caused by: java.lang.NoSuchMethodException: java.util.ArrayList.<init>(java.lang.String)
    at java.lang.Class.getConstructor0(Class.java:3082)
    at java.lang.Class.getConstructor(Class.java:1825)
    at org.yaml.snakeyaml.constructor.Constructor$ConstructScalar.construct(Constructor.java:404)
    ... 14 more

那里的Stringclass有问题吗

Yaml 对于预期用例无效。

改为:

#My project 
  name: glog
  dependencies: 
    - bling
    - blong

dependencies 不是标量,而是一个列表。必须明确定义列表值:

#My project 
  name: glog
  dependencies: 
    - bling
    - blong

您的 YAML 是正确的,依赖项是一个标量,在本例中是字符串 "bling blong"。

它总是有助于检查您的 YAML,例如通过使用一些在线 verifier/converter。例如。 Online YAML Parser。如果您转换为 JSON 就会看到问题,而且您可以交互式地尝试一些事情,直到输出更符合您在代码中的期望,然后使用它:

#My project
  name: glog
  dependencies:
    - bling
    - blong

请注意,某些在线解析器无法处理您的代码(IMO 不这样做是不正确的:例如 Convert YAML to JSON Online and Code Beautify 无法处理您的示例的原始形式和最终形式。)