当前位置:网站首页>@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的实例源码 - 编程字典
边栏推荐
- Explain the difference between void 0 and undefined
- Object. Specific implementation and difference between create() and new
- This comprehensive understanding
- Flutter 内外边距
- 微信小程序开发(原生和uniapp)DOM标签对比介绍
- Biological sequence intelligent analysis platform blog (1)
- **Count the characters with the largest number of words**
- 【迅为干货】龙芯2k1000开发板opencv 测试
- Atomicinteger atomic operation class
- Education expert wangzhongze shared his experience for many years: family education is not a vassal
猜你喜欢

The difference between arrow function and ordinary function

Henan college entrance examination vs Tianjin college entrance examination (2008-2021)

教育专家王中泽老师一招解决学生问题

WPF data binding (IV)

A highly controversial issue

First day of database

资深OpenStacker - 彭博、Vexxhost升级为OpenInfra基金会黄金成员

Biological sequence intelligent analysis platform blog (1)

Flutter Container组件
![[deploy private warehouse based on harbor] 4 push image to harbor](/img/af/8e28b229d94f3e6eab02308b69dc74.jpg)
[deploy private warehouse based on harbor] 4 push image to harbor
随机推荐
554. brick wall
Server parameter adjustment record
Cross-Modal Pattern-Propagation for RGB-T Tracking
Library management system 2- demand analysis
Method to determine whether it is an array
Analysis of key points and difficulties of ES6 promise source code
Experience record of rural housing integration script
Education expert wangzhongze solves students' problems with one move
Leetcode hot topic 100 topic 21-25 solution
Promises/a+ standard Chinese Translation
通过 Ingress 进行灰度发布
Shell脚本之启动Nacos服务端
Leetcode-104. Maximum Depth of Binary Tree
Saltstack deployment LNMP
Listen to the left width of the browser to calculate the distance
Library management system 1- project approval
Prototype and prototype chain
Completed in May, 22
The difference between TCP and UDP
Esp32 learning notes (49) - esp-wifi-mesh interface use