X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fjavascript%2Fjson%2FJSON.java;h=30e333647630f1765e92326dac42c7a6e831cc01;hb=d68d427703088222c2ca6f5968affd5932143eaf;hp=6cb7cb5ccc977a9e0d679a2668614cbb6ec3ed5c;hpb=6f1a50917d60504ae3eb46377d3020271accacaf;p=jalview.git diff --git a/src/jalview/javascript/json/JSON.java b/src/jalview/javascript/json/JSON.java index 6cb7cb5..30e3336 100644 --- a/src/jalview/javascript/json/JSON.java +++ b/src/jalview/javascript/json/JSON.java @@ -1,14 +1,28 @@ +/* + * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$) + * Copyright (C) $$Year-Rel$$ The Jalview Authors + * + * This file is part of Jalview. + * + * Jalview is free software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 + * of the License, or (at your option) any later version. + * + * Jalview is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Jalview. If not, see . + * The Jalview Authors are detailed in the 'AUTHORS' file. + */ package jalview.javascript.json; import java.io.BufferedReader; -import java.io.IOException; import java.io.InputStream; -import java.io.Reader; import java.net.URL; -import java.util.ArrayList; -import java.util.Hashtable; -import java.util.Iterator; -import java.util.List; /** * @@ -25,231 +39,53 @@ import java.util.List; * @author hansonr Bob Hanson St. Olaf College 1/24/2019 * */ -public class JSON { - - /** - * A privately initialized class that allows us to do the object - * conversion from JavaScript to "Java" on the fly. - * - * @author hansonr - * - */ - @SuppressWarnings("serial") - public static class JSONList extends ArrayList { - - - static ListIterator iter; - - JSONList(Object[] a) { - super(); - /** - * @j2sNative - * - * this.elementData = a; - * this.size = a.length; - */ - } - - public Object get(int i) { - Object o = null; - /** - * @j2sNative - * - * o = this.elementData[i]; - * - */ - return JSON.toObject(o); - } - - - @Override - public Iterator iterator() { - if (iter == null) - iter = new ListIterator(); - iter.pt = 0; - iter.list = this; - return iter; - } - - - /** - * - * @author hansonr - * - */ - public static class ListIterator implements Iterator { - - ListIterator() {/* restricted */} - - public JSONList list; - int pt = -1; - - @Override - public boolean hasNext() { - @SuppressWarnings("unused") - boolean more; - /** - * @j2sNative - * - * more = this.list && (this.pt < this.list.size); - * if (!more) { - * this.list = null; - * this.pt = -1; - * } - * return more; - */ - { - return pt < list.size(); - } - } - - @Override - public Object next() { - Object o = null; - /** - * @j2sNative o = this.list.elementData[this.pt++]; - * - */ - - return toObject(o); - } - } - } - - /** - * A simple encoding of sequential key/value pairs for a jQuery.ajax call. If - * the first key is "url" and the second is an object, then the ajax object is - * attached to that url as well, just for transport purposes within the system. - * - * @param keyValues assumed to be simple String,Object pairs. String objects - * will be surrounded by double quotes. - */ - public static Object setAjax(Object... keyValues) { - Object ajax = null; - /** - * @j2sNative ajax = {}; if (keyValues[0] == "url" && typeof keyValues[1] == - * "object") { ajax = keyValues[1].ajax || (keyValues[1].ajax = ajax); } - * - * for (var i = 0; i < keyValues.length;) { var key = keyValues[i++]; - * var val = keyValues[i++]; ajax[key] = val; } - */ - return ajax; - } - - public static void setAjax(URL url) { - setAjax("url", url, "dataType", "json", "async", Boolean.FALSE); - } - - public static BufferedReader getJSONReader(InputStream is) { - return new JSONReader(is); - } - - public static class JSONReader extends BufferedReader { - Object data; - - public JSONReader(InputStream in) { - super((Reader) (Object) in); - // could be buffered - data = toObject(/** @j2sNative $in._ajaxData || $in.$in && $in.$in._ajaxData || */ null); - - } - - public JSONReader(Reader in) { - super(in); - // could be buffered - data = toObject(/** @j2sNative $in._ajaxData || $in.$in && $in.$in._ajaxData || */ null); - } - - public JSONReader(String json) { - super((Reader) (Object) ""); - data = toObject(/** @j2sNative swingjs.JSUtil.parseJSON$S(json)|| */null); - } - - @Override - public void close() { - data = null; - try { - super.close(); - } catch (IOException e) { - // ignore, especially if we set $in to a string! - } - } - } - - @SuppressWarnings("resource") - public static Object parse(String json) { - return new JSONReader(json).data; - } - - public static Object parse(Reader br) { - return ((JSONReader) br).data; - } - - /** - * Get an object in the JSON associative array. - * @param br - * @param key - * @return - */ - @SuppressWarnings("unused") - public static Object toObject(Object o) { - if (o == null) - return null; - String type = /** @j2sNative (typeof o) + */""; - switch (type) { - case "string": - return o; - case "number": - double n = 0; - if (/** @j2sNative (n = o) == (n|0) || */false) - return Long.valueOf((long) n); - return Double.valueOf(n); - case "boolean": - return Boolean.valueOf(/** @j2sNative !!o || */false); - case "object": - boolean isArray = /** @j2sNative o instanceof Array || */false; - if (isArray) { - return toList((Object[]) o); - } - return toMap(o); - default: - return o; - } - } - - Object get(String key) { - /** - * @j2sNative - * - * return C$.toObject$O(this[key]); - */ - { - return null; - } - } - boolean contains(String key) { - /** - * @j2sNative - * - * return typeof this[key] != "undefined" - */ - { - return false; - } - } - - @SuppressWarnings("unchecked") - private static Hashtable toMap(Object map) { - /** - * @j2sNative - * map.get$O = C$.prototype.get$S; - * map.contains$O = C$.prototype.contains$S; - */ - return (Hashtable) map; - } - - private static List toList(Object[] a) { - return new JSONList(a); - } +public class JSON +{ + + /** + * A simple encoding of sequential key/value pairs for a jQuery.ajax call. If + * the first key is "url" and the second is an object, then the ajax object is + * attached to that url as well, just for transport purposes within the + * system. + * + * @param keyValues + * assumed to be simple String,Object pairs. String objects will be + * surrounded by double quotes. + */ + @SuppressWarnings("static-access") + public static Object setAjax(Object... keyValues) + { + return /** @j2sNative swingjs.JSUtil.setAjax$OA(keyValues) || */ + null; + } + + public static void setAjax(URL url) + { + /** @j2sNative swingjs.JSUtil.setAjax$java_net_URL(url); */ + } + + public static BufferedReader getJSONReader(InputStream is) + { + return /** @j2sNative swingjs.JSUtil.getJSONReader$O(is) || */ + null; + } + + /** + * + * @param obj + * as String, Reader, InputStream, or JavaScript Object or Array + * @return Map or List + */ + public static Object parse(Object obj) + { + return /** @j2sNative swingjs.JSUtil.parseJSON$O(obj) || */ + null; + } + + public static String stringify(Object obj) + { + return /** @j2sNative swingjs.JSUtil.stringifyJSON$O(obj) || */ + null; + } }