From: hansonr Date: Fri, 25 Jan 2019 09:24:37 +0000 (-0600) Subject: JSON refactoring X-Git-Tag: Release_2_11_4_0~45^2~18^2~291 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=8e7cf85a7f61f425e808cac53ead7bc27e402242;p=jalview.git JSON refactoring --- diff --git a/src/jalview/analysis/AlignmentUtils.java b/src/jalview/analysis/AlignmentUtils.java index 14096cf..4f8897a 100644 --- a/src/jalview/analysis/AlignmentUtils.java +++ b/src/jalview/analysis/AlignmentUtils.java @@ -2320,10 +2320,14 @@ public class AlignmentUtils int phase = 0; try { - phase = Integer.parseInt(sf.getPhase()); + String s = sf.getPhase(); + if (s != null) + { + phase = Integer.parseInt(s); + } } catch (NumberFormatException e) { - // ignore + // SwingJS -- need to avoid these. } /* * phase > 0 on first codon means 5' incomplete - skip to the start diff --git a/src/jalview/ext/ensembl/EnsemblFeatures.java b/src/jalview/ext/ensembl/EnsemblFeatures.java index 9a45dda..6a0d67c 100644 --- a/src/jalview/ext/ensembl/EnsemblFeatures.java +++ b/src/jalview/ext/ensembl/EnsemblFeatures.java @@ -35,10 +35,8 @@ import java.net.URL; import java.util.ArrayList; import java.util.Iterator; import java.util.List; +import java.util.Map; -import org.json.simple.JSONArray; -import org.json.simple.JSONObject; -import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; /** @@ -94,13 +92,9 @@ class EnsemblFeatures extends EnsemblRestClient // TODO: use a vararg String... for getSequenceRecords instead? List queries = new ArrayList<>(); queries.add(query); - BufferedReader fp = getSequenceReader(queries); - if (fp == null) - { - return null; - } - - SequenceI seq = parseFeaturesJson(fp); + SequenceI seq = parseFeaturesJson(queries); + if (seq == null) + return null; return new Alignment(new SequenceI[] { seq }); } @@ -111,30 +105,34 @@ class EnsemblFeatures extends EnsemblRestClient * @param br * @return */ - private SequenceI parseFeaturesJson(BufferedReader br) + @SuppressWarnings("unchecked") +private SequenceI parseFeaturesJson(List queries) { + + SequenceI seq = new Sequence("Dummy", ""); - JSONParser jp = new JSONParser(); try { - JSONArray responses = (JSONArray) jp.parse(br); - Iterator rvals = responses.iterator(); + + Iterator rvals = (Iterator) getJSON(null, queries, -1, MODE_ITERATOR, null); + if (rvals == null) + return null; while (rvals.hasNext()) { try { - JSONObject obj = (JSONObject) rvals.next(); + Map obj = (Map) rvals.next(); String type = obj.get("feature_type").toString(); int start = Integer.parseInt(obj.get("start").toString()); int end = Integer.parseInt(obj.get("end").toString()); String source = obj.get("source").toString(); String strand = obj.get("strand").toString(); String alleles = JSONUtils - .arrayToList((JSONArray) obj.get("alleles")); + .arrayToStringList((List) obj.get("alleles")); String clinSig = JSONUtils - .arrayToList( - (JSONArray) obj.get("clinical_significance")); + .arrayToStringList( + (List) obj.get("clinical_significance")); /* * convert 'variation' to 'sequence_variant', and 'cds' to 'CDS' @@ -169,20 +167,22 @@ class EnsemblFeatures extends EnsemblRestClient } } catch (ParseException | IOException e) { + e.printStackTrace(); // ignore } return seq; } - /** + +/** * Returns the first non-null attribute found (if any) as a string * * @param obj * @param keys * @return */ - protected String getFirstNotNull(JSONObject obj, String... keys) + protected String getFirstNotNull(Map obj, String... keys) { String desc = null; @@ -209,7 +209,7 @@ class EnsemblFeatures extends EnsemblRestClient * @param obj * @param key */ - protected void setFeatureAttribute(SequenceFeature sf, JSONObject obj, + protected void setFeatureAttribute(SequenceFeature sf, Map obj, String key) { Object object = obj.get(key); diff --git a/src/jalview/ext/ensembl/EnsemblInfo.java b/src/jalview/ext/ensembl/EnsemblInfo.java index fa24f1e..97a8e74 100644 --- a/src/jalview/ext/ensembl/EnsemblInfo.java +++ b/src/jalview/ext/ensembl/EnsemblInfo.java @@ -2,6 +2,7 @@ package jalview.ext.ensembl; import jalview.datamodel.AlignmentI; import jalview.datamodel.DBRefSource; +import jalview.util.JSONUtils; import java.io.BufferedReader; import java.io.IOException; @@ -13,8 +14,6 @@ import java.util.List; import java.util.Map; import java.util.Set; -import org.json.simple.JSONArray; -import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; public class EnsemblInfo extends EnsemblRestClient @@ -90,53 +89,16 @@ public class EnsemblInfo extends EnsemblRestClient * for convenience, pre-fill ensembl.org as the domain for "ENSEMBL" */ divisions.put(DBRefSource.ENSEMBL.toUpperCase(), ensemblDomain); - - BufferedReader br = null; try { - URL url = getDivisionsUrl(ensemblGenomesDomain); - if (url != null) - { - br = getHttpResponse(url, null); - } - parseResponse(br, ensemblGenomesDomain); - } catch (IOException e) - { - // ignore - } finally - { - if (br != null) - { - try - { - br.close(); - } catch (IOException e) - { - // ignore - } - } - } - } - - /** - * Parses the JSON response to /info/divisions, and add each to the lookup map - * - * @param br - * @param domain - */ - void parseResponse(BufferedReader br, String domain) - { - JSONParser jp = new JSONParser(); - - try - { - JSONArray parsed = (JSONArray) jp.parse(br); - - Iterator rvals = parsed.iterator(); + @SuppressWarnings("unchecked") + Iterator rvals = (Iterator) getJSON(getDivisionsUrl(ensemblGenomesDomain), null, -1, MODE_ITERATOR, null); + if (rvals == null) + return; while (rvals.hasNext()) { String division = rvals.next().toString(); - divisions.put(division.toUpperCase(), domain); + divisions.put(division.toUpperCase(), ensemblGenomesDomain); } } catch (IOException | ParseException | NumberFormatException e) { diff --git a/src/jalview/ext/ensembl/EnsemblLookup.java b/src/jalview/ext/ensembl/EnsemblLookup.java index c6b794a..fc37b8a 100644 --- a/src/jalview/ext/ensembl/EnsemblLookup.java +++ b/src/jalview/ext/ensembl/EnsemblLookup.java @@ -25,16 +25,14 @@ import jalview.datamodel.AlignmentI; import jalview.datamodel.GeneLociI; import jalview.util.MapList; -import java.io.BufferedReader; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.util.Arrays; import java.util.Collections; import java.util.List; +import java.util.Map; -import org.json.simple.JSONObject; -import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; /** @@ -151,7 +149,7 @@ public class EnsemblLookup extends EnsemblRestClient * @param br * @return */ - protected String parseGeneId(JSONObject val) + protected String parseGeneId(Map val) { if (val == null) { @@ -189,7 +187,7 @@ public class EnsemblLookup extends EnsemblRestClient public String getSpecies(String identifier) { String species = null; - JSONObject json = getResult(identifier, null); + Map json = getResult(identifier, null); if (json != null) { Object o = json.get(SPECIES); @@ -202,7 +200,7 @@ public class EnsemblLookup extends EnsemblRestClient } /** - * Calls the /lookup/id rest service and returns the response as a JSONObject, + * Calls the /lookup/id rest service and returns the response as a Map, * or null if any error * * @param identifier @@ -210,37 +208,20 @@ public class EnsemblLookup extends EnsemblRestClient * (optional) * @return */ - protected JSONObject getResult(String identifier, String objectType) + @SuppressWarnings("unchecked") + protected Map getResult(String identifier, String objectType) { List ids = Arrays.asList(new String[] { identifier }); - BufferedReader br = null; try { - URL url = getUrl(identifier, objectType); - - if (url != null) - { - br = getHttpResponse(url, ids); - } - return br == null ? null : (JSONObject) (new JSONParser().parse(br)); - } catch (IOException | ParseException e) + return (Map) getJSON(getUrl(identifier, objectType), ids, -1, MODE_MAP, null); + } + catch (IOException | ParseException e) { System.err.println("Error parsing " + identifier + " lookup response " + e.getMessage()); return null; - } finally - { - if (br != null) - { - try - { - br.close(); - } catch (IOException e) - { - // ignore - } - } } } @@ -264,7 +245,7 @@ public class EnsemblLookup extends EnsemblRestClient * @param json * @return */ - GeneLociI parseGeneLoci(JSONObject json) + GeneLociI parseGeneLoci(Map json) { if (json == null) { diff --git a/src/jalview/ext/ensembl/EnsemblMap.java b/src/jalview/ext/ensembl/EnsemblMap.java index f01bd4f..add71b3 100644 --- a/src/jalview/ext/ensembl/EnsemblMap.java +++ b/src/jalview/ext/ensembl/EnsemblMap.java @@ -3,6 +3,7 @@ package jalview.ext.ensembl; import jalview.datamodel.AlignmentI; import jalview.datamodel.DBRefSource; import jalview.datamodel.GeneLociI; +import jalview.util.JSONUtils; import jalview.util.MapList; import java.io.BufferedReader; @@ -13,10 +14,8 @@ import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; +import java.util.Map; -import org.json.simple.JSONArray; -import org.json.simple.JSONObject; -import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; public class EnsemblMap extends EnsemblRestClient @@ -119,30 +118,15 @@ public class EnsemblMap extends EnsemblRestClient String fromRef, String toRef, int[] queryRange) { URL url = null; - BufferedReader br = null; - try { url = getAssemblyMapUrl(species, chromosome, fromRef, toRef, queryRange[0], queryRange[1]); - br = getHttpResponse(url, null); - return (parseAssemblyMappingResponse(br)); + return (parseAssemblyMappingResponse(url)); } catch (Throwable t) { System.out.println("Error calling " + url + ": " + t.getMessage()); return null; - } finally - { - if (br != null) - { - try - { - br.close(); - } catch (IOException e) - { - // ignore - } - } } } @@ -161,22 +145,21 @@ public class EnsemblMap extends EnsemblRestClient * @param br * @return */ - protected int[] parseAssemblyMappingResponse(BufferedReader br) + @SuppressWarnings("unchecked") + protected int[] parseAssemblyMappingResponse(URL url) { int[] result = null; - JSONParser jp = new JSONParser(); try { - JSONObject parsed = (JSONObject) jp.parse(br); - JSONArray mappings = (JSONArray) parsed.get(MAPPINGS); - - Iterator rvals = mappings.iterator(); + Iterator rvals = (Iterator) getJSON(url, null, -1, MODE_ITERATOR, MAPPINGS); + if (rvals == null) + return null; while (rvals.hasNext()) { // todo check for "mapped" - JSONObject val = (JSONObject) rvals.next(); - JSONObject mapped = (JSONObject) val.get(MAPPED); + Map val = (Map) rvals.next(); + Map mapped = (Map) val.get(MAPPED); int start = Integer.parseInt(mapped.get("start").toString()); int end = Integer.parseInt(mapped.get("end").toString()); String strand = mapped.get("strand").toString(); @@ -236,37 +219,19 @@ public class EnsemblMap extends EnsemblRestClient int end, String cdsOrCdna) { URL url = null; - BufferedReader br = null; - try { String domain = new EnsemblInfo().getDomain(division); if (domain != null) { url = getIdMapUrl(domain, accession, start, end, cdsOrCdna); - br = getHttpResponse(url, null); - if (br != null) - { - return (parseIdMappingResponse(br, accession, domain)); - } + return (parseIdMappingResponse(url, accession, domain)); } return null; } catch (Throwable t) { System.out.println("Error calling " + url + ": " + t.getMessage()); return null; - } finally - { - if (br != null) - { - try - { - br.close(); - } catch (IOException e) - { - // ignore - } - } } } @@ -312,17 +277,16 @@ public class EnsemblMap extends EnsemblRestClient * @param domain * @return */ - GeneLociI parseIdMappingResponse(BufferedReader br, String accession, + @SuppressWarnings("unchecked") +GeneLociI parseIdMappingResponse(URL url, String accession, String domain) { - JSONParser jp = new JSONParser(); try { - JSONObject parsed = (JSONObject) jp.parse(br); - JSONArray mappings = (JSONArray) parsed.get(MAPPINGS); - - Iterator rvals = mappings.iterator(); + Iterator rvals = (Iterator) getJSON(url, null, -1, MODE_ITERATOR, MAPPINGS); + if (rvals == null) + return null; String assembly = null; String chromosome = null; int fromEnd = 0; @@ -330,11 +294,11 @@ public class EnsemblMap extends EnsemblRestClient while (rvals.hasNext()) { - JSONObject val = (JSONObject) rvals.next(); - JSONObject original = (JSONObject) val.get("original"); + Map val = (Map) rvals.next(); + Map original = (Map) val.get("original"); fromEnd = Integer.parseInt(original.get("end").toString()); - JSONObject mapped = (JSONObject) val.get(MAPPED); + Map mapped = (Map) val.get(MAPPED); int start = Integer.parseInt(mapped.get("start").toString()); int end = Integer.parseInt(mapped.get("end").toString()); String ass = mapped.get("assembly_name").toString(); diff --git a/src/jalview/ext/ensembl/EnsemblRestClient.java b/src/jalview/ext/ensembl/EnsemblRestClient.java index 5ebfcbf..b3113f9 100644 --- a/src/jalview/ext/ensembl/EnsemblRestClient.java +++ b/src/jalview/ext/ensembl/EnsemblRestClient.java @@ -21,7 +21,9 @@ package jalview.ext.ensembl; import jalview.bin.Cache; -import jalview.bin.Jalview; +import jalview.javascript.json.JSON; +import jalview.util.JSONUtils; +import jalview.util.Platform; import jalview.util.StringUtils; import java.io.BufferedReader; @@ -29,6 +31,7 @@ import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.io.Reader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.ProtocolException; @@ -39,9 +42,7 @@ import java.util.Map; import javax.ws.rs.HttpMethod; -import org.json.simple.JSONArray; -import org.json.simple.JSONObject; -import org.json.simple.parser.JSONParser; +import org.json.simple.parser.ParseException; /** * Base class for Ensembl REST service clients @@ -189,7 +190,8 @@ abstract class EnsemblRestClient extends EnsemblSequenceFetcher * @see http://rest.ensembl.org/documentation/info/ping * @return */ - boolean checkEnsembl() + @SuppressWarnings("unchecked") +boolean checkEnsembl() { BufferedReader br = null; String pingUrl = getDomain() + "/info/ping" + CONTENT_TYPE_JSON; @@ -197,20 +199,17 @@ abstract class EnsemblRestClient extends EnsemblSequenceFetcher { // note this format works for both ensembl and ensemblgenomes // info/ping.json works for ensembl only (March 2016) - URL ping = new URL(pingUrl); - + + + + /* * expect {"ping":1} if ok * if ping takes more than 2 seconds to respond, treat as if unavailable */ - br = getHttpResponse(ping, null, 2 * 1000); - if (br == null) - { - // error reponse status - return false; - } - JSONParser jp = new JSONParser(); - JSONObject val = (JSONObject) jp.parse(br); + Map val = (Map) getJSON(new URL(pingUrl), null, 2 * 1000, MODE_MAP, null); + if (val == null) + return false; String pingString = val.get("ping").toString(); return pingString != null; } catch (Throwable t) @@ -233,37 +232,41 @@ abstract class EnsemblRestClient extends EnsemblSequenceFetcher return false; } - /** - * Returns a reader to a (Json) response from the Ensembl sequence endpoint. - * If the request failed the return value may be null. - * - * @param ids - * @return - * @throws IOException - */ - protected BufferedReader getSequenceReader(List ids) - throws IOException - { - URL url = getUrl(ids); - - BufferedReader reader = getHttpResponse(url, ids); - return reader; - } - - /** - * Gets a reader to the HTTP response, using the default read timeout of 5 - * minutes - * - * @param url - * @param ids - * @return - * @throws IOException - */ - protected BufferedReader getHttpResponse(URL url, List ids) - throws IOException - { - return getHttpResponse(url, ids, DEFAULT_READ_TIMEOUT); - } + + protected final static int MODE_ARRAY = 0; + protected final static int MODE_MAP = 1; + protected final static int MODE_ITERATOR = 2; + +// /** +// * Returns a reader to a (Json) response from the Ensembl sequence endpoint. +// * If the request failed the return value may be null. +// * +// * @param ids +// * @return +// * @throws IOException +// * @throws ParseException +// */ +// protected Object getSequenceJSON(List ids, int mode) +// throws IOException, ParseException +// { +// URL url = getUrl(ids); +// return getJSON(url, ids, -1, mode); +// } +// +// /** +// * Gets a reader to the HTTP response, using the default read timeout of 5 +// * minutes +// * +// * @param url +// * @param ids +// * @return +// * @throws IOException +// */ +// protected BufferedReader getHttpResponse(URL url, List ids) +// throws IOException +// { +// return getHttpResponse(url, ids, DEFAULT_READ_TIMEOUT); +// } /** * Sends the HTTP request and gets the response as a reader. Returns null if @@ -277,13 +280,19 @@ abstract class EnsemblRestClient extends EnsemblSequenceFetcher * @return * @throws IOException */ - protected BufferedReader getHttpResponse(URL url, List ids, + private BufferedReader getHttpResponse(URL url, List ids, int readTimeout) throws IOException { + if (readTimeout < 0) + readTimeout = DEFAULT_READ_TIMEOUT; int retriesLeft = MAX_RETRIES; HttpURLConnection connection = null; int responseCode = 0; - + + if (Platform.isJS()) { + JSON.setAjax(url); + } + while (retriesLeft > 0) { connection = tryConnection(url, ids, readTimeout); @@ -304,18 +313,21 @@ abstract class EnsemblRestClient extends EnsemblSequenceFetcher * note: a GET request for an invalid id returns an error code e.g. 415 * but POST request returns 200 and an empty Fasta response */ - System.err.println("Response code " + responseCode + " for " + url); + System.err.println("Response code " + responseCode);// + " for " + url); return null; } + InputStream response = connection.getInputStream(); + + if (Platform.isJS()) { + return JSON.getJSONReader(response); + } // System.out.println(getClass().getName() + " took " // + (System.currentTimeMillis() - now) + "ms to fetch"); - BufferedReader reader = null; - reader = new BufferedReader(new InputStreamReader(response, "UTF-8")); - return reader; + return new BufferedReader(new InputStreamReader(response, "UTF-8")); } /** @@ -330,6 +342,7 @@ abstract class EnsemblRestClient extends EnsemblSequenceFetcher int readTimeout) throws IOException, ProtocolException { // System.out.println(System.currentTimeMillis() + " " + url); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); /* @@ -466,26 +479,87 @@ abstract class EnsemblRestClient extends EnsemblSequenceFetcher wr.close(); } + /** + * Primary access point to parsed JSON data, including the call to retrieve and + * parsing. + * + * @param url request url; if null, getUrl(ids) will be used + * @param ids optional; may be null + * @param msDelay -1 for default delay + * @param mode map, array, or array iterator + * @param mapKey an optional key for an outer map + * @return a Map, List, Iterator, or null + * @throws IOException + * @throws ParseException + * + * @author Bob Hanson 2019 + */ + @SuppressWarnings("unchecked") + protected Object getJSON(URL url, List ids, int msDelay, int mode, String mapKey) throws IOException, ParseException { + + if (url == null) + url = getUrl(ids); + + Platform.timeCheck("EnsembleRestClient.getJSON0 " + url, Platform.TIME_MARK); + + Reader br = null; + try { + br = (url == null ? null : getHttpResponse(url, ids, msDelay)); + + Platform.timeCheck("EnsembleRestClient.getJSON1 parsing... ", Platform.TIME_MARK); + + Object ret = (br == null ? null : JSONUtils.parse(br)); + + Platform.timeCheck("EnsembleRestClient.getJSON2 ...done ", Platform.TIME_MARK); + + if (ret != null && mapKey != null) + ret = ((Map) ret).get(mapKey); + if (ret == null) + { + return null; + } + switch (mode) { + case MODE_ARRAY: + case MODE_MAP: + break; + case MODE_ITERATOR: + ret = ((List) ret).iterator(); + break; + } + return ret; + + } finally + { + if (br != null) + { + try + { + br.close(); + } catch (IOException e) + { + // ignore + } + } + } + } + + + /** * Fetches and checks Ensembl's REST version number * * @return */ + @SuppressWarnings("unchecked") private void checkEnsemblRestVersion() { EnsemblData info = domainData.get(getDomain()); - JSONParser jp = new JSONParser(); - URL url = null; try { - url = new URL(getDomain() + "/info/rest" + CONTENT_TYPE_JSON); - BufferedReader br = getHttpResponse(url, null); - if (br == null) - { - return; - } - JSONObject val = (JSONObject) jp.parse(br); + Map val = (Map) getJSON(new URL(getDomain() + "/info/rest" + CONTENT_TYPE_JSON), null, -1, MODE_MAP, null); + if (val == null) + return; String version = val.get("release").toString(); String majorVersion = version.substring(0, version.indexOf(".")); String expected = info.expectedRestVersion; @@ -539,40 +613,21 @@ abstract class EnsemblRestClient extends EnsemblSequenceFetcher * * @return */ + @SuppressWarnings("unchecked") private void checkEnsemblDataVersion() { - JSONParser jp = new JSONParser(); - URL url = null; - BufferedReader br = null; - - try - { - url = new URL(getDomain() + "/info/data" + CONTENT_TYPE_JSON); - br = getHttpResponse(url, null); - if (br != null) - { - JSONObject val = (JSONObject) jp.parse(br); - JSONArray versions = (JSONArray) val.get("releases"); - domainData.get(getDomain()).dataVersion = versions.get(0) - .toString(); - } - } catch (Throwable t) - { - System.err.println( - "Error checking Ensembl data version: " + t.getMessage()); - } finally - { - if (br != null) - { - try - { - br.close(); - } catch (IOException e) - { - // ignore - } - } - } + Map val; + try + { + val = (Map) getJSON( + new URL(getDomain() + "/info/data" + CONTENT_TYPE_JSON), null, -1, MODE_MAP, null); + if (val == null) + return; + List versions = (List) val.get("releases"); + domainData.get(getDomain()).dataVersion = versions.get(0).toString(); + } catch (Throwable e) {//could be IOException | ParseException e) { + System.err.println("Error checking Ensembl data version: " + e.getMessage()); + } } public String getEnsemblDataVersion() diff --git a/src/jalview/ext/ensembl/EnsemblSeqProxy.java b/src/jalview/ext/ensembl/EnsemblSeqProxy.java index 6be0486..7bf2563 100644 --- a/src/jalview/ext/ensembl/EnsemblSeqProxy.java +++ b/src/jalview/ext/ensembl/EnsemblSeqProxy.java @@ -40,8 +40,8 @@ import jalview.util.Comparison; import jalview.util.DBRefUtils; import jalview.util.IntRangeComparator; import jalview.util.MapList; +import jalview.util.Platform; -import java.io.BufferedReader; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; @@ -49,9 +49,8 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; +import java.util.Map; -import org.json.simple.JSONObject; -import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; /** @@ -258,6 +257,7 @@ public abstract class EnsemblSeqProxy extends EnsemblRestClient String accId = querySeq.getName(); try { + System.out.println("Adding protein product for " + accId); AlignmentI protein = new EnsemblProtein(getDomain()) .getSequenceRecords(accId); if (protein == null || protein.getHeight() == 0) @@ -387,13 +387,11 @@ public abstract class EnsemblSeqProxy extends EnsemblRestClient inProgress = false; throw new JalviewException("ENSEMBL Rest API not available."); } - BufferedReader br = getSequenceReader(ids); - if (br == null) - { - return alignment; - } + Platform.timeCheck("EnsemblSeqProx.fetchSeq ", Platform.TIME_MARK); - List seqs = parseSequenceJson(br); + List seqs = parseSequenceJson(ids); + if (seqs == null) + return alignment; if (seqs.isEmpty()) { @@ -446,9 +444,9 @@ public abstract class EnsemblSeqProxy extends EnsemblRestClient * @return a single jalview.datamodel.Sequence * @see http://rest.ensembl.org/documentation/info/sequence_id */ - protected List parseSequenceJson(BufferedReader br) + @SuppressWarnings("unchecked") + protected List parseSequenceJson(List ids) { - JSONParser jp = new JSONParser(); List result = new ArrayList<>(); try { @@ -456,7 +454,9 @@ public abstract class EnsemblSeqProxy extends EnsemblRestClient * for now, assumes only one sequence returned; refactor if needed * in future to handle a JSONArray with more than one */ - final JSONObject val = (JSONObject) jp.parse(br); + Map val = (Map) getJSON(null, ids, -1, MODE_MAP, null); + if (val == null) + return null; Object s = val.get("desc"); String desc = s == null ? null : s.toString(); s = val.get("id"); diff --git a/src/jalview/ext/ensembl/EnsemblSymbol.java b/src/jalview/ext/ensembl/EnsemblSymbol.java index 40d6cad..f731e94 100644 --- a/src/jalview/ext/ensembl/EnsemblSymbol.java +++ b/src/jalview/ext/ensembl/EnsemblSymbol.java @@ -20,17 +20,14 @@ */ package jalview.ext.ensembl; -import java.io.BufferedReader; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.Iterator; import java.util.List; +import java.util.Map; -import org.json.simple.JSONArray; -import org.json.simple.JSONObject; -import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; /** @@ -56,39 +53,18 @@ public class EnsemblSymbol extends EnsemblXref super(domain, dbName, dbVersion); } - /** - * Returns the first "id" value in gene identifier format from the JSON - * response, or null if none found - * - * @param br - * @return - * @throws IOException - */ - protected String parseSymbolResponse(BufferedReader br) throws IOException - { - JSONParser jp = new JSONParser(); - String result = null; - try - { - JSONArray responses = (JSONArray) jp.parse(br); - Iterator rvals = responses.iterator(); - while (rvals.hasNext()) - { - JSONObject val = (JSONObject) rvals.next(); - String id = val.get(JSON_ID).toString(); - String type = val.get(TYPE).toString(); - if (id != null && GENE.equals(type)) - { - result = id; - break; - } - } - } catch (ParseException e) - { - // ignore - } - return result; - } +// /** +// * Returns the first "id" value in gene identifier format from the JSON +// * response, or null if none found +// * +// * @param br +// * @return +// * @throws IOException +// */ +// @SuppressWarnings("unchecked") +//protected String parseSymbolResponse(BufferedReader br) throws IOException +// { +// } /** * Constructs the URL for the REST symbol endpoint @@ -129,6 +105,7 @@ public class EnsemblSymbol extends EnsemblXref * @param identifier * @return */ + @SuppressWarnings("unchecked") public List getGeneIds(String identifier) { List result = new ArrayList(); @@ -136,43 +113,43 @@ public class EnsemblSymbol extends EnsemblXref ids.add(identifier); String[] queries = identifier.split(getAccessionSeparator()); - BufferedReader br = null; try { for (String query : queries) { for (Species taxon : Species.getModelOrganisms()) { - URL url = getUrl(query, taxon, GENE); - if (url != null) + String geneId = null;///parseSymbolResponse(br); + try { - br = getHttpResponse(url, ids); - if (br != null) + Iterator rvals = (Iterator) getJSON(getUrl(query, taxon, GENE), ids, -1, MODE_ITERATOR, null); + if (rvals == null) + continue; + while (rvals.hasNext()) { - String geneId = parseSymbolResponse(br); - if (geneId != null && !result.contains(geneId)) + Map val = (Map) rvals.next(); + String id = val.get(JSON_ID).toString(); + String type = val.get(TYPE).toString(); + if (id != null && GENE.equals(type)) { - result.add(geneId); + geneId = id; + break; } } + } catch (ParseException e) + { + // ignore + } + + if (geneId != null && !result.contains(geneId)) + { + result.add(geneId); } } } } catch (IOException e) { // ignore - } finally - { - if (br != null) - { - try - { - br.close(); - } catch (IOException e) - { - // ignore - } - } } return result; } diff --git a/src/jalview/ext/ensembl/EnsemblXref.java b/src/jalview/ext/ensembl/EnsemblXref.java index 77768a6..eedfe97 100644 --- a/src/jalview/ext/ensembl/EnsemblXref.java +++ b/src/jalview/ext/ensembl/EnsemblXref.java @@ -23,6 +23,7 @@ package jalview.ext.ensembl; import jalview.datamodel.AlignmentI; import jalview.datamodel.DBRefEntry; import jalview.util.DBRefUtils; +import jalview.util.JSONUtils; import java.io.BufferedReader; import java.io.IOException; @@ -31,10 +32,8 @@ import java.net.URL; import java.util.ArrayList; import java.util.Iterator; import java.util.List; +import java.util.Map; -import org.json.simple.JSONArray; -import org.json.simple.JSONObject; -import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; /** @@ -99,66 +98,19 @@ class EnsemblXref extends EnsemblRestClient * an Ensembl stable identifier * @return */ + @SuppressWarnings("unchecked") public List getCrossReferences(String identifier) { List result = new ArrayList<>(); List ids = new ArrayList<>(); ids.add(identifier); - BufferedReader br = null; try { - URL url = getUrl(identifier); - if (url != null) - { - br = getHttpResponse(url, ids); - if (br != null) - { - result = parseResponse(br); - } - } - } catch (IOException e) - { - // ignore - } finally - { - if (br != null) - { - try - { - br.close(); - } catch (IOException e) - { - // ignore - } - } - } - - return result; - } - - /** - * Parses "primary_id" and "dbname" values from the JSON response and - * constructs a DBRefEntry. Returns a list of the DBRefEntry created. Note we - * don't parse "synonyms" as they appear to be either redirected or obsolete - * in Uniprot. - * - * @param br - * @return - * @throws IOException - */ - protected List parseResponse(BufferedReader br) - throws IOException - { - JSONParser jp = new JSONParser(); - List result = new ArrayList<>(); - try - { - JSONArray responses = (JSONArray) jp.parse(br); - Iterator rvals = responses.iterator(); + Iterator rvals = (Iterator) getJSON(getUrl(identifier), ids, -1, MODE_ITERATOR, null); while (rvals.hasNext()) { - JSONObject val = (JSONObject) rvals.next(); + Map val = (Map) rvals.next(); String db = val.get("dbname").toString(); String id = val.get("primary_id").toString(); if (db != null && id != null @@ -169,13 +121,30 @@ class EnsemblXref extends EnsemblRestClient result.add(dbref); } } - } catch (ParseException e) + } catch (ParseException | IOException e) { // ignore } return result; } +// /** +// * Parses "primary_id" and "dbname" values from the JSON response and +// * constructs a DBRefEntry. Returns a list of the DBRefEntry created. Note we +// * don't parse "synonyms" as they appear to be either redirected or obsolete +// * in Uniprot. +// * +// * @param br +// * @return +// * @throws IOException +// */ +// @SuppressWarnings("unchecked") +//protected List parseResponse(BufferedReader br) +// throws IOException +// { +// return result; +// } +// private String xrefVersion = "ENSEMBL:0"; /** diff --git a/src/jalview/ext/paradise/Annotate3D.java b/src/jalview/ext/paradise/Annotate3D.java index d50ad87..3b7044b 100644 --- a/src/jalview/ext/paradise/Annotate3D.java +++ b/src/jalview/ext/paradise/Annotate3D.java @@ -20,6 +20,7 @@ */ package jalview.ext.paradise; +import jalview.util.JSONUtils; import jalview.util.MessageManager; import jalview.ws.HttpClientUtils; @@ -32,11 +33,10 @@ import java.net.URL; import java.util.ArrayList; import java.util.Iterator; import java.util.List; +import java.util.Map; import org.apache.http.NameValuePair; import org.apache.http.message.BasicNameValuePair; -import org.json.simple.JSONArray; -import org.json.simple.JSONObject; import org.json.simple.parser.ContentHandler; import org.json.simple.parser.ParseException; @@ -151,14 +151,19 @@ public class Annotate3D } + /** + * @param respons + * @return + * @throws Exception + */ public static Iterator processJsonResponseFor(Reader respons) throws Exception { - org.json.simple.parser.JSONParser jp = new org.json.simple.parser.JSONParser(); + // BH 2019 never called? try { - final JSONArray responses = (JSONArray) jp.parse(respons); - final Iterator rvals = responses.iterator(); + @SuppressWarnings("unchecked") + final Iterator rvals = ((List) JSONUtils.parse(respons)).iterator(); return new Iterator() { @Override @@ -167,10 +172,11 @@ public class Annotate3D return rvals.hasNext(); } - @Override + @SuppressWarnings("unchecked") + @Override public Reader next() { - JSONObject val = (JSONObject) rvals.next(); + Map val = (Map) rvals.next(); Object sval = null; try @@ -189,9 +195,7 @@ public class Annotate3D sval = ""; } - return new StringReader((sval instanceof JSONObject) - ? ((JSONObject) sval).toString() - : sval.toString()); + return new StringReader(sval.toString()); } diff --git a/src/jalview/fts/service/pdb/PDBFTSRestClient.java b/src/jalview/fts/service/pdb/PDBFTSRestClient.java index c0ca3a5..b4e5660 100644 --- a/src/jalview/fts/service/pdb/PDBFTSRestClient.java +++ b/src/jalview/fts/service/pdb/PDBFTSRestClient.java @@ -28,6 +28,7 @@ import jalview.fts.api.FTSRestClientI; import jalview.fts.core.FTSRestClient; import jalview.fts.core.FTSRestRequest; import jalview.fts.core.FTSRestResponse; +import jalview.util.JSONUtils; import jalview.util.MessageManager; import java.net.URI; @@ -35,13 +36,11 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.List; +import java.util.Map; import java.util.Objects; import javax.ws.rs.core.MediaType; -import org.json.simple.JSONArray; -import org.json.simple.JSONObject; -import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; import com.sun.jersey.api.client.Client; @@ -223,7 +222,8 @@ public class PDBFTSRestClient extends FTSRestClient * the JSON string containing error message from the server * @return the processed error message from the JSON string */ - public static String parseJsonExceptionString(String jsonErrorResponse) + @SuppressWarnings("unchecked") +public static String parseJsonExceptionString(String jsonErrorResponse) { StringBuilder errorMessage = new StringBuilder( "\n============= PDB Rest Client RunTime error =============\n"); @@ -250,13 +250,12 @@ public class PDBFTSRestClient extends FTSRestClient // try { - JSONParser jsonParser = new JSONParser(); - JSONObject jsonObj = (JSONObject) jsonParser.parse(jsonErrorResponse); - JSONObject errorResponse = (JSONObject) jsonObj.get("error"); + Map jsonObj = (Map) JSONUtils.parse(jsonErrorResponse); + Map errorResponse = (Map) jsonObj.get("error"); - JSONObject responseHeader = (JSONObject) jsonObj + Map responseHeader = (Map) jsonObj .get("responseHeader"); - JSONObject paramsObj = (JSONObject) responseHeader.get("params"); + Map paramsObj = (Map) responseHeader.get("params"); String status = responseHeader.get("status").toString(); String message = errorResponse.get("msg").toString(); String query = paramsObj.get("q").toString(); @@ -294,23 +293,20 @@ public class PDBFTSRestClient extends FTSRestClient List result = null; try { - JSONParser jsonParser = new JSONParser(); - JSONObject jsonObj = (JSONObject) jsonParser - .parse(pdbJsonResponseString); - - JSONObject pdbResponse = (JSONObject) jsonObj.get("response"); - String queryTime = ((JSONObject) jsonObj.get("responseHeader")) + Map jsonObj = (Map) JSONUtils.parse(pdbJsonResponseString); + Map pdbResponse = (Map) jsonObj.get("response"); + String queryTime = ((Map) jsonObj.get("responseHeader")) .get("QTime").toString(); int numFound = Integer .valueOf(pdbResponse.get("numFound").toString()); if (numFound > 0) { result = new ArrayList(); - JSONArray docs = (JSONArray) pdbResponse.get("docs"); - for (Iterator docIter = docs.iterator(); docIter + List docs = (List) pdbResponse.get("docs"); + for (Iterator docIter = docs.iterator(); docIter .hasNext();) { - JSONObject doc = docIter.next(); + Map doc = (Map) docIter.next(); result.add(getFTSData(doc, pdbRestRequest)); } searchResult.setNumberOfItemsFound(numFound); @@ -324,7 +320,7 @@ public class PDBFTSRestClient extends FTSRestClient return searchResult; } - public static FTSData getFTSData(JSONObject pdbJsonDoc, + public static FTSData getFTSData(Map pdbJsonDoc, FTSRestRequest request) { @@ -466,7 +462,8 @@ public class PDBFTSRestClient extends FTSRestClient return allDefaultDisplayedStructureDataColumns; } - public static void main(String[] args) { + @SuppressWarnings("unchecked") +public static void main(String[] args) { // check for transpiler fix associated with JSONParser yylex.java use of charAt() @@ -481,10 +478,9 @@ public class PDBFTSRestClient extends FTSRestClient int z = c ^ 5; String result = s +x + y + z; assert (result == "e103982102"); - JSONParser jsonParser = new JSONParser(); try { - JSONObject jsonObj = (JSONObject) jsonParser.parse("{\"a\":3}"); + Map jsonObj = (Map) JSONUtils.parse("{\"a\":3}"); System.out.println(jsonObj); } catch (ParseException e) { diff --git a/src/jalview/io/JSONFile.java b/src/jalview/io/JSONFile.java index 7071a9d..3f1b1a7 100644 --- a/src/jalview/io/JSONFile.java +++ b/src/jalview/io/JSONFile.java @@ -53,6 +53,7 @@ import jalview.schemes.JalviewColourScheme; import jalview.schemes.ResidueColourScheme; import jalview.util.ColorUtils; import jalview.util.Format; +import jalview.util.JSONUtils; import jalview.viewmodel.seqfeatures.FeaturesDisplayed; import java.awt.Color; @@ -62,12 +63,9 @@ import java.util.ArrayList; import java.util.Hashtable; import java.util.Iterator; import java.util.List; +import java.util.Map; import java.util.Vector; -import org.json.simple.JSONArray; -import org.json.simple.JSONObject; -import org.json.simple.parser.JSONParser; - public class JSONFile extends AlignFile implements ComplexAlignFile { private static String version = new BuildDetails().getVersion(); @@ -220,8 +218,7 @@ public class JSONFile extends AlignFile implements ComplexAlignFile jsonAlignmentPojo.getSeqGroups().add(seqGrpPojo); } } - org.json.JSONObject generatedJSon = new org.json.JSONObject( - jsonAlignmentPojo); + org.json.JSONObject generatedJSon = new org.json.JSONObject(jsonAlignmentPojo); jsonOutput = generatedJSon.toString(); return jsonOutput.replaceAll("xstart", "xStart").replaceAll("xend", "xEnd"); @@ -398,17 +395,15 @@ public class JSONFile extends AlignFile implements ComplexAlignFile { try { - JSONParser jsonParser = new JSONParser(); - JSONObject alignmentJsonObj = (JSONObject) jsonParser - .parse(jsonAlignmentString); - JSONArray seqJsonArray = (JSONArray) alignmentJsonObj.get("seqs"); - JSONArray alAnnotJsonArray = (JSONArray) alignmentJsonObj + Map alignmentJsonObj = (Map) JSONUtils.parse(jsonAlignmentString); + List seqJsonArray = (List) alignmentJsonObj.get("seqs"); + List alAnnotJsonArray = (List) alignmentJsonObj .get("alignAnnotation"); - JSONArray jsonSeqArray = (JSONArray) alignmentJsonObj + List jsonSeqArray = (List) alignmentJsonObj .get("seqFeatures"); - JSONArray seqGrpJsonArray = (JSONArray) alignmentJsonObj + List seqGrpJsonArray = (List) alignmentJsonObj .get("seqGroups"); - JSONObject jvSettingsJsonObj = (JSONObject) alignmentJsonObj + Map jvSettingsJsonObj = (Map) alignmentJsonObj .get("appSettings"); if (jvSettingsJsonObj != null) @@ -424,10 +419,9 @@ public class JSONFile extends AlignFile implements ComplexAlignFile hiddenSequences = new ArrayList<>(); seqMap = new Hashtable<>(); - for (Iterator sequenceIter = seqJsonArray - .iterator(); sequenceIter.hasNext();) + for (Iterator sequenceIter = seqJsonArray.iterator(); sequenceIter.hasNext();) { - JSONObject sequence = sequenceIter.next(); + Map sequence = (Map) sequenceIter.next(); String sequcenceString = sequence.get("seq").toString(); String sequenceName = sequence.get("name").toString(); String seqUniqueId = sequence.get("id").toString(); @@ -445,10 +439,9 @@ public class JSONFile extends AlignFile implements ComplexAlignFile parseFeatures(jsonSeqArray); - for (Iterator seqGrpIter = seqGrpJsonArray - .iterator(); seqGrpIter.hasNext();) + for (Iterator seqGrpIter = seqGrpJsonArray.iterator(); seqGrpIter.hasNext();) { - JSONObject seqGrpObj = seqGrpIter.next(); + Map seqGrpObj = (Map)seqGrpIter.next(); String grpName = seqGrpObj.get("groupName").toString(); String colourScheme = seqGrpObj.get("colourScheme").toString(); String description = (seqGrpObj.get("description") == null) ? null @@ -464,16 +457,15 @@ public class JSONFile extends AlignFile implements ComplexAlignFile int startRes = Integer .valueOf(seqGrpObj.get("startRes").toString()); int endRes = Integer.valueOf(seqGrpObj.get("endRes").toString()); - JSONArray sequenceRefs = (JSONArray) seqGrpObj.get("sequenceRefs"); + List sequenceRefs = (List) seqGrpObj.get("sequenceRefs"); ArrayList grpSeqs = new ArrayList<>(); if (sequenceRefs.size() > 0) { - Iterator seqHashIter = sequenceRefs.iterator(); + Iterator seqHashIter = sequenceRefs.iterator(); while (seqHashIter.hasNext()) { - String seqHash = seqHashIter.next(); - Sequence sequence = seqMap.get(seqHash); + Sequence sequence = seqMap.get(seqHashIter.next()); if (sequence != null) { grpSeqs.add(sequence); @@ -490,17 +482,15 @@ public class JSONFile extends AlignFile implements ComplexAlignFile } - for (Iterator alAnnotIter = alAnnotJsonArray - .iterator(); alAnnotIter.hasNext();) + for (Iterator alAnnotIter = alAnnotJsonArray.iterator(); alAnnotIter.hasNext();) { - JSONObject alAnnot = alAnnotIter.next(); - JSONArray annotJsonArray = (JSONArray) alAnnot.get("annotations"); + Map alAnnot = (Map) alAnnotIter.next(); + List annotJsonArray = (List) alAnnot.get("annotations"); Annotation[] annotations = new Annotation[annotJsonArray.size()]; int count = 0; - for (Iterator annotIter = annotJsonArray - .iterator(); annotIter.hasNext();) + for (Iterator annotIter = annotJsonArray.iterator(); annotIter.hasNext();) { - JSONObject annot = annotIter.next(); + Map annot = (Map) annotIter.next(); if (annot == null) { annotations[count] = null; @@ -536,7 +526,7 @@ public class JSONFile extends AlignFile implements ComplexAlignFile alignAnnot.graph = (alAnnot.get("graphType") == null) ? 0 : Integer.valueOf(alAnnot.get("graphType").toString()); - JSONObject diplaySettings = (JSONObject) alAnnot + Map diplaySettings = (Map) alAnnot .get("annotationSettings"); if (diplaySettings != null) { @@ -604,7 +594,7 @@ public class JSONFile extends AlignFile implements ComplexAlignFile return this; } - public void parseHiddenSeqRefsAsList(JSONObject jvSettingsJson) + public void parseHiddenSeqRefsAsList(Map jvSettingsJson) { hiddenSeqRefs = new ArrayList<>(); String hiddenSeqs = (String) jvSettingsJson.get("hiddenSeqs"); @@ -618,7 +608,7 @@ public class JSONFile extends AlignFile implements ComplexAlignFile } } - public void parseHiddenCols(JSONObject jvSettingsJson) + public void parseHiddenCols(Map jvSettingsJson) { String hiddenCols = (String) jvSettingsJson.get("hiddenCols"); if (hiddenCols != null && !hiddenCols.isEmpty()) @@ -635,15 +625,15 @@ public class JSONFile extends AlignFile implements ComplexAlignFile } @SuppressWarnings("unchecked") - private void parseFeatures(JSONArray jsonSeqFeatures) + private void parseFeatures(List jsonSeqFeatures) { if (jsonSeqFeatures != null) { displayedFeatures = new FeaturesDisplayed(); - for (Iterator seqFeatureItr = jsonSeqFeatures + for (Iterator seqFeatureItr = jsonSeqFeatures .iterator(); seqFeatureItr.hasNext();) { - JSONObject jsonFeature = seqFeatureItr.next(); + Map jsonFeature = (Map) seqFeatureItr.next(); Long begin = (Long) jsonFeature.get("xStart"); Long end = (Long) jsonFeature.get("xEnd"); String type = (String) jsonFeature.get("type"); @@ -665,14 +655,13 @@ public class JSONFile extends AlignFile implements ComplexAlignFile SequenceFeature sequenceFeature = new SequenceFeature(type, description, featureBegin, featureEnd, score, featureGrp); - JSONArray linksJsonArray = (JSONArray) jsonFeature.get("links"); + List linksJsonArray = (List) jsonFeature.get("links"); if (linksJsonArray != null && linksJsonArray.size() > 0) { - Iterator linkList = linksJsonArray.iterator(); + Iterator linkList = linksJsonArray.iterator(); while (linkList.hasNext()) { - String link = linkList.next(); - sequenceFeature.addLink(link); + sequenceFeature.addLink((String) linkList.next()); } } diff --git a/src/jalview/json/binding/biojs/BioJSRepositoryPojo.java b/src/jalview/json/binding/biojs/BioJSRepositoryPojo.java index 62fb20a..ad50305 100644 --- a/src/jalview/json/binding/biojs/BioJSRepositoryPojo.java +++ b/src/jalview/json/binding/biojs/BioJSRepositoryPojo.java @@ -20,14 +20,15 @@ */ package jalview.json.binding.biojs; +import jalview.util.JSONUtils; + import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; +import java.util.List; +import java.util.Map; import java.util.Objects; -import org.json.simple.JSONArray; -import org.json.simple.JSONObject; -import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; public class BioJSRepositoryPojo @@ -59,17 +60,16 @@ public class BioJSRepositoryPojo { Objects.requireNonNull(jsonString, "Supplied jsonString must not be null"); - JSONParser jsonParser = new JSONParser(); - JSONObject JsonObj = (JSONObject) jsonParser.parse(jsonString); + Map JsonObj = (Map) JSONUtils.parse(jsonString); this.description = (String) JsonObj.get("description"); this.latestReleaseVersion = (String) JsonObj .get("latestReleaseVersion"); - JSONArray repositoriesJsonArray = (JSONArray) JsonObj.get("releases"); - for (Iterator repoIter = repositoriesJsonArray + List repositoriesJsonArray = (List) JsonObj.get("releases"); + for (Iterator repoIter = repositoriesJsonArray .iterator(); repoIter.hasNext();) { - JSONObject repoObj = repoIter.next(); + Map repoObj = (Map) repoIter.next(); BioJSReleasePojo repo = new BioJSReleasePojo(); repo.setType((String) repoObj.get("type")); repo.setUrl((String) repoObj.get("url")); diff --git a/src/jalview/urls/IdentifiersUrlProvider.java b/src/jalview/urls/IdentifiersUrlProvider.java index f32e590..0dab719 100644 --- a/src/jalview/urls/IdentifiersUrlProvider.java +++ b/src/jalview/urls/IdentifiersUrlProvider.java @@ -25,20 +25,18 @@ import static jalview.util.UrlConstants.DB_ACCESSION; import static jalview.util.UrlConstants.DELIM; import static jalview.util.UrlConstants.SEP; +import jalview.util.JSONUtils; import jalview.util.UrlLink; -import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; +import java.util.Map; import java.util.StringTokenizer; -import org.json.simple.JSONArray; -import org.json.simple.JSONObject; -import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; /** @@ -76,19 +74,20 @@ public class IdentifiersUrlProvider extends UrlProviderImpl * name of identifiers.org download file * @return hashmap of identifiers.org data, keyed by MIRIAM id */ - private HashMap readIdentifiers(String idFileName) + @SuppressWarnings("unchecked") +private HashMap readIdentifiers(String idFileName) { - JSONParser parser = new JSONParser(); - // identifiers.org data HashMap idData = new HashMap(); String errorMessage = null; try { + // NOTE: THIS WILL FAIL IN SWINGJS BECAUSE IT INVOLVES A FILE READER + FileReader reader = new FileReader(idFileName); String key = ""; - JSONObject obj = (JSONObject) parser.parse(reader); + Map obj = (Map) JSONUtils.parse(reader); if (obj.containsKey(ID_ORG_KEY)) { key = ID_ORG_KEY; @@ -104,12 +103,12 @@ public class IdentifiersUrlProvider extends UrlProviderImpl return idData; } - JSONArray jsonarray = (JSONArray) obj.get(key); + List jsonarray = (List) obj.get(key); // loop over each entry in JSON array and build HashMap entry for (int i = 0; i < jsonarray.size(); i++) { - JSONObject item = (JSONObject) jsonarray.get(i); + Map item = (Map) jsonarray.get(i); String url = (String) item.get("url") + "/" + DELIM + DB_ACCESSION + DELIM; diff --git a/src/jalview/util/JSONUtils.java b/src/jalview/util/JSONUtils.java index cdfc88e..f91275d 100644 --- a/src/jalview/util/JSONUtils.java +++ b/src/jalview/util/JSONUtils.java @@ -1,6 +1,14 @@ package jalview.util; -import org.json.simple.JSONArray; +import jalview.javascript.json.JSON; + +import java.io.FileReader; +import java.io.IOException; +import java.io.Reader; +import java.util.List; + +import org.json.simple.parser.JSONParser; +import org.json.simple.parser.ParseException; public class JSONUtils { @@ -12,23 +20,68 @@ public class JSONUtils * @param jsonArray * @return */ - public static String arrayToList(JSONArray jsonArray) + public static String arrayToStringList(List jsonArray) { - if (jsonArray == null) + int n; + + if (jsonArray == null || (n = jsonArray.size()) == 0) { return null; } - StringBuilder sb = new StringBuilder(); - for (int i = 0; i < jsonArray.size(); i++) + /** + * @j2sNative + * + * return jsonArray.elementData.slice(0, n).join(","); + */ { - if (i > 0) - { - sb.append(","); - } - sb.append(jsonArray.get(i).toString()); + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < n; i++) + { + if (i > 0) + { + sb.append(","); + } + sb.append(jsonArray.get(i).toString()); + } + return sb.toString(); } - return sb.length() == 0 ? null : sb.toString(); + } + + /** + * The method all JSON parsing must go through for JavaScript. + * @param r a BufferedReader or a javascript.json.JSON.JSONReader + * @return + * @throws IOException + * @throws ParseException + */ + public static Object parse(Reader r) throws IOException, ParseException + { + // Using a file reader is not currently supported in SwingJS JavaScript + + if (r == null) + return null; +// +// Platform.timeCheck("JSONUtils.parse0 ", Platform.TIME_MARK); + + Object ret; + if (Platform.isJS()) + { + if (r instanceof FileReader) + { + throw new IOException("StringJS does not support FileReader parsing for JSON -- but it could..."); + } + return JSON.parse(r); + } else { + ret = new JSONParser().parse(r); + } +// Platform.timeCheck("JSONUtils.parse1 ", Platform.TIME_MARK); + return ret; + } + + public static Object parse(String json) throws ParseException + { + return (Platform.isJS() ? JSON.parse(json) : new JSONParser().parse(json)); } }