[英]Two questions about JsonProperty
问题1:
是
class Point {
private int x;
private int y;
@JsonCreator
public Point(@JsonProperty("x") int x, @JsonProperty("y") int y) {
this.x = x;
this.y = y;
}
}
和
class Point {
@JsonProperty("x")
private int x;
@JsonProperty("y")
private int y;
@JsonCreator
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
当量?
问题2:
如果我有一个字段没有出现在构造函数的参数中,例如:
class Point {
private int x;
private int y;
private int z;
@JsonCreator
public Point(@JsonProperty("x") int x, @JsonProperty("y") int y) {
this.x = x;
this.y = y;
z = 0;
}
}
杰克逊仍然知道那个领域(z)及其价值吗?
为了使用私有字段序列化/反序列化类,而没有正式的getter / setter,我必须执行以下操作:
mapper.enable(MapperFeature.CAN_OVERRIDE_ACCESS_MODIFIERS);
mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
并为我的类定义一个私有的默认构造函数,如下所示:
public class ElementDesc {
@SuppressWarnings("unused")
private ElementDesc() { this(null, null, false); }
public ElementDesc(/* a regular constructor with parameters etc */) {
...
}
private final String field1;
private final String field2;
private final boolean field3;
}
在这种情况下,Jackson可以成功地序列化/反序列化类的实例,而无需使用其常规构造函数和(访问器)方法(如果有)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.