当前位置:   article > 正文

jackson的@JsonProperty来对字段进行序列化和反序列化重命名_jackson readvalue反序列化修改字段名

jackson readvalue反序列化修改字段名

有时候在对字段进行序列化或者反序列化的时候需要对字段进行重命名,这时候可以通过jackson里面的@JsonProperty进行改名字见下代码

// 首先引入jack的pom包
		<dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.module</groupId>
            <artifactId>jackson-module-parameter-names</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-jdk8</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-jsr310</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-properties</artifactId>
        </dependency>

// 编写jack的工具类
public class JackUtils {
    public static ObjectMapper objectMapper = new ObjectMapper();

    static{
        //忽略空属性
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
        //设置转换模式
        objectMapper.enable(MapperFeature.USE_STD_BEAN_NAMING);
        objectMapper.configure(JsonGenerator.Feature.IGNORE_UNKNOWN, true);
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }

    public static <T> String obj2String(T obj) throws JsonProcessingException {
        if (obj == null) {
            return null;
        }
        return obj instanceof String ? (String) obj : objectMapper.writeValueAsString(obj);
    }
    
    public static <T> String obj2StringPretty(T obj) throws JsonProcessingException {
        if (obj == null) {
            return null;
        }
        return obj instanceof String ? (String) obj : objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
    }
    
    public static <T> T string2Obj(String str, Class<T> clazz) throws JsonProcessingException {
        if (str == null || str.length() == 0 || clazz == null) {
            return null;
        }
        return clazz.equals(String.class) ? (T) str : objectMapper.readValue(str, clazz);
    }
    
    public static <T> T string2Obj(String str, TypeReference<T> typeReference) throws JsonProcessingException {
        if (str == null || str.length() == 0 || typeReference == null) {
            return null;
        }
        return (T) (typeReference.getType().equals(String.class) ? str : objectMapper.readValue(str, typeReference));

    }
    
    public static <T> T string2Obj(String str, Class<?> collectionClazz, Class<?>... elementClazzes) throws JsonProcessingException {
        JavaType javaType = objectMapper.getTypeFactory().constructParametricType(collectionClazz, elementClazzes);
        return objectMapper.readValue(str, javaType);
    }

    public static <T> T obj2Obj(Object obj, TypeReference<T> typeReference) {
        if (obj == null || typeReference == null) {
            return null;
        }
        return objectMapper.convertValue(obj, typeReference);
    }
}

// 开始测试
@Data
public class Order implements Serializable {
    private String id;
    private String name;
    @JsonProperty(value = "package")
    private String packages;
    private List<String> list;

    @Override
    public String toString(){
        return JSON.toJSONString(this);
    }

    public static void main(String[] args) throws Exception {
        Order order = new Order();
        order.setPackages("666666");
        order.setId("123");
        order.setName("张三");
        String str = JackUtils.obj2String(order);
        System.out.println(str);

        Order order1 = JackUtils.string2Obj(str, Order.class);
        System.out.println(order1);

        String str2 = "{\"id\":\"123\",\"name\":\"张三\",\"packages\":\"666666\"}";
        Order order2 = JackUtils.string2Obj(str2, Order.class);
        System.out.println(order2);
    }
}
结果:使用了@JsonProperty(value = "package")里面指定的名字序列化后就会将属性名变成\
指定的序列化名反序列化会根据注解里面的名字进行给值
{"id":"123","name":"张三","package":"666666"}
{"id":"123","name":"张三","packages":"666666"}
{"id":"123","name":"张三"}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小舞很执着/article/detail/982450
推荐阅读
相关标签
  

闽ICP备14008679号