All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.zapodot.jackson.java8.JavaOptionalSerializer Maven / Gradle / Ivy

Go to download

Jackson Databind module for serializing and deserializing Java 8 java.util.Option objects. This tool is forked from original source created by @realjenius

There is a newer version: 2.6.1
Show newest version
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);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy