Monday, November 20, 2017

How to rename JSON properties using Jackson annotations?

JsonIgnore Property Example :

  • In this example you will know how to ignore json property using @JsonIgnore jackson annotation.
  • If you annotate any property with @JsonIgnore annotation, it will be ignored at runtime.
=======================================================================
package com.coding.Json;

import java.io.File;
import java.io.IOException;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonIgnoreExample {

public static void main(String[] args) {
// TODO Auto-generated method stub
ObjectMapper mapper = new ObjectMapper();
        try {
            // reading json input from the file and mapping to object
            File jsonInputFile = new File("D:\\Employee.txt");
            Employee emp = mapper.readValue(jsonInputFile, Employee.class);
            System.out.println(emp);
            // converting object to string value
            String jsonStr 
                    = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(emp);
            System.out.println(jsonStr);
             
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
=======================================================================

class Employee {

@JsonProperty("emp_id")
private int empId;

@JsonProperty("emp_name")
private String name;

@JsonProperty("emp_designation")
private String designation;

private String department;
@JsonIgnore
private int salary; // salary ignored

public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("************************************");
sb.append("\nempId: ").append(empId);
sb.append("\nname: ").append(name);
sb.append("\ndesignation: ").append(designation);
sb.append("\ndepartment: ").append(department);
sb.append("\nsalary: ").append(salary);
sb.append("\n************************************");
return sb.toString();
}

public int getEmpId() {
return empId;
}

public void setEmpId(int empId) {
this.empId = empId;
}

public String getName() {
return name;
}

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

public String getDesignation() {
return designation;
}

public void setDesignation(String designation) {
this.designation = designation;
}

public String getDepartment() {
return department;
}

public void setDepartment(String department) {
this.department = department;
}

public int getSalary() {
return salary;
}

public void setSalary(int salary) {
this.salary = salary;
}
}
========================================================================
OUTPUT:

************************************
empId: 1017
name: Nagesh Y
designation: Manager
department: Java2Novice
salary: 0
************************************
{
  "department" : "Java2Novice",
  "emp_id" : 1017,
  "emp_name" : "Nagesh Y",
  "emp_designation" : "Manager"
}


No comments:

Post a Comment

How to remove duplicate elements from an ArrayList in Java

How to remove duplicate elements from an ArrayList in Java. Ø   ArrayList allows adding duplicate elements, sometime there may be...