1 package jalview.javascript.json;
3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.io.InputStream;
8 import java.util.ArrayList;
9 import java.util.Hashtable;
10 import java.util.Iterator;
11 import java.util.List;
15 * A rudimentary JSON converter/iterator that uses the browser's native AJAX
16 * json data type delivery mechanism.
18 * Arrays are delivered as ArrayList<Object> where Object may be Boolean,
19 * String, Long, Double, ArrayList, and "Map-like object".
21 * For speed, the maps returned are just JavaScript maps with a few added
22 * methods for extracting data. [get(), contains(), probably should add keySet,
23 * valueSet, and entrySet].
25 * @author hansonr Bob Hanson St. Olaf College 1/24/2019
31 * A privately initialized class that allows us to do the object
32 * conversion from JavaScript to "Java" on the fly.
37 @SuppressWarnings("serial")
38 public static class JSONList extends ArrayList<Object> {
41 static ListIterator iter;
43 JSONList(Object[] a) {
48 * this.elementData = a;
49 * this.size = a.length;
53 public Object get(int i) {
58 * o = this.elementData[i];
61 return JSON.toObject(o);
66 public Iterator<Object> iterator() {
68 iter = new ListIterator();
80 public static class ListIterator implements Iterator<Object> {
82 ListIterator() {/* restricted */}
88 public boolean hasNext() {
89 @SuppressWarnings("unused")
94 * more = this.list && (this.pt < this.list.size);
102 return pt < list.size();
107 public Object next() {
110 * @j2sNative o = this.list.elementData[this.pt++];
120 * A simple encoding of sequential key/value pairs for a jQuery.ajax call. If
121 * the first key is "url" and the second is an object, then the ajax object is
122 * attached to that url as well, just for transport purposes within the system.
124 * @param keyValues assumed to be simple String,Object pairs. String objects
125 * will be surrounded by double quotes.
127 public static Object setAjax(Object... keyValues) {
130 * @j2sNative ajax = {}; if (keyValues[0] == "url" && typeof keyValues[1] ==
131 * "object") { ajax = keyValues[1].ajax || (keyValues[1].ajax = ajax); }
133 * for (var i = 0; i < keyValues.length;) { var key = keyValues[i++];
134 * var val = keyValues[i++]; ajax[key] = val; }
139 public static void setAjax(URL url) {
140 setAjax("url", url, "dataType", "json", "async", Boolean.FALSE);
143 public static BufferedReader getJSONReader(InputStream is) {
144 return new JSONReader(is);
147 public static class JSONReader extends BufferedReader {
150 public JSONReader(InputStream in) {
151 super((Reader) (Object) in);
153 data = toObject(/** @j2sNative $in._ajaxData || $in.$in && $in.$in._ajaxData || */ null);
157 public JSONReader(Reader in) {
160 data = toObject(/** @j2sNative $in._ajaxData || $in.$in && $in.$in._ajaxData || */ null);
163 public JSONReader(String json) {
164 super((Reader) (Object) "");
165 data = toObject(/** @j2sNative swingjs.JSUtil.parseJSON$S(json)|| */null);
169 public void close() {
173 } catch (IOException e) {
174 // ignore, especially if we set $in to a string!
179 @SuppressWarnings("resource")
180 public static Object parse(String json) {
181 return new JSONReader(json).data;
184 public static Object parse(Reader br) {
185 return ((JSONReader) br).data;
189 * Get an object in the JSON associative array.
194 @SuppressWarnings("unused")
195 public static Object toObject(Object o) {
198 String type = /** @j2sNative (typeof o) + */"";
204 if (/** @j2sNative (n = o) == (n|0) || */false)
205 return Long.valueOf((long) n);
206 return Double.valueOf(n);
208 return Boolean.valueOf(/** @j2sNative !!o || */false);
210 boolean isArray = /** @j2sNative o instanceof Array || */false;
212 return toList((Object[]) o);
220 Object get(String key) {
224 * return C$.toObject$O(this[key]);
230 boolean contains(String key) {
234 * return typeof this[key] != "undefined"
241 @SuppressWarnings("unchecked")
242 private static Hashtable<String, Object> toMap(Object map) {
245 * map.get$O = C$.prototype.get$S;
246 * map.contains$O = C$.prototype.contains$S;
248 return (Hashtable<String, Object>) map;
251 private static List<Object> toList(Object[] a) {
252 return new JSONList(a);