下面列出了怎么用com.fasterxml.jackson.databind.ser.std.NullSerializer的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public List<BeanPropertyWriter> changeProperties(SerializationConfig config, BeanDescription beanDesc, List<BeanPropertyWriter> beanProperties) {
FilterableType filterableType = _config.getFilterableType(beanDesc.getBeanClass());
if (filterableType == null)
return beanProperties;
for (final BeanPropertyWriter beanPropertyWriter : beanProperties) {
final FilterableProperty property = filterableType.getProperty(beanPropertyWriter.getName());
if (property == null || property.getSerializationFilter() == null)
continue;
beanPropertyWriter.assignSerializer(new StdSerializer<Object>(Object.class) {
private static final long serialVersionUID = 1L;
@Override
public void serialize(Object value, JsonGenerator gen, final SerializerProvider provider) throws IOException {
JsonSerializer<Object> serializer = ConcurrentHashMapValues.getOrAdd(_customSerializers, beanPropertyWriter.getType(),
new Func<JsonSerializer<Object>>() {
@Override
public JsonSerializer<Object> execute() {
try {
return BeanSerializerFactory.instance.createSerializer(provider, beanPropertyWriter.getType());
} catch (Throwable ex) {
_logger.error("Error occurred when creating custom serializer for type: " + beanPropertyWriter.getType(), ex);
return NullSerializer.instance;
}
}
});
value = property.getSerializationFilter().filter(property, value);
if (value == null) {
gen.writeNull();
return;
}
serializer.serialize(value, gen, provider);
}
});
_logger.info("Will apply filter {} for property: {}.", property.getSerializationFilter(), property.name());
}
return beanProperties;
}
protected void serializeFieldTyped(
Entity entity,
JsonGenerator gen,
SerializerProvider serializers,
BeanDescription beanDescription,
BeanPropertyDefinition beanPropertyDefinition,
TypeSerializer typeSerializerBase) {
TypeSerializer typeSerializer = typeSerializerBase;
try {
if (typeSerializer == null) {
typeSerializer = serializers.findTypeSerializer(serializers.constructType(beanPropertyDefinition.getAccessor().getRawType()));
}
if (typeSerializer == null) {
// if not static type if available use dynamic type if available
Object propertyValue = beanPropertyDefinition.getAccessor().getValue(entity);
if (propertyValue != null) {
typeSerializer = serializers.findTypeSerializer(serializers.constructType(propertyValue.getClass()));
}
}
JsonInclude.Value inclusion = beanPropertyDefinition.findInclusion();
JsonInclude.Value defaultInclusion = serializers.getConfig().getDefaultPropertyInclusion();
JsonInclude.Value usedInclusion = defaultInclusion.withOverrides(inclusion);
BeanPropertyWriter bpw = new BeanPropertyWriter(
beanPropertyDefinition,
beanPropertyDefinition.getAccessor(),
beanDescription.getClassAnnotations(),
beanPropertyDefinition.getAccessor().getType(),
null, // will be searched automatically
typeSerializer, // will not be searched automatically
beanPropertyDefinition.getAccessor().getType(),
suppressNulls(usedInclusion),
suppressableValue(serializers.getConfig().getDefaultPropertyInclusion()),
null);
if (!bpw.willSuppressNulls()) {
bpw.assignNullSerializer(NullSerializer.instance);
}
bpw.serializeAsField(entity, gen, serializers);
} catch (Exception ex) {
LOGGER.error("Failed to serialise entity", ex);
}
}