【发布时间】:2017-09-29 20:39:14
【问题描述】:
假设我有一个简单的类,它包含两个字段,getter 和 setter。我有时想序列化和反序列化这个类的对象。
public class Foo {
private String a;
private int b;
public void setA(String a) {
System.out.println("a setter called");
this.a = Objects.requireNonNull(a, "Required non null field.");
}
public void setB(int b) {
System.out.println("b setter called");
this.b = b;
}
public String getA() {
return a;
}
public int getB() {
return b;
}
public static void main(String[] args) {
Representer representer = new Representer();
representer.getPropertyUtils().setSkipMissingProperties(false);
Yaml yaml = new Yaml(new Constructor(), representer);
String doc = "b: 10";
Foo testBean = yaml.loadAs(doc, Foo.class);
}
}
我希望 main 方法中的代码抛出一些异常,因为 doc 中缺少字段 a。不幸的是,默认情况下它不起作用。
我可以以某种方式配置 SnakeYaml 来做到这一点吗?
【问题讨论】:
-
我也想知道 SnakeYaml 是否可以自己做到这一点......从目前唯一的答案来看,没有另一个库是不可能的。
标签: java serialization deserialization yaml snakeyaml