[英]yaml using SnakeYaml
---
university: scsb
country: us
Entities:
!Entity
name: john
subjects:
-math
-English
-C++
!Entity
name: mary
subjects:
-science
-French
I am trying to load the above file into a map, with data under entities section to be mapped to collection of entity objects. 我正在尝试将上述文件加载到映射中,其中实体部分下的数据将映射到实体对象的集合。 Is this a correct yaml syntax as I get yaml parser error. 当我收到yaml解析器错误时,这是正确的yaml语法吗?
I had more luck with the following: 我在以下方面更加幸运:
---
university: scsb
country: us
Entities: {
!Entity {
name: john,
subjects:
-math
-English
-C++
},
!Entity {
name: mary,
subjects:
-science
-French
}
}
Here is an example of using a custom tag (!dice) with SnakeYAML. 这是在SnakeYAML中使用自定义标记(!dice)的示例。 The full example is here . 完整的示例在这里 。 It comes from the SnakeYAML documentation . 它来自SnakeYAML文档 。
class DiceConstructor extends SafeConstructor {
public DiceConstructor() {
this.yamlConstructors.put(new Tag("!dice"), new ConstructDice());
}
private class ConstructDice extends AbstractConstruct {
public Object construct(Node node) {
String val = (String) constructScalar((ScalarNode) node);
int position = val.indexOf('d');
Integer a = new Integer(val.substring(0, position));
Integer b = new Integer(val.substring(position + 1));
return new Dice(a, b);
}
}
}
@SuppressWarnings("unchecked")
public void testConstructor() {
Yaml yaml = new Yaml(new DiceConstructor());
Object data = yaml.load("{initial hit points: !dice '8d4'}");
Map<String, Dice> map = (Map<String, Dice>) data;
assertEquals(new Dice(8, 4), map.get("initial hit points"));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.