当前位置:网站首页>@JsonProperty注解

@JsonProperty注解

2022-06-11 07:07:00 四问四不知

序言

@JsonProperty

当一个Java对象转换成Json字符串后,如果不是正确的实际名称有可能会出现异常。比如数据库中的坐标名称是x_axis,而定义Java对象是是xAxis,那么这时就需要使用到@JsonProperty注解,并且配合ObjectMapper.writeValueAsString方法使用去序列化对象成字符串。如下示例demo,

@JsonProperty(value = "", index = 1, access = JsonProperty.Access.xxx)

其中value为成员变量真实名称,index为序列化之后所展示的顺序,access为该对象的访问控制权限。

@Slf4j
public class JsonPropertyDemo {
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @Builder
    @ToString
    private static class Coordinate {
        @JsonProperty(value = "x_axis", index = 1, access = JsonProperty.Access.WRITE_ONLY)
        private String xAxis;
        @JsonProperty(value = "y_axis", index = 2, access = JsonProperty.Access.READ_WRITE)
        private String yAxis;
        @JsonProperty(value = "z_axis", index = 3, access = JsonProperty.Access.READ_WRITE)
        private String zAxis;
    }

    public static void main(String[] args) {
        Coordinate coordinate = Coordinate.builder()
                .xAxis("113.58")
                .yAxis("37.86")
                .zAxis("40.05")
                .build();
        String jsonStr = JSON.toJSONString(coordinate);
        log.info("serializes the specified object into its equivalent Json representation :" + jsonStr);
        ObjectMapper mapper = new ObjectMapper();
        try {
            String str = mapper.writeValueAsString(coordinate);
            log.info("serialize any Java value as a String : " + str);
            Object bean = mapper.readerFor(Coordinate.class).readValue(str);
            log.info("read or update instances of specified type : " + bean);
        } catch (JsonProcessingException e) {
            log.error("error message : " + e);
        }
    }
}

注解一般都是通过反射拿到对映的成员变量然后再进行增强,@JsonProperty把成员变量序列化成另外一个名称,并且它在序列化和反序列化的过程中都是使用的实际名称。

@JsonAlias

com.fasterxml.jackson.annotation中的@JsonProperty是可以在序列化和反序列化中使用,而@JsonAlias只在反序列化中起作用,指定Java属性可以接受的更多名称。文末链接也有JsonAlias的实例源码,下面就简单举一个例子,

@Slf4j
public class JsonAliasDemo {
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @Builder
    @ToString
    private static class Coordinate {
        @JsonAlias(value = "x_location")
        @JsonProperty(value = "x_axis")
        private String xAxis;
        @JsonProperty(value = "y_axis")
        @JsonAlias(value = "y_location")
        private String yAxis;
        @JsonProperty(value = "z_axis")
        @JsonAlias(value = "z_location")
        private String zAxis;
    }

    public static void main(String[] args) {
        String location = "{\"x_location\":\"113.58\",\"y_location\":\"37.86\",\"z_location\":\"40.05\"}";
        ObjectMapper mapper = new ObjectMapper();
        try {
            Object bean = mapper.readValue(location, Coordinate.class);
            log.info("read or update instances of specified type : " + bean);
        } catch (JsonProcessingException e) {
            log.error("error message : " + e);
        }
    }
}

@JsonAlias里的别名的json字符串,在反序列化时可以识别出来,不会反序列化失败,结果如下图,

 

参考链接:

1、JSON在线 | JSON解析格式化—SO JSON在线工具

2、JsonProperty (Jackson JSON Processor)

3、Java类com.fasterxml.jackson.annotation.JsonProperty的实例源码 - 编程字典

4、Java类com.fasterxml.jackson.annotation.JsonAlias的实例源码 - 编程字典

5、Jackson data binding - 知乎

原网站

版权声明
本文为[四问四不知]所创,转载请带上原文链接,感谢
https://blog.csdn.net/zkkzpp258/article/details/125120601