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

org.zapodot.jackson.java8.JavaOptionalDeserializer 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.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;

import java.io.IOException;
import java.util.Optional;

/**
 * @author R.J. Lorimer [[email protected]]
 */
public class JavaOptionalDeserializer extends StdDeserializer> {
    private final JavaType _referenceType;

    public JavaOptionalDeserializer(JavaType valueType) {
        super(valueType);
        _referenceType = valueType.containedType(0);
    }

    @Override
    public Optional getNullValue() {
        return Optional.empty();
    }

    @Override
    public Optional deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        Object reference = ctxt.findRootValueDeserializer(_referenceType).deserialize(jp, ctxt);
        return Optional.of(reference);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy