是否可以使用Jackson JSON进行非对称序列化和反序列化?

时间:2014-06-06 07:27:56

标签: java json jackson

说我有这个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到底是什么?看起来它并没有影响我的对象的序列化方式。

1 个答案:

答案 0 :(得分:1)

我更新到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 };
}
相关问题