繁体   English   中英

使用SnakeYaml的Yaml

[英]yaml using SnakeYaml

---
university: scsb
country: us

Entities:
 !Entity 
   name: john
   subjects:
    -math
    -English
    -C++
 !Entity
   name: mary
   subjects:
    -science
    -French

我正在尝试将上述文件加载到映射中,其中实体部分下的数据将映射到实体对象的集合。 当我收到yaml解析器错误时,这是​​正确的yaml语法吗?

我在以下方面更加幸运:

---
university: scsb
country: us

Entities: {
 !Entity {
   name: john,
   subjects:
    -math
    -English
    -C++
 },
 !Entity {
   name: mary,
   subjects:
    -science
    -French
 }
}

这是在SnakeYAML中使用自定义标记(!dice)的示例。 完整的示例在这里 它来自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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM