Jackson - 自定义序列化程序,仅覆盖特定字段 [英] Jackson - custom serializer that overrides only specific fields

查看:1256
本文介绍了Jackson - 自定义序列化程序,仅覆盖特定字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何在Jackson中使用自定义序列化程序(通过扩展 JsonSerializer ),但我希望默认的序列化程序适用于所有字段,只有1个字段,我想使用自定义序列化器覆盖它。

I know how to use a custom serializer in Jackson (by extending JsonSerializer), but I want the default serializer to work for all fields, except for just 1 field, which I want to override using the custom serializer.

注释不是一个选项,因为我正在序列化生成的类(来自Thrift)。

Annotations are not an option, because I am serializing a generated class (from Thrift).

在编写自定义jackson序列化程序时,如何仅指定要覆盖的某些字段?

How do I specify only certain fields to be overridden when writing a custom jackson serializer?

更新:

这是我要序列化的课程:

Here's the class I want to serialize:

class Student {
    int age;
    String firstName;
    String lastName;
    double average;
    int numSubjects

    // .. more such properties ...
}

上面的类有很多特性,其中大多数都使用原生类型。我想覆盖自定义序列化程序中的一些属性,让Jackson像往常一样处理其余的属性。对于例如我只想将age字段转换为自定义输出。

The above class has many properies, most of which use native types. I want to just override a few properties in the custom serializer and let Jackson deal with the rest as usual. For e.g. I just want to convert the "age" field to a custom output.

推荐答案

我遇到了同样的问题,我用 CustomSerializerFactory

I faced the same issue, and I solved it with CustomSerializerFactory.

此方法允许您忽略所有对象或特定类型的某些特定字段。

This approach allows you to ignore some specific field for either for all objects, or for specific types.

public class EntityCustomSerializationFactory extends CustomSerializerFactory {

    //ignored fields
    private static final Set<String> IGNORED_FIELDS = new HashSet<String>(
            Arrays.asList(
                    "class",
                    "value",
                    "some"
            )
    );


    public EntityCustomSerializationFactory() {
        super();
    }

    public EntityCustomSerializationFactory(Config config) {
        super(config);
    }

    @Override
    protected void processViews(SerializationConfig config, BeanSerializerBuilder builder) {
        super.processViews(config, builder);

        //ignore fields only for concrete class
        //note, that you can avoid or change this check
        if (builder.getBeanDescription().getBeanClass().equals(Entity.class)){
            //get original writer
            List<BeanPropertyWriter> originalWriters = builder.getProperties();

            //create actual writers
            List<BeanPropertyWriter> writers = new ArrayList<BeanPropertyWriter>();

            for (BeanPropertyWriter writer: originalWriters){
                String propName = writer.getName();

                //if it isn't ignored field, add to actual writers list
                if (!IGNORED_FIELDS.contains(propName)){
                    writers.add(writer);
                }
            }

            builder.setProperties(writers);
        }

    }
}

之后您可以使用以下内容:

And afterwards you can use it something like the following:

objectMapper.setSerializerFactory(new EntityCustomSerializationFactory());
objectMapper.writeValueAsString(new Entity());//response will be without ignored fields

这篇关于Jackson - 自定义序列化程序,仅覆盖特定字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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