我有一个如下所示的课程: -
@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;
}
}
如果我有一个没有设置int字段的JsonPojo实例,那么在将它转换为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
的数据类型转换为intVal
的情况下未设置intVal
,我是否可以忽略Json输出中的Integer
?如果我将intVal
转换为整数,那么当未设置此值时,它将为null&在转换为json期间,执行JsonSerialize(include = Inclusion.NON_EMPTY)
会跳过intVal
。
但我想知道,如果不将intVal
从Integer
转换为int
,是否有可能实现相同的行为?
我正在使用杰克逊1.9。
答案 0 :(得分:3)
Inclusion.NON_EMPTY
应该做你期望的事。来自文档:
表示仅具有值的值的属性的值 如果为空或者被认为是空的,则不包括在内。
Java原语int
默认为0(这被认为是空的)。因此,如果您从未设置它或将其设置为0,则输出应为{"strVal":"Hello World"}
我在jackson 2.6.3上尝试过此操作并且似乎有效,所以我认为1.9上存在问题
您可以尝试以下操作,也许可以使用1.9:
@JsonInclude(Include.NON_DEFAULT)
private int intVal;
并删除include = Inclusion.NON_EMPTY
如果0是有效值,您可以为pojo添加一个标志。在您调用setter时,标志应该变为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;
}
答案 1 :(得分:2)
您可以尝试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;
}}
答案 2 :(得分:1)
以防这是否是可接受的解决方案:
我们可以应用自定义过滤器来检查是否设置了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) {
}
}