ASTERIXDB-1714: Eliminate dependency on org.json

Change-Id: Ie9c5400fd134ae75d43385255af7794e968b1c7e
Reviewed-on: https://asterix-gerrit.ics.uci.edu/1392
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
BAD: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: Till Westmann <tillw@apache.org>
Integration-Tests: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
diff --git a/hyracks-fullstack/hyracks/hyracks-control/hyracks-control-common/pom.xml b/hyracks-fullstack/hyracks/hyracks-control/hyracks-control-common/pom.xml
index 9f7d9dd..1c8ae20 100644
--- a/hyracks-fullstack/hyracks/hyracks-control/hyracks-control-common/pom.xml
+++ b/hyracks-fullstack/hyracks/hyracks-control/hyracks-control-common/pom.xml
@@ -58,11 +58,6 @@
       <version>${project.version}</version>
     </dependency>
     <dependency>
-      <groupId>org.json</groupId>
-      <artifactId>json</artifactId>
-      <version>20090211</version>
-    </dependency>
-    <dependency>
       <groupId>org.apache.httpcomponents</groupId>
       <artifactId>httpcore</artifactId>
       <version>4.4.5</version>
@@ -72,5 +67,9 @@
       <artifactId>httpclient</artifactId>
       <version>4.5.2</version>
     </dependency>
+    <dependency>
+      <groupId>com.fasterxml.jackson.core</groupId>
+      <artifactId>jackson-databind</artifactId>
+    </dependency>
   </dependencies>
 </project>
diff --git a/hyracks-fullstack/hyracks/hyracks-control/hyracks-control-common/src/main/java/org/apache/hyracks/control/common/job/profiling/om/AbstractProfile.java b/hyracks-fullstack/hyracks/hyracks-control/hyracks-control-common/src/main/java/org/apache/hyracks/control/common/job/profiling/om/AbstractProfile.java
index 525be8c..d1d33a5 100644
--- a/hyracks-fullstack/hyracks/hyracks-control/hyracks-control-common/src/main/java/org/apache/hyracks/control/common/job/profiling/om/AbstractProfile.java
+++ b/hyracks-fullstack/hyracks/hyracks-control/hyracks-control-common/src/main/java/org/apache/hyracks/control/common/job/profiling/om/AbstractProfile.java
@@ -26,9 +26,9 @@
 import java.util.Map;
 import java.util.Map.Entry;
 
-import org.json.JSONArray;
-import org.json.JSONException;
-import org.json.JSONObject;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
 
 import org.apache.hyracks.api.io.IWritable;
 
@@ -45,17 +45,18 @@
         return counters;
     }
 
-    public abstract JSONObject toJSON() throws JSONException;
+    public abstract ObjectNode toJSON() ;
 
-    protected void populateCounters(JSONObject jo) throws JSONException {
-        JSONArray countersObj = new JSONArray();
+    protected void populateCounters(ObjectNode jo) {
+        ObjectMapper om = new ObjectMapper();
+        ArrayNode countersObj = om.createArrayNode();
         for (Map.Entry<String, Long> e : counters.entrySet()) {
-            JSONObject jpe = new JSONObject();
+            ObjectNode jpe = om.createObjectNode();
             jpe.put("name", e.getKey());
             jpe.put("value", e.getValue());
-            countersObj.put(jpe);
+            countersObj.add(jpe);
         }
-        jo.put("counters", countersObj);
+        jo.set("counters", countersObj);
     }
 
     protected void merge(AbstractProfile profile) {
diff --git a/hyracks-fullstack/hyracks/hyracks-control/hyracks-control-common/src/main/java/org/apache/hyracks/control/common/job/profiling/om/JobProfile.java b/hyracks-fullstack/hyracks/hyracks-control/hyracks-control-common/src/main/java/org/apache/hyracks/control/common/job/profiling/om/JobProfile.java
index e8fec1b..79a5538 100644
--- a/hyracks-fullstack/hyracks/hyracks-control/hyracks-control-common/src/main/java/org/apache/hyracks/control/common/job/profiling/om/JobProfile.java
+++ b/hyracks-fullstack/hyracks/hyracks-control/hyracks-control-common/src/main/java/org/apache/hyracks/control/common/job/profiling/om/JobProfile.java
@@ -25,10 +25,9 @@
 import java.util.Map;
 import java.util.Map.Entry;
 
-import org.json.JSONArray;
-import org.json.JSONException;
-import org.json.JSONObject;
-
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
 import org.apache.hyracks.api.job.JobId;
 
 public class JobProfile extends AbstractProfile {
@@ -62,16 +61,17 @@
     }
 
     @Override
-    public JSONObject toJSON() throws JSONException {
-        JSONObject json = new JSONObject();
+    public ObjectNode toJSON()  {
+        ObjectMapper om = new ObjectMapper();
+        ObjectNode json = om.createObjectNode();
 
         json.put("job-id", jobId.toString());
         populateCounters(json);
-        JSONArray jobletsArray = new JSONArray();
+        ArrayNode jobletsArray = om.createArrayNode();
         for (JobletProfile p : jobletProfiles.values()) {
-            jobletsArray.put(p.toJSON());
+            jobletsArray.add(p.toJSON());
         }
-        json.put("joblets", jobletsArray);
+        json.set("joblets", jobletsArray);
 
         return json;
     }
diff --git a/hyracks-fullstack/hyracks/hyracks-control/hyracks-control-common/src/main/java/org/apache/hyracks/control/common/job/profiling/om/JobletProfile.java b/hyracks-fullstack/hyracks/hyracks-control/hyracks-control-common/src/main/java/org/apache/hyracks/control/common/job/profiling/om/JobletProfile.java
index 33d5a6c..c3792df 100644
--- a/hyracks-fullstack/hyracks/hyracks-control/hyracks-control-common/src/main/java/org/apache/hyracks/control/common/job/profiling/om/JobletProfile.java
+++ b/hyracks-fullstack/hyracks/hyracks-control/hyracks-control-common/src/main/java/org/apache/hyracks/control/common/job/profiling/om/JobletProfile.java
@@ -25,10 +25,10 @@
 import java.util.Map;
 import java.util.Map.Entry;
 
-import org.json.JSONArray;
-import org.json.JSONException;
-import org.json.JSONObject;
 
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
 import org.apache.hyracks.api.dataflow.TaskAttemptId;
 
 public class JobletProfile extends AbstractProfile {
@@ -62,16 +62,18 @@
     }
 
     @Override
-    public JSONObject toJSON() throws JSONException {
-        JSONObject json = new JSONObject();
+    public ObjectNode toJSON()  {
+
+        ObjectMapper om = new ObjectMapper();
+        ObjectNode json = om.createObjectNode();
 
         json.put("node-id", nodeId.toString());
         populateCounters(json);
-        JSONArray tasks = new JSONArray();
+        ArrayNode tasks = om.createArrayNode();
         for (TaskProfile p : taskProfiles.values()) {
-            tasks.put(p.toJSON());
+            tasks.add(p.toJSON());
         }
-        json.put("tasks", tasks);
+        json.set("tasks", tasks);
 
         return json;
     }
diff --git a/hyracks-fullstack/hyracks/hyracks-control/hyracks-control-common/src/main/java/org/apache/hyracks/control/common/job/profiling/om/TaskProfile.java b/hyracks-fullstack/hyracks/hyracks-control/hyracks-control-common/src/main/java/org/apache/hyracks/control/common/job/profiling/om/TaskProfile.java
index a77c56f..680d2f9 100644
--- a/hyracks-fullstack/hyracks/hyracks-control/hyracks-control-common/src/main/java/org/apache/hyracks/control/common/job/profiling/om/TaskProfile.java
+++ b/hyracks-fullstack/hyracks/hyracks-control/hyracks-control-common/src/main/java/org/apache/hyracks/control/common/job/profiling/om/TaskProfile.java
@@ -25,10 +25,9 @@
 import java.util.Map;
 import java.util.Map.Entry;
 
-import org.json.JSONArray;
-import org.json.JSONException;
-import org.json.JSONObject;
-
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
 import org.apache.hyracks.api.dataflow.TaskAttemptId;
 import org.apache.hyracks.api.partitions.PartitionId;
 import org.apache.hyracks.control.common.job.profiling.counters.MultiResolutionEventProfiler;
@@ -64,39 +63,41 @@
     }
 
     @Override
-    public JSONObject toJSON() throws JSONException {
-        JSONObject json = new JSONObject();
+    public ObjectNode toJSON()  {
+
+        ObjectMapper om = new ObjectMapper();
+        ObjectNode json = om.createObjectNode();
 
         json.put("activity-id", taskAttemptId.getTaskId().getActivityId().toString());
         json.put("partition", taskAttemptId.getTaskId().getPartition());
         json.put("attempt", taskAttemptId.getAttempt());
         if (partitionSendProfile != null) {
-            JSONArray pspArray = new JSONArray();
+            ArrayNode pspArray = om.createArrayNode();
             for (PartitionProfile pp : partitionSendProfile.values()) {
-                JSONObject ppObj = new JSONObject();
+                ObjectNode ppObj = om.createObjectNode();
                 PartitionId pid = pp.getPartitionId();
-                JSONObject pidObj = new JSONObject();
-                pidObj.put("job-id", pid.getJobId());
-                pidObj.put("connector-id", pid.getConnectorDescriptorId());
+                ObjectNode pidObj = om.createObjectNode();
+                pidObj.put("job-id", pid.getJobId().toString());
+                pidObj.put("connector-id", pid.getConnectorDescriptorId().toString());
                 pidObj.put("sender-index", pid.getSenderIndex());
                 pidObj.put("receiver-index", pid.getReceiverIndex());
-                ppObj.put("partition-id", pidObj);
+                ppObj.set("partition-id", pidObj);
                 ppObj.put("open-time", pp.getOpenTime());
                 ppObj.put("close-time", pp.getCloseTime());
                 MultiResolutionEventProfiler samples = pp.getSamples();
                 ppObj.put("offset", samples.getOffset());
                 int resolution = samples.getResolution();
                 int sampleCount = samples.getCount();
-                JSONArray ftA = new JSONArray();
+                ArrayNode ftA = om.createArrayNode();
                 int[] ft = samples.getSamples();
                 for (int i = 0; i < sampleCount; ++i) {
-                    ftA.put(ft[i]);
+                    ftA.add(ft[i]);
                 }
-                ppObj.put("frame-times", ftA);
+                ppObj.set("frame-times", ftA);
                 ppObj.put("resolution", resolution);
-                pspArray.put(ppObj);
+                pspArray.add(ppObj);
             }
-            json.put("partition-send-profile", pspArray);
+            json.set("partition-send-profile", pspArray);
         }
         populateCounters(json);
 
diff --git a/hyracks-fullstack/hyracks/hyracks-control/hyracks-control-common/src/main/java/org/apache/hyracks/control/common/logs/LogFile.java b/hyracks-fullstack/hyracks/hyracks-control/hyracks-control-common/src/main/java/org/apache/hyracks/control/common/logs/LogFile.java
index 8f95c35..c0f88eb 100644
--- a/hyracks-fullstack/hyracks/hyracks-control/hyracks-control-common/src/main/java/org/apache/hyracks/control/common/logs/LogFile.java
+++ b/hyracks-fullstack/hyracks/hyracks-control/hyracks-control-common/src/main/java/org/apache/hyracks/control/common/logs/LogFile.java
@@ -18,12 +18,12 @@
  */
 package org.apache.hyracks.control.common.logs;
 
+import com.fasterxml.jackson.databind.node.ObjectNode;
+
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.PrintWriter;
 
-import org.json.JSONObject;
-
 public class LogFile {
     private final File root;
 
@@ -38,12 +38,12 @@
         openTime = System.currentTimeMillis();
     }
 
-    public synchronized void log(JSONObject object) throws Exception {
+    public synchronized void log(ObjectNode object) throws Exception {
         if (out == null) {
             root.mkdirs();
             out = new PrintWriter(new FileOutputStream(new File(root, openTime + ".log"), true));
         }
-        out.println(object.toString(1));
+        out.println(object.asText());
         out.flush();
     }
 
diff --git a/hyracks-fullstack/hyracks/hyracks-control/hyracks-control-common/src/main/java/org/apache/hyracks/control/common/utils/ThreadDumpHelper.java b/hyracks-fullstack/hyracks/hyracks-control/hyracks-control-common/src/main/java/org/apache/hyracks/control/common/utils/ThreadDumpHelper.java
index eacb9e0..1d6dbcd 100644
--- a/hyracks-fullstack/hyracks/hyracks-control/hyracks-control-common/src/main/java/org/apache/hyracks/control/common/utils/ThreadDumpHelper.java
+++ b/hyracks-fullstack/hyracks/hyracks-control/hyracks-control-common/src/main/java/org/apache/hyracks/control/common/utils/ThreadDumpHelper.java
@@ -18,6 +18,13 @@
  */
 package org.apache.hyracks.control.common.utils;
 
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.SerializationFeature;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+
+import java.io.IOException;
 import java.lang.management.ThreadInfo;
 import java.lang.management.ThreadMXBean;
 import java.util.ArrayList;
@@ -26,15 +33,12 @@
 import java.util.List;
 import java.util.Map;
 
-import org.json.JSONException;
-import org.json.JSONObject;
-
 public class ThreadDumpHelper {
 
     private ThreadDumpHelper() {
     }
 
-    public static String takeDumpJSON(ThreadMXBean threadMXBean) throws JSONException {
+    public static String takeDumpJSON(ThreadMXBean threadMXBean) throws IOException {
         ThreadInfo[] threadInfos = threadMXBean.dumpAllThreads(true, true);
         List<Map<String, Object>> threads = new ArrayList<>();
 
@@ -69,18 +73,20 @@
             }
             threads.add(threadMap);
         }
-        JSONObject json = new JSONObject();
-        json.put("date", new Date());
-        json.put("threads", threads);
+        ObjectMapper om = new ObjectMapper();
+        ObjectNode json = om.createObjectNode();
+        json.put("date", new Date().toString());
+        json.putPOJO("threads", threads);
 
-        long [] deadlockedThreads = threadMXBean.findDeadlockedThreads();
-        long [] monitorDeadlockedThreads = threadMXBean.findMonitorDeadlockedThreads();
+        long[] deadlockedThreads = threadMXBean.findDeadlockedThreads();
+        long[] monitorDeadlockedThreads = threadMXBean.findMonitorDeadlockedThreads();
         if (deadlockedThreads != null && deadlockedThreads.length > 0) {
-            json.put("deadlocked_thread_ids", deadlockedThreads);
+            json.putPOJO("deadlocked_thread_ids", deadlockedThreads);
         }
         if (monitorDeadlockedThreads != null && monitorDeadlockedThreads.length > 0) {
-            json.put("monitor_deadlocked_thread_ids", monitorDeadlockedThreads);
+            json.putPOJO("monitor_deadlocked_thread_ids", monitorDeadlockedThreads);
         }
-        return json.toString();
+        om.enable(SerializationFeature.INDENT_OUTPUT);
+        return om.writerWithDefaultPrettyPrinter().writeValueAsString(json);
     }
 }