java - SnakeYaml 未序列化私有或受保护字段
问题描述
我正在尝试使用 SnakeYaml 转储/序列化 Java POJO,并且未序列化私有和受保护字段。
public class Dice {
private Integer a;
protected Integer b;
public Integer c;
public Dice(Integer a, Integer b, Integer c) {
this.a = a;
this.b = b;
this.c = c;
}
public Integer getA() {
return a;
}
public Integer getB() {
return b;
}
public Integer getC() {
return c;
}
public static void main(String[] args) {
Dice dice = new Dice(1,2,3);
Yaml yaml = new Yaml();
String output = yaml.dump(dice);
System.out.println(output);
}
}
产生以下内容:
!!com.ibm.watson.pml.gpm.plan.Dice {c: 3}
我已经看到很多例子表明私有和受保护的字段被正确序列化。我尝试了 1.17 和 1.23,结果相同。
解决方案
Yaml.setBeanAccess(BeanAccess.FIELD);
推荐阅读
- javascript - 我无法在 jQuery 中访问集合类型单选按钮的值
- vb.net - 使用VB隐藏DataGridView中的所有行
- c++ - 重载地图的“<<”运算符
- c# - GMT 偏移量的 C# DateFormatString
- c# - Microsoft 聊天机器人 - 删除机器人响应中的超链接
- javascript - 使用 Face-api.js 检测人脸时出错 HTML视频元素 | HTMLCanvasElement
- python - 如果键和值与其他字典不匹配,则从字典列表中删除字典
- user-interface - Material design框架的建议
- laravel - Laravel - 如何访问集合内的数组
- java - 无法完全模拟 RestHighLevelClient