summaryrefslogtreecommitdiffstats
path: root/utils/src/main
diff options
context:
space:
mode:
authorJim Hahn <jrh3@att.com>2020-01-31 11:15:44 -0500
committerJim Hahn <jrh3@att.com>2020-01-31 13:11:22 -0500
commit4f909d754b638ab4563eb9466836cc2d7a184d63 (patch)
treea4cbe8aa18aef9620731c1c86508a27bff567c90 /utils/src/main
parentb2899115452784712443429704d5956075e95557 (diff)
Add pretty() method to Coder
"Pretty" JSON is often needed, so added Coder.pretty() to facilitate that. Incorporated review comment(s): - changed pretty() to encode(object, pretty) Issue-ID: POLICY-1625 Signed-off-by: Jim Hahn <jrh3@att.com> Change-Id: I180fa6de416836008bf1c410132ae30f4dde9271
Diffstat (limited to 'utils/src/main')
-rw-r--r--utils/src/main/java/org/onap/policy/common/utils/coder/Coder.java14
-rw-r--r--utils/src/main/java/org/onap/policy/common/utils/coder/StandardCoder.java40
-rw-r--r--utils/src/main/java/org/onap/policy/common/utils/coder/StandardValCoder.java13
-rw-r--r--utils/src/main/java/org/onap/policy/common/utils/coder/StandardYamlCoder.java8
4 files changed, 67 insertions, 8 deletions
diff --git a/utils/src/main/java/org/onap/policy/common/utils/coder/Coder.java b/utils/src/main/java/org/onap/policy/common/utils/coder/Coder.java
index bb51f2b9..ec0e5e42 100644
--- a/utils/src/main/java/org/onap/policy/common/utils/coder/Coder.java
+++ b/utils/src/main/java/org/onap/policy/common/utils/coder/Coder.java
@@ -1,8 +1,8 @@
/*
* ============LICENSE_START=======================================================
- * ONAP PAP
+ * ONAP
* ================================================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019-2020 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,6 +41,16 @@ public interface Coder {
String encode(Object object) throws CoderException;
/**
+ * Encodes an object into json, optionally making it "pretty".
+ *
+ * @param object object to be encoded
+ * @param pretty {@code true} if it should be encoded as "pretty" json, {@code false} otherwise
+ * @return a json string representing the object
+ * @throws CoderException if an error occurs
+ */
+ String encode(Object object, boolean pretty) throws CoderException;
+
+ /**
* Encodes an object into json, writing to the given target.
*
* @param target target to which to write the encoded json
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 6d0cbc9d..13973f1c 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
@@ -54,9 +54,21 @@ public class StandardCoder implements Coder {
* Gson object used to encode and decode messages.
*/
@Getter(AccessLevel.PROTECTED)
- private static final Gson GSON = GsonMessageBodyHandler.configBuilder(
- new GsonBuilder().registerTypeAdapter(StandardCoderObject.class, new StandardTypeAdapter()))
- .create();
+ private static final Gson GSON;
+
+ /**
+ * Gson object used to encode messages in "pretty" format.
+ */
+ @Getter(AccessLevel.PROTECTED)
+ private static final Gson GSON_PRETTY;
+
+ static {
+ GsonBuilder builder = GsonMessageBodyHandler.configBuilder(
+ new GsonBuilder().registerTypeAdapter(StandardCoderObject.class, new StandardTypeAdapter()));
+
+ GSON = builder.create();
+ GSON_PRETTY = builder.setPrettyPrinting().create();
+ }
/**
* Constructs the object.
@@ -67,8 +79,18 @@ public class StandardCoder implements Coder {
@Override
public String encode(Object object) throws CoderException {
+ return encode(object, false);
+ }
+
+ @Override
+ public String encode(Object object, boolean pretty) throws CoderException {
try {
- return toJson(object);
+ if (pretty) {
+ return toPrettyJson(object);
+
+ } else {
+ return toJson(object);
+ }
} catch (RuntimeException e) {
throw new CoderException(e);
@@ -151,6 +173,16 @@ public class StandardCoder implements Coder {
}
}
+ /**
+ * Encodes the object as "pretty" json.
+ *
+ * @param object object to be encoded
+ * @return the encoded object
+ */
+ protected String toPrettyJson(Object object) {
+ return GSON_PRETTY.toJson(object);
+ }
+
@Override
public StandardCoderObject toStandard(Object object) throws CoderException {
try {
diff --git a/utils/src/main/java/org/onap/policy/common/utils/coder/StandardValCoder.java b/utils/src/main/java/org/onap/policy/common/utils/coder/StandardValCoder.java
index 378254b8..6e08e722 100644
--- a/utils/src/main/java/org/onap/policy/common/utils/coder/StandardValCoder.java
+++ b/utils/src/main/java/org/onap/policy/common/utils/coder/StandardValCoder.java
@@ -62,10 +62,21 @@ public class StandardValCoder extends StandardCoder {
}
@Override
+ protected String toPrettyJson(Object object) {
+ /*
+ * The validator strips off the "pretty" stuff (i.e., spaces), thus we have to validate
+ * and generate the pretty JSON in separate steps.
+ */
+ getGSON().toJson(object, object.getClass(), validatorApi.createJsonWriter(validator, new StringWriter()));
+
+ return super.toPrettyJson(object);
+ }
+
+ @Override
protected String toJson(@NonNull Object object) {
StringWriter output = new StringWriter();
toJson(output, object);
- return String.valueOf(output);
+ return output.toString();
}
@Override
diff --git a/utils/src/main/java/org/onap/policy/common/utils/coder/StandardYamlCoder.java b/utils/src/main/java/org/onap/policy/common/utils/coder/StandardYamlCoder.java
index 36f15b96..1bcf6ac0 100644
--- a/utils/src/main/java/org/onap/policy/common/utils/coder/StandardYamlCoder.java
+++ b/utils/src/main/java/org/onap/policy/common/utils/coder/StandardYamlCoder.java
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* ONAP
* ================================================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019-2020 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.
@@ -38,6 +38,12 @@ public class StandardYamlCoder extends StandardCoder {
}
@Override
+ protected String toPrettyJson(Object object) {
+ // YAML is already "pretty"
+ return toJson(object);
+ }
+
+ @Override
protected String toJson(Object object) {
return translator.toYaml(object);
}