当前位置:网站首页>@Jsonproperty annotation
@Jsonproperty annotation
2022-06-11 07:13:00 【Four questions and four unknowns】
preface
@JsonProperty
When one Java Object conversion to Json After the string , Exceptions may occur if the actual name is not correct . For example, the coordinate name in the database is x_axis, And definition Java The target is xAxis, Then you need to use @JsonProperty annotation , And cooperate with ObjectMapper.writeValueAsString Method to deserialize an object into a string . The following example demo,
@JsonProperty(value = "", index = 1, access = JsonProperty.Access.xxx)
among value True name for member variable ,index For the sequence shown after serialization ,access Access control permissions for this object .
@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);
}
}
}
Annotations are usually reflected to get mapped member variables and then enhanced ,@JsonProperty Serialize member variables into another name , And it is the actual name used in both serialization and deserialization .
@JsonAlias
com.fasterxml.jackson.annotation Medium @JsonProperty Can be used in serialization and deserialization , and @JsonAlias Only works in deserialization , Appoint Java More names that properties can accept . There are also links at the end of the article JsonAlias Instance source code , Here is a simple example ,
@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 Alias in json character string , It can be recognized during deserialization , No deserialization failure , The results are as follows ,

Reference link :
1、JSON On-line | JSON Parsing formatting —SO JSON Online tools
2、JsonProperty (Jackson JSON Processor)
4、Java class com.fasterxml.jackson.annotation.JsonAlias Instance source code - Programming dictionary
边栏推荐
- 通过 Ingress 进行灰度发布
- 資深OpenStacker - 彭博、Vexxhost昇級為OpenInfra基金會黃金成員
- 二、用户登录和注册
- Prototype and prototype chain
- Listen to the left width of the browser to calculate the distance
- Atomicinteger atomic operation class
- Bat (batch processing) processing special symbols (exclamation point, percent sign), etc
- MS office level II wrong question record [7]
- Interview question 02.06 Palindrome linked list
- The difference between TCP and UDP
猜你喜欢

Mobile console Gobang (first draft of detailed design)

教育专家王中泽老师:家庭教育重在自己成长

No response from win10 explorer when dragging files

WPF data binding (IV)

big.js--使用/实例

Luogu p1091 chorus formation (longest ascending subsequence)

Mistakes in Niuke JS exercise

Deep Attentive Tracking via Reciprocative Learning

Leetcode-141. Linked List Cycle

Object. Specific implementation and difference between create() and new
随机推荐
【CF #277.5 (Div. 2)】B. BerSU Ball
Promises/a+ standard Chinese Translation
Typora set markdown syntax inline mode
Atomicinteger atomic operation class
Calculate the day of the week for a specific month, year and day
Atom, the top stream editor, will leave the historical stage on December 15
Shuttle container component
213. house raiding II
**Count the characters with the largest number of words**
Janus feature草稿
Latex various arrows / arrows with text labels / variable length arrows
[并发进阶]——线程池总结
Dynamically change the direction of this
Smart pointer (simple version)
资深OpenStacker - 彭博、Vexxhost升级为OpenInfra基金会黄金成员
Notes on learning Es5 and ES6
MS office level II wrong question record [6]
Henan college entrance examination vs Tianjin college entrance examination (2008-2021)
[probability theory and mathematical statistics] Dr. monkey's notes p41-44 statistics related topics, determination of three distributions, properties, statistics subject to normal distribution in gen
This comprehensive understanding