Friday, 12 August 2016

[JSON] How to skip serialize/deserialize variables using jackson annotation

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
public class JacksonTest {
 
 private String name = null;
 private int age = 0;
 private String[] emails = null;
 private java.util.List< String> favorites = null;
 private String mobile = null;
 private java.util.concurrent.BlockingQueue< String> concurrentQueue = null;
 
 public JacksonTest() {
 }
 
 public JacksonTest( String name, int age, String[] emails, java.util.List< String> favorites, String mobile) {
  this.name = name;
  this.age = age;
  this.emails = emails;
  this.favorites = favorites;
  this.mobile = mobile;
  
 }
 
 public String getName() {
  return name;
 }
 
 public int getAge() {
  return age;
 }
 
 public String[] getEmails() {
  return this.emails;
 }

 public void setEmails(String[] emails) {
  this.emails = emails;
 }
 

 public java.util.List<String> getFavorites() {
  return this.favorites;
 }

 public void setFavorites(java.util.List<String> favorites) {
  this.favorites = favorites;
 }
 

 public String getMobile() {
  return this.mobile;
 }

 public void setMobile(String mobile) {
  this.mobile = mobile;
 }
 

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

 public void setAge(int age) {
  this.age = age;
 }
 
 public void setConcurrentQueue( java.util.concurrent.BlockingQueue< String> concurrentQueue) {
  this.concurrentQueue = concurrentQueue;
 }
 
 public java.util.concurrent.BlockingQueue< String> getConcurrentQueue() {
  return this.concurrentQueue;
  
 }

 public static void main(String[] args) throws IOException {
  JacksonTest jackson = new JacksonTest( "dorbae", 27, new String[] { "jangsb89@gamil.com", "dorbae@nate.com"}, null, null );
  java.util.List< String> favorites = new java.util.ArrayList< String>();
  favorites.add( "programming");
  favorites.add( "data analytics");
  favorites.add( "data modeling");
  favorites.add( "swimming");
  jackson.setFavorites( favorites);
  
  JacksonTest jackson2 = null;
  
  // Object -> JSON
  com.fasterxml.jackson.databind.ObjectMapper mapper = new com.fasterxml.jackson.databind.ObjectMapper();
  String json = mapper.writeValueAsString( jackson);
  System.out.println( "======== Object -> JSON ========");
  System.out.printf( "%s\n\n", json);
  
  // JSON -> Object
  mapper = new ObjectMapper();
  jackson2 = mapper.readValue( json, JacksonTest.class);
  System.out.println( "======== JSON -> Object ========");
  System.out.printf( "name=%s, age=%d\n", jackson2.getName(), jackson2.getAge());
  
 }

}








 1
 2
 3
 4
 5
 6
 7
 8
 9
10
    @JsonIgnore
 public void setConcurrentQueue( java.util.concurrent.BlockingQueue< String> concurrentQueue) {
  this.concurrentQueue = concurrentQueue;
 }
 
 @JsonIgnore
 public java.util.concurrent.BlockingQueue< String> getConcurrentQueue() {
  return this.concurrentQueue;
  
 }

No comments:

Post a Comment