简体   繁体   English

@jsonproperty以相同的属性名称序列化不同的pojo元素

[英]@jsonproperty serialize different pojo elements in the same attribute name

I have a simple pojo 我有一个简单的pojo

public class C {
   public String f1;
   public Integer f2;
}

At runtime I am sure at most one of the fields is not null (ie if f1 is "hello" , I'm sure f2 is null , and vice versa) 在运行时,我确定最多其中一个字段不为null(即,如果f1"hello" ,则我确定f2null ,反之亦然)

I would like to serialize the object using always the same name; 我想使用始终相同的名称来序列化对象; for example, with 例如,

C c1 = new C();
c1.f1 = "hello"
C c2 = new C();
c2.f2 = Integer.valueOf(99)

I would like to have c1 serialized as {"samekey":"hello"} and c2 as {"samekey":99} . 我想将c1序列化为{"samekey":"hello"} ,将c2序列化为{"samekey":99}

I know I can use @JsonProperty to set the serialized name, but I cannot set the same name to both the fields. 我知道我可以使用@JsonProperty设置序列化名称,但是不能在两个字段中都设置相同的名称。

Is there a way to say that? 有没有办法这么说?

Here is the CustomSerializer, checks if f2 value not null then f2 value is copied to f1 and making f2=null.And excluding nulls in the model so always you will get one key&value as other one will be null. 这是CustomSerializer,检查f2值是否不为null,然后将f2值复制到f1并使f2 = null。并且在模型中排除null,因此您总是会得到一个key&value,因为其他键和值将为null。

public class CustomSerializer extends JsonSerializer<C> {

@Override
public void serialize(final C c, final JsonGenerator gen, final SerializerProvider serializers) throws IOException, JsonProcessingException {
    if(c.getF2() != null)  // considering always you need f1 key && value
    {
        c.setF1(c.getF2().toString());
        c.setF2(null);
    }
}

} }

Annotate serializer & notnull in model as 注释序列化器,并在模型中注释为null

@JsonSerialize(using = CustomSerializer.class)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class C {
   private String f1;
   private Integer f2;

   //getters & setters
}

Hope this is what you are expecting. 希望这就是您的期望。

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

相关问题 具有相同 JsonProperty 属性的不同字段 - Different fields with same JsonProperty attribute @JsonIgnore 和 @JsonProperty 在同一属性上 - @JsonIgnore and @JsonProperty on same attribute Jackson 在同一个 POJO 中映射不同的展开元素 - Jackson map different unwrapped elements in same POJO 使用 Jackson 序列化两个具有相同 id 的不同 POJO 对象 - Serialize two different POJO object with the same id with Jackson 在 Java 中,如何将 JSON 或 XML 字符串映射到同一个 POJO,但 XML 具有与 JSON 不同的字段/属性名称? - In Java, how can I map a string that is either JSON or XML to the same POJO, but with the XML having one different field/attribute name from the JSON? 在java pojo中将相同的json字段设置为不同的属性 - Same json field set to different attribute in java pojo 如何使用Jackson API在序列化和反序列化上使用不同的JSONProperty? - How to use different JSONProperty on Serialize & Deserialize using Jackson API? Json 与 POJO + Jackson 具有相同名称和不同类型的任何字段的模式 - Json schema with anyOf field with same name and different types to POJO + Jackson 在同一字段上读取和写入的不同@JsonProperty 配置? - Different @JsonProperty config for READ and WRITE on same field? 同一用户dto,但两个服务的JsonProperty不同 - Same User dto but with different JsonProperty for two services
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM