是否可以使用Jackson JSON进行非对称序列化和反序列化? [英] Is it possible to have asymmetric serialization and de-serialization using Jackson JSON?

查看:146
本文介绍了是否可以使用Jackson JSON进行非对称序列化和反序列化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有这个JSON

{
  "propertA" : "test"
}

使用此类反序列化为对象

gets deserialized into an object using this class

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,因此我可以将该对象实例序列化为以下内容: / p>

I used @JsonGetter so I could serialize that object instance into the following:

{
  "properta" : "test"
}

但事实并非如此,我仍然得到以下结论:

But it didn't, I still get the following:

{
  "propertA" : "test"
}

我做错了什么?我期待@JsonGetter将我的类实例属性propertya序列化为propertya,但似乎@JsonSetter在序列化时接管了控件。究竟@JsonGetter到底是什么?看起来它并没有影响我的对象如何被序列化。

What am I doing wrong? I was expecting that @JsonGetter will serialize my class instance property "propertya" into "propertya" but it seems @JsonSetter took over the control when serializing. What exactly @JsonGetter does? It looks like it's not influencing how my object will be serialized.

推荐答案

我更新到2.4.0版本并且它有效。但我必须将@JsonIgnore添加到正常的字段。

I updated to version 2.4.0 and it worked. But I would have to add @JsonIgnore to fields which is fine.

使用2.4.0时,以下代码应该可以工作:

With 2.4.0, the following code should work:

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 };
}

这篇关于是否可以使用Jackson JSON进行非对称序列化和反序列化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆