假设我有这个 JSON
{
"propertA" : "test"
}
使用此类反序列化为对象
public static class MyClass
{
private String propertya;
@JsonGetter( "propertya" )
public String getPropertya() { return this.propertya; }
@JsonSetter( "propertyA" )
public void setPropertya( String a ){ this.propertya = a };
}
我使用了@JsonGetter,因此我可以将该对象实例序列化为以下内容:
{
"properta" : "test"
}
但事实并非如此,我仍然得到以下信息:
{
"propertA" : "test"
}
我做错了什么?我期望 @JsonGetter 会将我的类实例属性“propertya”序列化为“propertya”,但似乎 @JsonSetter 在序列化时接管了控制权。 @JsonGetter 到底是做什么的?看起来它不会影响我的对象的序列化方式。
最佳答案
我更新到版本 2.4.0 并且它有效。但我必须将 @JsonIgnore 添加到字段中,这很好。
在 2.4.0 中,以下代码应该可以工作:
public static class MyClass
{
@JsonIgnore
private String propertya;
@JsonGetter( "propertya" )
public String getPropertya() { return this.propertya; }
@JsonSetter( "propertyA" )
public void setPropertya( String a ){ this.propertya = a };
}
关于java - 是否可以使用 Jackson JSON 进行非对称序列化和反序列化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24076474/