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

io.prestosql.jdbc.$internal.jackson.datatype.guava.deser.RangeSetDeserializer Maven / Gradle / Ivy

There is a newer version: 350
Show newest version
package io.prestosql.jdbc.$internal.jackson.datatype.guava.deser;

import io.prestosql.jdbc.$internal.jackson.core.JsonParseException;
import io.prestosql.jdbc.$internal.jackson.core.JsonParser;
import io.prestosql.jdbc.$internal.jackson.databind.BeanProperty;
import io.prestosql.jdbc.$internal.jackson.databind.DeserializationContext;
import io.prestosql.jdbc.$internal.jackson.databind.JavaType;
import io.prestosql.jdbc.$internal.jackson.databind.JsonDeserializer;
import io.prestosql.jdbc.$internal.jackson.databind.deser.ContextualDeserializer;
import io.prestosql.jdbc.$internal.guava.collect.ImmutableRangeSet;
import io.prestosql.jdbc.$internal.guava.collect.Range;
import io.prestosql.jdbc.$internal.guava.collect.RangeSet;

import java.io.IOException;
import java.util.List;

public class RangeSetDeserializer
    extends JsonDeserializer>>
    implements ContextualDeserializer
{
    private JavaType genericRangeListType;

    @Override
    public RangeSet> deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
        if (genericRangeListType == null) {
            throw new JsonParseException(p, "RangeSetJsonSerializer was not contextualized (no deserialize target type). " +
                    "You need to specify the generic type down to the generic parameter of RangeSet.");
        } else {
            @SuppressWarnings("unchecked") final Iterable>> ranges
                    = (Iterable>>) ctxt
                    .findContextualValueDeserializer(genericRangeListType, null).deserialize(p, ctxt);
            ImmutableRangeSet.Builder> builder = ImmutableRangeSet.builder();
            for (Range> range : ranges) {
                builder.add(range);
            }
            return builder.build();
        }
    }

    @Override
    public JsonDeserializer createContextual(DeserializationContext ctxt, BeanProperty property) {
        final JavaType genericType = ctxt.getContextualType().containedType(0);
        if (genericType == null) return this;
        final RangeSetDeserializer deserializer = new RangeSetDeserializer();
        deserializer.genericRangeListType = ctxt.getTypeFactory().constructCollectionType(List.class,
                ctxt.getTypeFactory().constructParametricType(Range.class, genericType));
        return deserializer;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy