Preface
@JSONField
It is an annotation provided by Alibaba's open source JSON processing library FastJSON, which is used to provide more granular control over the behavior of fields when serializing and deserializing Java objects and JSON data. The following is about@JSONField
Detailed introduction to the notes:
1. Introduction of annotations
In use@JSONField
Before annotating, you need to introduce FastJSON dependencies into the project. If you are using the Maven project, you canAdd the following dependencies to:
<dependency> <groupId></groupId> <artifactId>fastjson</artifactId> <version>2.0.33</version> </dependency>
2. Common properties and usage
2.1 name attribute
Function: Specify the name of this field in JSON data. When the field name of a Java object does not match the field name in JSON data, this property can be used for mapping.
Example:
import ; public class User { @JSONField(name = "user_name") private String name; private int age; public User(String name, int age) { = name; = age; } // Getters and Setters public String getName() { return name; } public void setName(String name) { = name; } public int getAge() { return age; } public void setAge(int age) { = age; } }
import ; public class Main { public static void main(String[] args) { User user = new User("John", 25); String jsonStr = (user); (jsonStr); // Output: {"user_name":"John","age":25} } }
2.2 format attribute
Function: Used to specify the formatting method of the date type field. When serializing and deserializing, the date type is converted in the specified format.
Example:
import ; import ; public class Event { private String eventName; @JSONField(format = "yyyy-MM-dd HH:mm:ss") private Date eventTime; public Event(String eventName, Date eventTime) { = eventName; = eventTime; } // Getters and Setters public String getEventName() { return eventName; } public void setEventName(String eventName) { = eventName; } public Date getEventTime() { return eventTime; } public void setEventTime(Date eventTime) { = eventTime; } }
import ; import ; public class Main { public static void main(String[] args) { Event event = new Event("Conference", new Date()); String jsonStr = (event); (jsonStr); // Output: {"eventName":"Conference","eventTime":"2024-10-01 12:34:56"} (Date is based on actual situation) } }
2.3 serialize and deserialize properties
effect:serialize
Used to control whether this field is included in JSON data when serialized.deserialize
Used to control whether this field is read from JSON data when deserialized.
Example:
import ; public class Product { private String productName; @JSONField(serialize = false) private double costPrice; private double sellingPrice; public Product(String productName, double costPrice, double sellingPrice) { = productName; = costPrice; = sellingPrice; } // Getters and Setters public String getProductName() { return productName; } public void setProductName(String productName) { = productName; } public double getCostPrice() { return costPrice; } public void setCostPrice(double costPrice) { = costPrice; } public double getSellingPrice() { return sellingPrice; } public void setSellingPrice(double sellingPrice) { = sellingPrice; } }
import ; public class Main { public static void main(String[] args) { Product product = new Product("Laptop", 500, 800); String jsonStr = (product); (jsonStr); // Output: {"productName":"Laptop","sellingPrice":800} } }
2.4 Ordinal attributes
Function: Specify the order of fields in JSON data, the smaller the value, the higher the number.
Example:
import ; public class Book { @JSONField(ordinal = 2) private String title; @JSONField(ordinal = 1) private String author; public Book(String author, String title) { = author; = title; } // Getters and Setters public String getTitle() { return title; } public void setTitle(String title) { = title; } public String getAuthor() { return author; } public void setAuthor(String author) { = author; } }
import ; public class Main { public static void main(String[] args) { Book book = new Book(". Rowling", "Harry Potter"); String jsonStr = (book); (jsonStr); // Output: {"author":". Rowling","title":"Harry Potter"} } }
3. Use scenarios
3.1 Data Interaction
During the data interaction between front and backend, the front-end and backend may inconsistently naming the fields.@JSONField
ofname
Properties can be conveniently mapped to ensure the correct transmission of data.
3.2 Data security
For some sensitive information, such as user's password, product cost price, etc., it is not intended to be exposed to the outside during serialization, and can be usedserialize = false
Properties to exclude these fields.
3.3 Date formatting
When processing date type data, different systems may have different requirements for the format of the date.format
Attributes can unify the serialization and deserialization formats of dates.
4. Practical precautions
- Compatibility issues: FastJSON may have some compatibility issues between different versions. It is recommended to specify the FastJSON version used in the project and conduct sufficient testing when upgrading.
- Performance Impact: Although FastJSON's performance is usually better, frequent use of annotations may have a certain impact on performance when processing large amounts of data, and performance testing and optimization are required.
- Security issues: FastJSON has deserialization vulnerabilities in some versions. You need to pay attention to the version selection when using it and update to a secure version in a timely manner.
By reasonable use@JSONField
Annotation allows more flexible control of the serialization and deserialization process between Java objects and JSON data, improving development efficiency and data processing accuracy.
The above is the detailed explanation of the use of @JSONField annotation in Java. For more information about Java @JSONField annotation, please follow my other related articles!