aboutsummaryrefslogtreecommitdiffstats
path: root/utils/src/main/java/org/onap/policy/common/utils/coder/StandardCoder.java
diff options
context:
space:
mode:
Diffstat (limited to 'utils/src/main/java/org/onap/policy/common/utils/coder/StandardCoder.java')
-rw-r--r--utils/src/main/java/org/onap/policy/common/utils/coder/StandardCoder.java94
1 files changed, 65 insertions, 29 deletions
diff --git a/utils/src/main/java/org/onap/policy/common/utils/coder/StandardCoder.java b/utils/src/main/java/org/onap/policy/common/utils/coder/StandardCoder.java
index 13973f1c..d6135afd 100644
--- a/utils/src/main/java/org/onap/policy/common/utils/coder/StandardCoder.java
+++ b/utils/src/main/java/org/onap/policy/common/utils/coder/StandardCoder.java
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* ONAP
* ================================================================================
- * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -41,40 +41,82 @@ import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import lombok.AccessLevel;
-import lombok.Getter;
+import lombok.AllArgsConstructor;
import org.onap.policy.common.gson.DoubleConverter;
import org.onap.policy.common.gson.GsonMessageBodyHandler;
/**
* JSON encoder and decoder using the "standard" mechanism, which is currently gson.
*/
+@AllArgsConstructor(access = AccessLevel.PROTECTED)
public class StandardCoder implements Coder {
/**
* Gson object used to encode and decode messages.
*/
- @Getter(AccessLevel.PROTECTED)
- private static final Gson GSON;
+ private static final Gson GSON_STD;
/**
* Gson object used to encode messages in "pretty" format.
*/
- @Getter(AccessLevel.PROTECTED)
- private static final Gson GSON_PRETTY;
+ private static final Gson GSON_STD_PRETTY;
static {
GsonBuilder builder = GsonMessageBodyHandler.configBuilder(
new GsonBuilder().registerTypeAdapter(StandardCoderObject.class, new StandardTypeAdapter()));
- GSON = builder.create();
- GSON_PRETTY = builder.setPrettyPrinting().create();
+ GSON_STD = builder.create();
+ GSON_STD_PRETTY = builder.setPrettyPrinting().create();
}
/**
+ * Gson object used to encode and decode messages.
+ */
+ protected final Gson gson;
+
+ /**
+ * Gson object used to encode messages in "pretty" format.
+ */
+ protected final Gson gsonPretty;
+
+ /**
* Constructs the object.
*/
public StandardCoder() {
- super();
+ this(GSON_STD, GSON_STD_PRETTY);
+ }
+
+ @Override
+ public <S, T> T convert(S source, Class<T> clazz) throws CoderException {
+ if (source == null) {
+ return null;
+
+ } else if (clazz == source.getClass()) {
+ // same class - just cast it
+ return clazz.cast(source);
+
+ } else if (clazz == String.class) {
+ // target is a string - just encode the source
+ return (clazz.cast(encode(source)));
+
+ } else if (source.getClass() == String.class) {
+ // source is a string - just decode it
+ return decode(source.toString(), clazz);
+
+ } else {
+ /*
+ * Do it the long way: encode to a tree and then decode the tree. This entire
+ * method could have been left out and the default Coder.convert() used
+ * instead, but this should perform slightly better as it only uses a
+ * JsonElement as the intermediate data structure, while Coder.convert() goes
+ * all the way to a String as the intermediate data structure.
+ */
+ try {
+ return fromJson(toJsonTree(source), clazz);
+ } catch (RuntimeException e) {
+ throw new CoderException(e);
+ }
+ }
}
@Override
@@ -110,7 +152,7 @@ public class StandardCoder implements Coder {
@Override
public void encode(OutputStream target, Object object) throws CoderException {
try {
- Writer wtr = makeWriter(target);
+ var wtr = makeWriter(target);
toJson(wtr, object);
// flush, but don't close
@@ -123,7 +165,7 @@ public class StandardCoder implements Coder {
@Override
public void encode(File target, Object object) throws CoderException {
- try (Writer wtr = makeWriter(target)) {
+ try (var wtr = makeWriter(target)) {
toJson(wtr, object);
// no need to flush or close here
@@ -165,7 +207,7 @@ public class StandardCoder implements Coder {
@Override
public <T> T decode(File source, Class<T> clazz) throws CoderException {
- try (Reader input = makeReader(source)) {
+ try (var input = makeReader(source)) {
return fromJson(input, clazz);
} catch (RuntimeException | IOException e) {
@@ -180,13 +222,13 @@ public class StandardCoder implements Coder {
* @return the encoded object
*/
protected String toPrettyJson(Object object) {
- return GSON_PRETTY.toJson(object);
+ return gsonPretty.toJson(object);
}
@Override
public StandardCoderObject toStandard(Object object) throws CoderException {
try {
- return new StandardCoderObject(GSON.toJsonTree(object));
+ return new StandardCoderObject(gson.toJsonTree(object));
} catch (RuntimeException e) {
throw new CoderException(e);
@@ -196,7 +238,7 @@ public class StandardCoder implements Coder {
@Override
public <T> T fromStandard(StandardCoderObject sco, Class<T> clazz) throws CoderException {
try {
- return GSON.fromJson(sco.getData(), clazz);
+ return gson.fromJson(sco.getData(), clazz);
} catch (RuntimeException e) {
throw new CoderException(e);
@@ -254,7 +296,7 @@ public class StandardCoder implements Coder {
* @return a json element representing the object
*/
protected JsonElement toJsonTree(Object object) {
- return GSON.toJsonTree(object);
+ return gson.toJsonTree(object);
}
/**
@@ -264,7 +306,7 @@ public class StandardCoder implements Coder {
* @return a json string representing the object
*/
protected String toJson(Object object) {
- return GSON.toJson(object);
+ return gson.toJson(object);
}
/**
@@ -274,7 +316,7 @@ public class StandardCoder implements Coder {
* @param object object to be encoded
*/
protected void toJson(Writer target, Object object) {
- GSON.toJson(object, object.getClass(), target);
+ gson.toJson(object, object.getClass(), target);
}
/**
@@ -285,7 +327,7 @@ public class StandardCoder implements Coder {
* @return the object represented by the given json element
*/
protected <T> T fromJson(JsonElement json, Class<T> clazz) {
- return convertFromDouble(clazz, GSON.fromJson(json, clazz));
+ return convertFromDouble(clazz, gson.fromJson(json, clazz));
}
/**
@@ -296,7 +338,7 @@ public class StandardCoder implements Coder {
* @return the object represented by the given json string
*/
protected <T> T fromJson(String json, Class<T> clazz) {
- return convertFromDouble(clazz, GSON.fromJson(json, clazz));
+ return convertFromDouble(clazz, gson.fromJson(json, clazz));
}
/**
@@ -307,7 +349,7 @@ public class StandardCoder implements Coder {
* @return the object represented by the given json string
*/
protected <T> T fromJson(Reader source, Class<T> clazz) {
- return convertFromDouble(clazz, GSON.fromJson(source, clazz));
+ return convertFromDouble(clazz, gson.fromJson(source, clazz));
}
/**
@@ -330,20 +372,14 @@ public class StandardCoder implements Coder {
/**
* Adapter for standard objects.
*/
- private static class StandardTypeAdapter extends TypeAdapter<StandardCoderObject> {
+ @AllArgsConstructor
+ protected static class StandardTypeAdapter extends TypeAdapter<StandardCoderObject> {
/**
* Used to read/write a JsonElement.
*/
private static TypeAdapter<JsonElement> elementAdapter = new Gson().getAdapter(JsonElement.class);
- /**
- * Constructs the object.
- */
- public StandardTypeAdapter() {
- super();
- }
-
@Override
public void write(JsonWriter out, StandardCoderObject value) throws IOException {
elementAdapter.write(out, value.getData());