Using @JsonProperty
Following example shows how to use @JsonProperty annotation to rename properties. This annotation can be used on fields or getters or setters.
public class ExampleMain {
public static void main(String[] args) throws IOException {
Employee employee = new Employee();
employee.setName("Trish");
employee.setDept("Admin");
//convert to json
String jsonString = toJson(employee);
System.out.println(jsonString);
//convert to object
Employee e = toEmployee(jsonString);
System.out.println(e);
}
private static Employee toEmployee(String jsonData) throws IOException {
ObjectMapper om = new ObjectMapper();
return om.readValue(jsonData, Employee.class);
}
private static String toJson(Employee employee) throws IOException {
ObjectMapper om = new ObjectMapper();
return om.writeValueAsString(employee);
}
private static class Employee {
@JsonProperty("employee-name")
private String name;
@JsonProperty("employee-dept")
private String dept;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDept() {
return dept;
}
public void setDept(String dept) {
this.dept = dept;
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", dept='" + dept + '\'' +
'}';
}
}
} {"employee-name":"Trish","employee-dept":"Admin"} Employee{name='Trish', dept='Admin'}
Using @JsonGetter and @JsonSetter
@JsonGetter and @JsonSetter are old alternatives to @JsonProperty.
public class ExampleMain2 {
public static void main(String[] args) throws IOException {
Employee employee = new Employee();
employee.setName("Trish");
employee.setDept("Admin");
//convert to json
String jsonString = toJson(employee);
System.out.println(jsonString);
//convert to object
Employee e = toEmployee(jsonString);
System.out.println(e);
}
private static Employee toEmployee(String jsonData) throws IOException {
ObjectMapper om = new ObjectMapper();
return om.readValue(jsonData, Employee.class);
}
private static String toJson(Employee employee) throws IOException {
ObjectMapper om = new ObjectMapper();
return om.writeValueAsString(employee);
}
private static class Employee {
private String name;
private String dept;
@JsonGetter("employee-name")
public String getName() {
return name;
}
@JsonSetter("employee-name")
public void setName(String name) {
this.name = name;
}
@JsonGetter("employee-dept")
public String getDept() {
return dept;
}
@JsonSetter("employee-dept")
public void setDept(String dept) {
this.dept = dept;
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", dept='" + dept + '\'' +
'}';
}
}
} {"employee-name":"Trish","employee-dept":"Admin"} Employee{name='Trish', dept='Admin'}
Handling Nulls with @JsonSetter
Besides renaming , the annotation @JsonSetter can be used to specify how 'null' values in JSON should be mapped to Java properties. For example:
public class ExampleMain3 {
public static void main(String[] args) throws IOException {
String jsonString = "{\"name\":null}";
System.out.println(jsonString);
//convert to Person
Person person = toPerson(jsonString);
System.out.println(person);
}
private static Person toPerson(String jsonData) throws IOException {
ObjectMapper om = new ObjectMapper();
return om.readValue(jsonData, Person.class);
}
private static class Person {
private String name;
@JsonSetter(nulls = Nulls.AS_EMPTY)
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
'}';
}
}
} {"name":null} Person{name=''}
Without @JsonSetter (or with @JsonSetter but without any Nulls parameter) the output would be:
{"name":null} Person{name='null'}
Other elements of Nulls enum are: SET , SKIP , FAIL , AS_EMPTY and DEFAULT . Example ProjectDependencies and Technologies Used: - jackson-databind 2.9.3: General data-binding functionality for Jackson: works on core streaming API.
- JDK 9
- Maven 3.3.9
|