Jackson ObjectMapper setSerializationInclusion()不起作用(Jackson ObjectMapper setSerializationInclusion() not working)
我只是熟悉杰克逊的装订。 但是,当我测试setSerializationInclusion(JsonInclude.Include.NON_NULL)时,我发现它有时不起作用。
这是我的代码
package com.blithe.main; import com.blithe.model.Student; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude.Include; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; public class Jackson_2_NullValue { public static void main(String[] args) throws JsonProcessingException { ObjectMapper mapper = new ObjectMapper(); Student s = new Student(); String stundetString = mapper.writeValueAsString(s); System.out.println(stundetString); // exclude null fields mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); s.setName("ss"); stundetString = mapper.writeValueAsString(s); System.out.println(stundetString); } }
和POJO
package com.blithe.model; import java.util.Date; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude.Include; // @JsonIgnoreProperties(ignoreUnknown = true) // exclude null fields for the whole class // @JsonInclude(Include.NON_NULL) public class Student { // exclude the field whe it's empty ("") // @JsonInclude(value=Include.NON_EMPTY) private String name; private Integer age; private Date birth; // Jackson ignores it @JsonIgnore private String nickName; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Date getBirth() { return birth; } public void setBirth(Date birth) { this.birth = birth; } public String getNickName() { return nickName; } public void setNickName(String nickName) { this.nickName = nickName; } }
输出是
{"name":null,"age":null,"birth":null} {"name":"ss","age":null,"birth":null}
后者应该排除空值,但事实并非如此。
但是,当我以这种方式放置代码时。
package com.blithe.main; import com.blithe.model.Student; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude.Include; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; public class Jackson_2_NullValue { public static void main(String[] args) throws JsonProcessingException { ObjectMapper mapper = new ObjectMapper(); mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); Student s = new Student(); String stundetString = mapper.writeValueAsString(s); System.out.println(stundetString); // exclude null fields // mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); s.setName("ss"); stundetString = mapper.writeValueAsString(s); System.out.println(stundetString); } }
它适用于下面的输出
{} {"name":"ss"}
这是正常还是只是某种错误? 我想念什么吗? 唯一的maven依赖是jackson-databind 2.7.4。 欢迎任何讨论。 谢谢!
I'm just getting familiar with Jackson binding. However, when I'm testing setSerializationInclusion(JsonInclude.Include.NON_NULL), I found that it's not working sometimes.
Here is my code
package com.blithe.main; import com.blithe.model.Student; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude.Include; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; public class Jackson_2_NullValue { public static void main(String[] args) throws JsonProcessingException { ObjectMapper mapper = new ObjectMapper(); Student s = new Student(); String stundetString = mapper.writeValueAsString(s); System.out.println(stundetString); // exclude null fields mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); s.setName("ss"); stundetString = mapper.writeValueAsString(s); System.out.println(stundetString); } }
and the POJO
package com.blithe.model; import java.util.Date; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude.Include; // @JsonIgnoreProperties(ignoreUnknown = true) // exclude null fields for the whole class // @JsonInclude(Include.NON_NULL) public class Student { // exclude the field whe it's empty ("") // @JsonInclude(value=Include.NON_EMPTY) private String name; private Integer age; private Date birth; // Jackson ignores it @JsonIgnore private String nickName; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Date getBirth() { return birth; } public void setBirth(Date birth) { this.birth = birth; } public String getNickName() { return nickName; } public void setNickName(String nickName) { this.nickName = nickName; } }
the output is
{"name":null,"age":null,"birth":null} {"name":"ss","age":null,"birth":null}
The later one should be null-value excluded, but it doesn't.
However, when I put my code this way.
package com.blithe.main; import com.blithe.model.Student; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude.Include; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; public class Jackson_2_NullValue { public static void main(String[] args) throws JsonProcessingException { ObjectMapper mapper = new ObjectMapper(); mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); Student s = new Student(); String stundetString = mapper.writeValueAsString(s); System.out.println(stundetString); // exclude null fields // mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); s.setName("ss"); stundetString = mapper.writeValueAsString(s); System.out.println(stundetString); } }
It works with the output below
{} {"name":"ss"}
Is this normal or just some kind of bug? Do I miss anything? The only maven dependency is jackson-databind 2.7.4. Any discussion is welcomed. Thanks!
原文:https://stackoverflow.com/questions/37406817
最满意答案
由于宽度以%计算,因此不能精确地削减10px。 一种可能的解决方法是稍微移动一个起点(例如5)并将宽度从100%(例如减少到90%):
child.children[0].style.width = ((color.RND[type][mode] - max[type][mode][0]) / (max[type][mode][1] - max[type][mode][0]) * 100) * 0.9 + 5 + '%';
Since width is calculated in % you can't cut 10px precisly. A possible workaround is to move a starting point a little (e.g. 5) and reduce width from 100% (e.g. to 90%):
child.children[0].style.width = ((color.RND[type][mode] - max[type][mode][0]) / (max[type][mode][1] - max[type][mode][0]) * 100) * 0.9 + 5 + '%';
相关问答
更多-
根据你想要支持的浏览器,一个简单的选择是使用calc : width: calc(100% + 10px); 另请参见: 我可以使用calc() 例如: http : //jsfiddle.net/hJXng/19/ I'm not sure where xpy's answers have gone, but his/her comments finally made my day. Had to add two wrapper divs, since a div with "display: tabl ...
-
在ie7中div绝对位置的问题,div向右移动10px(Problem with position absolute in ie7, div moves 10px to the right)[2023-07-24]
添加position:relative对于#center ,然后left:0px到#menu 。 绝对定位的元素相对于它们最接近的定位父亲而定位。 最好给出想要定位左/右和上/下坐标的项目,以防止发现奇怪的结果。 Add position:relative to #center and then left:0px to #menu. Absolutely positioned elements are positioned relative to their closest positioned paren ... -
由于利润率不会加起来,它们会崩溃。 如果您有两个元素A和B,A margin = 10px,B margin = 15px,则A和B之间的间距将为15px。 两种解决方案 使用margin-bottom:20px,margin-top:0px 将它们包装到容器中并应用填充:10px 0; Because margins don't add up, they collapse. If you have two elements A and B, A margin=10px and B margin=15px, ...
-
你有没有尝试将Rectangle的边距设置为0 ? 编辑 :填充必须来自按钮控件模板。 尝试使用自定义模板: