JYAML:序列化/反序列化嵌套/内部类
首先祝大家圣诞快乐!
现在回答我的问题: 假设我有一个 Outer
类和一些内部类 Inner。作为 Outer
中的字段,我有一个 List
,然后我想将其转储到 YAML 文件。我这样做:
Outer o = new Outer();
o.innerList = new ArrayList<Inner>();
o.innerList.add(new o.Inner());
...
Yaml.dump(o, new File("test.yml");
这给了我一个例外: Exception in thread "main" org.ho.yaml.exception.ObjectCreationException: Error close line 0: Can't create object of type class game.Outer$Inner using默认构造函数。
我尝试提供自定义构造函数并将访问级别更改为公共,但没有帮助。有什么想法吗?
First of all, Merry Christmas to everyone!
Now to my question:
Let's say I have the class Outer
with some inner class Inner. As a field in Outer
, I have a List<Inner>
, which i then want to dump to a YAML file. I do this like so:
Outer o = new Outer();
o.innerList = new ArrayList<Inner>();
o.innerList.add(new o.Inner());
...
Yaml.dump(o, new File("test.yml");
This gives me the exception: Exception in thread "main" org.ho.yaml.exception.ObjectCreationException: Error near line 0: Can't create object of type class game.Outer$Inner using default constructor.
I tried providing a custom constructor and changing the access level to public, to no help. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
SnakeYAML 有很多带有内部类的示例。
YAML 文档 (test.yml) 是什么样的?
Inner 是静态内部类吗?
SnakeYAML has a lot of examples with inner classes.
How does the YAML document (test.yml) look like ?
Is Inner a static inner class ?
首先是检查 YAML 是否支持内部类序列化。
First thing is check that YAML supports the Inner class serialization.