While serialization of the class DataType the dbOptions is been ignored but dataType is being printed with its value.
Note I need to ignore the these property only during serialization and not deserialization.
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXTERNAL_PROPERTY, property = "dataType")
@JsonSubTypes(value = {
@JsonSubTypes.Type(value = DefaultType.class, name = "Default"),
@JsonSubTypes.Type(value = NumberRangeType.class, name = "NumberRange"),
})
public abstract class DataType {
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
protected String dataType;
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
protected String dbOptions;
public String getDataType() {
return dataType;
}
public void setDataType(String dataType) {
this.dataType = dataType;
}
public String getDbOptions() {
return dbOptions;
}
public void setDbOptions(String dbOptions) {
this.dbOptions = dbOptions;
}
abstract
public void compute() throws ParseException;
}
Sample output is :
"options":{"dataType":"NumberRange","id":"1","min":0,"max":30}
Don't want dataType to get printed in the output
It seems this unexpected behaviour is a bug (see https://github.com/FasterXML/jackson-databind/issues/935). So you have to work around the issue. One of the solutions is explained here http://www.davismol.net/2015/03/21/jackson-using-jsonignore-and-jsonproperty-annotations-to-exclude-a-property-only-from-json-deserialization/.
The following is an example from the latter link adapted to mimic the intended behaviour of the @JsonProperty(access = JsonProperty.Access.WRITE_ONLY) annotation.
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.io.Serializable;
public class User implements Serializable {
@JsonIgnore
private String password;
@JsonIgnore
public String getPassword() {
return password;
}
@JsonProperty
public void setPassword(String password) {
this.password = password;
}
}