Jackson (1): common annotations

1. Description

Noun declaration: serialization refers to converting entity objects to json strings; deserialization refers to converting json strings to entity objects .

1.@JsonInclude

Purpose : When the entity is serialized into a json string, it indicates the eligible serialized fields in the entity.
Position : On the class name, it takes effect on all attributes of the class; on the attribute, it takes effect only on the attribute.
Usage : @JsonInclude(Include.value).
(Only a few commonly used enumeration values ​​are described. For other enumeration value descriptions, please refer to the comments in the source code)
Value :
NON_NULL, only serialize properties with non-null values.
NON_EMPTY, only serialize properties with non-empty values ​​(including null).
NON_DEFAULT, only serialize properties with non-default initial values.

2.@JsonIgnoreProperties

Purpose : If the json field is converted to an entity class, that is, when it is deserialized, if there is a field that cannot correspond to the entity class, a json parsing exception will be reported. Using this annotation, you can ignore the content in the json string that does not correspond to the property name of the bean object.
Location : Class on.
Usage :
①@JsonIgnoreProperties({"property name", "property name"}): Ignore the specified property when deserializing
②@JsonIgnoreProperties(ignoreUnknown = true): Ignore unknown properties when deserializing

3.@JsonProperty

Purpose : When deserializing, convert the fields in the json string to the specified attributes; when serializing, convert the attributes of the object into the specified field names.
Location : on the property.
Usage : @JsonProperty("Define fields in json")

Two, example

1. Import library

	<dependency>
		<groupId>com.fasterxml.jackson.core</groupId>
		<artifactId>jackson-core</artifactId>
		<version>${jackson.version}</version>
	</dependency>
	<dependency>
		<groupId>com.fasterxml.jackson.core</groupId>
		<artifactId>jackson-databind</artifactId>
		<version>${jackson.version}</version>
	</dependency>
	<dependency>
		<groupId>com.fasterxml.jackson.core</groupId>
		<artifactId>jackson-annotations</artifactId>
		<version>${jackson.version}</version>
	</dependency>

2. Operation

SkyLinkSerializer is a jackson tool class encapsulated by myself

@JsonInclude
entity class

@Getter
@Setter
@ToString
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class Person {
    
    
    private int id;
    private String name;
}

Test Results

 @Test
  public void test07() throws SkyLinkException {
    
    
    SkyLinkSerializer se = new SkyLinkSerializer();
    Person p1 = new Person();
    p1.setId(12);
    System.out.println(se.encode(p1));
}

{
    
    "id":12}

@JsonIgnoreProperties
entity class

@Getter
@Setter
@ToString
@JsonIgnoreProperties(ignoreUnknown = true)
public class Person {
    
    
    private int id;
    private String name;
}

Test Results

  @Test
  public void test07() throws SkyLinkException {
    
    
    SkyLinkSerializer se = new SkyLinkSerializer();
    String p2 = "{" +
                 "\"id\":12," +
                 "\"ne\":\"www\"," +
                 "\"other\":\"cccc\"" +
                "}";
    System.out.println(se.decode(p2, Person.class));
  }
}

Person(id=12, name=null)

@JsonProperty
@JsonIgnoreProperties
entity class

@Getter
@Setter
@ToString
@JsonIgnoreProperties(ignoreUnknown = true)
public class Person {
    
    
    private int id;
    @JsonProperty("ne")
    private String name;
}

Test Results

  @Test
  public void test07() throws SkyLinkException {
    
    
    SkyLinkSerializer se = new SkyLinkSerializer();
    String p2 = "{" +
                 "\"id\":12," +
                 "\"ne\":\"www\"," +
                 "\"other\":\"cccc\"" +
                "}";
    System.out.println(se.decode(p2, Person.class));
  }
}

Person(id=12, name=www)

Guess you like

Origin blog.csdn.net/weixin_42717117/article/details/121396981