我想使用SnakeYaml
阅读一个简单的YAML文件#My project
name: glog
dependencies:
bling
blong
使用这个简单的Java类:
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
那里的 String
课程有问题吗?
答案 0 :(得分:2)
你的YAML是正确的,依赖是一个标量,在这种情况下是字符串&#34; bling blong&#34;。
检查您的YAML总是有帮助的,例如通过使用一些在线验证器/转换器。例如。 Online YAML Parser。如果您转换为JSON,那么您会看到问题,而且您可以交互式地尝试,直到输出更符合您在代码中的预期,然后使用它:
#My project
name: glog
dependencies:
- bling
- blong
请注意,某些在线解析器无法处理您的代码(并且他们是IMO不正确的不这样做:例如Convert YAML to JSON Online和Code Beautify无法处理您的原始示例,也不能处理最终形式。)
答案 1 :(得分:0)
Yaml对预期的用例无效。
而是写:
#My project
name: glog
dependencies:
- bling
- blong
答案 2 :(得分:0)
dependencies
不是标量,而是列表。必须明确定义列表值:
#My project
name: glog
dependencies:
- bling
- blong