Total Pageviews

2018/01/12

[JSON] Jackson Annotations for JSON

Assume I have two Java beans:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
  package test.albert.jackson;
    
  import lombok.Builder;
  import lombok.Data;
    
  @Data
  @Builder
  public class Employee {
    
      private Integer id;
    
      private String name;
    
      private String email;
    
      private String phone;
        
      private Address address;
   
  }


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
  package test.albert.jackson;
    
  import lombok.Builder;
  import lombok.Data;
    
  @Data
  @Builder
  public class Address {
    
      private String streetAddress;
      private String city;
      private String zipCode;
    
  }
    


I am using ObjectMapper to convert object into JSON string, the example code is as bellows:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
  package test.albert.jackson;
    
  import com.fasterxml.jackson.core.JsonProcessingException;
  import com.fasterxml.jackson.databind.ObjectMapper;
    
  import lombok.extern.slf4j.Slf4j;
    
  @Slf4j
  public class JacksonTest {
    
      public static void main(String[] args) throws JsonProcessingException {
          Address address = Address.builder().zipCode("110").city("台北市").streetAddress("信義區信義路五段7號").build();
          Employee albert = Employee.builder().id(1).name("Albert").email("albert@gmail.com").phone("0900123456")
                  .address(address).build();
    
          ObjectMapper mapper = new ObjectMapper();
          String json = mapper.writeValueAsString(albert);
          log.debug(json);
      }
    
  }
    

The generated JSON looks like:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
{
 "id": 1,
 "name": "Albert",
 "email": "albert@gmail.com",
 "phone": "0900123456",
 "address": {
  "streetAddress": "信義區信義路五段7號",
  "city": "台北市",
  "zipCode": "110"
 }
}


If I would like to ignore id, just add @JsonIgnore to id



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
  package test.albert.jackson;
  
  import lombok.Builder;
  import lombok.Data;
  
  @Data
  @Builder
  public class Employee {
  
      @JsonIgnore
      private Integer id;
  
      private String name;
  
      private String email;
  
      private String phone;
  
      private Address address;
  
  }

If I would like to get rid of id and phone in Employee, add 
@JsonIgnoreProperties({"id", "phone"}) to Employee class:



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  package test.albert.jackson;
  
  import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
  
  import lombok.Builder;
  import lombok.Data;
  
  @Data
  @Builder
  @JsonIgnoreProperties({"id", "phone"})
  public class Employee {
  
      private Integer id;
  
      private String name;
  
      private String email;
  
      private String phone;
  
      private Address address;
  
  }


If I want to remove address from Employee class, just add @JsonIgnoreType to Address class.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
  package test.albert.jackson;
  
  import com.fasterxml.jackson.annotation.JsonIgnoreType;
  
  import lombok.Builder;
  import lombok.Data;
  
  @Data
  @Builder
  @JsonIgnoreType
  public class Address {
  
      private String streetAddress;
      private String city;
      private String zipCode;
  
  }

No comments: