package com.json.demo6_3_3;
import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonIgnoreType;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.json.demo6_3_3.Person.BooksRead;
import com.json.demo6_3_3.Person.QUALIFICAION;
public class JsonAnnotationsDemo {
public static void main(String[] args) throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
Person person = new Person(1,"Tyson",70,true);
person.setQualification(QUALIFICAION.MCA);
BooksRead manSearchMeaning = person.new BooksRead(111,"Mans Search for Meaning",300);
person.getListOfBooksRead().add(manSearchMeaning);
System.out.println(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(person));
}
}
@JsonPropertyOrder({"id","name","age"})
@JsonInclude(JsonInclude.Include.NON_NULL) //removed justNull value from json
@JsonIgnoreProperties({"ownsCar"})
class Person {
boolean ownsCar;
@JsonProperty("Person ID ")
int id;
@JsonProperty("Person Age")
int age;
@JsonProperty("Person Name")
String name;
String justNull;
List<BooksRead> listOfBooksRead = new ArrayList<>();
QUALIFICAION qualification;
@JsonIgnoreType //For Objects and enum and not for primitive data types
enum QUALIFICAION { BCA,BSCIT,MCA,MSCIT,MS };
Person(int id,String name,int age,boolean ownsCar){
this.id = id;
this.name = name;
this.age = age;
this.ownsCar = ownsCar;
}
class BooksRead{
int bookId;
String bookName;
@JsonIgnore //Will not include bookPages in Json
int bookPages;
BooksRead(int bookId,String bookName,int bookPages){
this.bookId = bookId;
this.bookName = bookName;
this.bookPages = bookPages;
}
public int getBookId() {
return bookId;
}
public void setBookId(int bookId) {
this.bookId = bookId;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public int getBookPages() {
return bookPages;
}
public void setBookPages(int bookPages) {
this.bookPages = bookPages;
}
}
public String toString() {
return "Id : "+id+" \nName : "+name+" \nAge : "+age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean isOwnsCar() {
return ownsCar;
}
public void setOwnsCar(boolean ownsCar) {
this.ownsCar = ownsCar;
}
public List<BooksRead> getListOfBooksRead() {
return listOfBooksRead;
}
public void setListOfBooksRead(List<BooksRead> listOfBooksRead) {
this.listOfBooksRead = listOfBooksRead;
}
public QUALIFICAION getQualification() {
return qualification;
}
public void setQualification(QUALIFICAION qualification) {
this.qualification = qualification;
}
public String getJustNull() {
return justNull;
}
public void setJustNull(String justNull) {
this.justNull = justNull;
}
}