![](/img/trans.png)
[英]Jackson converting java object primitive properties as optional in json string
[英]Ignoring not set primitive type while converting an object to json using jackson
我有一堂課,看起來像下面:
@JsonSerialize(include = Inclusion.NON_EMPTY)
public class JsonPojo {
private int intVal;
private String strVal;
public int getIntVal() {
return intVal;
}
public void setIntVal(int intVal) {
this.intVal = intVal;
}
public String getStrVal() {
return strVal;
}
public void setStrVal(String strVal) {
this.strVal = strVal;
}
}
如果我有一個JsonPojo實例,其中未設置int字段,則在將其轉換為json jackson時為其分配默認值0,如下所示:-
public static void main(String[] args) {
JsonPojo jsonPojo = new JsonPojo();
jsonPojo.setStrVal("Hello World");
try {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(Feature.FAIL_ON_EMPTY_BEANS, false);
String json = mapper.writeValueAsString(jsonPojo);
System.out.println(json);
} catch (Exception e) {
}
}
這將輸出:- {"intVal":0,"strVal":"Hello World"}
有沒有一種方法,我可以忽略intVal
在JSON輸出時intVal
也不是沒有的數據類型轉換設置intVal
到Integer
? 如果我轉換intVal
為整數,然后當這個值未設置這將是空和做JsonSerialize(include = Inclusion.NON_EMPTY)
將跳過intVal
轉換為JSON過程中。
但是我想知道是否有可能在不將intVal
從int
轉換為Integer
情況下實現相同的行為?
我正在使用傑克遜1.9。
Inclusion.NON_EMPTY
應該可以達到您的期望。 從文檔中:
表示僅包含值為空或視為空的值的屬性的值。
Java原始int
默認情況下為0(這被認為是空的)。 因此,如果您從未設置它或將其設置為0,則輸出應為{"strVal":"Hello World"}
我在傑克遜2.6.3上嘗試過此方法,並且似乎可以正常工作,所以我認為1.9上存在問題
您可以嘗試以下操作,也許可以在1.9上運行:
@JsonInclude(Include.NON_DEFAULT)
private int intVal;
並刪除include = Inclusion.NON_EMPTY
如果0是有效值,則可以在pojo中添加一個標志。 在您調用設置器時,標志應變為true:
@JsonIgnore
public boolean set = false;
public Integer getIntVal() {
if (!set) {
return null;
}
return Integer.valueOf(intVal);
}
public void setIntVal(int intVal) {
set = true;
this.intVal = intVal;
}
您可以嘗試jackson.annotations包中的@JsonInclude
和@JsonProperty
批注。 這是示例代碼:
JsonPojo類:
public class JsonPojo {
@JsonInclude(Include.NON_DEFAULT)
private int intVal;
@JsonProperty
private String strVal;
public int getIntVal() {
return intVal;
}
public void setIntVal(int intVal) {
this.intVal = intVal;
}
public String getStrVal() {
return strVal;
}
public void setStrVal(String strVal) {
this.strVal = strVal;
}}
以防萬一這是可以接受的解決方案:
我們可以應用自定義過濾器來檢查是否設置了intVal
:
@JsonSerialize(include = Inclusion.NON_EMPTY)
@JsonFilter("theFilter")
public class JsonPojo {
private int intVal;
private String strVal;
private boolean isSetIntVal = false;
@JsonIgnore
public boolean getIsSetIntVal() {
return isSetIntVal;
}
public int getIntVal() {
return intVal;
}
public void setIntVal(int intVal) {
this.intVal = intVal;
this.isSetintVal = true;
}
public String getStrVal() {
return strVal;
}
public void setStrVal(String strVal) {
this.strVal = strVal;
}
}
實現自定義過濾器,然后將其應用於編寫器:
public static void main(String[] args) {
SimpleBeanPropertyFilter theFilter = new SimpleBeanPropertyFilter() {
@Override
public void serializeAsField(Object pojo, JsonGenerator jgen, SerializerProvider provider,
BeanPropertyWriter writer) throws Exception {
if (!writer.getName().equals("intVal")) {
writer.serializeAsField(pojo, jgen, provider);
return;
}
if (((JsonPojo) pojo).getIsSetIntVal()) {
writer.serializeAsField(pojo, jgen, provider);
}
}
};
JsonPojo jsonPojo = new JsonPojo();
jsonPojo.setStrVal("Hello World");
jsonPojo.setIntVal(0);
try {
ObjectMapper mapper = new ObjectMapper();
FilterProvider filters = new SimpleFilterProvider().addFilter("theFilter", theFilter);
mapper.configure(Feature.FAIL_ON_EMPTY_BEANS, false);
String json = mapper.writer(filters).writeValueAsString(jsonPojo);
System.out.println(json);
} catch (Exception e) {
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.