SnakeYaml 中的多态集合 [英] Polymorphic collections in SnakeYaml

查看:47
本文介绍了SnakeYaml 中的多态集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的意图是使用 jackson 拥有像 JSON 中的多态集合,也许在标签的帮助下.

My intention is to have polymorphic collections like the ones in JSON using jackson, maybe with the help of tags.

我似乎无法正确配置它.

I can't seem to able to configure it properly tho.

我的 yaml 文件是:

My yaml file is:

!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.

Which in code would map to a hierarchy with an (abstract?) BaseSkill with description and name, and 3 children: dev, soft and sports.

我的问题是,我对 SnakeYAML 的文档不够了解,无法允许这样做.我目前的选择是:

My problem is, I don't understand SnakeYAML's documentation enough to allow this. My current options are:

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
    ^

推荐答案

这个帖子已经过时了,但我找到了解决方案,希望它仍然对某人有帮助.你的错误是你应该将标签和类型描述符添加到构造函数中,让 SnakeYaml 找出对象结构.在你的情况下:

This thread is quit old but I found a solution for it, hope its still help someone. your mistake is that you should add the tags and type descriptor to the constructor and let SnakeYaml figure out the object structure. In you case:

Constructor constructor = new Constructor(Person.class);
constructor.addTypeDescription(new TypeDescription(SoftSkill.class, new Tag("!softkill"));
constructor.addTypeDescription(new TypeDescription(DevSkill.class, new Tag("!devkill"));
constructor.addTypeDescription(new TypeDescription(SportsSkill.class, new Tag("!sportskill"));

你没有提到你使用的 SnakeYaml 版本,但我使用的是 1.16

you didnt mention the SnakeYaml version you use but I am using 1.16

这篇关于SnakeYaml 中的多态集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆