繁体   English   中英

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

[英]Is it possible to have asymmetric serialization and de-serialization using Jackson JSON?

说我有这个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 };
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM