Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Jackson Custom Serializer
package homo.efficio.json.jackson.custom.serialization;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import homo.efficio.json.jackson.custom.serialization.domain.CellPhone;
import homo.efficio.json.jackson.custom.serialization.domain.FamilyMember;
import homo.efficio.json.jackson.custom.serialization.domain.MobileVendor;
import homo.efficio.json.jackson.custom.serialization.serializer.CellPhoneSerializer;
import homo.efficio.json.jackson.custom.serialization.serializer.FamilyMemberSerializer;
import java.util.LinkedHashSet;
import java.util.Set;
/**
* Created by HomoEfficio on 2016-10-22.
*/
public class CustomSerializationMain {
public static void main(String[] args) throws JsonProcessingException {
ObjectMapper objectMapper = getObjectMapper();
FamilyMember 천하대장군 = getFamilyMember();
System.out.println(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(천하대장군));
}
private static ObjectMapper getObjectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
SimpleModule simpleModule = new SimpleModule();
simpleModule.addSerializer(FamilyMember.class, new FamilyMemberSerializer());
simpleModule.addSerializer(CellPhone.class, new CellPhoneSerializer());
objectMapper.registerModule(simpleModule);
return objectMapper;
}
private static FamilyMember getFamilyMember() {
FamilyMember 손녀딸1 = new FamilyMember(
31L, "손녀딸", null, null
);
FamilyMember 손자2 = new FamilyMember(
32L, "손자2", null, null
);
Set<FamilyMember> childrens1 = new LinkedHashSet<>();
childrens1.add(손녀딸1);
childrens1.add(손자2);
FamilyMember 아들1 = new FamilyMember(
21L, "아들1", new CellPhone("01087879898", MobileVendor.KT), childrens1
);
FamilyMember 며느리1 = new FamilyMember(
22L, "며느리1", new CellPhone("01082825353", MobileVendor.SKT), childrens1
);
Set<FamilyMember> childrens = new LinkedHashSet<>();
childrens.add(아들1);
childrens.add(며느리1);
return new FamilyMember(
21L, "천하대장군", new CellPhone("01056561253", MobileVendor.LGT), childrens
);
}
}
package homo.efficio.json.jackson.custom.serialization.serializer;
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 homo.efficio.json.jackson.custom.serialization.domain.FamilyMember;
import java.io.IOException;
/**
* Created by HomoEfficio on 2016-10-22.
*/
public class FamilyMemberSerializer extends JsonSerializer<FamilyMember> {
@Override
public void serialize(FamilyMember value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException {
gen.writeStartObject();
gen.writeFieldName("id");
gen.writeString(String.valueOf(value.getId()));
gen.writeFieldName("name");
gen.writeString(value.getName());
gen.writeFieldName("cellPhone");
gen.writeObject(value.getCellPhone());
gen.writeFieldName("children");
gen.writeObject(value.getChildren());
gen.writeEndObject();
}
}
package homo.efficio.json.jackson.custom.serialization.serializer;
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 homo.efficio.json.jackson.custom.serialization.domain.CellPhone;
import java.io.IOException;
/**
* Created by HomoEfficio on 2016-10-22.
*/
public class CellPhoneSerializer extends JsonSerializer<CellPhone> {
@Override
public void serialize(CellPhone value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException {
gen.writeStartObject();
gen.writeFieldName("phoneNumber");
gen.writeString(value.getNumber());
gen.writeEndObject();
}
}
package homo.efficio.json.jackson.custom.serialization.domain;
import java.util.Set;
/**
* Created by HomoEfficio on 2016-10-22.
*/
public class FamilyMember {
private Long id;
private String name;
private CellPhone cellPhone;
private Set<FamilyMember> children;
public FamilyMember(Long id, String name, CellPhone cellPhone, Set<FamilyMember> children) {
this.id = id;
this.name = name;
this.cellPhone = cellPhone;
this.children = children;
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
public CellPhone getCellPhone() {
return cellPhone;
}
public Set<FamilyMember> getChildren() {
return children;
}
}
package homo.efficio.json.jackson.custom.serialization.domain;
/**
* Created by hanmomhanda on 2016-10-22.
*/
public class CellPhone {
private String number;
private MobileVendor vendor;
public CellPhone(String number, MobileVendor vendor) {
this.number = number;
this.vendor = vendor;
}
public String getNumber() {
return number;
}
public MobileVendor getVendor() {
return vendor;
}
}
package homo.efficio.json.jackson.custom.serialization.domain;
/**
* Created by HomoEfficio on 2016-10-22.
*/
public enum MobileVendor {
KT, SKT, LGT
}
// Gist counts commit in GitHub account?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment