3 import jalview.javascript.json.JSON;
4 import jalview.json.binding.biojson.v1.AlignmentPojo;
6 import java.io.FileReader;
7 import java.io.IOException;
11 import org.json.simple.parser.JSONParser;
12 import org.json.simple.parser.ParseException;
14 public class JSONUtils
18 * Converts a JSONArray of values to a string as a comma-separated list.
19 * Answers null if the array is null or empty.
24 public static String arrayToStringList(List<Object> jsonArray)
28 if (jsonArray == null || (n = jsonArray.size()) == 0)
36 * return jsonArray.elementData.slice(0, n).join(",");
39 StringBuilder sb = new StringBuilder();
40 for (int i = 0; i < n; i++)
46 sb.append(jsonArray.get(i).toString());
53 * The method all JSON parsing must go through for JavaScript.
54 * @param r a BufferedReader or a javascript.json.JSON.JSONReader
57 * @throws ParseException
59 public static Object parse(Reader r) throws IOException, ParseException
61 // Using a file reader is not currently supported in SwingJS JavaScript
66 // Platform.timeCheck("JSONUtils.parse0 ", Platform.TIME_MARK);
71 if (r instanceof FileReader)
73 throw new IOException("StringJS does not support FileReader parsing for JSON -- but it could...");
77 ret = new JSONParser().parse(r);
79 // Platform.timeCheck("JSONUtils.parse1 ", Platform.TIME_MARK);
83 public static Object parse(String json) throws ParseException
85 return (Platform.isJS() ? JSON.parse(json) : new JSONParser().parse(json));
88 public static String stringify(Object obj) {
89 return new org.json.JSONObject(obj).toString();