3 import jalview.javascript.json.JSON;
5 import java.io.FileReader;
6 import java.io.IOException;
10 import org.json.simple.parser.JSONParser;
11 import org.json.simple.parser.ParseException;
13 public class JSONUtils
17 * Converts a JSONArray of values to a string as a comma-separated list.
18 * Answers null if the array is null or empty.
23 public static String arrayToStringList(List<Object> jsonArray)
27 if (jsonArray == null || (n = jsonArray.size()) == 0)
35 * return jsonArray.elementData.slice(0, n).join(",");
38 StringBuilder sb = new StringBuilder();
39 for (int i = 0; i < n; i++)
45 sb.append(jsonArray.get(i).toString());
52 * The method all JSON parsing must go through for JavaScript.
53 * @param r a BufferedReader or a javascript.json.JSON.JSONReader
56 * @throws ParseException
58 public static Object parse(Reader r) throws IOException, ParseException
60 // Using a file reader is not currently supported in SwingJS JavaScript
65 // Platform.timeCheck("JSONUtils.parse0 ", Platform.TIME_MARK);
70 if (r instanceof FileReader)
72 throw new IOException("StringJS does not support FileReader parsing for JSON -- but it could...");
76 ret = new JSONParser().parse(r);
78 // Platform.timeCheck("JSONUtils.parse1 ", Platform.TIME_MARK);
82 public static Object parse(String json) throws ParseException
84 return (Platform.isJS() ? JSON.parse(json) : new JSONParser().parse(json));