简体   繁体   English

序列化和反序列化时 JSON 属性的不同名称

[英]Different names of JSON property during serialization and deserialization

Is it possible: to have one field in class, but different names for it during serialization/deserialization in Jackson library?是否有可能:在 class 中有一个字段,但在 Jackson 库中的序列化/反序列化过程中有不同的名称?

For example, I have class "Coordiantes".例如,我有 class “坐标”。

class Coordinates{
  int red;
}

For deserialization from JSON want to have format like this:对于 JSON 的反序列化,希望具有如下格式:

{
  "red":12
}

But when I will serialize object, result should be like this one:但是当我序列化 object 时,结果应该是这样的:

{
  "r":12
}

I tried to implement this by applying @JsonProperty annotation both on getter and setter (with different values):我试图通过在 getter 和 setter(具有不同的值)上应用@JsonProperty注释来实现这一点:

class Coordiantes{
    int red;

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

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

but I got an exception:但我有一个例外:

org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "red" org.codehaus.jackson.map.exc.UnrecognizedPropertyException:无法识别的字段“红色”

Just tested and this works:刚刚测试,这有效:

public class Coordinates {
    byte red;

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

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

The idea is that method names should be different, so jackson parses it as different fields, not as one field.这个想法是方法名称应该不同,因此杰克逊将其解析为不同的字段,而不是一个字段。

Here is test code:这是测试代码:

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());

Result:结果:

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

You can use @jsonAlias which got introduced in jackson 2.9.0您可以使用在 jackson 2.9.0 中引入的@jsonAlias

Example:例子:

public class Info {
  @JsonAlias({ "red" })
  public String r;
}

This uses r during serialization, but allows red as an alias during deserialization.这在序列化期间使用r ,但在反序列化期间允许red作为别名。 This still allows r to be deserialized as well, though.不过,这仍然允许r被反序列化。

You can use a combination of @JsonSetter , and @JsonGetter to control the deserialization, and serialization of your property, respectively.您可以使用@JsonSetter@JsonGetter的组合来分别控制属性的反序列化和序列化。 This will also allow you to keep standardized getter and setter method names that correspond to your actual field name.这也将允许您保留与您的实际字段名称相对应的标准化 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;
    }
}

I would bind two different getters/setters pair to one variable:我会将两个不同的 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;
    }
}

It's possible to have normal getter/setter pair.可以有正常的 getter/setter 对。 You just need to specify access mode in @JsonProperty您只需要在@JsonProperty中指定访问模式

Here is unit test for that:这是对此的单元测试:

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

I got the output as follows:我得到的输出如下:

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

You can use this variant:您可以使用此变体:

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;

with Lombok getter带龙目岛吸气剂

This was not what I was expecting as a solution (though it is a legitimate use case).这不是我所期望的解决方案(尽管它是一个合法的用例)。 My requirement was to allow an existing buggy client (a mobile app which already released) to use alternate names.我的要求是允许现有的错误客户端(已经发布的移动应用程序)使用备用名称。

The solution lies in providing a separate setter method like this:解决方案在于提供一个单独的 setter 方法,如下所示:

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

Annotating with @JsonAlias which got introduced with Jackson 2.9+, without mentioning @JsonProperty on the item to be deserialized with more than one alias(different names for a json property) works fine.使用 Jackson 2.9+ 引入的@JsonAlias进行注释,而没有提及要使用多个别名(json 属性的不同名称)反序列化的项目上的@JsonProperty工作正常。

I used com.fasterxml.jackson.annotation.JsonAlias for package consistency with com.fasterxml.jackson.databind.ObjectMapper for my use-case.对于我的用例,我使用com.fasterxml.jackson.annotation.JsonAliascom.fasterxml.jackson.databind.ObjectMapper保持包的一致性。

For eg:例如:

@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));

}

just works fine.只是工作正常。

我知道这是一个老问题,但对我来说,当我发现它与 Gson 库冲突时,我就开始工作了,所以如果你使用 Gson,那么使用@SerializedName("name")而不是@JsonProperty("name")希望这会有所帮助

They must have included this as a feature, because now setting a different @JsonProperty for a getter and setter results in exactly what you would expect (different property name during serialization and deserialization for the same field).他们必须将此作为一项功能包括在内,因为现在为 getter 和 setter 设置不同的@JsonProperty会产生您所期望的结果(同一字段的序列化和反序列化期间的不同属性名称)。 Jackson version 2.6.7杰克逊版本 2.6.7

In my case, I had to read inputs in Brazilian portuguese and generate outputs in english.就我而言,我必须阅读巴西葡萄牙语的输入并生成英语输出。

So, a workaround which worked for me was using @JsonAlias instead of @JsonProperty :因此,对我有用的解决方法是使用@JsonAlias而不是@JsonProperty


// pseudo-java
@Value
public class User {

   String username;

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

}

You can write a serialize class to do that:您可以编写一个序列化类来做到这一点:

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

For Kotlin guys:对于 Kotlin 的家伙:

data class TestClassDTO(
    @JsonProperty("user_name")
    val username: String
)

You will successfull handle {"user_name": "John"} from POST payload in RestControllers您将成功处理来自 RestControllers 中 POST 有效负载的{"user_name": "John"}

But when you need to serialize back with same name of @JsonProperty you can use this reflexe-approach但是当您需要使用与@JsonProperty 相同的名称进行序列化时,您可以使用这种反射方法

fun Any.forceSerialize(separator: String, sorted: Boolean = false): String {
    var fieldNameToAnnotatedNameMap = this.javaClass.declaredFields.map { it.name }.associateWith { fieldName ->
        val jsonFieldName =
            this::class.primaryConstructor?.parameters?.first { it.name == fieldName }?.annotations?.firstOrNull { it is JsonProperty }
        val serializedName = if (jsonFieldName != null) (jsonFieldName as JsonProperty).value else fieldName
        serializedName
    }
    if (sorted)
        fieldNameToAnnotatedNameMap = fieldNameToAnnotatedNameMap.toList().sortedBy { (_, value) -> value}.toMap()
    return fieldNameToAnnotatedNameMap.entries.joinToString(separator) { e ->
        val field = this::class.memberProperties.first { it.name == e.key }
        "${e.value}=${field.javaGetter?.invoke(this)}"
    }
}

Use both JsonAlias and JsonProperty on the attribute.在属性上同时使用 JsonAlias 和 JsonProperty。

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

In this case paymentAccountId can serialized from JSON either by payment_account_id or by payout_account_id , but when deserialized back to JSON JSONProperty will be used, and payment_account_id will be used.在这种情况下, paymentAccountId可以通过payment_account_idpayout_account_id从 JSON 序列化,但是当反序列化回 JSON 时,将使用 JSONProperty,并使用payment_account_id

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

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