Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@JsonProperty(access = JsonProperty.Access.WRITE_ONLY) not working

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

like image 470
souravlahoti Avatar asked Jun 08 '16 08:06

souravlahoti


People also ask

What is JsonProperty access Write_only?

WRITE_ONLY. public static final JsonProperty.Access WRITE_ONLY. Access setting that means that the property may only be written (set) for deserialization, but will not be read (get) on serialization, that is, the value of the property is not included in serialization.

How do I ignore JsonProperty?

To ignore individual properties, use the [JsonIgnore] attribute. You can specify conditional exclusion by setting the [JsonIgnore] attribute's Condition property. The JsonIgnoreCondition enum provides the following options: Always - The property is always ignored.

Is JsonProperty annotation necessary?

You definitely don't need all those @jsonProperty . Jackson mapper can be initialized to sereliazie/deserialize according to getters or private members, you of course need only the one you are using.

What is difference between JsonProperty and JsonAlias?

@JsonProperty can change the visibility of logical property using its access element during serialization and deserialization of JSON. @JsonAlias defines one or more alternative names for a property to be accepted during deserialization.


1 Answers

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;
  }
}
like image 199
ioikka Avatar answered Oct 21 '22 01:10

ioikka