SoFunction
Updated on 2025-03-04

Instructions for using JsonProperty and JSONField annotations

Use of JsonProperty and JSONField annotations

1.1. Description

@JsonPropertyAnnotation is annotation in the Jackson library and is widely used for serialization and deserialization between Java objects and JSON data.

Jackson is a popular Java library that enables converting Java objects to JSON format and vice versa.

1.2. Main functions

  • Specify the attribute name: @JsonPropertyCan be used to define the property name used in JSON, even if it is different from the property name in a Java class. This is useful when you need to match a specific JSON format.
  • Handle field visibility: You can control whether the field should be serialized or deserialized.
  • Set default values: Can provide default values ​​for fields.
  • Necessity for marking fields: Can indicate that a field is required during deserialization

1.3. Common properties

  • value: Specifies the name of the JSON field.
  • required: Boolean, indicating whether the field is required during deserialization (default is false).
  • defaultValue: Specifies a default value to use when the field is not provided in the JSON data.
import ;

public class Product {

    @JsonProperty(value = "product_id", required = true)
    private int id;

    @JsonProperty(value = "product_name", defaultValue = "Unnamed Product")
    private String name;

    // Getters and Setters...
}

1.4. Example

import ;

public class User {
    
    @JsonProperty("user_id")
    private int id;

    @JsonProperty("user_name")
    private String name;

    @JsonProperty("user_email")
    private String email;

    // Getters and Setters
    public int getId() {
        return id;
    }

    public void setId(int id) {
         = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
         = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
         = email;
    }
}

Given the aboveUserClass, the following is its corresponding JSON representation:

{
    "user_id": 1,
    "user_name": "John Doe",
    "user_email": "@"
}

2.1 Description

@JSONFieldIt is an annotation from Alibaba's Fastjson library, which is mainly used to handle serialization and deserialization between Java objects and JSON data.

Fastjson is a high-performance JSON processing tool written in the Java language. Fastjson2 is generally used.

2.2 Main functions

  • Specify the field name: @JSONFieldCan be used to define the property name used in JSON, even if it does not match the property name in the Java class. This is useful for scenarios where you need to match a specific JSON format.
  • Control serialization/deserialization: It can control whether a field participates in serialization and deserialization. For example, you can set itserializeordeserializeThe attribute isfalseto exclude this field.
  • Format date: You can specify the format of the date field in JSON, which is particularly important for date type fields.
  • Define default values: During deserialization, you can specify a default value for use when the field is missing in JSON data.

2.3 Common properties

  • name: Specifies the name of the JSON field.
  • serialize: Boolean value indicating whether the field participates in serialization (default is true).
  • deserialize: Boolean, indicating whether the field participates in deserialization (default is true).
  • format: Used to format the time field, e.g."yyyy-MM-dd"
import ;

public class Product {

    @JSONField(name = "product_id", serialize = true, deserialize = true)
    private int id;

    @JSONField(name = "product_name", defaultValue = "Unnamed Product")
    private String name;

    @JSONField(name = "created_at", format = "yyyy-MM-dd HH:mm:ss")
    private Date createdAt;

    // Getters and Setters...
}

2.4 Example

import ;

public class User {
    
    @JSONField(name = "user_id")
    private int id;

    @JSONField(name = "user_name")
    private String name;

    @JSONField(name = "user_email")
    private String email;

    // Getters and Setters
    public int getId() {
        return id;
    }

    public void setId(int id) {
         = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
         = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
         = email;
    }
}

Use the aboveUserClass, the following is its corresponding JSON representation:

{
    "user_id": 1,
    "user_name": "John Doe",
    "user_email": "@"
}

3. Usage scenarios

Avoid using lombok's data annotation, which causes inconsistency between the key of the json content received by the front-end and the bean's id in the back-end. You can use the above two annotations, and of course you can also write the get set method by hand to avoid this problem.

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.