JAL-3143 comma-delimited feature attributes for alleles,
[jalview.git] / src / jalview / util / JSONUtils.java
diff --git a/src/jalview/util/JSONUtils.java b/src/jalview/util/JSONUtils.java
new file mode 100644 (file)
index 0000000..cdfc88e
--- /dev/null
@@ -0,0 +1,34 @@
+package jalview.util;
+
+import org.json.simple.JSONArray;
+
+public class JSONUtils
+{
+
+  /**
+   * Converts a JSONArray of values to a string as a comma-separated list.
+   * Answers null if the array is null or empty.
+   * 
+   * @param jsonArray
+   * @return
+   */
+  public static String arrayToList(JSONArray jsonArray)
+  {
+    if (jsonArray == null)
+    {
+      return null;
+    }
+
+    StringBuilder sb = new StringBuilder();
+    for (int i = 0; i < jsonArray.size(); i++)
+    {
+      if (i > 0)
+      {
+        sb.append(",");
+      }
+      sb.append(jsonArray.get(i).toString());
+    }
+    return sb.length() == 0 ? null : sb.toString();
+  }
+
+}