How to not include composed object into json?
我有以下课程:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
我将
现在我希望如果
正如我所说,我应该编写cistom序列化程序,但我不明白哪个实体。
附:
我的问题不是使用Jackson忽略JSON对象上的新字段
本主题解释了如何忽略Container内的空值。 我的问题是如何忽略Container里面只有空值的对象
受StaxMan答案启发的另一个想法是实现一个"空元素模块",它将
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | public class EmptyElementModule extends SimpleModule { private static final String NAME ="EmptyElementModule"; public EmptyElementModule() { super(NAME); setSerializerModifier(new EmptyElementSerializerModifier()); } public static class EmptyElementSerializerModifier extends BeanSerializerModifier { @Override public JsonSerializer< ? > modifySerializer(SerializationConfig config, BeanDescription beanDesc, JsonSerializer< ? > serializer) { Class< ? > beanClass = beanDesc.getBeanClass(); if (beanClass == Element1.class) { return new EmptyElement1Serializer((JsonSerializer<Object>) serializer); } else if (beanClass == Element2.class) { // return element 2 serializer } return serializer; } } public static class EmptyElement1Serializer extends JsonSerializer<Element1> { private final JsonSerializer<Object> defaultSerializer; public EmptyElement1Serializer(JsonSerializer<Object> defaultSerializer) { this.defaultSerializer = checkNotNull(defaultSerializer); } @Override public void serialize(Element1 value, JsonGenerator gen, SerializerProvider serializers) throws IOException { defaultSerializer.serialize(value, gen, serializers); } @Override public boolean isEmpty(SerializerProvider provider, Element1 value) { return value.f11 == null && value.f12 == null; } } } |
虽然您可能确实需要自定义序列化程序,但是需要覆盖的方法是
1 | @JsonInclude(JsonInclude.Include.NON_EMPTY) |
这将让"拥有"对象确定要避免序列化:否则将调用自定义序列化程序并且它必须写一个值 - 当然你可以编写
要忽略容器中只有空/空成员(容器的成员,成员)的元素,您可以为
创建
Jackson
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | public class ContainerModule extends SimpleModule { private static final String NAME ="ContainerModule"; public ContainerModule() { super(NAME); addSerializer(Container.class, new ContainerSerializer()); } public static class ContainerSerializer extends JsonSerializer<Container> { @Override public void serialize(Container container, JsonGenerator jg, SerializerProvider serializers) throws IOException { if (container != null) { // start object and write description jg.writeStartObject(); jg.writeStringField("description", container.description); // only write Element1 if one of its members exist Element1 element1 = container.element1; if (element1 != null) { if (!Strings.isNullOrEmpty(element1.f11) && !Strings.isNullOrEmpty(element1.f12)) { jg.writeFieldName("element1"); jg.writeObject(element1); } } // only write Element2 if one of its members exist Element2 element2 = container.element2; if (element2 != null) { if (!Strings.isNullOrEmpty(element2.f21) && !Strings.isNullOrEmpty(element2.f22)) { jg.writeFieldName("element2"); jg.writeObject(element2); } } // close the Container object jg.writeEndObject(); } } } } |
注册自定义序列化程序
可以使用
1 2 | ObjectMapper om = new ObjectMapper() .registerModule(new ContainerModule()); |
班级注册
1 2 3 4 5 6 |
这应该产生以下结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | // Full container { "description" :"an awesome description", "element1" : { "f11" :"f11 value", "f12" :"f12 value" }, "element2" : { "f21" :"f21 value", "f22" :"f22 value" } } // Null Element1 properties { "description" :"an awesome description", "element2" : { "f21" :"f21 value", "f22" :"f22 value" } } // Null Element2 properties { "description" :"an awesome description", "element1" : { "f11" :"f11 value", "f12" :"f12 value" } } // Null Element1 and 2 properties { "description" :"an awesome description" } |
如果你使用注释,这应该工作正常:
1 2 3 4 5 6 7 |
所以杰克逊忽略了@JsonIgnore注释的这两个