[NO ISSUE] JSONUtil minor improvement
Avoid intermediate string representation when writing converted node.
Change-Id: I7795890eec2d65ad9a286dd1a11713b350a1bdc5
Reviewed-on: https://asterix-gerrit.ics.uci.edu/2041
Sonar-Qube: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: Xikui Wang <xkkwww@gmail.com>
Contrib: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Integration-Tests: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: Ian Maxon <imaxon@apache.org>
diff --git a/asterixdb/asterix-app/src/main/java/org/apache/asterix/api/http/server/ClusterApiServlet.java b/asterixdb/asterix-app/src/main/java/org/apache/asterix/api/http/server/ClusterApiServlet.java
index 3f065ee..165c104 100644
--- a/asterixdb/asterix-app/src/main/java/org/apache/asterix/api/http/server/ClusterApiServlet.java
+++ b/asterixdb/asterix-app/src/main/java/org/apache/asterix/api/http/server/ClusterApiServlet.java
@@ -80,7 +80,7 @@
default:
throw new IllegalArgumentException();
}
- responseWriter.write(JSONUtil.convertNode(json));
+ JSONUtil.writeNode(responseWriter, json);
} catch (IllegalArgumentException e) { // NOSONAR - exception not logged or rethrown
response.setStatus(HttpResponseStatus.NOT_FOUND);
} catch (Exception e) {
diff --git a/asterixdb/asterix-app/src/main/java/org/apache/asterix/api/http/server/DiagnosticsApiServlet.java b/asterixdb/asterix-app/src/main/java/org/apache/asterix/api/http/server/DiagnosticsApiServlet.java
index 3c58f30..3fe591f 100644
--- a/asterixdb/asterix-app/src/main/java/org/apache/asterix/api/http/server/DiagnosticsApiServlet.java
+++ b/asterixdb/asterix-app/src/main/java/org/apache/asterix/api/http/server/DiagnosticsApiServlet.java
@@ -67,7 +67,7 @@
if (!"".equals(localPath(request))) {
throw new IllegalArgumentException();
}
- responseWriter.write(JSONUtil.convertNode(getClusterDiagnosticsJSON()));
+ JSONUtil.writeNode(responseWriter, getClusterDiagnosticsJSON());
} catch (IllegalStateException e) { // NOSONAR - exception not logged or rethrown
response.setStatus(HttpResponseStatus.SERVICE_UNAVAILABLE);
} catch (IllegalArgumentException e) { // NOSONAR - exception not logged or rethrown
diff --git a/asterixdb/asterix-app/src/main/java/org/apache/asterix/api/http/server/ShutdownApiServlet.java b/asterixdb/asterix-app/src/main/java/org/apache/asterix/api/http/server/ShutdownApiServlet.java
index 1731697..ddd0f1f 100644
--- a/asterixdb/asterix-app/src/main/java/org/apache/asterix/api/http/server/ShutdownApiServlet.java
+++ b/asterixdb/asterix-app/src/main/java/org/apache/asterix/api/http/server/ShutdownApiServlet.java
@@ -94,7 +94,7 @@
}
jsonObject.set("cluster", clusterState);
final PrintWriter writer = response.writer();
- writer.print(JSONUtil.convertNode(jsonObject));
+ JSONUtil.writeNode(writer, jsonObject);
// accept no further queries once this servlet returns
csm.setState(SHUTTING_DOWN);
writer.close();
diff --git a/hyracks-fullstack/hyracks/hyracks-util/src/main/java/org/apache/hyracks/util/JSONUtil.java b/hyracks-fullstack/hyracks/hyracks-util/src/main/java/org/apache/hyracks/util/JSONUtil.java
index 51855c6..dcdf140 100644
--- a/hyracks-fullstack/hyracks/hyracks-util/src/main/java/org/apache/hyracks/util/JSONUtil.java
+++ b/hyracks-fullstack/hyracks/hyracks-util/src/main/java/org/apache/hyracks/util/JSONUtil.java
@@ -19,6 +19,7 @@
package org.apache.hyracks.util;
import java.io.IOException;
+import java.io.Writer;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
@@ -28,6 +29,7 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.SerializationFeature;
public class JSONUtil {
@@ -37,6 +39,7 @@
private static final String INDENT = "\t";
private static final ObjectMapper SORTED_MAPPER = new ObjectMapper();
+ private static final ObjectWriter PRETTY_SORTED_WRITER;
private JSONUtil() {
}
@@ -44,11 +47,15 @@
static {
SORTED_MAPPER.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
SORTED_MAPPER.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);
+ PRETTY_SORTED_WRITER = SORTED_MAPPER.writerWithDefaultPrettyPrinter();
}
public static String convertNode(final JsonNode node) throws JsonProcessingException {
- final Object obj = SORTED_MAPPER.treeToValue(node, Object.class);
- return SORTED_MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
+ return PRETTY_SORTED_WRITER.writeValueAsString(SORTED_MAPPER.treeToValue(node, Object.class));
+ }
+
+ public static void writeNode(final Writer writer, final JsonNode node) throws IOException {
+ PRETTY_SORTED_WRITER.writeValue(writer, SORTED_MAPPER.treeToValue(node, Object.class));
}
public static String indent(String str, int initialIndent) {