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

com.facebook.presto.jdbc.internal.airlift.json.JsonBinder Maven / Gradle / Ivy

There is a newer version: 0.289
Show newest version
/*
 * Copyright 2010 Proofpoint, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.facebook.presto.jdbc.internal.airlift.json;

import com.facebook.presto.jdbc.internal.jackson.databind.JsonDeserializer;
import com.facebook.presto.jdbc.internal.jackson.databind.JsonSerializer;
import com.facebook.presto.jdbc.internal.jackson.databind.KeyDeserializer;
import com.facebook.presto.jdbc.internal.jackson.databind.Module;
import com.facebook.presto.jdbc.internal.guava.base.Preconditions;
import com.google.inject.Binder;
import com.google.inject.TypeLiteral;
import com.google.inject.binder.LinkedBindingBuilder;
import com.google.inject.multibindings.MapBinder;
import com.google.inject.multibindings.Multibinder;

public class JsonBinder
{
    private final MapBinder, JsonSerializer> keySerializerMapBinder;
    private final MapBinder, KeyDeserializer> keyDeserializerMapBinder;
    private final MapBinder, JsonSerializer> serializerMapBinder;
    private final MapBinder, JsonDeserializer> deserializerMapBinder;
    private final Multibinder moduleBinder;

    public static JsonBinder jsonBinder(Binder binder)
    {
        return new JsonBinder(binder);
    }

    private JsonBinder(Binder binder)
    {
        keySerializerMapBinder = MapBinder.newMapBinder(binder, new TypeLiteral>() { }, new TypeLiteral>() {}, JsonKeySerde.class);
        keyDeserializerMapBinder = MapBinder.newMapBinder(binder, new TypeLiteral>() { }, new TypeLiteral() {}, JsonKeySerde.class);
        serializerMapBinder = MapBinder.newMapBinder(binder, new TypeLiteral>() { }, new TypeLiteral>() {});
        deserializerMapBinder = MapBinder.newMapBinder(binder, new TypeLiteral>() { }, new TypeLiteral>() {});
        moduleBinder = Multibinder.newSetBinder(binder, Module.class);
    }

    public LinkedBindingBuilder> addKeySerializerBinding(Class type)
    {
        Preconditions.checkNotNull(type, "type is null");
        return keySerializerMapBinder.addBinding(type);
    }

    public LinkedBindingBuilder addKeyDeserializerBinding(Class type)
    {
        Preconditions.checkNotNull(type, "type is null");
        return keyDeserializerMapBinder.addBinding(type);
    }

    public LinkedBindingBuilder> addSerializerBinding(Class type)
    {
        Preconditions.checkNotNull(type, "type is null");
        return serializerMapBinder.addBinding(type);
    }

    public LinkedBindingBuilder> addDeserializerBinding(Class type)
    {
        Preconditions.checkNotNull(type, "type is null");
        return deserializerMapBinder.addBinding(type);
    }

    public LinkedBindingBuilder addModuleBinding()
    {
        return moduleBinder.addBinding();
    }


    public  void bindSerializer(JsonSerializer jsonSerializer)
    {
        Preconditions.checkNotNull(jsonSerializer, "jsonSerializer is null");

        Class type = jsonSerializer.handledType();
        Preconditions.checkNotNull(type, "jsonSerializer.handledType is null");
        Preconditions.checkArgument(type == Object.class, "jsonSerializer.handledType can not be Object.class");
        serializerMapBinder.addBinding(type).toInstance(jsonSerializer);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy