简体   繁体   中英

@JSON annotation to replace a String value with null in Java

I am looking for an annotation I can use to replace String value to null in Java

There are some @Json annotation that can do customization while serializing a JSON object to POJO,

I want to do something like this:

@JsonFormat(<if incoming value is SOMESTRING>, <set incoming value to null>)
String valueChangedUsingAnnotation;

Use @JsonIgnore annotation. This annotation is used when you want to ignore certain properties of a java class. It will ignore those fields annotated with when reading from json to java object and also, writing java object to json.

If incoming value is SOMETHING, set its value to null. For this you can update the setter method of that particular field in POJO class. Because, when incoming object is deserialized it invokes setter methods of each fields.

Something like below:

class Test{

//constructor, other fields and their getters and setters

private String target;

public void setTarget(String target) {
if(target.equals("abcd"){
   this.target=null
}
else {
    this.target=target;
  } 
} 
} 

There is no need of any JSON annotations.

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