com.facebook.presto.jdbc.internal.airlift.json.JsonBinder Maven / Gradle / Ivy
/*
* 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;
import static com.google.inject.multibindings.Multibinder.newSetBinder;
import static java.util.Objects.requireNonNull;
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)
{
binder = requireNonNull(binder, "binder is null").skipSources(getClass());
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 = newSetBinder(binder, Module.class);
}
public LinkedBindingBuilder> addKeySerializerBinding(Class> type)
{
requireNonNull(type, "type is null");
return keySerializerMapBinder.addBinding(type);
}
public LinkedBindingBuilder addKeyDeserializerBinding(Class> type)
{
requireNonNull(type, "type is null");
return keyDeserializerMapBinder.addBinding(type);
}
public LinkedBindingBuilder> addSerializerBinding(Class> type)
{
requireNonNull(type, "type is null");
return serializerMapBinder.addBinding(type);
}
public LinkedBindingBuilder> addDeserializerBinding(Class> type)
{
requireNonNull(type, "type is null");
return deserializerMapBinder.addBinding(type);
}
public LinkedBindingBuilder addModuleBinding()
{
return moduleBinder.addBinding();
}
public void bindSerializer(JsonSerializer jsonSerializer)
{
requireNonNull(jsonSerializer, "jsonSerializer is null");
Class> type = jsonSerializer.handledType();
requireNonNull(type, "jsonSerializer.handledType is null");
Preconditions.checkArgument(type == Object.class, "jsonSerializer.handledType can not be Object.class");
serializerMapBinder.addBinding(type).toInstance(jsonSerializer);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy