Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add support for binding Json key serializers and deserializers

  • Loading branch information...
commit 8bbf9bee6672807ae483e3d089e738f419856328 1 parent 65d1289
Dain Sundstrom authored
17 json/src/main/java/io/airlift/json/JsonBinder.java
View
@@ -7,9 +7,12 @@
import com.google.inject.multibindings.MapBinder;
import org.codehaus.jackson.map.JsonDeserializer;
import org.codehaus.jackson.map.JsonSerializer;
+import org.codehaus.jackson.map.KeyDeserializer;
public class JsonBinder
{
+ private final MapBinder<Class<?>, JsonSerializer<?>> keySerializerMapBinder;
+ private final MapBinder<Class<?>, KeyDeserializer> keyDeserializerMapBinder;
private final MapBinder<Class<?>, JsonSerializer<?>> serializerMapBinder;
private final MapBinder<Class<?>, JsonDeserializer<?>> deserializerMapBinder;
@@ -20,10 +23,24 @@ public static JsonBinder jsonBinder(Binder binder)
private JsonBinder(Binder binder)
{
+ keySerializerMapBinder = MapBinder.newMapBinder(binder, new TypeLiteral<Class<?>>() { }, new TypeLiteral<JsonSerializer<?>>() {}, JsonKeySerde.class);
+ keyDeserializerMapBinder = MapBinder.newMapBinder(binder, new TypeLiteral<Class<?>>() { }, new TypeLiteral<KeyDeserializer>() {}, JsonKeySerde.class);
serializerMapBinder = MapBinder.newMapBinder(binder, new TypeLiteral<Class<?>>() { }, new TypeLiteral<JsonSerializer<?>>() {});
deserializerMapBinder = MapBinder.newMapBinder(binder, new TypeLiteral<Class<?>>() { }, new TypeLiteral<JsonDeserializer<?>>() {});
}
+ public LinkedBindingBuilder<JsonSerializer<?>> addKeySerializerBinding(Class<?> type)
+ {
+ Preconditions.checkNotNull(type, "type is null");
+ return keySerializerMapBinder.addBinding(type);
+ }
+
+ public LinkedBindingBuilder<KeyDeserializer> addKeyDeserializerBinding(Class<?> type)
+ {
+ Preconditions.checkNotNull(type, "type is null");
+ return keyDeserializerMapBinder.addBinding(type);
+ }
+
public LinkedBindingBuilder<JsonSerializer<?>> addSerializerBinding(Class<?> type)
{
Preconditions.checkNotNull(type, "type is null");
18 json/src/main/java/io/airlift/json/JsonKeySerde.java
View
@@ -0,0 +1,18 @@
+package io.airlift.json;
+
+import com.google.inject.BindingAnnotation;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.ElementType.PARAMETER;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+@Retention(RUNTIME)
+@Target({FIELD, PARAMETER, METHOD})
+@BindingAnnotation
+@interface JsonKeySerde
+{
+}
33 json/src/main/java/io/airlift/json/ObjectMapperProvider.java
View
@@ -6,6 +6,7 @@
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.JsonDeserializer;
import org.codehaus.jackson.map.JsonSerializer;
+import org.codehaus.jackson.map.KeyDeserializer;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
import org.codehaus.jackson.map.module.SimpleModule;
@@ -22,6 +23,8 @@
public class ObjectMapperProvider implements Provider<ObjectMapper>
{
+ private Map<Class<?>, JsonSerializer<?>> keySerializers;
+ private Map<Class<?>, KeyDeserializer> keyDeserializers;
private Map<Class<?>, JsonSerializer<?>> jsonSerializers;
private Map<Class<?>, JsonDeserializer<?>> jsonDeserializers;
@@ -37,6 +40,18 @@ public void setJsonDeserializers(Map<Class<?>, JsonDeserializer<?>> jsonDeserial
this.jsonDeserializers = jsonDeserializers;
}
+ @Inject(optional = true)
+ public void setKeySerializers(@JsonKeySerde Map<Class<?>, JsonSerializer<?>> keySerializers)
+ {
+ this.keySerializers = keySerializers;
+ }
+
+ @Inject(optional = true)
+ public void setKeyDeserializers(@JsonKeySerde Map<Class<?>, KeyDeserializer> keyDeserializers)
+ {
+ this.keyDeserializers = keyDeserializers;
+ }
+
@Override
public ObjectMapper get()
{
@@ -58,7 +73,7 @@ public ObjectMapper get()
objectMapper.getSerializationConfig().disable(AUTO_DETECT_GETTERS);
objectMapper.getSerializationConfig().disable(AUTO_DETECT_IS_GETTERS);
- if (jsonSerializers != null || jsonDeserializers != null) {
+ if (jsonSerializers != null || jsonDeserializers != null || keySerializers != null || keyDeserializers != null) {
SimpleModule module = new SimpleModule(getClass().getName(), new Version(1, 0, 0, null));
if (jsonSerializers != null) {
for (Entry<Class<?>, JsonSerializer<?>> entry : jsonSerializers.entrySet()) {
@@ -71,6 +86,17 @@ public ObjectMapper get()
addDeserializer(module, entry.getKey(), entry.getValue());
}
}
+ if (keySerializers != null) {
+ for (Entry<Class<?>, JsonSerializer<?>> entry : keySerializers.entrySet()) {
+ addKeySerializer(module, entry.getKey(), entry.getValue());
+
+ }
+ }
+ if (keyDeserializers != null) {
+ for (Entry<Class<?>, KeyDeserializer> entry : keyDeserializers.entrySet()) {
+ module.addKeyDeserializer(entry.getKey(), entry.getValue());
+ }
+ }
objectMapper.registerModule(module);
}
@@ -93,4 +119,9 @@ public ObjectMapper get()
{
module.addDeserializer((Class<T>) type, (JsonDeserializer<? extends T>) jsonDeserializer);
}
+
+ private <T> void addKeySerializer(SimpleModule module, Class<?> type, JsonSerializer<?> keySerializer)
+ {
+ module.addKeySerializer((Class<? extends T>) type, (JsonSerializer<T>) keySerializer);
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.