如何在不重写serialize方法的情况下覆盖特定类的JsonSerializer的isEmpty方法? [英] How to override isEmpty method of JsonSerializer for specific class without overriding serialize method?

查看:195
本文介绍了如何在不重写serialize方法的情况下覆盖特定类的JsonSerializer的isEmpty方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为 isEmpty 方法添加自定义行为。

I want to add custom behaviour for isEmpty method.

当我从 JsonSerializer< MySpecificClass> <延伸时

我应该覆盖serialize方法,因为它是抽象的。

I should override serialize method because it abstract.

我想要离开序列化方法as is and override only isEmpty method。

I want leave serialize method as is and override only isEmpty method.

推荐答案

为了修改 isEmpty 行为但保持默认序列化可以利用序列化修饰符。您仍然必须实现自定义序列化程序,但您可以非常干净地利用默认序列化。

In order to modify the isEmpty behavior but maintain default serialization you can take advantage of a serializer modifier. You still have to implement a custom serializer, but you can utilize default serialization pretty cleanly.

defaultSerializer 变量注入自定义序列化程序类。我们将在实现修饰符时看到此序列化程序的来源。在这个类中,您将覆盖 isEmpty 方法来完成您的需要。下面,如果 MySpecificClass 有一个空 id ,杰克逊认为它是空的。

Inject a defaultSerializer variable in to your custom serializer class. You will see where this serializer comes from when we implement the modifier. In this class you will override the isEmpty method to accomplish what you need. Below, if MySpecificClass has a null id it is considered empty by Jackson.

public class MySpecificClassSerializer extends JsonSerializer<MySpecificClass> {
    private final JsonSerializer<Object> defaultSerializer;

    public MySpecificClassSerializer(JsonSerializer<Object> defaultSerializer) {
        this.defaultSerializer = checkNotNull(defaultSerializer);
    }

    @Override
    public void serialize(MySpecificClass value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        defaultSerializer.serialize(value, gen, serializers);
    }

    @Override
    public boolean isEmpty(SerializerProvider provider, MySpecificClass value) {
        return value.id == null;
    }
}



创建自定义 BeanSerializerModifier



扩展 BeanSerializerModifier 并覆盖 modifySerializer 方法。在此方法中,您可以对要操作的类类型进行过滤,并相应地返回自定义序列化程序。

Create a custom BeanSerializerModifier

Extend BeanSerializerModifier and override the modifySerializer method. Inside of this method you can filter on the class type that you want to operate on, and return your custom serializer accordingly.

public class MyClassSerializerModifier extends BeanSerializerModifier {
    @Override
    public JsonSerializer<?> modifySerializer(SerializationConfig config, BeanDescription beanDesc, JsonSerializer<?> serializer) {
        if (beanDesc.getBeanClass() == MySpecificClass.class) {
            return new MySpecificClassSerializer((JsonSerializer<Object>) serializer);
        }
        return serializer;
    }
}



使用注册修饰符ObjectMapper



注册修饰符将允许序列化程序在 modifySerializer 中的条件下随时触发已经满足。

Register the modifier with the ObjectMapper

Registering the modifier will allow your serializer to trigger anytime the condition in modifySerializer is met.

ObjectMapper om = new ObjectMapper()
        .registerModule(new SimpleModule()
                .setSerializerModifier(new MyClassSerializerModifier()));

这篇关于如何在不重写serialize方法的情况下覆盖特定类的JsonSerializer的isEmpty方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆