![](/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.