Home >Java >javaTutorial >How do annotations in the Jackson library control JSON serialization and deserialization?

How do annotations in the Jackson library control JSON serialization and deserialization?

王林
王林Original
2024-05-06 22:09:02948browse

Annotations in the Jackson library control JSON serialization and deserialization: Serialization: @JsonIgnore: Ignore the property @JsonProperty: Specify the name @JsonGetter: Use the get method @JsonSetter: Use the set method Deserialization: @JsonIgnoreProperties: Ignore the property @JsonProperty: Specify the name @JsonCreator: Use the constructor @JsonDeserialize: Custom logic

How do annotations in the Jackson library control JSON serialization and deserialization?

Annotations are used in the Jackson library to control JSON serialization and deserialization

Introduction

The Jackson library is a popular Java library for binding JSON data. Annotations play a vital role in the Jackson library, allowing us to control the JSON serialization and deserialization process.

Serialization annotations

  • @JsonIgnore: Ignore a property or method so that it is not included in the JSON output.
  • @JsonProperty: Specifies the name of the property in the JSON output.
  • @JsonGetter: Get the value returned when the method is called instead of accessing the property directly.
  • @JsonSetter: Set the value of the property when the setter method is called, rather than assigning the value directly.

Deserialization annotations

  • @JsonIgnoreProperties: Ignore the properties specified in the JSON input.
  • @JsonProperty: Specify the name of the property in the JSON input and map it to a property or method.
  • @JsonCreator: Create objects using non-default constructors instead of using setter methods.
  • @JsonDeserialize: Specify custom deserialization logic, you can use a custom type converter or handler.

Practical case

The following is an example class showing how to use Jackson annotations:

@JsonIgnoreProperties(ignoreUnknown = true)
public class Person {

    private String name;

    @JsonProperty("age")
    private int yearsOld;

    @JsonGetter("education")
    public String getEducation() {
        return "College";
    }

    @JsonSetter("years_old")
    public void setAge(int yearsOld) {
        this.yearsOld = yearsOld;
    }
}

In this example:

  • @JsonIgnoreProperties(ignoreUnknown = true) Ignore properties that do not exist in the class in the JSON input.
  • @JsonProperty("age") Specifies that the name of the yearsOld property in the JSON output is "age".
  • @JsonGetter("education") Returns the value of the "education" attribute through the get method "getEducation".
  • @JsonSetter("years_old") Set the value of the "yearsOld" attribute through the setting method "setAge".

You can use the Jackson library for serialization and deserialization in the following ways:

// 序列化
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(person);

// 反序列化
Person person = mapper.readValue(json, Person.class);

The above is the detailed content of How do annotations in the Jackson library control JSON serialization and deserialization?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn