引言
我试图使用金丝豆 (v1.0)将YAML文档的内容反序列化为Java对象。下面是我试图反序列化的YAML文档(topologyGrammar.yml):
---
rules:
- !tp.aoi.topology.TopologyRule { labels: ["empty_A"], output: ["entry_B"] }下面是我如何尝试反序列化的方法:
mainTG = (TopologyGrammar) loadYAML(TopologyGrammar.class, "grammars/topologyGrammar.yml");其中loadYAML看起来如下所示:
public Object loadYAML(Class<?> daClass, String URL) throws YamlException {
FileHandle handle = Gdx.files.internal(URL);
YamlConfig config = new YamlConfig();
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
config.readConfig.setClassLoader(classLoader);
YamlReader reader = new YamlReader(handle.reader(), config);
return reader.read(daClass);
} 此方法在加载我尝试过的其他YAML文件时没有问题。我在堆栈跟踪的顶部得到以下信息:
net.sourceforge.yamlbeans.YamlReader$YamlReaderException: Line 8, column 7: Expected scalar for String type but found: sequence start
at net.sourceforge.yamlbeans.YamlReader.readValueInternal(YamlReader.java:175)
at net.sourceforge.yamlbeans.YamlReader.readValue(YamlReader.java:150)
at net.sourceforge.yamlbeans.YamlReader.readValueInternal(YamlReader.java:261)
at net.sourceforge.yamlbeans.YamlReader.readValue(YamlReader.java:150)
at net.sourceforge.yamlbeans.YamlReader.readValueInternal(YamlReader.java:298)
at net.sourceforge.yamlbeans.YamlReader.readValue(YamlReader.java:150)
at net.sourceforge.yamlbeans.YamlReader.readValueInternal(YamlReader.java:261)
at net.sourceforge.yamlbeans.YamlReader.readValue(YamlReader.java:150)
at net.sourceforge.yamlbeans.YamlReader.read(YamlReader.java:106)
at net.sourceforge.yamlbeans.YamlReader.read(YamlReader.java:91)
at tp.aoi.grammars.YAMLparser.loadYAML(YAMLparser.java:69)
at tp.aoi.grammars.YAMLparser.<init>(YAMLparser.java:43)
...对YAMLparser.java:43的引用引用了上面粘贴的反序列化行。
类定义
以下是我对TopologyGrammar.java的定义:
public class TopologyGrammar {
public List<TopologyRule> rules;
}我对TopologyRule.java的定义是:
public class TopologyRule {
public List<String> labels;
public List<String> output;
}附加思想
在我看来,Expected scalar for String type but found: sequence start指的是文档的labels: ["empty_A"]部分。我真正想知道的是,当我说YamlReader是一个List<String> (因此应该表示为文档中的序列)时,为什么labels会期待一个List<String>呢?
问题
YamlReader 需要字符串而不是 List<String>**?**的原因是什么?
我希望我的问题不是特定于yamlbean项目。在我看来,这只是YAML语法与类定义之间的关系中的一个错误。
发布于 2016-09-14 19:50:16
YamlBeans 1.09似乎没有处理YAML 1.2规范中的所有特性,这可能是造成这里问题的原因。
此后,我转到了SnakeYaml库,发现它在许多方面都有很大的改进,并且它正确地对文档进行了反序列化。
https://stackoverflow.com/questions/38808388
复制相似问题