Wednesday, 20 April 2016

Gson: Expose specific fields (ignore fields)

Some times you don’t want to expose specific properties like password, pin etc., by using Expose annotation, you can specify the properties that are exposed for JSON serialization or deserialization.


For example,
public class Employee {
 @Expose private String id;
 @Expose private String firstName;
 @Expose private String lastName;
 private String password;

 .....
 .....
}

Above entity exposes only id, firstName and lastName fields for JSON serialization and deserialization.


To work with Expose annotation, you need to create gson instance like below.
Gson exclusedGson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
exclusedGson.toJson(obj);

Following is the complete working example.


Employee.java
import com.google.gson.annotations.Expose;

public class Employee {
 @Expose private String id;
 @Expose private String firstName;
 @Expose private String lastName;
 private String password;

 public String getId() {
  return id;
 }

 public void setId(String id) {
  this.id = id;
 }

 public String getFirstName() {
  return firstName;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public String getLastName() {
  return lastName;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

 public String getPassword() {
  return password;
 }

 public void setPassword(String password) {
  this.password = password;
 }

}


JSONUtil.java
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

/**
 * 
 * @author harikrishna_gurram
 *
 */
public class JSONUtil {
 private static Gson gson = new Gson();
 private static Gson exclusedGson = new GsonBuilder()
   .excludeFieldsWithoutExposeAnnotation().create();
 private static Gson prettyGson = new GsonBuilder().setPrettyPrinting()
   .create();

 /**
  * @param obj
  * @return json string of this object.
  */
 public static String getJson(Object obj) {
  return gson.toJson(obj);
 }

 /**
  * @param obj
  * @return json string of this object.
  */
 public static String getOnlyExposedJson(Object obj) {
  return exclusedGson.toJson(obj);
 }

 /**
  * @param obj
  * @return json string of this object (Pretty json).
  */
 public static String getPrettyJson(Object obj) {
  return prettyGson.toJson(obj);
 }

 /**
  * Convert given json string to object.
  * 
  * @param json
  * @param obj
  * @return an object by populating properties with json data.
  */
 public static <T> T getObject(String json, Class<T> clazz) {
  return gson.fromJson(json, clazz);
 }

}


Test.java
public class Test {

 public static void main(String[] args) {
  Employee emp = new Employee();

  emp.setFirstName("Gopi");
  emp.setLastName("Battu");
  emp.setId("1234");
  emp.setPassword("password123");

  System.out.println(JSONUtil.getOnlyExposedJson(emp));

 }
}


Output
{"id":"1234","firstName":"Gopi","lastName":"Battu"}




Previous                                                 Next                                                 Home

No comments:

Post a Comment