- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我的 Java 应用程序中使用 Jackson 进行序列化(POJO 到 JSON)和反序列化(JSON 到 POJO)时,我通常希望保留所有字段,因此使用(默认)JsonInclude.Value.ALWAYS
.
为了允许通过应用程序的 Rest API 进行部分更新,我还想区分设置为 null
的值。具体而言,该值保持不变。为此,Java8 Optional<?>
类似乎是正确的选择。
为了获得适当的支持,我必须添加 Jdk8Module
到ObjectMapper
。一切都非常简单。
开箱即用的反序列化行为正是我想要的。不存在的字段保留其默认值(此处: null
)和显式提供的 null
值被反序列化为 Optional.empty()
(或Optional.ofNullable(null)
)。
我想要的是 Optional<?>
显式值为 null
的字段从生成的 JSON 中排除,但始终包含任何其他字段(例如普通 Integer
)(即使它是 null
)。
众多可用选项之一是 MixIn
。不幸的是,一个MixIn
可能适用于其他注释,但不适用于 @JsonInclude
(这似乎是 jackson 的一个错误)。
public class OptionalObjectMappingTest {
public static class MyBean {
public Integer one;
public Optional<Integer> two;
public Optional<Integer> three;
public Optional<Integer> four;
}
@JsonInclude(JsonInclude.Include.NOT_NULL)
public static class OptionalMixIn {}
private ObjectMapper initObjectMapper() {
return new ObjectMapper()
.registerModule(new Jdk8Module())
.setSerialisationInclusion(JsonInclude.Include.ALWAYS)
.addMixIn(Optional.class, OptionalMixIn.class);
}
@Test
public void testDeserialisation() {
String json = "{\"one\":null,\"two\":2,\"three\":null}";
MyBean bean = initObjectMapper().readValue(json, MyBean.class);
Assert.assertNull(bean.one);
Assert.assertEquals(Optional.of(2), bean.two);
Assert.assertEquals(Optional.empty(), bean.three);
Assert.assertNull(bean.four);
}
@Test
public void testSerialisation() {
MyBean bean = new MyBean();
bean.one = null;
bean.two = Optional.of(2);
bean.three = Optional.empty();
bean.four = null;
String result = initObjectMapper().writeValueAsString(bean);
String expected = "{\"one\":null,\"two\":2,\"three\":null}";
// FAILS, due to result = "{one:null,two:2,three:null,four:null}"
Assert.assertEquals(expected, result);
}
}
<小时/>
有多种方法可以(动态)包含/排除字段及其值,但似乎没有一种“官方”方法可以解决问题:
@JsonInclude
每个 Optional<?>
上的注释字段实际上做了我想做的事情,但这太容易忘记而且很麻烦。MixIn
应该允许 JsonInclude
的全局定义每个类型的注释,但显然没有应用(根据上面的示例测试)。@JsonIgnore
(和相关的)注释是静态的,不关心字段的值。@JsonFilter
需要在包含 Optional
的每个类上进行设置字段,您需要了解 PropertyFilter
中的每种受影响的类型。 IE。甚至比仅仅添加 JsonInclude
更有值(value)每个 Optional
领域。@JsonView
不允许根据给定 bean 实例的字段值动态包含/排除字段。JsonSerializer<?>
通过ObjectMapper.setNullValueSerializer()
注册仅在插入字段名称后调用,即如果我们不执行任何操作,生成的 JSON 将无效。BeanSerializerModifier
在将字段名称插入 JSON 之前涉及,但它无法访问字段的值。最佳答案
Edit:<小时/>As per StaxMan's answer, this doesn't seem to be a bug but a feature, as mix-ins are not meant to add annotations for every property of a certain type. The attempt of using mix-ins as described in the question just adds the
@JsonInclude
annotation on theOptional
class which has a different meaning (already described in that other answer).
由于 mix-ins 的设计行为不同,因此 ObjectMapper
的 configOverride()
上有一个新的配置选项:
setIncludeAsProperty()
配置就这么简单:
private ObjectMapper initObjectMapper() {
ObjectMapper objectMapper = new ObjectMapper()
.registerModule(new Jdk8Module())
.setSerializationInclusion(JsonInclude.Include.ALWAYS);
objectMapper.configOverride(Optional.class)
.setIncludeAsProperty(JsonInclude.Value
.construct(JsonInclude.Include.NON_NULL, null));
return objectMapper;
}
<小时/>
调整后的示例如下所示:
<小时/>public class OptionalObjectMappingTest {
public static class MyBean {
public Integer one;
public Optional<Integer> two;
public Optional<Integer> three;
public Optional<Integer> four;
}
private ObjectMapper initObjectMapper() {
ObjectMapper objectMapper = new ObjectMapper()
.registerModule(new Jdk8Module())
.setSerializationInclusion(JsonInclude.Include.ALWAYS);
objectMapper.configOverride(Optional.class)
.setIncludeAsProperty(JsonInclude.Value
.construct(JsonInclude.Include.NON_NULL, null));
return objectMapper;
}
@Test
public void testRoundTrip() throws Exception {
String originalJson = "{\"one\":null,\"two\":2,\"three\":null}";
ObjectMapper mapper = initObjectMapper();
MyBean bean = mapper.readValue(originalJson, MyBean.class);
String resultingJson = mapper.writeValueAsString(bean);
// SUCCESS: no "four:null" field is being added
Assert.assertEquals(originalJson, resultingJson);
}
}
<小时/>
一个可行的解决方案是覆盖 JacksonAnnotationIntrospector
并将其设置在 ObjectMapper
上。
只需包含自定义内省(introspection)器类并将 initObjectMapper()
方法更改为以下内容,给定的测试就会成功:
public static class OptionalAwareAnnotationIntrospector
extends JacksonAnnotationIntrospector {
@Override
public JsonInclude.Value findPropertyInclusion(Annotated a) {
if (Optional.class.equals(a.getRawType())) {
return JsonInclude.Value.construct(JsonInclude.Include.NON_NULL, JsonInclude.Include.NON_NULL);
}
return super.findPropertyInclusion(a);
}
}
private ObjectMapper initObjectMapper() {
return new ObjectMapper()
.registerModule(new Jdk8Module())
.setSerialisationInclusion(JsonInclude.Include.ALWAYS)
.setAnnotationIntrospector(new OptionalAwareAnnotationIntrospector());
}
关于java - Jackson:特定类型的所有属性都有不同的 JsonIninclude (例如可选),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41811931/
我正在尝试用 Swift 编写这段 JavaScript 代码:k_combinations 到目前为止,我在 Swift 中有这个: import Foundation import Cocoa e
我是一名优秀的程序员,十分优秀!