简体   繁体   中英

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

Say I have this 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 };
}

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

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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