是否可能:在类中有一个字段,但在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”


当前回答

就我而言,我必须阅读巴西葡萄牙语的输入,并生成英语的输出。

所以,一个变通的工作,为我使用@JsonAlias而不是@JsonProperty:


// pseudo-java
@Value
public class User {

   String username;

   public User(
      @JsonAlias("nome_usuario") String username) {
     // ...
   }

}

其他回答

你可以写一个序列化类来实现:

public class Symbol

{
     private String symbol;

     private String name;

     public String getSymbol() {
        return symbol;
    }
    public void setSymbol(String symbol) {
        this.symbol = symbol;
    }    
    public String getName() {
        return name;
    }    
    public void setName(String name) {
        this.name = name;
    }
}
public class SymbolJsonSerializer extends JsonSerializer<Symbol> {

    @Override
    public void serialize(Symbol symbol, JsonGenerator jgen, SerializerProvider serializers) throws IOException, JsonProcessingException {
        jgen.writeStartObject();

        jgen.writeStringField("symbol", symbol.getSymbol());
         //Changed name to full_name as the field name of Json string
        jgen.writeStringField("full_name", symbol.getName());
        jgen.writeEndObject(); 
    }
}

            ObjectMapper mapper = new ObjectMapper();

            SimpleModule module = new SimpleModule();
            module.addSerializer(Symbol.class, new SymbolJsonSerializer());
            mapper.registerModule(module); 

            //only convert non-null field, option...
            mapper.setSerializationInclusion(Include.NON_NULL); 

            String jsonString = mapper.writeValueAsString(symbolList);

可以有普通的getter/setter对。您只需要在@JsonProperty中指定访问模式

下面是单元测试:

public class JsonPropertyTest {

  private static class TestJackson {

    private String color;

    @JsonProperty(value = "device_color", access = JsonProperty.Access.READ_ONLY)
    public String getColor() {
      return color;
    };

    @JsonProperty(value = "color", access = JsonProperty.Access.WRITE_ONLY)
    public void setColor(String color) {
      this.color = color;
    }

  }

  @Test
  public void shouldParseWithAccessModeSpecified() throws Exception {
    String colorJson = "{\"color\":\"red\"}";
    ObjectMapper mapper = new ObjectMapper();
    TestJackson colotObject = mapper.readValue(colorJson, TestJackson.class);

    String ser = mapper.writeValueAsString(colotObject);
    System.out.println("Serialized colotObject: " + ser);
  }
}

我得到的输出如下:

Serialized colotObject: {"device_color":"red"}

您可以组合使用@JsonSetter和@JsonGetter来分别控制属性的反序列化和序列化。这也将允许您保持标准化的getter和setter方法名,它们与您实际的字段名相对应。

import com.fasterxml.jackson.annotation.JsonSetter;    
import com.fasterxml.jackson.annotation.JsonGetter;

class Coordinates {
    private int red;

    //# Used during serialization
    @JsonGetter("r")
    public int getRed() {
        return red;
    }

    //# Used during deserialization
    @JsonSetter("red")
    public void setRed(int red) {
        this.red = red;
    }
}

编辑:更新了文档链接,因为fastxmlgithub页面现在返回404。

这并不是我所期望的解决方案(尽管这是一个合理的用例)。我的要求是允许一个存在bug的客户端(一个已经发布的移动应用程序)使用替代名称。

解决方案在于提供一个单独的setter方法,如下所示:

@JsonSetter( "r" )
public void alternateSetRed( byte red ) {
    this.red = red;
}

他们必须将此作为一个特性包含进来,因为现在为getter和setter设置不同的@JsonProperty会得到您所期望的结果(在同一字段的序列化和反序列化期间,属性名不同)。Jackson 2.6.7版本