j2sNative clean-up. Fixes problem with second try of sequence fetching
[jalview.git] / src / jalview / util / JSONUtils.java
1 package jalview.util;
2
3 import java.io.IOException;
4 import java.io.Reader;
5 import java.util.List;
6
7 import org.json.simple.parser.ParseException;
8
9 public class JSONUtils
10 {
11
12   /**
13    * Converts a JSONArray of values to a string as a comma-separated list.
14    * Answers null if the array is null or empty.
15    * 
16    * @param jsonArray
17    * @return
18    */
19   public static String arrayToStringList(List<Object> jsonArray)
20   {
21     int n;
22
23     if (jsonArray == null || (n = jsonArray.size()) == 0)
24     {
25       return null;
26     }
27
28     /**
29      * BH TODO to Platform?
30      * 
31      * @j2sNative
32      * 
33      *            return jsonArray.elementData.slice(0, n).join(",");
34      */
35     {
36       StringBuilder sb = new StringBuilder();
37       for (int i = 0; i < n; i++)
38       {
39         if (i > 0)
40         {
41           sb.append(",");
42         }
43         sb.append(jsonArray.get(i).toString());
44       }
45       return sb.toString();
46     }
47   }
48
49   /**
50    * The method all JSON parsing must go through for JavaScript.
51    * 
52    * @param r
53    *          a BufferedReader or a javascript.json.JSON.JSONReader
54    * @return
55    * @throws IOException
56    * @throws ParseException
57    */
58   public static Object parse(Reader r) throws IOException, ParseException
59   {
60     return Platform.parseJSON(r);
61   }
62
63   public static Object parse(String json) throws ParseException
64   {
65     return Platform.parseJSON(json);
66   }
67
68   public static String stringify(Object obj)
69   {
70     return new org.json.JSONObject(obj).toString();
71   }
72
73 }