Jackson-Serialiser:序列化时忽略字段 [英] Jackson-Serialiser: Ignore Field at Serialisation Time
问题描述
我的情况要求更复杂的序列化.我有一个类Available
(这是一个非常简化的代码段):
My situation asks for a bit more complex serialisation. I have a class Available
(this is a very simplified snippet):
public class Available<T> {
private T value;
private boolean available;
...
}
所以是POJO
class Tmp {
private Available<Integer> myInt = Available.of(123);
private Available<Integer> otherInt = Available.clean();
...
}
通常会导致
{"myInt":{available:true,value:123},"otherInt":{available:false,value:null}}
但是,我希望序列化器像这样渲染相同的POJO:
However, I want a serialiser to render the same POJO like this:
{"myInt":123}
我现在所拥有的:
public class AvailableSerializer extends JsonSerializer<Available<?>> {
@Override
public void serialize(Available<?> available, JsonGenerator jsonGenerator, SerializerProvider provider) throws IOException, JsonProcessingException {
if (available != null && available.isAvailable()) {
jsonGenerator.writeObject(available.getValue());
}
// MISSING: nothing at all should be rendered here for the field
}
@Override
public Class<Available<?>> handledType() {
@SuppressWarnings({ "unchecked", "rawtypes" })
Class<Available<?>> clazz = (Class) Available.class;
return clazz;
}
}
测试
@Test
public void testSerialize() throws Exception {
SimpleModule module = new SimpleModule().addSerializer(new AvailableSerializer());
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(module);
System.out.println(objectMapper.writeValueAsString(new Tmp()));
}
输出
{"myInt":123,"otherInt"}
有人可以告诉我如何进行"MISSING"操作吗?或者,如果我做错了一切,该怎么办?
Can anyone tell me how to do the "MISSING"-stuff? Or if I'm doing it all wrong, how do I do it then?
我的限制是我不希望开发人员一直向Available
类型的字段添加@Json...
-批注.因此,上面的Tmp
类是一个典型的使用类的外观示例. 如果可能的话...
The restriction I have is that I don't want the developers to add @Json...
-annotations all the time to fields of type Available
. So the Tmp
-class above is an example of what a typical using class should look like. If that's possible...
推荐答案
在MichałZiober的答案之后,我不得不寻找关于Include.NON_DEFAULT
和默认对象的内容,并遇到了此答案,解释了Include.NON_EMPTY
Google在我的计算机中未返回首次研究(感谢Google).
After Michał Ziober's answer I had to look for something regarding Include.NON_DEFAULT
and the default object and ran into this answer explaining Include.NON_EMPTY
that Google didn't return in my first research (thanks Google).
现在事情变得简单了,现在:
So things become easier, it's now:
public class AvailableSerializer extends JsonSerializer<Available<?>> {
@Override
public void serialize(Available<?> available, JsonGenerator jsonGenerator, SerializerProvider provider) throws IOException, JsonProcessingException {
jsonGenerator.writeObject(available.getValue());
}
@Override
public Class<Available<?>> handledType() {
@SuppressWarnings({ "unchecked", "rawtypes" })
Class<Available<?>> clazz = (Class) Available.class;
return clazz;
}
@Override
public boolean isEmpty(SerializerProvider provider, Available<?> value) {
return value == null || !value.isAvailable();
}
}
通过测试
@Test
public void testSerialize() throws Exception {
SimpleModule module = new SimpleModule().addSerializer(availableSerializer);
objectMapper.registerModule(module);
objectMapper.configOverride(Available.class).setInclude(
// the call comes from JavaDoc of objectMapper.setSerializationInclusion(...)
JsonInclude.Value.construct(JsonInclude.Include.NON_EMPTY, JsonInclude.Include.ALWAYS));
Tmp tmp = new Tmp();
assertThat(objectMapper.writeValueAsString(tmp)).isEqualTo("{\"myInt\":123}");
tmp.otherInt.setValue(123);
assertThat(objectMapper.writeValueAsString(tmp)).isEqualTo("{\"myInt\":123,\"otherInt\":123}");
}
因此,如果您赞成我的回答,请也赞成MichałZiober's,因为这也可以采用一种稍有不同的方法.
So please, if you upvote my answer please also upvote Michał Ziober's as that's also working with a mildly different approach.
这篇关于Jackson-Serialiser:序列化时忽略字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!