jackson:根据字段类型添加后缀

pqwbnv8z  于 2021-07-06  发布在  Java
关注(0)|答案(2)|浏览(427)

这是我的 POJO :

public class AutorDenormalized {

    private String id;
    private Long unitatId;
    private String grupId;
    private String descripcio;

    public AutorDenormalized() {

    }

    // getters $ setters

}

我想序列化这类对象,根据字段类型添加后缀。我是说,
如果字段类型是字符串->则添加 *_s 后缀
如果字段类型是long->,则添加 *_l 后缀
否则继续
你有什么办法解决这个问题吗?

vfwfrxfs

vfwfrxfs1#

您需要实现自定义 BeanPropertyWriter 它可以生成带有后缀的属性名。注册自定义 BeanPropertyWriter 您需要创建自定义 BeanSerializerModifier .
下面的示例显示了简化的实现,它显示了如何实现上述结果:

import com.fasterxml.jackson.databind.BeanDescription;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationConfig;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
import com.fasterxml.jackson.databind.ser.BeanSerializerModifier;
import com.fasterxml.jackson.databind.util.NameTransformer;

import java.io.IOException;
import java.util.List;

public class JsonTypeInfoApp {

    public static void main(String[] args) throws IOException {
        SimpleModule typeSuffixModule = new SimpleModule();
        typeSuffixModule.setSerializerModifier(new TypeSuffixBeanSerializerModifier());

        ObjectMapper mapper = new ObjectMapper();
        mapper.enable(SerializationFeature.INDENT_OUTPUT);
        mapper.registerModule(typeSuffixModule);

        System.out.println(mapper.writeValueAsString(new AutorDenormalized()));
    }
}

class TypeSuffixBeanSerializerModifier extends BeanSerializerModifier {

    @Override
    public List<BeanPropertyWriter> changeProperties(SerializationConfig config, BeanDescription beanDesc, List<BeanPropertyWriter> beanProperties) {
        for (int i = 0; i < beanProperties.size(); ++i) {
            final BeanPropertyWriter writer = beanProperties.get(i);
            Class<?> rawType = writer.getType().getRawClass();
            if (supports(rawType)) {
                final String suffix = constructSuffix(rawType);

                beanProperties.set(i, writer.rename(NameTransformer.simpleTransformer(null, suffix)));
            }
        }
        return beanProperties;
    }

    private String constructSuffix(Class<?> rawType) {
        return "_" + Character.toLowerCase(rawType.getSimpleName().charAt(0));
    }

    private boolean supports(Class<?> rawClass) {
        return rawClass == String.class || rawClass == Long.class;
    }
}

以上代码打印:

{
  "id_s" : "1",
  "unitatId_l" : 123,
  "grupId_s" : "2",
  "descripcio_s" : "3"
}

另请参见:
自定义序列化和反序列化

nvbavucw

nvbavucw2#

除了公认的答案(这很好用)之外,您还可以考虑实现 PropertyNameStrategy :它允许您重命名属性并获取字段、setter/getter、creator参数(您需要找到属性的类型)。可能会少一点工作量。

相关问题