我的意图是使用杰克逊拥有像 JSON 中的多态集合,也许在标签的帮助下。
我似乎无法正确配置它。
我的 yaml 文件是:
!person
age: 27
job: dev
name: me
skills:
- !devSkill {
description: 'software development',
name: android,
language: java, c++
years: 7
}
- !softSkill {
description: 'good person',
name: <3,
reason: lots of NGO work
}
- !sportsSkill {
description: 'racing legend',
name: vrooom,
championships: - San Marino 2012
- San Marino 2015
}
代码中的哪个将映射到具有(抽象?)带有描述和名称的 BaseSkill 以及 3 个子项:dev、soft 和 sports 的层次结构。
我的问题是,我对 SnakeYAML 的文档的理解不足以允许这样做。我目前的选择是:
Constructor constructor = new Constructor(Person.class);
TypeDescription carDescription = new TypeDescription(Person.class);
carDescription.putListPropertyType("skills", SportsSkill.class);
carDescription.putListPropertyType("skills", SoftSkill.class);
carDescription.putListPropertyType("skills", DevSkill.class);
// Apparently the last is the winner here because it overrides
constructor.addTypeDescription(carDescription);
Representer representer = new Representer();
representer.addClassTag(Person.class, new Tag("!person"));
representer.addClassTag(SoftSkill.class, new Tag("!Softkill"));
representer.addClassTag(DevSkill.class, new Tag("!devSkill"));
representer.addClassTag(SportsSkill.class, new Tag("!portsSkill"));
DumperOptions options = new DumperOptions();
options.setPrettyFlow(true);
Yaml yaml = new Yaml(constructor, representer, options);
错误在于
E/YAML﹕ Can't construct a java object for tag:yaml.org,2002:app.yamlmodel.Person; exception=Cannot create property=skills for JavaBean=Person(name=me, job=dev, age=27, skills=null); null; Can't construct a java object for !sportSkill; exception=Invalid tag: !sportSkill
in "<reader>", line 1, column 1:
name: me
^