Summary Table
Categories |
Total Count |
PII |
0 |
URL |
0 |
DNS |
0 |
EKL |
0 |
IP |
0 |
PORT |
0 |
VsID |
0 |
CF |
0 |
AI |
0 |
VPD |
0 |
PL |
0 |
Other |
0 |
File Content
package gov.va.med.pbm.ampl.configuration;
import java.io.IOException;
import java.util.function.Function;
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.fasterxml.jackson.databind.module.SimpleModule;
/**
* A simple Java 8 centric Jackson Module wrapper for making custom serializers lambda friendly.
*
* @author Ian Meinert
*
*/
public class Jackson8Module extends SimpleModule {
/**
* Default serial.
*/
private static final long serialVersionUID = -3503680986020666055L;
/**
* This method adds a custom serializer to the Jackson module.
*
* @param clazz The class to serialize to
* @param serializeFunction The function to be serialized
* @param <T> The anonymous class being processed
*/
public <T> void addCustomSerializer(Class<T> clazz, SerializeFunction<T> serializeFunction) {
JsonSerializer<T> jsonSerializer = new JsonSerializer<T>() {
@Override
public void serialize(T t, JsonGenerator jgen, SerializerProvider serializerProvider)
throws IOException, JsonProcessingException {
serializeFunction.serialize(t, jgen);
}
};
addSerializer(clazz, jsonSerializer);
}
/**
* This method adds a String serializer to the Jackson module.
*
* @param clazz The class to serialize to
* @param serializeFunction The function to be serialized
* @param <T> The anonymous class being processed
*/
public <T> void addStringSerializer(Class<T> clazz, Function<T, String> serializeFunction) {
JsonSerializer<T> jsonSerializer = new JsonSerializer<T>() {
@Override
public void serialize(T t, JsonGenerator jgen, SerializerProvider serializerProvider)
throws IOException, JsonProcessingException {
String val = serializeFunction.apply(t);
jgen.writeString(val);
}
};
addSerializer(clazz, jsonSerializer);
}
/**
* This interface directly supports the Jackson8Module class for it's serializer provider.
*
* @author Ian Meinert
*
* @param <T> The generic type to be serialized
*/
@FunctionalInterface
public static interface SerializeFunction<T> {
/**
* This method serializes a given object.
*
* @param t the generic Type
* @param jgen the JsonGenerator
* @throws IOException I/O Exception
* @throws JsonProcessingException JsonProcessingException
*/
public void serialize(T t, JsonGenerator jgen) throws IOException, JsonProcessingException;
}
}