summaryrefslogtreecommitdiffstats
path: root/gson/src/main/java/org/onap/policy/common/gson/GsonMessageBodyHandler.java
diff options
context:
space:
mode:
authorJim Hahn <jrh3@att.com>2019-04-10 20:08:30 -0400
committerJim Hahn <jrh3@att.com>2019-04-11 08:58:10 -0400
commitea29ed1a002ab282a36761448575ffc2e517284d (patch)
tree1127a1d9acd8899b5d39323b56465455e26320fe /gson/src/main/java/org/onap/policy/common/gson/GsonMessageBodyHandler.java
parentf5586edf6bb68016b4259829b57ca7e3528b126a (diff)
Don't map JSON values to Double
By default, gson treats all numbers as Double when placed into a generic Map. This is not backward compatible with existing policy APIs. Added a type adapter that walks Map objects and converts the Double values to Integer or Long, where possible. Made this the default behavior in the GsonMessageBodyHandler, the JacksonHandler, and the StandardCoder. Also fixed a couple of checkstyle errors in the gson project. Change-Id: I9ac0c77e6592d1c039646f0662c077b77a1e9aaf Issue-ID: POLICY-1542 Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'gson/src/main/java/org/onap/policy/common/gson/GsonMessageBodyHandler.java')
-rw-r--r--gson/src/main/java/org/onap/policy/common/gson/GsonMessageBodyHandler.java15
1 files changed, 6 insertions, 9 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 9dad6db8..a36f8a07 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
@@ -21,7 +21,7 @@
package org.onap.policy.common.gson;
import com.google.gson.Gson;
-
+import com.google.gson.GsonBuilder;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
@@ -37,9 +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.Getter;
-
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -60,10 +58,11 @@ public class GsonMessageBodyHandler implements MessageBodyReader<Object>, Messag
private final Gson gson;
/**
- * Constructs the object, using a plain Gson object.
+ * Constructs the object, using a Gson object that translates Doubles inside of Maps
+ * into Integer/Long, where possible.
*/
public GsonMessageBodyHandler() {
- this(new Gson());
+ this(new GsonBuilder().registerTypeAdapterFactory(new MapDoubleAdapterFactory()).create());
logger.info("Using GSON for REST calls");
}
@@ -89,8 +88,7 @@ public class GsonMessageBodyHandler implements MessageBodyReader<Object>, Messag
@Override
public void writeTo(Object object, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType,
- MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream)
- throws IOException {
+ MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException {
try (OutputStreamWriter writer = new OutputStreamWriter(entityStream, StandardCharsets.UTF_8)) {
Type jsonType = (type.equals(genericType) ? type : genericType);
@@ -126,8 +124,7 @@ public class GsonMessageBodyHandler implements MessageBodyReader<Object>, Messag
@Override
public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType,
- MultivaluedMap<String, String> httpHeaders, InputStream entityStream)
- throws IOException {
+ MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException {
try (InputStreamReader streamReader = new InputStreamReader(entityStream, StandardCharsets.UTF_8)) {
Type jsonType = (type.equals(genericType) ? type : genericType);