Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
468 views
in Technique[技术] by (71.8m points)

java - Jackson custom annotation for custom NULL value serialization

According to this answer: https://stackoverflow.com/a/43342675/5810648

I wrote such serializer:

public class CustomSerializer extends StdSerializer<Double> implements ContextualSerializer {

    private final NAifNull annotation;

    public CustomSerializer() {
        super(Double.class);
        this.annotation = null;
    }

    public CustomSerializer(NAifNull annotation) {
        super(Double.class);
        this.annotation = annotation;
    }

    @Override
    public void serialize(Double value, JsonGenerator gen, SerializerProvider provider) throws IOException {
        if (annotation != null && value == null) {
            gen.writeString("N/A");
        } else {
            gen.writeNumber(value);
        }
    }

    @Override
    public JsonSerializer<?> createContextual(SerializerProvider prov, BeanProperty property) {
        NAifNull annotation = property.getAnnotation(NAifNull.class);
        return new CustomSerializer(annotation);
    }
}

Witch supposed to write string "N/A" if the annotation is present and field is null. But method serialize is called only for not null fields.

Also, I have tried to call setNullValueSerializer:

@Override
public JsonSerializer<?> createContextual(SerializerProvider prov, BeanProperty property) {
    NAifNull annotation = property.getAnnotation(NAifNull.class);
    prov.setNullValueSerializer(new CustomNullSerializer(annotation));
    return new CustomSerializer(annotation);
}

With such implementation:

private static class CustomNullSerializer extends JsonSerializer<Object> {
    private final NAifNull annotation;

    public CustomNullSerializer(NAifNull annotation) {
        this.annotation = annotation;
    }

    @Override
    public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        if (annotation != null) {
            gen.writeString("N/A");
        } else {
            gen.writeNull();
        }
    }
}

But no result.

How to handle null fields in such way?

Update

According to discussion: https://github.com/FasterXML/jackson-databind/issues/2057

prov.setNullValueSerializer(new CustomNullSerializer(annotation));

Is not supposed to be called from CreateContextual method.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use a BeanSerializerModifier to customize the null serializer for a particular property:

public class CustomBeanSerializerModifier extends BeanSerializerModifier {

    @Override
    public List<BeanPropertyWriter> changeProperties(SerializationConfig config, 
           BeanDescription beanDesc, List<BeanPropertyWriter> beanProperties) {

        for (BeanPropertyWriter beanProperty : beanProperties) {
            if (beanProperty.getAnnotation(NAifNull.class) != null) {
                beanProperty.assignNullSerializer(new CustomNullSerializer());
            }
        }

        return beanProperties;
    }
}

Where @NAifNull and CustomNullSerializer are define as follows:

public class CustomNullSerializer extends JsonSerializer<Object> {

    @Override
    public void serialize(Object value, JsonGenerator jgen, 
           SerializerProvider provider) throws IOException {
        jgen.writeString("N/A");
    }
}
@Target({ ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
@interface NAifNull {

}

Then use it as follows:

ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new SimpleModule() {

    @Override
    public void setupModule(SetupContext context) {
        super.setupModule(context);
        context.addBeanSerializerModifier(new CustomBeanSerializerModifier());
    }
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...