By default Jackson use, name() method of enum to serialize and deserialize the information.
package com.sample.app.model;
public enum Month {
SUNDAY("Sunny Sunday"), MONDAY("Morning Monday"), TUESDAY("Good tuesday"), WEDNESDAY("Happy Wednesday"),
THURSDAY("Great Tuesday"), FRIDAY("Good Friday"), SATURDAY("Sleepy Saturday");
private String data;
Month(String data) {
this.data = data;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
}
Employee.java
package com.sample.app.model;
public class Employee {
private String firstName;
private String lastName;
private Month monthOfBirth;
public Employee() {
}
public Employee(String firstName, String lastName, Month monthOfBirth) {
this.firstName = firstName;
this.lastName = lastName;
this.monthOfBirth = monthOfBirth;
}
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 Month getMonthOfBirth() {
return monthOfBirth;
}
public void setMonthOfBirth(Month monthOfBirth) {
this.monthOfBirth = monthOfBirth;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Employee [firstName=");
builder.append(firstName);
builder.append(", lastName=");
builder.append(lastName);
builder.append(", monthOfBirth=");
builder.append(monthOfBirth);
builder.append("]");
return builder.toString();
}
}
App.java
package com.sample.app;
import com.sample.app.model.Employee;
import com.sample.app.model.Month;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class App {
public static void main(String args[]) throws JsonGenerationException, JsonMappingException, IOException {
Employee emp1 = new Employee("Rama Krishna", "Gurram", Month.TUESDAY);
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(emp1);
System.out.println("Serialized Data :\n" + json + "\n");
Employee emp2 = mapper.readValue(json, Employee.class);
System.out.println("DeSerialized Data :\n" + emp2 + "\n");
}
}
Output
Serialized Data : {"firstName":"Rama Krishna","lastName":"Gurram","monthOfBirth":"TUESDAY"} DeSerialized Data : Employee [firstName=Rama Krishna, lastName=Gurram, monthOfBirth=TUESDAY]
Customize serialization and deserialization
By providing custom serializer and deserializer you can customize serialization process.
Create MonthSerializer and MonthDeserializer classes.
MonthSerializer.java
package com.sample.app.serializer; import java.io.IOException; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonSerializer; import com.fasterxml.jackson.databind.SerializerProvider; import com.sample.app.model.Month; public class MonthSerializer extends JsonSerializer<Month> { @Override public void serialize(Month month, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException { gen.writeStartObject(); gen.writeStringField("monthOfBirth", month.getData()); gen.writeEndObject(); } }
MonthDeserializer.java
package com.sample.app.serializer;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.TreeNode;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.sample.app.model.Month;
public class MonthDeserializer extends JsonDeserializer<Month> {
@Override
public Month deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
TreeNode node = jp.getCodec().readTree(jp);
String monthOfBirth = node.get("monthOfBirth").toString();
// REMOVE DOUBLE QUOTES
monthOfBirth = monthOfBirth.substring(1, monthOfBirth.length()-1);
if (monthOfBirth.equals(Month.SUNDAY.getData())) {
return Month.SUNDAY;
} else if (monthOfBirth.equals(Month.MONDAY.getData())) {
return Month.MONDAY;
} else if (monthOfBirth.equals(Month.TUESDAY.getData())) {
return Month.TUESDAY;
} else if (monthOfBirth.equals(Month.WEDNESDAY.getData())) {
return Month.WEDNESDAY;
} else if (monthOfBirth.equals(Month.THURSDAY.getData())) {
return Month.THURSDAY;
} else if (monthOfBirth.equals(Month.FRIDAY.getData())) {
return Month.FRIDAY;
} else {
return Month.SATURDAY;
}
}
}
Create custom module and register serializer, deserializers.
package com.sample.app.serializer;
import com.fasterxml.jackson.core.util.VersionUtil;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.sample.app.model.Month;
public class MonthModule extends SimpleModule{
private static final long serialVersionUID = 1L;
private static final String NAME = "CustomAddressModule";
private static final VersionUtil VERSION_UTIL = new VersionUtil() {
};
public MonthModule() {
super(NAME, VERSION_UTIL.version());
addSerializer(Month.class, new MonthSerializer());
addDeserializer(Month.class, new MonthDeserializer());
}
}
App.java
package com.sample.app;
import com.sample.app.model.Employee;
import com.sample.app.model.Month;
import com.sample.app.serializer.MonthModule;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class App {
public static void main(String args[]) throws JsonGenerationException, JsonMappingException, IOException {
Employee emp1 = new Employee("Rama Krishna", "Gurram", Month.TUESDAY);
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new MonthModule());
String json = mapper.writeValueAsString(emp1);
System.out.println("Serialized Data :\n" + json + "\n");
Employee emp2 = mapper.readValue(json, Employee.class);
System.out.println("DeSerialized Data :\n" + emp2 + "\n");
}
}
Output
Serialized Data : {"firstName":"Rama Krishna","lastName":"Gurram","monthOfBirth":{"monthOfBirth":"Good tuesday"}} DeSerialized Data : Employee [firstName=Rama Krishna, lastName=Gurram, monthOfBirth=TUESDAY]
No comments:
Post a Comment