X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fio%2FJSONFile.java;h=2ee305f811678d8819d329f1ffa0f2f2d3e335c4;hb=5a352aa2f3330ae269d9b70c4a7374c2518bfb2e;hp=7071a9dd6083791effbb7db481ee0b26c60cc719;hpb=dd8112aefe2c3a08c51f1684d5cda16be938c74f;p=jalview.git diff --git a/src/jalview/io/JSONFile.java b/src/jalview/io/JSONFile.java index 7071a9d..2ee305f 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,17 +63,14 @@ 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(); - private String webstartUrl = "http://www.jalview.org/services/launchApp"; + private String webstartUrl = "https://www.jalview.org/services/launchApp"; private String application = "Jalview"; @@ -220,9 +218,8 @@ public class JSONFile extends AlignFile implements ComplexAlignFile jsonAlignmentPojo.getSeqGroups().add(seqGrpPojo); } } - org.json.JSONObject generatedJSon = new org.json.JSONObject( - jsonAlignmentPojo); - jsonOutput = generatedJSon.toString(); + + jsonOutput = JSONUtils.stringify(jsonAlignmentPojo); return jsonOutput.replaceAll("xstart", "xStart").replaceAll("xend", "xEnd"); } catch (Exception e) @@ -294,16 +291,16 @@ public class JSONFile extends AlignFile implements ComplexAlignFile /* * get all features currently visible (and any non-positional features) */ - List seqFeatures = seq.getFeatures().getAllFeatures( - visibleFeatureTypes); + List seqFeatures = seq.getFeatures() + .getAllFeatures(visibleFeatureTypes); for (SequenceFeature sf : seqFeatures) { SequenceFeaturesPojo jsonFeature = new SequenceFeaturesPojo( String.valueOf(seq.hashCode())); - String featureColour = (fr == null) ? null : Format - .getHexString(finder.findFeatureColour(Color.white, seq, - seq.findIndex(sf.getBegin()))); + String featureColour = (fr == null) ? null + : Format.getHexString(finder.findFeatureColour(Color.white, + seq, seq.findIndex(sf.getBegin()))); int xStart = sf.getBegin() == 0 ? 0 : seq.findIndex(sf.getBegin()) - 1; int xEnd = sf.getEnd() == 0 ? 0 : seq.findIndex(sf.getEnd()); @@ -398,17 +395,17 @@ public class JSONFile extends AlignFile implements ComplexAlignFile { try { - JSONParser jsonParser = new JSONParser(); - JSONObject alignmentJsonObj = (JSONObject) jsonParser + Map alignmentJsonObj = (Map) JSONUtils .parse(jsonAlignmentString); - JSONArray seqJsonArray = (JSONArray) alignmentJsonObj.get("seqs"); - JSONArray alAnnotJsonArray = (JSONArray) alignmentJsonObj + 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 +421,11 @@ public class JSONFile extends AlignFile implements ComplexAlignFile hiddenSequences = new ArrayList<>(); seqMap = new Hashtable<>(); - for (Iterator sequenceIter = seqJsonArray + 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 +443,11 @@ public class JSONFile extends AlignFile implements ComplexAlignFile parseFeatures(jsonSeqArray); - for (Iterator seqGrpIter = seqGrpJsonArray + 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 +463,16 @@ 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 +489,20 @@ public class JSONFile extends AlignFile implements ComplexAlignFile } - for (Iterator alAnnotIter = alAnnotJsonArray + 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 + for (Iterator annotIter = annotJsonArray .iterator(); annotIter.hasNext();) { - JSONObject annot = annotIter.next(); + Map annot = (Map) annotIter + .next(); if (annot == null) { annotations[count] = null; @@ -536,7 +538,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,30 +606,30 @@ 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"); if (hiddenSeqs != null && !hiddenSeqs.isEmpty()) { String[] seqRefs = hiddenSeqs.split(";"); - for (String seqRef : seqRefs) + for (int i = 0, n = seqRefs.length; i < n; i++) { - hiddenSeqRefs.add(seqRef); + hiddenSeqRefs.add(seqRefs[i]); } } } - public void parseHiddenCols(JSONObject jvSettingsJson) + public void parseHiddenCols(Map jvSettingsJson) { String hiddenCols = (String) jvSettingsJson.get("hiddenCols"); if (hiddenCols != null && !hiddenCols.isEmpty()) { hiddenColumns = new HiddenColumns(); String[] rangeStrings = hiddenCols.split(";"); - for (String rangeString : rangeStrings) + for (int i = 0, n = rangeStrings.length; i < n; i++) { - String[] range = rangeString.split("-"); + String[] range = rangeStrings[i].split("-"); hiddenColumns.hideColumns(Integer.valueOf(range[0]), Integer.valueOf(range[1])); } @@ -635,15 +637,14 @@ 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 - .iterator(); seqFeatureItr.hasNext();) + for (Object o : jsonSeqFeatures) { - JSONObject jsonFeature = seqFeatureItr.next(); + Map jsonFeature = (Map) o; Long begin = (Long) jsonFeature.get("xStart"); Long end = (Long) jsonFeature.get("xEnd"); String type = (String) jsonFeature.get("type"); @@ -657,22 +658,22 @@ public class JSONFile extends AlignFile implements ComplexAlignFile /* * begin/end of 0 is for a non-positional feature */ - int featureBegin = begin.intValue() == 0 ? 0 : seq - .findPosition(begin.intValue()); - int featureEnd = end.intValue() == 0 ? 0 : seq.findPosition(end - .intValue()) - 1; + int featureBegin = begin.intValue() == 0 ? 0 + : seq.findPosition(begin.intValue()); + int featureEnd = end.intValue() == 0 ? 0 + : seq.findPosition(end.intValue()) - 1; 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()); } }