diff options
Diffstat (limited to 'gson/src/main')
-rw-r--r-- | gson/src/main/java/org/onap/policy/common/gson/GsonMessageBodyHandler.java | 26 | ||||
-rw-r--r-- | gson/src/main/java/org/onap/policy/common/gson/JacksonHandler.java | 27 |
2 files changed, 33 insertions, 20 deletions
diff --git a/gson/src/main/java/org/onap/policy/common/gson/GsonMessageBodyHandler.java b/gson/src/main/java/org/onap/policy/common/gson/GsonMessageBodyHandler.java index ad270910..37d24177 100644 --- a/gson/src/main/java/org/onap/policy/common/gson/GsonMessageBodyHandler.java +++ b/gson/src/main/java/org/onap/policy/common/gson/GsonMessageBodyHandler.java @@ -37,6 +37,7 @@ import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.ext.MessageBodyReader; import javax.ws.rs.ext.MessageBodyWriter; import javax.ws.rs.ext.Provider; +import lombok.AccessLevel; import lombok.Getter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -54,7 +55,7 @@ public class GsonMessageBodyHandler implements MessageBodyReader<Object>, Messag /** * Object to be used to serialize and de-serialize. */ - @Getter + @Getter(AccessLevel.PROTECTED) private final Gson gson; /** @@ -62,17 +63,7 @@ public class GsonMessageBodyHandler implements MessageBodyReader<Object>, Messag * into Integer/Long, where possible. */ public GsonMessageBodyHandler() { - this(new GsonBuilder()); - } - - /** - * Constructs the object, using a Gson object that translates Doubles inside of Maps - * into Integer/Long, where possible. - * - * @param builder builder to use to create the gson object - */ - public GsonMessageBodyHandler(GsonBuilder builder) { - this(builder.registerTypeAdapterFactory(new MapDoubleAdapterFactory()).create()); + this(configBuilder(new GsonBuilder()).create()); } /** @@ -86,6 +77,17 @@ public class GsonMessageBodyHandler implements MessageBodyReader<Object>, Messag logger.info("Using GSON for REST calls"); } + /** + * Configures a builder with the adapters normally used by this handler (e.g., mapper + * that converts Double to Integer). + * + * @param builder builder to be configured + * @return the configured builder + */ + public static GsonBuilder configBuilder(GsonBuilder builder) { + return builder.registerTypeAdapterFactory(new MapDoubleAdapterFactory()); + } + @Override public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { return canHandle(mediaType); diff --git a/gson/src/main/java/org/onap/policy/common/gson/JacksonHandler.java b/gson/src/main/java/org/onap/policy/common/gson/JacksonHandler.java index 6d946439..6df5ad2e 100644 --- a/gson/src/main/java/org/onap/policy/common/gson/JacksonHandler.java +++ b/gson/src/main/java/org/onap/policy/common/gson/JacksonHandler.java @@ -20,6 +20,7 @@ package org.onap.policy.common.gson; +import com.google.gson.Gson; import com.google.gson.GsonBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -36,20 +37,30 @@ public class JacksonHandler extends GsonMessageBodyHandler { * Constructs the object. */ public JacksonHandler() { - this(new GsonBuilder()); + this(configBuilder(new GsonBuilder()).create()); + } + /** + * Constructs the object. + * + * @param gson the Gson object to be used to serialize and de-serialize + */ + public JacksonHandler(Gson gson) { + super(gson); logger.info("Using GSON with Jackson behaviors for REST calls"); } /** - * Constructs the object. - * @param builder builder to use to create the gson object + * Configures a builder with the adapters normally used by this handler (e.g., + * adapters for GsonJsonXxx annotations). + * + * @param builder builder to be configured + * @return the configured builder */ - public JacksonHandler(GsonBuilder builder) { - super(builder - .registerTypeAdapterFactory(new JacksonFieldAdapterFactory()) + public static GsonBuilder configBuilder(GsonBuilder builder) { + return builder.registerTypeAdapterFactory(new JacksonFieldAdapterFactory()) .registerTypeAdapterFactory(new JacksonMethodAdapterFactory()) - .setExclusionStrategies(new JacksonExclusionStrategy())); + .registerTypeAdapterFactory(new MapDoubleAdapterFactory()) + .setExclusionStrategies(new JacksonExclusionStrategy()); } - } |