jackson的@JsonProperty来对字段记性序列化和反序列化重命名

有时候在对字段进行序列化或者反序列化的时候需要对字段进行重命名,这时候可以通过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":"张三"}

你可能感兴趣的:(java,jackson,java,后端)