Annotate your field and getter/setter
with @JsonIgnore. Make sure you annotated field, getter and setter method of
that field. Following program ignore password field from Employee class, while
serializing and de-serializing.
import com.owlike.genson.annotation.JsonIgnore; public class Employee { private String id; private String firstName; private String lastName; @JsonIgnore 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; } @JsonIgnore public String getPassword() { return password; } @JsonIgnore public void setPassword(String password) { this.password = password; } @Override public String toString() { StringBuilder builder = new StringBuilder(); builder.append("Employee [id=").append(id).append(", firstName=") .append(firstName).append(", lastName=").append(lastName) .append(", password=").append(password).append("]"); return builder.toString(); } }
import java.util.List; import java.util.Map; import com.owlike.genson.GenericType; import com.owlike.genson.Genson; public class JSONUtil { private static Genson genson = new Genson(); public static String getJson(Object obj) { return genson.serialize(obj); } public static <T> T deserializeJson(String json, Class<T> clazz) { return genson.deserialize(json, clazz); } public static <T> List<T> getListOfObjects(String json, GenericType<List<T>> list) { return genson.deserialize(json, list); } public static <K, V> Map<K, V> getMap(String json, GenericType<Map<K, V>> map) { return genson.deserialize(json, map); } }
public class Test { public static void main(String args[]) { Employee emp1 = new Employee(); emp1.setId("E432156"); emp1.setFirstName("Hari krishna"); emp1.setLastName("Gurram"); emp1.setPassword("Password123"); String json = JSONUtil.getJson(emp1); System.out.println(json); } }
Output
{"firstName":"Hari
krishna","id":"E432156","lastName":"Gurram"}
No comments:
Post a Comment