org.zapodot.jackson.java8.JavaOptionalSerializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jackson-databind-java-optional Show documentation
Show all versions of jackson-databind-java-optional Show documentation
Jackson Databind module for serializing and deserializing Java 8 java.util.Option objects.
This tool is forked from original source created by @realjenius
package org.zapodot.jackson.java8;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import java.io.IOException;
import java.util.Optional;
/**
* @author R.J. Lorimer [[email protected]]
*/
public class JavaOptionalSerializer extends StdSerializer> {
public JavaOptionalSerializer(JavaType type) {
super(type);
}
@Override
public void serialize(Optional> value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonGenerationException {
if(value.isPresent()) {
provider.defaultSerializeValue(value.get(), jgen);
} else {
provider.defaultSerializeNull(jgen);
}
}
}