杰克逊:如何在不修改POJO的情况下将现有属性编辑为JSON? [英] Jackson: How to edit existing property to the JSON without modifying the POJO?

查看:120
本文介绍了杰克逊:如何在不修改POJO的情况下将现有属性编辑为JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在POJO中编辑"existing field"的名称,而不是添加"extra_field".可以使用下面的方法引用的链接吗?

I need to edit the name of "existing field" in POJO instead of adding "extra_field". Is it possible with the approach referenced link below?

请注意,我不想使用 @JsonProperty 注释.

Please note I do not want to use @JsonProperty annotation.

要求是,我有一个POJO,并且希望每次都使用不同的字段名而不更改POJO.例如,我在POJO中有一个字段 c_id ,有时它需要写为 cust_id ,而有时它是 my_id .

Requirement is, I have a POJO and want to use different field name every time without change in POJO. For example I have a field c_id in POJO and some times it need to write as cust_id and another time it would be my_id.

还请注意,我无法更改POJO的实现,因为它已在多个模块中使用并且具有通用实现.

Also note I cannot change implementation of POJO as it is already used in several modules and have generic implementation.

POJO示例:

class MyPojo {
    String id; 
    // getter and setters
}

预期的输出可以是以下内容:(字段名称可以更改)

Expected output can be the following: (name of field can be changed)

  1. {"cust_id":"123"}
  2. {"my_id":"123"}

推荐答案

杂类

在不向原始POJO添加注释的情况下修改Jackson的输出的最简单方法是使用mixins.

The easiest way to modify the output of Jackson without adding annotations to the original POJO is using mixins.

只需定义带有必要注释的mixin类,并向Jackson指示您要在序列化原始对象时使用mixin.

Just define a mixin-class with the necessary annotations and indicate to Jackson that you want to use the mixin when serializing the original object.

private static class MyPojoMixin {
    @JsonProperty("cust_id")
    private String id;
}

public String serializeWithMixin(MyPojo p) throws JsonProcessingException {
    ObjectMapper mapper = new ObjectMapper();
    mapper.addMixIn(MyPojo.class, MyPojoMixin.class);

    return mapper.writeValueAsString(p);
}

自定义媒体资源命名策略

如果需要以编程方式更改字段名称,则可能无法使用mixin解决方案.然后,您可以使用自定义的PropertyNamingStrategy:

If you need to programmatically change the field-name, you might not be able to use the mixin solution. You could then use a custom PropertyNamingStrategy:

public class IdRenamingStrategy extends PropertyNamingStrategy {
    private final PropertyNamingStrategy inner;
    private final String newIdPropertyName;

    public IdRenamingStrategy(String newIdPropertyName) {
        this(PropertyNamingStrategy.LOWER_CAMEL_CASE, newIdPropertyName);
    }

    public IdRenamingStrategy(PropertyNamingStrategy inner, String newIdPropertyName) {
        this.inner = inner;
        this.newIdPropertyName = newIdPropertyName;
    }

    private String translate(String propertyName) {
        if ("id".equals(propertyName)) {
            return newIdPropertyName;
        } else {
            return propertyName;
        }
    }

    @Override
    public String nameForField(MapperConfig<?> config, AnnotatedField field, String defaultName) {
        return inner.nameForField(config, field, translate(defaultName));
    }

    @Override
    public String nameForGetterMethod(MapperConfig<?> config, AnnotatedMethod method, String defaultName) {
        return inner.nameForGetterMethod(config, method, translate(defaultName));
    }

    @Override
    public String nameForSetterMethod(MapperConfig<?> config, AnnotatedMethod method, String defaultName) {
        return inner.nameForSetterMethod(config, method, translate(defaultName));
    }

    @Override
    public String nameForConstructorParameter(MapperConfig<?> config, AnnotatedParameter ctorParam, String defaultName) {
        return inner.nameForConstructorParameter(config, ctorParam, translate(defaultName));
    }
}

可以这样使用:

public String serializeWithPropertyNamingStrategy(MyPojo p) throws JsonProcessingException {
    ObjectMapper mapper = new ObjectMapper();
    mapper.setPropertyNamingStrategy(new IdRenamingStrategy("cust_id"));

    return mapper.writeValueAsString(p));
}

这篇关于杰克逊:如何在不修改POJO的情况下将现有属性编辑为JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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