是否可能:在类中有一个字段,但在Jackson库中序列化/反序列化期间为它取不同的名称?

例如,我有一个类“coordindiantes”。

class Coordinates{
  int red;
}

对于JSON的反序列化,希望有这样的格式:

{
  "red":12
}

但是当我序列化对象时,结果应该是这样的:

{
  "r":12
}

我试图通过在getter和setter上应用@JsonProperty注释来实现这一点(具有不同的值):

class Coordiantes{
    int red;

    @JsonProperty("r")
    public byte getRed() {
      return red;
    }

    @JsonProperty("red")
    public void setRed(byte red) {
      this.red = red;
    }
}

但我有个例外:

org.codehaus.jackson。map。exx . unrecognizedpropertyexception:无法识别的字段“red”


当前回答

我将两个不同的getter /setter对绑定到一个变量:

class Coordinates{
    int red;

    @JsonProperty("red")
    public byte getRed() {
      return red;
    }

    public void setRed(byte red) {
      this.red = red;
    }

    @JsonProperty("r")
    public byte getR() {
      return red;
    }

    public void setR(byte red) {
      this.red = red;
    }
}

其他回答

在属性上同时使用JsonAlias和JsonProperty。

data class PayoutMethodCard(
    @JsonProperty("payment_account_id")
    @JsonAlias("payout_account_id")
    val payoutAccountId: Long
)

在这种情况下,paymentAccountId可以通过payment_account_id或payout_account_id从JSON序列化,但当反序列化回JSON时,将使用JSONProperty,并使用payment_account_id。

我将两个不同的getter /setter对绑定到一个变量:

class Coordinates{
    int red;

    @JsonProperty("red")
    public byte getRed() {
      return red;
    }

    public void setRed(byte red) {
      this.red = red;
    }

    @JsonProperty("r")
    public byte getR() {
      return red;
    }

    public void setR(byte red) {
      this.red = red;
    }
}

你可以使用这个变体:

import lombok.Getter;
import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonProperty;

//...

@JsonProperty(value = "rr") // for deserialization
@Getter(onMethod_ = {@JsonGetter(value = "r")}) // for serialization
private String rrrr;

使用Lombok吸气器

使用Jackson 2.9+引入的@JsonAlias进行注释,而不用在要用多个别名(json属性的不同名称)反序列化的项上提到@JsonProperty就可以了。

我使用com.fasterxml.jackson.annotation.JsonAlias与com.fasterxml.jackson.databind.ObjectMapper作为我的用例来实现包的一致性。

例如:

@Data
@Builder
public class Chair {

    @JsonAlias({"woodenChair", "steelChair"})
    private String entityType;

}


@Test
public void test1() {

   String str1 = "{\"woodenChair\":\"chair made of wood\"}";
   System.out.println( mapper.readValue(str1, Chair.class));
   String str2 = "{\"steelChair\":\"chair made of steel\"}";
   System.out.println( mapper.readValue(str2, Chair.class));

}

工作得很好。

刚刚测试,这是有效的:

public class Coordinates {
    byte red;

    @JsonProperty("r")
    public byte getR() {
      return red;
    }

    @JsonProperty("red")
    public void setRed(byte red) {
      this.red = red;
    }
}

其思想是方法名应该是不同的,因此jackson将其解析为不同的字段,而不是一个字段。

下面是测试代码:

Coordinates c = new Coordinates();
c.setRed((byte) 5);

ObjectMapper mapper = new ObjectMapper();
System.out.println("Serialization: " + mapper.writeValueAsString(c));

Coordinates r = mapper.readValue("{\"red\":25}",Coordinates.class);
System.out.println("Deserialization: " + r.getR());

结果:

Serialization: {"r":5}
Deserialization: 25