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

org.javafunk.funk.jackson.monad.OptionDeserializer Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (C) 2011-Present Funk committers.
 * All rights reserved.
 *
 * The software in this package is published under the terms of the BSD
 * style license a copy of which has been included with this distribution in
 * the LICENSE.txt file.
 */

package org.javafunk.funk.jackson.monad;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.deser.ContextualDeserializer;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;
import org.javafunk.funk.functors.functions.UnaryFunction;
import org.javafunk.funk.monads.Option;

import java.io.IOException;
import java.util.concurrent.Callable;

import static org.javafunk.funk.monads.Option.none;
import static org.javafunk.funk.monads.Option.option;

public class OptionDeserializer extends StdDeserializer> implements ContextualDeserializer {
    protected final JavaType referenceType;
    protected final Option> valueHandler;
    protected final Option typeHandler;

    public OptionDeserializer(JavaType valueType, Option typeHandler, Option> valueHandler) {
        super(valueType);
        referenceType = valueType.containedType(0);
        this.typeHandler = typeHandler;
        this.valueHandler = valueHandler;
    }

    @Override
    public Option getNullValue() {
        return none();
    }

    @Override
    public Option deserialize(JsonParser jp, DeserializationContext context) throws IOException {
        Object reference = null;

        if (valueHandler.hasValue()) {
            if (typeHandler.hasNoValue()) {
                reference = valueHandler.get().deserialize(jp, context);
            } else {
                reference = valueHandler.get().deserializeWithType(jp, context, typeHandler.get());
            }
        }

        return option(reference);
    }

    /**
     * Method called to finalize setup of this deserializer, after deserializer itself has been registered. This is
     * needed to handle recursive and transitive dependencies.
     */
    @Override
    public JsonDeserializer createContextual(final DeserializationContext context, final BeanProperty property) throws JsonMappingException {
        Option> contextualValueHandler = valueHandler.or(new Callable>>() {
            @Override
            public Option> call() throws Exception {
                return option(context.findContextualValueDeserializer(referenceType, property));
            }
        });

        Option contextualTypeHandler = typeHandler.flatMap(new UnaryFunction>() {
            @Override
            public Option call(TypeDeserializer typeDeserializer) {
                return option(typeDeserializer.forProperty(property));
            }
        });

        if (contextualValueHandler.equals(valueHandler) && contextualTypeHandler.equals(typeHandler)) {
            return this;
        }

        return new OptionDeserializer(referenceType, contextualTypeHandler, contextualValueHandler);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy