我需要能够阻止某些对象字段被序列化,主要是基于它们的类型。例如,请考虑以下对象:
class MyPojo {
private int myInt;
private boolean myBoolean;
// getters, setters, etc.
}
我希望能够在序列化时不将boolean字段序列化为false。如果它为零,则不序列化int。基本上,不基于其类型或特定值的任何属性来序列化任何特定字段。
我知道JsonSerializers,我过去常常解决我的问题,但它是impossible to choose not to serialize a field in a JsonSerializer。
我最接近的是实现我自己的PropertyFilter,并通过@JsonFilter
将其应用于我的对象:
public class XmlPropertyFilter implements PropertyFilter {
@Override
public void serializeAsField(Object pojo, JsonGenerator gen, SerializerProvider prov, PropertyWriter writer) throws Exception {
JavaType type = writer.getType();
if (writer instanceof BeanPropertyWriter) {
BeanPropertyWriter bWriter = (BeanPropertyWriter) writer;
String fieldName = bWriter.getSerializedName().getValue();
Field f = pojo.getClass().getDeclaredField(fieldName);
f.setAccessible(true);
Object value = f.get(pojo);
if (!type.isTypeOrSubTypeOf(int.class) && value != null) {
// Serialize everything that isn't an int and doesn't have a null value
prov.defaultSerializeField(fieldName, value, gen);
} else if (type.isTypeOrSubTypeOf(int.class)) {
// Only serialize ints if the value isn't 0
if (value != 0) prov.defaultSerializeField(fieldName, value, gen);
}
}
}
// ...
}
这正是我想要的,除了它具有打破包装的令人讨厌的副作用(例如序列化列表)。根据{{1}}文档,将过滤器应用于字段而不是整个类是有效的,这将是很棒的,但我已经尝试过,我似乎无法让它工作。< / p>
答案 0 :(得分:0)
我找到了解决方案,这正是我想要的。秘诀是方法BeanPropertyWriter#serializeAsOmittedField(Object, JsonGenerator, SerializerProvider)
。这完全是JsonSerializer内部无法做到的事情 - 它完全从输出中删除了字段。
以下是此DynamicPropertyFilter的示例:
public class DynamicPropertyFilter implements PropertyFilter {
public void serializeAsField(Object pojo, JsonGenerator jgen, SerializerProvider prov, PropertyWriter writer) throws Exception {
if (writer instanceof BeanPropertyWriter) {
BeanPropertyWriter bWriter = (BeanPropertyWriter) writer;
String fieldName = bWriter.getFullName().getSimpleName();
Field field = pojo.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
Object object = field.get(pojo);
if (Double.class.isInstance(object) && (double) object == 0.0) {
// Remove all double fields that are equal to 0.0
bWriter.serializeAsOmittedField(pojo, jgen, prov);
return;
} else if (Boolean.class.isInstance(object)) {
// Change all boolean fields to 1 and 0 instead of true and false
prov.defaultSerializeField(fieldName, (boolean) object ? 1 : 0, jgen);
return;
}
}
// Serialize field as normal if property is not filtered
writer.serializeAsField(pojo, jgen, prov);
}
public void serializeAsElement(Object elementValue, JsonGenerator jgen, SerializerProvider prov, PropertyWriter writer) throws Exception {
writer.serializeAsField(elementValue, jgen, prov);
}
public void depositSchemaProperty(PropertyWriter writer, JsonObjectFormatVisitor objectVisitor, SerializerProvider provider) throws JsonMappingException {
writer.depositSchemaProperty(objectVisitor, provider);
}
@Deprecated
public void depositSchemaProperty(PropertyWriter writer, ObjectNode propertiesNode, SerializerProvider provider) throws JsonMappingException {
writer.depositSchemaProperty(propertiesNode, provider);
}
}
我不仅可以过滤字段,这主要是我想要的,但我也可以更改它们(如布尔示例中所示)。这消除了对PropertyFilter和JsonSerializer的需要。