diff --git a/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/analytics/PackageVisitor.java b/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/analytics/PackageVisitor.java index 2ae51713b..67c6c0071 100644 --- a/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/analytics/PackageVisitor.java +++ b/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/analytics/PackageVisitor.java @@ -16,8 +16,10 @@ import org.hl7.fhir.utilities.SimpleHTTPClient; import org.hl7.fhir.utilities.SimpleHTTPClient.HTTPResult; import org.hl7.fhir.utilities.TextFile; import org.hl7.fhir.utilities.Utilities; -import org.hl7.fhir.utilities.json.JsonTrackingParser; -import org.hl7.fhir.utilities.json.JsonUtilities; +import org.hl7.fhir.utilities.json.model.JsonArray; +import org.hl7.fhir.utilities.json.model.JsonElement; +import org.hl7.fhir.utilities.json.model.JsonObject; +import org.hl7.fhir.utilities.json.parser.JsonParser; import org.hl7.fhir.utilities.npm.FilesystemPackageCacheManager; import org.hl7.fhir.utilities.npm.NpmPackage; import org.hl7.fhir.utilities.npm.PackageClient; @@ -28,10 +30,6 @@ import org.w3c.dom.Document; import org.w3c.dom.Element; import org.xml.sax.SAXException; -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; - public class PackageVisitor { public interface IPackageVisitorProcessor { @@ -140,11 +138,11 @@ public class PackageVisitor { } i++; } - JsonObject json = JsonTrackingParser.fetchJson("https://raw.githubusercontent.com/FHIR/ig-registry/master/fhir-ig-list.json"); + JsonObject json = JsonParser.parseObjectFromUrl("https://raw.githubusercontent.com/FHIR/ig-registry/master/fhir-ig-list.json"); i = 0; - List objects = JsonUtilities.objects(json, "guides"); + List objects = json.getJsonObjects("guides"); for (JsonObject o : objects) { - String pid = JsonUtilities.str(o, "npm-name"); + String pid = o.asString("npm-name"); if (pid != null && !cpidSet.contains(pid)) { cpidSet.add(pid); List vList = listVersions(pid); @@ -195,11 +193,10 @@ public class PackageVisitor { private Map getAllCIPackages() throws IOException { Map res = new HashMap<>(); if (current) { - JsonArray json = JsonTrackingParser.fetchJsonArray("https://build.fhir.org/ig/qas.json"); - for (JsonElement j : json) { - JsonObject o = (JsonObject) j; - String url = JsonUtilities.str(o, "repo"); - res.put(url, JsonUtilities.str(o, "package-id")); + JsonArray json = (JsonArray) JsonParser.parseFromUrl("https://build.fhir.org/ig/qas.json"); + for (JsonObject o : json.asJsonObjects()) { + String url = o.asString("repo"); + res.put(url, o.asString("package-id")); } } return res; @@ -220,13 +217,13 @@ public class PackageVisitor { for (PackageInfo i : pc.search(null, null, null, false)) { list.add(i.getId()); } - JsonObject json = JsonTrackingParser.fetchJson("https://raw.githubusercontent.com/FHIR/ig-registry/master/fhir-ig-list.json"); - for (JsonObject ig : JsonUtilities.objects(json, "guides")) { - list.add(JsonUtilities.str(ig, "npm-name")); + JsonObject json = JsonParser.parseObjectFromUrl("https://raw.githubusercontent.com/FHIR/ig-registry/master/fhir-ig-list.json"); + for (JsonObject ig : json.getJsonObjects("guides")) { + list.add(ig.asString("npm-name")); } - json = JsonTrackingParser.fetchJson("https://raw.githubusercontent.com/FHIR/ig-registry/master/package-feeds.json"); - for (JsonObject feed : JsonUtilities.objects(json, "feeds")) { - processFeed(list, JsonUtilities.str(feed, "url")); + json = JsonParser.parseObjectFromUrl("https://raw.githubusercontent.com/FHIR/ig-registry/master/package-feeds.json"); + for (JsonObject feed : json.getJsonObjects("feeds")) { + processFeed(list, feed.asString("url")); } return list; diff --git a/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/misc/CorePackageTools.java b/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/misc/CorePackageTools.java index ea3da93bb..61fef5bb5 100644 --- a/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/misc/CorePackageTools.java +++ b/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/misc/CorePackageTools.java @@ -7,10 +7,10 @@ import java.io.IOException; import org.hl7.fhir.exceptions.FHIRFormatError; import org.hl7.fhir.utilities.Utilities; -import org.hl7.fhir.utilities.json.JsonTrackingParser; +import org.hl7.fhir.utilities.json.model.JsonObject; +import org.hl7.fhir.utilities.json.parser.JsonParser; import org.hl7.fhir.utilities.npm.NpmPackage; -import com.google.gson.JsonObject; public class CorePackageTools { @@ -33,7 +33,7 @@ public class CorePackageTools { private void buildXml(String json, String xml, String version) throws FHIRFormatError, IOException { for (File f : new File(Utilities.path(json, "package")).listFiles()) { if (f.getName().endsWith(".json")) { - JsonObject j = new JsonTrackingParser().parseJson(f); + JsonObject j = JsonParser.parseObject(f); if (j.has("resourceType")) { if ("1.4".equals(version)) { String n = f.getName(); diff --git a/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/misc/ExamplesPackageBuilder.java b/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/misc/ExamplesPackageBuilder.java index c80be57c8..7397e6f64 100644 --- a/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/misc/ExamplesPackageBuilder.java +++ b/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/misc/ExamplesPackageBuilder.java @@ -10,9 +10,8 @@ import java.util.Set; import org.hl7.fhir.convertors.factory.VersionConvertorFactory_40_50; import org.hl7.fhir.exceptions.FHIRFormatError; import org.hl7.fhir.utilities.TextFile; -import org.hl7.fhir.utilities.json.JsonTrackingParser; - -import com.google.gson.JsonObject; +import org.hl7.fhir.utilities.json.model.JsonObject; +import org.hl7.fhir.utilities.json.parser.JsonParser; public class ExamplesPackageBuilder { @@ -25,10 +24,10 @@ public class ExamplesPackageBuilder { Set set = new HashSet<>(); for (File f : new File(source).listFiles()) { if (f.getName().endsWith(".json")) { - JsonObject obj = JsonTrackingParser.parseJson(new FileInputStream(f)); + JsonObject obj = JsonParser.parseObject(new FileInputStream(f)); if (obj.has("resourceType") && obj.has("id")) { - String type = obj.get("resourceType").getAsString(); - String id = obj.get("id").getAsString(); + String type = obj.asString("resourceType"); + String id = obj.asString("id"); byte[] content = TextFile.fileToBytes(f); if (type.equals("ConceptMap")) { System.out.println("convert "+f.getName()); diff --git a/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/misc/ICD11Generator.java b/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/misc/ICD11Generator.java index 208cf3e74..61b498057 100644 --- a/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/misc/ICD11Generator.java +++ b/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/misc/ICD11Generator.java @@ -28,10 +28,9 @@ import org.hl7.fhir.r4.utils.ToolingExtensions; import org.hl7.fhir.utilities.SimpleHTTPClient; import org.hl7.fhir.utilities.SimpleHTTPClient.HTTPResult; import org.hl7.fhir.utilities.Utilities; -import org.hl7.fhir.utilities.json.JsonTrackingParser; - -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; +import org.hl7.fhir.utilities.json.model.JsonElement; +import org.hl7.fhir.utilities.json.model.JsonObject; +import org.hl7.fhir.utilities.json.parser.JsonParser; public class ICD11Generator { @@ -42,12 +41,12 @@ public class ICD11Generator { private void execute(String base, String dest) throws IOException { CodeSystem cs = makeMMSCodeSystem(); JsonObject version = fetchJson(Utilities.pathURL(base, "/icd/release/11/mms")); - String[] p = version.get("latestRelease").getAsString().split("\\/"); + String[] p = version.asString("latestRelease").split("\\/"); cs.setVersion(p[6]); - JsonObject root = fetchJson(url(base, version.get("latestRelease").getAsString())); - cs.setDateElement(new DateTimeType(root.get("releaseDate").getAsString())); - for (JsonElement child : root.getAsJsonArray("child")) { - processMMSEntity(cs, base, child.getAsString(), cs.addConcept(), dest); + JsonObject root = fetchJson(url(base, version.asString("latestRelease"))); + cs.setDateElement(new DateTimeType(root.asString("releaseDate"))); + for (JsonElement child : root.getJsonArray("child")) { + processMMSEntity(cs, base, child.asString(), cs.addConcept(), dest); System.out.println(); } new XmlParser(XmlVersion.V1_1).setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(Utilities.path(dest, "icd-11-mms.xml")), cs); @@ -55,12 +54,12 @@ public class ICD11Generator { cs = makeEntityCodeSystem(); root = fetchJson(Utilities.pathURL(base, "/icd/entity")); - cs.setVersion(root.get("releaseId").getAsString()); - cs.setDateElement(new DateTimeType(root.get("releaseDate").getAsString())); + cs.setVersion(root.asString("releaseId")); + cs.setDateElement(new DateTimeType(root.asString("releaseDate"))); cs.setTitle(readString(root, "title")); Set ids = new HashSet<>(); - for (JsonElement child : root.getAsJsonArray("child")) { - processEntity(cs, ids, base, tail(child.getAsString()), dest); + for (JsonElement child : root.getJsonArray("child")) { + processEntity(cs, ids, base, tail(child.asString()), dest); System.out.println(); } new XmlParser(XmlVersion.V1_1).setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(Utilities.path(dest, "icd-11-foundation.xml")), cs); @@ -80,34 +79,34 @@ public class ICD11Generator { cc.setDefinition(d); } if (entity.has("inclusion")) { - for (JsonElement child : entity.getAsJsonArray("inclusion")) { + for (JsonElement child : entity.getJsonArray("inclusion")) { JsonObject co = (JsonObject) child; String v = readString(co, "label"); if (v != null) { if (co.has("foundationReference")) { - cc.addProperty().setValue(new Coding().setSystem("http://id.who.int/icd11/foundation").setCode(tail(co.get("foundationReference").getAsString())).setDisplay(v)).setCode("inclusion"); + cc.addProperty().setValue(new Coding().setSystem("http://id.who.int/icd11/foundation").setCode(tail(co.asString("foundationReference"))).setDisplay(v)).setCode("inclusion"); } } } } if (entity.has("exclusion")) { - for (JsonElement child : entity.getAsJsonArray("exclusion")) { + for (JsonElement child : entity.getJsonArray("exclusion")) { JsonObject co = (JsonObject) child; String v = readString(co, "label"); if (v != null) { if (co.has("foundationReference")) { - cc.addProperty().setValue(new Coding().setSystem("http://id.who.int/icd11/foundation").setCode(tail(co.get("foundationReference").getAsString())).setDisplay(v)).setCode("exclusion"); + cc.addProperty().setValue(new Coding().setSystem("http://id.who.int/icd11/foundation").setCode(tail(co.asString("foundationReference"))).setDisplay(v)).setCode("exclusion"); } } } } if (entity.has("narrowerTerm")) { - for (JsonElement child : entity.getAsJsonArray("narrowerTerm")) { + for (JsonElement child : entity.getJsonArray("narrowerTerm")) { JsonObject co = (JsonObject) child; String v = readString(co, "label"); if (v != null) { if (co.has("narrowerTerm")) { - cc.addProperty().setValue(new Coding().setSystem("http://id.who.int/icd11/foundation").setCode(tail(co.get("foundationReference").getAsString())).setDisplay(v)).setCode("narrowerTerm"); + cc.addProperty().setValue(new Coding().setSystem("http://id.who.int/icd11/foundation").setCode(tail(co.asString("foundationReference"))).setDisplay(v)).setCode("narrowerTerm"); } } } @@ -115,22 +114,22 @@ public class ICD11Generator { addDesignation(readString(entity, "longDefinition"), cc, "http://id.who.int/icd11/mms/designation", "longDefinition"); addDesignation(readString(entity, "fullySpecifiedName"), cc, "http://snomed.info/sct", "900000000000003001"); if (entity.has("synonym")) { - for (JsonElement j : entity.getAsJsonArray("synonym")) { + for (JsonElement j : entity.getJsonArray("synonym")) { String v = readString((JsonObject) j, "label"); if (v != null && !v.equals(cc.getDisplay())) { addDesignation(v, cc, "http://id.who.int/icd11/mms/designation", "synonym"); } } } - for (JsonElement j : entity.getAsJsonArray("parent")) { - String v = j.getAsString(); + for (JsonElement j : entity.getJsonArray("parent")) { + String v = j.asString(); if (!"http://id.who.int/icd/entity".equals(v)) { cc.addProperty().setValue(new CodeType(tail(v))).setCode("narrowerTerm"); } } if (entity.has("child")) { - for (JsonElement j : entity.getAsJsonArray("child")) { - String v = j.getAsString(); + for (JsonElement j : entity.getJsonArray("child")) { + String v = j.asString(); cc.addProperty().setValue(new CodeType(tail(v))).setCode("child"); processEntity(cs, ids, base, tail(v), dest); } @@ -180,16 +179,16 @@ public class ICD11Generator { System.out.print("."); JsonObject entity = fetchJson(url(base, ref)); cc.setId(tail(ref)); - if (entity.has("code") && !Utilities.noString(entity.get("code").getAsString())) { - cc.setCode(entity.get("code").getAsString()); - } else if (entity.has("blockId") && !Utilities.noString(entity.get("blockId").getAsString())) { - cc.setCode(entity.get("blockId").getAsString()); + if (entity.has("code") && !Utilities.noString(entity.asString("code"))) { + cc.setCode(entity.asString("code")); + } else if (entity.has("blockId") && !Utilities.noString(entity.asString("blockId"))) { + cc.setCode(entity.asString("blockId")); } else { cc.setCode(cc.getId()); cc.addProperty().setCode("abstract").setValue(new BooleanType(true)); } - if (entity.has("classKind") && !Utilities.noString(entity.get("classKind").getAsString()) && !"category".equals(entity.get("classKind").getAsString())) { - cc.addProperty().setCode("kind").setValue(new CodeType(entity.get("classKind").getAsString())); + if (entity.has("classKind") && !Utilities.noString(entity.asString("classKind")) && !"category".equals(entity.asString("classKind"))) { + cc.addProperty().setCode("kind").setValue(new CodeType(entity.asString("classKind"))); } cc.setDisplay(readString(entity, "title")); StringBuilder defn = new StringBuilder(); @@ -203,7 +202,7 @@ public class ICD11Generator { if (entity.has("inclusion")) { defn.append(". Includes: "); boolean first = true; - for (JsonElement child : entity.getAsJsonArray("inclusion")) { + for (JsonElement child : entity.getJsonArray("inclusion")) { if (first) first = false; else defn.append(", "); defn.append(readString((JsonObject) child, "label")); @@ -212,7 +211,7 @@ public class ICD11Generator { if (entity.has("exclusion")) { defn.append(". Excludes: "); boolean first = true; - for (JsonElement child : entity.getAsJsonArray("exclusion")) { + for (JsonElement child : entity.getJsonArray("exclusion")) { if (first) first = false; else defn.append(", "); JsonObject co = (JsonObject) child; @@ -220,7 +219,7 @@ public class ICD11Generator { if (v != null) { defn.append(v); if (co.has("linearizationReference")) { - cc.addProperty().setValue(new Coding().setSystem("http://id.who.int/icd11/mms").setCode(tail(co.get("linearizationReference").getAsString())).setDisplay(v)).setCode("exclusion"); + cc.addProperty().setValue(new Coding().setSystem("http://id.who.int/icd11/mms").setCode(tail(co.asString("linearizationReference"))).setDisplay(v)).setCode("exclusion"); } } } @@ -231,33 +230,33 @@ public class ICD11Generator { addProperty(readString(entity, "codingNote"), cc, "codingNote"); if (entity.has("indexTerm")) { // CommaSeparatedStringBuilder b = new CommaSeparatedStringBuilder("; "); -// for (JsonElement child : entity.getAsJsonArray("indexTerm")) { +// for (JsonElement child : entity.getJsonArray("indexTerm")) { // processIndexTerm(cc, b, (JsonObject) child); // } // if (b.length() > 0) { // cc.addProperty().setCode("terms").setValue(new StringType(b.toString())); // } - for (JsonElement child : entity.getAsJsonArray("indexTerm")) { + for (JsonElement child : entity.getJsonArray("indexTerm")) { processIndexTerm(cc, (JsonObject) child); } } if (entity.has("postcoordinationScale")) { - for (JsonElement child : entity.getAsJsonArray("postcoordinationScale")) { + for (JsonElement child : entity.getJsonArray("postcoordinationScale")) { JsonObject o = (JsonObject) child; - String name = tail(o.get("axisName").getAsString()); + String name = tail(o.asString("axisName")); ConceptPropertyComponent prop = cc.addProperty(); prop.setCode("postcoordinationScale"); prop.setValue(new CodeType(name)); - ToolingExtensions.addBooleanExtension(prop, "http://id.who.int/icd11/extensions/required", o.get("requiredPostcoordination").getAsBoolean()); - ToolingExtensions.addBooleanExtension(prop, "http://id.who.int/icd11/extensions/repeats", o.get("allowMultipleValues").getAsBoolean()); + ToolingExtensions.addBooleanExtension(prop, "http://id.who.int/icd11/extensions/required", o.asBoolean("requiredPostcoordination")); + ToolingExtensions.addBooleanExtension(prop, "http://id.who.int/icd11/extensions/repeats", o.asBoolean("allowMultipleValues")); if (o.has("scaleEntity")) { ToolingExtensions.addUriExtension(prop, "http://id.who.int/icd11/extensions/valueSet", buildValueSet(cs, cc.getCode(), name, o, dest)); } } } if (entity.has("child")) { - for (JsonElement child : entity.getAsJsonArray("child")) { - processMMSEntity(cs, base, child.getAsString(), cc.addConcept(), dest); + for (JsonElement child : entity.getJsonArray("child")) { + processMMSEntity(cs, base, child.asString(), cc.addConcept(), dest); } } } @@ -279,8 +278,8 @@ public class ICD11Generator { vs.setStatus(cs.getStatus()); ConceptSetComponent inc = vs.getCompose().addInclude(); inc.setSystem(cs.getUrl()); - for (JsonElement e : o.getAsJsonArray("scaleEntity")) { - inc.addFilter().setProperty("concept").setOp(FilterOperator.ISA).setValue(tail(e.getAsString())); + for (JsonElement e : o.getJsonArray("scaleEntity")) { + inc.addFilter().setProperty("concept").setOp(FilterOperator.ISA).setValue(tail(e.asString())); } new XmlParser(XmlVersion.V1_1).setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(Utilities.path(dest, "vs-" + id + ".xml")), vs); return url; @@ -328,12 +327,12 @@ public class ICD11Generator { } private String readString(JsonObject obj, String name) { - JsonObject p = obj.getAsJsonObject(name); + JsonObject p = obj.getJsonObject(name); if (p == null) { return null; } if (p.has("@value")) { - return p.get("@value").getAsString(); + return p.get("@value").asString(); } return null; } @@ -400,7 +399,7 @@ public class ICD11Generator { http.addHeader("Accept-Language", "en"); HTTPResult res = http.get(source, "application/json"); res.checkThrowException(); - return JsonTrackingParser.parseJson(res.getContent()); + return JsonParser.parseObject(res.getContent()); } diff --git a/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/misc/JsonProcessor.java b/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/misc/JsonProcessor.java index a46bd5c5b..2a0b11ec2 100644 --- a/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/misc/JsonProcessor.java +++ b/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/misc/JsonProcessor.java @@ -1,11 +1,12 @@ package org.hl7.fhir.convertors.misc; import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; -import org.hl7.fhir.utilities.json.JsonTrackingParser; +import org.hl7.fhir.utilities.json.model.JsonObject; +import org.hl7.fhir.utilities.json.parser.JsonParser; -import com.google.gson.JsonObject; public class JsonProcessor { public static void main(String[] args) throws Exception { @@ -13,9 +14,9 @@ public class JsonProcessor { } private void process(String source) throws IOException { - JsonObject json = JsonTrackingParser.parseJsonFile(source); + JsonObject json = JsonParser.parseObjectFromFile(source); process(json); - JsonTrackingParser.write(json, new File(source), true); + JsonParser.compose(json, new FileOutputStream(source), true); } @@ -178,11 +179,11 @@ public class JsonProcessor { } private void process(JsonObject json, String name) { - JsonObject j = json.getAsJsonObject(name); + JsonObject j = json.getJsonObject(name); if (j == null) { System.out.println("Can't find "+name); } else { - j.addProperty("modifier", true); + j.add("modifier", true); } } diff --git a/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/misc/NpmPackageVersionConverter.java b/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/misc/NpmPackageVersionConverter.java index 4b7a6d345..6c19fea01 100644 --- a/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/misc/NpmPackageVersionConverter.java +++ b/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/misc/NpmPackageVersionConverter.java @@ -29,13 +29,12 @@ import org.hl7.fhir.convertors.factory.VersionConvertorFactory_30_50; import org.hl7.fhir.convertors.factory.VersionConvertorFactory_40_50; import org.hl7.fhir.utilities.TextFile; import org.hl7.fhir.utilities.VersionUtilities; -import org.hl7.fhir.utilities.json.JsonTrackingParser; -import org.hl7.fhir.utilities.json.JsonUtilities; +import org.hl7.fhir.utilities.json.model.JsonArray; +import org.hl7.fhir.utilities.json.model.JsonObject; +import org.hl7.fhir.utilities.json.parser.JsonParser; import org.hl7.fhir.utilities.npm.NpmPackageIndexBuilder; import com.google.common.base.Charsets; -import com.google.gson.JsonArray; -import com.google.gson.JsonObject; public class NpmPackageVersionConverter { @@ -105,7 +104,7 @@ public class NpmPackageVersionConverter { if (!e.getKey().equals("package/package.json")) { byte[] cnv = e.getValue(); try { - JsonObject json = JsonTrackingParser.parseJson(e.getValue()); + JsonObject json = JsonParser.parseObject(e.getValue()); if (json.has("resourceType")) { cnv = convertResource(e.getKey(), e.getValue()); } @@ -174,11 +173,11 @@ public class NpmPackageVersionConverter { } private byte[] convertPackage(byte[] cnt) throws IOException { - JsonObject json = JsonTrackingParser.parseJson(cnt); - currentVersion = json.getAsJsonArray("fhirVersions").get(0).getAsString(); - String name = JsonUtilities.str(json, "name"); + JsonObject json = JsonParser.parseObject(cnt); + currentVersion = json.getJsonArray("fhirVersions").get(0).asString(); + String name = json.asString("name"); json.remove("name"); - json.addProperty("name", name + "." + vCode); + json.add("name", name + "." + vCode); json.remove("fhirVersions"); json.remove("dependencies"); JsonArray fv = new JsonArray(); @@ -186,8 +185,8 @@ public class NpmPackageVersionConverter { fv.add(version); JsonObject dep = new JsonObject(); json.add("dependencies", dep); - dep.addProperty(VersionUtilities.packageForVersion(version), version); - return JsonTrackingParser.write(json).getBytes(Charsets.UTF_8); + dep.add(VersionUtilities.packageForVersion(version), version); + return JsonParser.composeBytes(json); } private byte[] convertResource(String n, byte[] cnt) { diff --git a/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/misc/PackageMaintainer.java b/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/misc/PackageMaintainer.java index 021066544..198823f85 100644 --- a/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/misc/PackageMaintainer.java +++ b/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/misc/PackageMaintainer.java @@ -7,7 +7,8 @@ import java.util.List; import org.hl7.fhir.utilities.TextFile; import org.hl7.fhir.utilities.Utilities; -import org.hl7.fhir.utilities.json.JsonTrackingParser; +import org.hl7.fhir.utilities.json.model.JsonObject; +import org.hl7.fhir.utilities.json.parser.JsonParser; /* Copyright (c) 2011+, HL7, Inc. @@ -38,11 +39,6 @@ import org.hl7.fhir.utilities.json.JsonTrackingParser; */ - -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.google.gson.JsonObject; - public class PackageMaintainer { @@ -96,11 +92,10 @@ public class PackageMaintainer { if (f.isDirectory()) strip(f); else if (f.getName().endsWith(".json")) { - JsonObject json = JsonTrackingParser.parseJson(f); + JsonObject json = JsonParser.parseObject(f); if (json.has("resourceType") && json.has("text")) { json.remove("text"); - Gson gson = new GsonBuilder().create(); - String src = gson.toJson(json); + String src = JsonParser.compose(json); TextFile.stringToFile(src, f.getAbsolutePath()); } } diff --git a/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/misc/XMLPackageConvertor.java b/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/misc/XMLPackageConvertor.java index 974eb2093..8de59681a 100644 --- a/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/misc/XMLPackageConvertor.java +++ b/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/misc/XMLPackageConvertor.java @@ -5,12 +5,10 @@ import java.io.FileInputStream; import java.io.IOException; import java.util.Map.Entry; +import org.hl7.fhir.utilities.json.model.JsonObject; +import org.hl7.fhir.utilities.json.model.JsonProperty; import org.hl7.fhir.utilities.npm.NpmPackage; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; - - public class XMLPackageConvertor { public static void main(String[] args) throws IOException { @@ -26,13 +24,13 @@ public class XMLPackageConvertor { System.out.println("Package " + f.getAbsolutePath()); NpmPackage p = NpmPackage.fromPackage(new FileInputStream(f)); if (p.getNpm().has("dependencies")) { - JsonObject dep = p.getNpm().getAsJsonObject("dependencies"); - if (dep.entrySet().isEmpty()) { + JsonObject dep = p.getNpm().getJsonObject("dependencies"); + if (dep.getProperties().isEmpty()) { System.out.println(" Dependencies: none"); } else { System.out.println(" Dependencies:"); - for (Entry e : dep.entrySet()) { - System.out.println(" " + e.getKey() + ": " + e.getValue().getAsString()); + for (JsonProperty e : dep.getProperties()) { + System.out.println(" " + e.getName() + ": " + e.getValue().toString()); } } } else { diff --git a/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/misc/xver/CorePackageVersionConvertor.java b/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/misc/xver/CorePackageVersionConvertor.java index 5df0aaadd..7c8069c53 100644 --- a/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/misc/xver/CorePackageVersionConvertor.java +++ b/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/misc/xver/CorePackageVersionConvertor.java @@ -17,13 +17,12 @@ import org.hl7.fhir.convertors.factory.VersionConvertorFactory_40_50; import org.hl7.fhir.convertors.misc.xver.CorePackageVersionConvertor.BaseConvertor; import org.hl7.fhir.utilities.Utilities; import org.hl7.fhir.utilities.VersionUtilities; -import org.hl7.fhir.utilities.json.JsonTrackingParser; -import org.hl7.fhir.utilities.json.JsonUtilities; +import org.hl7.fhir.utilities.json.model.JsonArray; +import org.hl7.fhir.utilities.json.model.JsonObject; +import org.hl7.fhir.utilities.json.parser.JsonParser; import org.hl7.fhir.utilities.npm.NpmPackage; import org.hl7.fhir.utilities.npm.NpmPackage.NpmPackageFolder; -import com.google.gson.JsonArray; -import com.google.gson.JsonObject; public class CorePackageVersionConvertor { @@ -582,13 +581,13 @@ public class CorePackageVersionConvertor { } private byte[] convertPackage(byte[] cnt, String version) throws IOException { - JsonObject json = JsonTrackingParser.parseJson(cnt); + JsonObject json = JsonParser.parseObject(cnt); json.remove("fhir-version-list"); JsonArray vl = new JsonArray(); json.add("fhirVersions", vl); vl.add(version); - json.addProperty("name", JsonUtilities.str(json, "name")+".as."+VersionUtilities.getNameForVersion(version).toLowerCase()); - json.addProperty("title", JsonUtilities.str(json, "title")+" (as Version "+VersionUtilities.getNameForVersion(version).toLowerCase()+")"); - return JsonTrackingParser.write(json).getBytes(StandardCharsets.UTF_8); + json.add("name", json.asString("name")+".as."+VersionUtilities.getNameForVersion(version).toLowerCase()); + json.add("title", json.asString("title")+" (as Version "+VersionUtilities.getNameForVersion(version).toLowerCase()+")"); + return JsonParser.composeBytes(json); } } diff --git a/org.hl7.fhir.dstu3/src/main/java/org/hl7/fhir/dstu3/context/SimpleWorkerContext.java b/org.hl7.fhir.dstu3/src/main/java/org/hl7/fhir/dstu3/context/SimpleWorkerContext.java index 0dd0887fe..bc6b2bec3 100644 --- a/org.hl7.fhir.dstu3/src/main/java/org/hl7/fhir/dstu3/context/SimpleWorkerContext.java +++ b/org.hl7.fhir.dstu3/src/main/java/org/hl7/fhir/dstu3/context/SimpleWorkerContext.java @@ -156,7 +156,7 @@ public class SimpleWorkerContext extends BaseWorkerContext implements IWorkerCon public static SimpleWorkerContext fromPackage(NpmPackage pi, IContextResourceLoader loader) throws FileNotFoundException, IOException, FHIRException { SimpleWorkerContext res = new SimpleWorkerContext(); res.setAllowLoadingDuplicates(true); - res.version = pi.getNpm().get("version").getAsString(); + res.version = pi.getNpm().asString("version"); res.loadFromPackage(pi, loader); return res; } diff --git a/org.hl7.fhir.r4/src/main/java/org/hl7/fhir/r4/context/SimpleWorkerContext.java b/org.hl7.fhir.r4/src/main/java/org/hl7/fhir/r4/context/SimpleWorkerContext.java index c2c5c332f..2d273468d 100644 --- a/org.hl7.fhir.r4/src/main/java/org/hl7/fhir/r4/context/SimpleWorkerContext.java +++ b/org.hl7.fhir.r4/src/main/java/org/hl7/fhir/r4/context/SimpleWorkerContext.java @@ -163,7 +163,7 @@ public class SimpleWorkerContext extends BaseWorkerContext implements IWorkerCon public static SimpleWorkerContext fromPackage(NpmPackage pi, IContextResourceLoader loader) throws FileNotFoundException, IOException, FHIRException { SimpleWorkerContext res = new SimpleWorkerContext(); res.setAllowLoadingDuplicates(true); - res.version = pi.getNpm().get("version").getAsString(); + res.version = pi.getNpm().asString("version"); res.loadFromPackage(pi, loader); return res; } diff --git a/org.hl7.fhir.r4b/src/main/java/org/hl7/fhir/r4b/context/SimpleWorkerContext.java b/org.hl7.fhir.r4b/src/main/java/org/hl7/fhir/r4b/context/SimpleWorkerContext.java index c87ea3b92..6dedc8028 100644 --- a/org.hl7.fhir.r4b/src/main/java/org/hl7/fhir/r4b/context/SimpleWorkerContext.java +++ b/org.hl7.fhir.r4b/src/main/java/org/hl7/fhir/r4b/context/SimpleWorkerContext.java @@ -216,7 +216,7 @@ public class SimpleWorkerContext extends BaseWorkerContext implements IWorkerCon public static SimpleWorkerContext fromPackage(NpmPackage pi, IContextResourceLoader loader) throws FileNotFoundException, IOException, FHIRException { SimpleWorkerContext res = new SimpleWorkerContext(); res.setAllowLoadingDuplicates(true); - res.version = pi.getNpm().get("version").getAsString(); + res.version = pi.getNpm().asString("version"); res.loadFromPackage(pi, loader); res.finishLoading(); return res; diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/SimpleWorkerContext.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/SimpleWorkerContext.java index 014838813..f046e386a 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/SimpleWorkerContext.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/SimpleWorkerContext.java @@ -250,7 +250,7 @@ public class SimpleWorkerContext extends BaseWorkerContext implements IWorkerCon public SimpleWorkerContext fromPackage(NpmPackage pi, IContextResourceLoader loader) throws IOException, FHIRException { SimpleWorkerContext context = getSimpleWorkerContextInstance(); context.setAllowLoadingDuplicates(allowLoadingDuplicates); - context.version = pi.getNpm().get("version").getAsString(); + context.version = pi.getNpm().asString("version"); context.loadFromPackage(pi, loader); context.finishLoading(); return build(context); diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/NPMPackageGenerator.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/NPMPackageGenerator.java index fa68b2629..bc11bf9c4 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/NPMPackageGenerator.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/NPMPackageGenerator.java @@ -62,16 +62,14 @@ import org.hl7.fhir.r5.model.ImplementationGuide.ImplementationGuideDependsOnCom import org.hl7.fhir.utilities.CommaSeparatedStringBuilder; import org.hl7.fhir.utilities.TextFile; import org.hl7.fhir.utilities.Utilities; +import org.hl7.fhir.utilities.json.model.JsonArray; +import org.hl7.fhir.utilities.json.model.JsonObject; +import org.hl7.fhir.utilities.json.model.JsonString; +import org.hl7.fhir.utilities.json.parser.JsonParser; import org.hl7.fhir.utilities.npm.NpmPackageIndexBuilder; import org.hl7.fhir.utilities.npm.ToolsVersion; import org.hl7.fhir.utilities.npm.PackageGenerator.PackageType; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.google.gson.JsonArray; -import com.google.gson.JsonObject; -import com.google.gson.JsonPrimitive; - public class NPMPackageGenerator { public enum Category { @@ -118,13 +116,13 @@ public class NPMPackageGenerator { public static NPMPackageGenerator subset(NPMPackageGenerator master, String destFile, String id, String name, Date date, boolean notForPublication) throws FHIRException, IOException { JsonObject p = master.packageJ.deepCopy(); p.remove("name"); - p.addProperty("name", id); + p.add("name", id); p.remove("type"); - p.addProperty("type", PackageType.CONFORMANCE.getCode()); + p.add("type", PackageType.CONFORMANCE.getCode()); p.remove("title"); - p.addProperty("title", name); + p.add("title", name); if (notForPublication) { - p.addProperty("notForPublication", true); + p.add("notForPublication", true); } return new NPMPackageGenerator(destFile, p, date, notForPublication); @@ -142,17 +140,16 @@ public class NPMPackageGenerator { String dt = new SimpleDateFormat("yyyyMMddHHmmss").format(date); packageJ = npm; packageManifest = new JsonObject(); - packageManifest.addProperty("version", npm.get("version").getAsString()); - packageManifest.addProperty("date", dt); + packageManifest.set("version", npm.asString("version")); + packageManifest.set("date", dt); if (notForPublication) { - packageManifest.addProperty("notForPublication", true); + packageManifest.add("notForPublication", true); } - npm.addProperty("date", dt); - packageManifest.addProperty("name", npm.get("name").getAsString()); + npm.set("date", dt); + packageManifest.set("name", npm.asString("name")); this.destFile = destFile; start(); - Gson gson = new GsonBuilder().setPrettyPrinting().create(); - String json = gson.toJson(npm); + String json = JsonParser.compose(npm, true); try { addFile(Category.RESOURCE, "package.json", json.getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { @@ -183,31 +180,31 @@ public class NPMPackageGenerator { } JsonObject npm = new JsonObject(); - npm.addProperty("name", ig.getPackageId()); - npm.addProperty("version", ig.getVersion()); + npm.add("name", ig.getPackageId()); + npm.add("version", ig.getVersion()); igVersion = ig.getVersion(); - npm.addProperty("tools-version", ToolsVersion.TOOLS_VERSION); - npm.addProperty("type", kind.getCode()); - npm.addProperty("date", dt); + npm.add("tools-version", ToolsVersion.TOOLS_VERSION); + npm.add("type", kind.getCode()); + npm.add("date", dt); if (ig.hasLicense()) { - npm.addProperty("license", ig.getLicense().toCode()); + npm.add("license", ig.getLicense().toCode()); } - npm.addProperty("canonical", canonical); + npm.add("canonical", canonical); if (notForPublication) { - npm.addProperty("notForPublication", true); + npm.add("notForPublication", true); } - npm.addProperty("url", web); + npm.add("url", web); if (ig.hasTitle()) { - npm.addProperty("title", ig.getTitle()); + npm.add("title", ig.getTitle()); } if (ig.hasDescription()) { - npm.addProperty("description", ig.getDescription()+ " (built "+dtHuman+timezone()+")"); + npm.add("description", ig.getDescription()+ " (built "+dtHuman+timezone()+")"); } JsonArray vl = new JsonArray(); npm.add("fhirVersions", vl); for (String v : fhirVersion) { - vl.add(new JsonPrimitive(v)); + vl.add(new JsonString(v)); } if (kind != PackageType.CORE) { @@ -216,15 +213,15 @@ public class NPMPackageGenerator { for (String v : fhirVersion) { String vp = packageForVersion(v); if (vp != null ) { - dep.addProperty(vp, v); + dep.add(vp, v); } } for (ImplementationGuideDependsOnComponent d : ig.getDependsOn()) { - dep.addProperty(d.getPackageId(), d.getVersion()); + dep.add(d.getPackageId(), d.getVersion()); } } if (ig.hasPublisher()) { - npm.addProperty("author", ig.getPublisher()); + npm.add("author", ig.getPublisher()); } JsonArray m = new JsonArray(); for (ContactDetail t : ig.getContact()) { @@ -233,23 +230,22 @@ public class NPMPackageGenerator { if (t.hasName() & (email != null || url != null)) { JsonObject md = new JsonObject(); m.add(md); - md.addProperty("name", t.getName()); + md.add("name", t.getName()); if (email != null) - md.addProperty("email", email); + md.add("email", email); if (url != null) - md.addProperty("url", url); + md.add("url", url); } } if (m.size() > 0) npm.add("maintainers", m); if (ig.getManifest().hasRendering()) - npm.addProperty("homepage", ig.getManifest().getRendering()); + npm.add("homepage", ig.getManifest().getRendering()); JsonObject dir = new JsonObject(); npm.add("directories", dir); - dir.addProperty("lib", "package"); - dir.addProperty("example", "example"); - Gson gson = new GsonBuilder().setPrettyPrinting().create(); - String json = gson.toJson(npm); + dir.add("lib", "package"); + dir.add("example", "example"); + String json = JsonParser.compose(npm, true); try { addFile(Category.RESOURCE, "package.json", json.getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { @@ -257,14 +253,14 @@ public class NPMPackageGenerator { packageJ = npm; packageManifest = new JsonObject(); - packageManifest.addProperty("version", ig.getVersion()); + packageManifest.add("version", ig.getVersion()); JsonArray fv = new JsonArray(); for (String v : fhirVersion) { fv.add(v); } packageManifest.add("fhirVersion", fv); - packageManifest.addProperty("date", dt); - packageManifest.addProperty("name", ig.getPackageId()); + packageManifest.add("date", dt); + packageManifest.add("name", ig.getPackageId()); } @@ -380,8 +376,7 @@ public class NPMPackageGenerator { OutputStream.close(); TextFile.bytesToFile(OutputStream.toByteArray(), destFile); // also, for cache management on current builds, generate a little manifest - Gson gson = new GsonBuilder().setPrettyPrinting().create(); - String json = gson.toJson(packageManifest); + String json = JsonParser.compose(packageManifest, true); TextFile.stringToFile(json, Utilities.changeFileExt(destFile, ".manifest.json"), false); } diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/XVerExtensionManager.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/XVerExtensionManager.java index 6e36e6be6..efec9156f 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/XVerExtensionManager.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/XVerExtensionManager.java @@ -62,9 +62,9 @@ public class XVerExtensionManager { } } JsonObject root = lists.get(v); - JsonObject path = root.getObject(e); + JsonObject path = root.getJsonObject(e); if (path == null) { - path = root.getObject(e+"[x]"); + path = root.getJsonObject(e+"[x]"); } if (path == null) { return XVerExtensionStatus.Unknown; @@ -85,9 +85,9 @@ public class XVerExtensionManager { String verTarget = VersionUtilities.getMajMin(context.getVersion()); String e = url.substring(54); JsonObject root = lists.get(verSource); - JsonObject path = root.getObject(e); + JsonObject path = root.getJsonObject(e); if (path == null) { - path = root.getObject(e+"[x]"); + path = root.getJsonObject(e+"[x]"); } StructureDefinition sd = new StructureDefinition(); @@ -116,9 +116,9 @@ public class XVerExtensionManager { populateTypes(path, val, verSource, verTarget); } else if (path.has("elements")) { for (JsonElement i : path.forceArray("elements").getItems()) { - JsonObject elt = root.getObject(e+"."+i.toString()); + JsonObject elt = root.getJsonObject(e+"."+i.asString()); if (elt != null) { - String s = i.toString().replace("[x]", ""); + String s = i.asString().replace("[x]", ""); sd.getDifferential().addElement().setPath("Extension.extension").setSliceName(s); sd.getDifferential().addElement().setPath("Extension.extension.extension").setMax("0"); sd.getDifferential().addElement().setPath("Extension.extension.url").setFixed(new UriType(s)); @@ -144,7 +144,7 @@ public class XVerExtensionManager { public void populateTypes(JsonObject path, ElementDefinition val, String verSource, String verTarget) { for (JsonElement i : path.forceArray("types").getItems()) { - String s = i.toString(); + String s = i.asString(); if (!s.startsWith("!")) { if (s.contains("(")) { String t = s.substring(0, s.indexOf("(")); diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/json/model/JsonArray.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/json/model/JsonArray.java index 7a46c1acc..33ff0411b 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/json/model/JsonArray.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/json/model/JsonArray.java @@ -1,12 +1,13 @@ package org.hl7.fhir.utilities.json.model; import java.util.ArrayList; +import java.util.Iterator; import java.util.List; import org.hl7.fhir.utilities.json.JsonException; -public class JsonArray extends JsonElement { +public class JsonArray extends JsonElement implements Iterable { private List items = new ArrayList<>(); private List noCommas; // validator use private List unQuoted; // validator use @@ -15,7 +16,7 @@ public class JsonArray extends JsonElement { List list = new ArrayList<>(); for (JsonElement n : items) { if (n instanceof JsonPrimitive) { - list.add(n.toString()); + list.add(n.asJsonPrimitive().getValue()); } } return list; @@ -25,7 +26,7 @@ public class JsonArray extends JsonElement { return items; } - public List asObjects() { + public List asJsonObjects() { List list = new ArrayList<>(); for (JsonElement n : items) { if (n instanceof JsonObject) { @@ -79,11 +80,50 @@ public class JsonArray extends JsonElement { public JsonObject findByStringProp(JsonArray arr, String prop, String value) { - for (JsonObject obj : asObjects()) { - if (obj.has(prop) && value.equals(obj.getString(prop))) + for (JsonObject obj : asJsonObjects()) { + if (obj.has(prop) && value.equals(obj.asString(prop))) return obj; } return null; } + public Iterator iterator() { + return items.iterator(); + } + + public JsonElement get(int i) { + return items.get(i); + } + + public JsonArray deepCopy() { + return (JsonArray) make().copy(this); + } + + @Override + protected JsonElement copy(JsonElement other) { + JsonArray o = (JsonArray) other; + for (JsonElement p : o.getItems()) { + add(p.deepCopy()); + } + return this; + } + + @Override + protected JsonElement make() { + return new JsonArray(); + } + + @Override + public String toString() { + StringBuilder b = new StringBuilder(); + b.append("[ "); + boolean first = true; + for (JsonElement p : items) { + if (first) first = false; else b.append(", "); + b.append(p.toString()); + } + b.append(" ]"); + return b.toString(); + } + } diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/json/model/JsonBoolean.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/json/model/JsonBoolean.java index 9d63174f2..4f9b34816 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/json/model/JsonBoolean.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/json/model/JsonBoolean.java @@ -8,6 +8,9 @@ public class JsonBoolean extends JsonPrimitive { this.value = value; } + private JsonBoolean() { + } + public JsonElementType elementType() { return JsonElementType.BOOLEAN; } @@ -20,6 +23,7 @@ public class JsonBoolean extends JsonPrimitive { this.value = value; } + @Override public String getValue() { return value ? "true" : "false"; } @@ -28,4 +32,15 @@ public class JsonBoolean extends JsonPrimitive { public String toString() { return getValue(); } + + @Override + protected JsonElement copy(JsonElement other) { + value = ((JsonBoolean) other).value; + return this; + } + + @Override + protected JsonElement make() { + return new JsonBoolean(); + } } diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/json/model/JsonComment.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/json/model/JsonComment.java deleted file mode 100644 index c5fb78ba0..000000000 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/json/model/JsonComment.java +++ /dev/null @@ -1,5 +0,0 @@ -package org.hl7.fhir.utilities.json.model; - -public class JsonComment { - -} diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/json/model/JsonElement.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/json/model/JsonElement.java index 456792aae..aa60d31ca 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/json/model/JsonElement.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/json/model/JsonElement.java @@ -45,4 +45,71 @@ public abstract class JsonElement { public boolean hasComments() { return comments != null && !comments.isEmpty(); } + + public JsonElement deepCopy() { + return make().copy(this); + } + + protected abstract JsonElement copy(JsonElement jsonElement); + protected abstract JsonElement make(); + + public boolean isJsonObject() { + return elementType() == JsonElementType.OBJECT; + } + + public boolean isJsonArray() { + return elementType() == JsonElementType.ARRAY; + } + + public boolean isJsonPrimitive() { + return isJsonBoolean() || isJsonString() || isJsonNull() || isJsonNumber(); + } + + public boolean isJsonBoolean() { + return elementType() == JsonElementType.BOOLEAN; + } + + public boolean isJsonString() { + return elementType() == JsonElementType.STRING; + } + + public boolean isJsonNumber() { + return elementType() == JsonElementType.NUMBER; + } + + public boolean isJsonNull() { + return elementType() == JsonElementType.NULL; + } + + public JsonObject asJsonObject() { + return isJsonObject() ? (JsonObject) this : null; + } + + public JsonArray asJsonArray() { + return isJsonArray() ? (JsonArray) this : null; + } + + public JsonPrimitive asJsonPrimitive() { + return isJsonPrimitive() ? (JsonPrimitive) this : null; + } + + public JsonBoolean asJsonBoolean() { + return isJsonBoolean() ? (JsonBoolean) this : null; + } + + public JsonString asJsonString() { + return isJsonString() ? (JsonString) this : null; + } + + public JsonNumber asJsonNumber() { + return isJsonNumber() ? (JsonNumber) this : null; + } + + public JsonNull asJsonNull() { + return isJsonNull() ? (JsonNull) this : null; + } + + public String asString() { + return isJsonPrimitive() ? ((JsonPrimitive) this).getValue() : null; + } } diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/json/model/JsonNull.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/json/model/JsonNull.java index 2b5f15c65..48785ce24 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/json/model/JsonNull.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/json/model/JsonNull.java @@ -6,7 +6,7 @@ public class JsonNull extends JsonPrimitive { return JsonElementType.NULL; } - + @Override public String getValue() { return "null"; } @@ -15,4 +15,14 @@ public class JsonNull extends JsonPrimitive { public String toString() { return getValue(); } + + @Override + protected JsonElement copy(JsonElement other) { + return this; + } + + @Override + protected JsonElement make() { + return new JsonNull(); + } } diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/json/model/JsonNumber.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/json/model/JsonNumber.java index 58cc923cc..4941423e5 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/json/model/JsonNumber.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/json/model/JsonNumber.java @@ -10,10 +10,18 @@ public class JsonNumber extends JsonPrimitive { this.value = value; } + public JsonNumber(int value) { + this.value = Integer.toString(value); + } + + private JsonNumber() { + } + public JsonElementType elementType() { return JsonElementType.NUMBER; } + @Override public String getValue() { return value; } @@ -30,4 +38,15 @@ public class JsonNumber extends JsonPrimitive { return null; } } + + @Override + protected JsonElement copy(JsonElement other) { + value = ((JsonNumber) other).value; + return this; + } + + @Override + protected JsonElement make() { + return new JsonNumber(); + } } diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/json/model/JsonObject.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/json/model/JsonObject.java index fadec26b3..b28c18152 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/json/model/JsonObject.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/json/model/JsonObject.java @@ -1,9 +1,12 @@ package org.hl7.fhir.utilities.json.model; +import java.time.Instant; +import java.time.OffsetDateTime; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Map.Entry; import org.hl7.fhir.utilities.Utilities; import org.hl7.fhir.utilities.json.JsonException; @@ -19,8 +22,8 @@ public class JsonObject extends JsonElement { } public JsonObject add(String name, JsonElement value) throws JsonException { - check(name != null, "Numm is null"); - check(value != null, "Numm is null"); + check(name != null, "Name is null"); + check(value != null, "Value is null"); check(get(name) == null, "Name '"+name+"' already exists"); JsonProperty p = new JsonProperty(name, value); properties.add(p); @@ -30,8 +33,8 @@ public class JsonObject extends JsonElement { // this is used by the parser which can allow duplicates = true (for the validator). You should not otherwise use it public JsonObject addForParser(String name, JsonElement value, boolean noComma, boolean nameUnquoted, boolean valueUnquoted) throws JsonException { - check(name != null, "Numm is null"); - check(value != null, "Numm is null"); + check(name != null, "Name is null"); + check(value != null, "Value is null"); JsonProperty p = new JsonProperty(name, value); p.setNoComma(noComma); p.setUnquotedName(nameUnquoted); @@ -42,13 +45,64 @@ public class JsonObject extends JsonElement { } public JsonObject add(String name, String value) throws JsonException { + check(name != null, "Name is null"); return add(name, new JsonString(value)); } public JsonObject add(String name, boolean value) throws JsonException { + check(name != null, "Name is null"); return add(name, new JsonBoolean(value)); } + public JsonObject add(String name, int value) throws JsonException { + check(name != null, "Name is null"); + return add(name, new JsonNumber(value)); + } + + public JsonObject set(String name, JsonElement value) throws JsonException { + check(name != null, "Name is null"); + check(value != null, "Value is null"); + JsonProperty p = propMap.get(name); + if (p != null) { + p.setValue(value); + return this; + } else { + return add(name, value); + } + } + + public JsonObject set(String name, String value) throws JsonException { + check(name != null, "Name is null"); + JsonProperty p = propMap.get(name); + if (p != null) { + p.setValue(new JsonString(value)); + return this; + } else { + return add(name, new JsonString(value)); + } + } + + public JsonObject set(String name, boolean value) throws JsonException { + check(name != null, "Name is null"); + JsonProperty p = propMap.get(name); + if (p != null) { + p.setValue(new JsonBoolean(value)); + return this; + } else { + return add(name, new JsonBoolean(value)); + } + } + + public JsonObject set(String name, int value) throws JsonException { + check(name != null, "Name is null"); + JsonProperty p = propMap.get(name); + if (p != null) { + p.setValue(new JsonNumber(value)); + return this; + } else { + return add(name, new JsonNumber(value)); + } + } public JsonElement get(String name) { if (propMap.containsKey(name)) { @@ -62,7 +116,7 @@ public class JsonObject extends JsonElement { return propMap.containsKey(name); } - public void drop(String name) { + public void remove(String name) { if (propMap.containsKey(name)) { propMap.remove(name); properties.removeIf((JsonProperty item) -> name.equals(item.getName())); @@ -74,8 +128,8 @@ public class JsonObject extends JsonElement { } public String str(String name) { - if (has(name)) { - return get(name).toString(); + if (hasPrimitive(name)) { + return get(name).asJsonPrimitive().getValue(); } else { return null; } @@ -110,27 +164,27 @@ public class JsonObject extends JsonElement { } - public JsonObject getObject(String name) { + public JsonObject getJsonObject(String name) { return hasObject(name) ? (JsonObject) get(name) : null; } - public JsonString getString(String name) { + public JsonString getJsonString(String name) { return hasString(name) ? (JsonString) get(name) : null; } - public JsonBoolean getBoolean(String name) { + public JsonBoolean getJsonBoolean(String name) { return hasBoolean(name) ? (JsonBoolean) get(name) : null; } - public JsonNumber getNumber(String name) { + public JsonNumber getJsonNumber(String name) { return hasNumber(name) ? (JsonNumber) get(name) : null; } - public JsonNull getNull(String name) { + public JsonNull getJsonNull(String name) { return hasNull(name) ?(JsonNull) get(name) : null; } - public JsonArray getArray(String name) { + public JsonArray getJsonArray(String name) { return hasArray(name) ? (JsonArray) get(name) : null; } @@ -148,7 +202,16 @@ public class JsonObject extends JsonElement { } public String asString(String name) { - return hasPrimitive(name) ? ((JsonPrimitive) get(name)).toString() : null; + return hasPrimitive(name) ? ((JsonPrimitive) get(name)).getValue() : null; + } + + public String asString(String... names) { + for (String n : names) { + if (hasPrimitive(n)) { + return asString(n); + } + } + return null; } public boolean asBoolean(String name) { @@ -167,25 +230,101 @@ public class JsonObject extends JsonElement { return false; } + public Instant asDate(String name) { + String source = asString(name); + if (Utilities.noString(source)) { + return null; + } else { + OffsetDateTime odt = OffsetDateTime.parse(source); + return odt.toInstant(); + } + } + public JsonObject forceObject(String name) throws JsonException { if (has(name) && !hasObject(name)) { - drop(name); + remove(name); } if (!has(name)) { add(name, new JsonObject()); } - return getObject(name); + return getJsonObject(name); } public JsonArray forceArray(String name) throws JsonException { if (has(name) && !hasArray(name)) { - drop(name); + remove(name); } if (!has(name)) { add(name, new JsonArray()); } - return getArray(name); + return getJsonArray(name); + } + + public List getJsonObjects(String name) { + List res = new ArrayList<>(); + if (hasArray(name)) { + res.addAll(getJsonArray(name).asJsonObjects()); + } else if (hasObject(name)) { + res.add(getJsonObject(name)); + } + return res; } + public List getStrings(String name) { + List res = new ArrayList<>(); + if (hasArray(name)) { + res.addAll(getJsonArray(name).asStrings()); + } else if (hasPrimitive(name)) { + res.add(asString(name)); + } + return res; + } + + public JsonObject deepCopy() { + return (JsonObject) make().copy(this); + } + @Override + protected JsonElement copy(JsonElement other) { + JsonObject o = (JsonObject) other; + for (JsonProperty p : o.getProperties()) { + add(p.getName(), p.getValue().deepCopy()); + } + return this; + } + + @Override + protected JsonElement make() { + return new JsonObject(); + } + + public void merge(JsonObject source) { + for (JsonProperty pp : source.getProperties()) { + if (has(pp.getName())) { + JsonElement te = get(pp.getName()); + if (te.isJsonObject() && pp.getValue().isJsonObject()) { + ((JsonObject) te).merge((JsonObject) pp.getValue()); + } else { + set(pp.getName(), pp.getValue()); + } + } else { + add(pp.getName(), pp.getValue()); + } + } + } + + + @Override + public String toString() { + StringBuilder b = new StringBuilder(); + b.append("{ "); + boolean first = true; + for (JsonProperty p : properties) { + if (first) first = false; else b.append(", "); + b.append(p.toString()); + } + b.append(" }"); + return b.toString(); + } + } diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/json/model/JsonPrimitive.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/json/model/JsonPrimitive.java index b2b280469..7c1d76b77 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/json/model/JsonPrimitive.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/json/model/JsonPrimitive.java @@ -2,4 +2,9 @@ package org.hl7.fhir.utilities.json.model; public abstract class JsonPrimitive extends JsonElement { + public abstract String getValue(); + + public String toJson() { + return getValue(); + } } diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/json/model/JsonProperty.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/json/model/JsonProperty.java index 7999d279b..d5e89225e 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/json/model/JsonProperty.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/json/model/JsonProperty.java @@ -49,5 +49,10 @@ public class JsonProperty { public void setUnquotedValue(boolean unquotedValue) { this.unquotedValue = unquotedValue; } + + @Override + public String toString() { + return "\""+name+"\" : "+value.toString(); + } } diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/json/model/JsonString.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/json/model/JsonString.java index 978650c49..c80d013a2 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/json/model/JsonString.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/json/model/JsonString.java @@ -1,5 +1,7 @@ package org.hl7.fhir.utilities.json.model; +import org.hl7.fhir.utilities.Utilities; + public class JsonString extends JsonPrimitive { private String value; @@ -8,10 +10,14 @@ public class JsonString extends JsonPrimitive { this.value = value; } + private JsonString() { + } + public JsonElementType elementType() { return JsonElementType.STRING; } + @Override public String getValue() { return value; } @@ -22,7 +28,23 @@ public class JsonString extends JsonPrimitive { @Override public String toString() { - return value; + throw new Error("This should not be called"); + // return "\""+ Utilities.escapeJson(value)+"\""; } + @Override + public String toJson() { + return "\""+ Utilities.escapeJson(value)+"\""; + } + + @Override + protected JsonElement copy(JsonElement other) { + value = ((JsonString) other).value; + return this; + } + + @Override + protected JsonElement make() { + return new JsonString(); + } } diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/json/parser/JsonParser.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/json/parser/JsonParser.java index a6fbb5b3c..28ee5175a 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/json/parser/JsonParser.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/json/parser/JsonParser.java @@ -220,8 +220,11 @@ public class JsonParser { lexer.next(); lexer.getStates().push(new State("", true)); } - else + else if (lexer.getType() != null) { throw lexer.error("Unexpected content at start of JSON: "+lexer.getType().toString()); + } else { + throw lexer.error("Unexpected content at start of JSON"); + } if (lexer.getType() != TokenType.Close) { parseProperty(); @@ -568,7 +571,7 @@ public class JsonParser { int length = 0; for (JsonElement i : arr.getItems()) { if (i instanceof JsonPrimitive) { - length = length + i.toString().length(); + length = length + ((JsonPrimitive)i).toJson().length(); } if (i.elementType() == JsonElementType.ARRAY || i.elementType() == JsonElementType.OBJECT || i.hasComments()) { // 20 is a somewhat arbitrary cut off diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/FilesystemPackageCacheManager.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/FilesystemPackageCacheManager.java index f2a6380d0..d5ebd2092 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/FilesystemPackageCacheManager.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/FilesystemPackageCacheManager.java @@ -32,8 +32,10 @@ import org.hl7.fhir.utilities.SimpleHTTPClient.HTTPResult; import org.hl7.fhir.utilities.TextFile; import org.hl7.fhir.utilities.Utilities; import org.hl7.fhir.utilities.VersionUtilities; -import org.hl7.fhir.utilities.json.JsonTrackingParser; -import org.hl7.fhir.utilities.json.JsonUtilities; +import org.hl7.fhir.utilities.json.model.JsonArray; +import org.hl7.fhir.utilities.json.model.JsonElement; +import org.hl7.fhir.utilities.json.model.JsonObject; +import org.hl7.fhir.utilities.json.parser.JsonParser; import org.hl7.fhir.utilities.npm.NpmPackage.NpmPackageFolder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -67,12 +69,6 @@ import org.slf4j.LoggerFactory; */ - -import com.google.gson.GsonBuilder; -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; - /** * This is a package cache manager implementation that uses a local disk cache * @@ -420,18 +416,18 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple log(" done."); } pck = loadPackageInfo(packRoot); - if (!id.equals(JsonUtilities.str(npm.getNpm(), "name")) || !v.equals(JsonUtilities.str(npm.getNpm(), "version"))) { - if (!id.equals(JsonUtilities.str(npm.getNpm(), "name"))) { - npm.getNpm().addProperty("original-name", JsonUtilities.str(npm.getNpm(), "name")); + if (!id.equals(npm.getNpm().asString("name")) || !v.equals(npm.getNpm().asString("version"))) { + if (!id.equals(npm.getNpm().asString("name"))) { + npm.getNpm().add("original-name", npm.getNpm().asString("name")); npm.getNpm().remove("name"); - npm.getNpm().addProperty("name", id); + npm.getNpm().add("name", id); } - if (!v.equals(JsonUtilities.str(npm.getNpm(), "version"))) { - npm.getNpm().addProperty("original-version", JsonUtilities.str(npm.getNpm(), "version")); + if (!v.equals(npm.getNpm().asString("version"))) { + npm.getNpm().add("original-version", npm.getNpm().asString("version")); npm.getNpm().remove("version"); - npm.getNpm().addProperty("version", v); + npm.getNpm().add("version", v); } - TextFile.stringToFile(new GsonBuilder().setPrettyPrinting().create().toJson(npm.getNpm()), Utilities.path(cacheFolder, id + "#" + v, "package", "package.json"), false); + TextFile.stringToFile(JsonParser.compose(npm.getNpm(), true), Utilities.path(cacheFolder, id + "#" + v, "package", "package.json"), false); } } catch (Exception e) { try { @@ -570,10 +566,9 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple private String getPackageUrlFromBuildList(String packageId) throws IOException { checkBuildLoaded(); - for (JsonElement n : buildInfo) { - JsonObject o = (JsonObject) n; - if (packageId.equals(JsonUtilities.str(o, "package-id"))) { - return JsonUtilities.str(o, "url"); + for (JsonObject o : buildInfo.asJsonObjects()) { + if (packageId.equals(o.asString("package-id"))) { + return o.asString("url"); } } return null; @@ -583,8 +578,8 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple checkBuildLoaded(); for (JsonElement n : buildInfo) { JsonObject o = (JsonObject) n; - if (!specList.containsKey(JsonUtilities.str(o, "package-id"))) { - specList.put(JsonUtilities.str(o, "package-id"), JsonUtilities.str(o, "url")); + if (!specList.containsKey(o.asString("package-id"))) { + specList.put(o.asString("package-id"), o.asString("url")); } } } @@ -609,9 +604,9 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple try { for (String pf : listPackages()) { if (new File(Utilities.path(cacheFolder, pf, "package", "package.json")).exists()) { - JsonObject npm = JsonTrackingParser.parseJsonFile(Utilities.path(cacheFolder, pf, "package", "package.json")); - if (canonicalUrl.equals(JsonUtilities.str(npm, "canonical"))) { - return JsonUtilities.str(npm, "name"); + JsonObject npm = JsonParser.parseObjectFromFile(Utilities.path(cacheFolder, pf, "package", "package.json")); + if (canonicalUrl.equals(npm.asString("canonical"))) { + return npm.asString("name"); } } } @@ -630,14 +625,14 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple if (buildInfo != null) { for (JsonElement n : buildInfo) { JsonObject o = (JsonObject) n; - if (canonical.equals(JsonUtilities.str(o, "url"))) { - return JsonUtilities.str(o, "package-id"); + if (canonical.equals(o.asString("url"))) { + return o.asString("package-id"); } } for (JsonElement n : buildInfo) { JsonObject o = (JsonObject) n; - if (JsonUtilities.str(o, "url").startsWith(canonical + "/ImplementationGuide/")) { - return JsonUtilities.str(o, "package-id"); + if (o.asString("url").startsWith(canonical + "/ImplementationGuide/")) { + return o.asString("package-id"); } } } @@ -649,8 +644,8 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple // special case: current versions roll over, and we have to check their currency try { String url = ciList.get(id); - JsonObject json = JsonTrackingParser.fetchJson(Utilities.pathURL(url, "package.manifest.json")); - String currDate = JsonUtilities.str(json, "date"); + JsonObject json = JsonParser.parseObjectFromUrl(Utilities.pathURL(url, "package.manifest.json")); + String currDate = json.asString("date"); String packDate = p.date(); if (!currDate.equals(packDate)) { return null; // nup, we need a new copy @@ -678,17 +673,17 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple HTTPResult res = http.get("https://build.fhir.org/ig/qas.json?nocache=" + System.currentTimeMillis()); res.checkThrowException(); - buildInfo = (JsonArray) new com.google.gson.JsonParser().parse(TextFile.bytesToString(res.getContent())); + buildInfo = (JsonArray) JsonParser.parse(TextFile.bytesToString(res.getContent())); List builds = new ArrayList<>(); for (JsonElement n : buildInfo) { JsonObject o = (JsonObject) n; - if (o.has("url") && o.has("package-id") && o.get("package-id").getAsString().contains(".")) { - String u = o.get("url").getAsString(); + if (o.has("url") && o.has("package-id") && o.asString("package-id").contains(".")) { + String u = o.asString("url"); if (u.contains("/ImplementationGuide/")) u = u.substring(0, u.indexOf("/ImplementationGuide/")); - builds.add(new BuildRecord(u, o.get("package-id").getAsString(), getRepo(o.get("repo").getAsString()), readDate(o.get("date").getAsString()))); + builds.add(new BuildRecord(u, o.asString("package-id"), getRepo(o.asString("repo")), readDate(o.asString("date")))); } } Collections.sort(builds, new BuildRecordSorter()); @@ -735,7 +730,7 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple String aurl = pu; JsonObject json; try { - json = JsonTrackingParser.fetchJson(pu); + json = JsonParser.parseObjectFromUrl(pu); } catch (Exception e) { String pv = Utilities.pathURL(url, v, "package.tgz"); try { @@ -746,13 +741,12 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple throw new FHIRException("Error fetching package directly (" + pv + "), or fetching package list for " + id + " from " + pu + ": " + e1.getMessage(), e1); } } - if (!id.equals(JsonUtilities.str(json, "package-id"))) - throw new FHIRException("Package ids do not match in " + pu + ": " + id + " vs " + JsonUtilities.str(json, "package-id")); - for (JsonElement e : json.getAsJsonArray("list")) { - JsonObject vo = (JsonObject) e; - if (v.equals(JsonUtilities.str(vo, "version"))) { - aurl = Utilities.pathURL(JsonUtilities.str(vo, "path"), "package.tgz"); - String u = Utilities.pathURL(JsonUtilities.str(vo, "path"), "package.tgz"); + if (!id.equals(json.asString("package-id"))) + throw new FHIRException("Package ids do not match in " + pu + ": " + id + " vs " + json.asString("package-id")); + for (JsonObject vo : json.getJsonObjects("list")) { + if (v.equals(vo.asString("version"))) { + aurl = Utilities.pathURL(vo.asString("path"), "package.tgz"); + String u = Utilities.pathURL(vo.asString("path"), "package.tgz"); return new InputStreamWithSrc(fetchFromUrlSpecific(u, true), u, v); } } @@ -776,13 +770,12 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple throw new FHIRException("Unable to resolve package id " + id); } String pu = Utilities.pathURL(url, "package-list.json"); - JsonObject json = JsonTrackingParser.fetchJson(pu); - if (!id.equals(JsonUtilities.str(json, "package-id"))) - throw new FHIRException("Package ids do not match in " + pu + ": " + id + " vs " + JsonUtilities.str(json, "package-id")); - for (JsonElement e : json.getAsJsonArray("list")) { - JsonObject vo = (JsonObject) e; - if (JsonUtilities.bool(vo, "current")) { - return JsonUtilities.str(vo, "version"); + JsonObject json = JsonParser.parseObjectFromUrl(pu); + if (!id.equals(json.asString("package-id"))) + throw new FHIRException("Package ids do not match in " + pu + ": " + id + " vs " + json.asString("package-id")); + for (JsonObject vo : json.getJsonObjects("list")) { + if (vo.asBoolean("current")) { + return vo.asString("version"); } } diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/NpmPackage.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/NpmPackage.java index 7e6e96b9d..67b1df1f2 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/NpmPackage.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/NpmPackage.java @@ -66,15 +66,13 @@ import org.hl7.fhir.utilities.SimpleHTTPClient; import org.hl7.fhir.utilities.SimpleHTTPClient.HTTPResult; import org.hl7.fhir.utilities.TextFile; import org.hl7.fhir.utilities.Utilities; -import org.hl7.fhir.utilities.json.JsonTrackingParser; -import org.hl7.fhir.utilities.json.JsonUtilities; +import org.hl7.fhir.utilities.json.model.JsonArray; +import org.hl7.fhir.utilities.json.model.JsonElement; +import org.hl7.fhir.utilities.json.model.JsonObject; +import org.hl7.fhir.utilities.json.model.JsonProperty; +import org.hl7.fhir.utilities.json.parser.JsonParser; import org.hl7.fhir.utilities.npm.PackageGenerator.PackageType; -import com.google.gson.GsonBuilder; -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; - /** * info and loader for a package * @@ -111,13 +109,13 @@ public class NpmPackage { public PackageResourceInformation(String root, JsonObject fi) throws IOException { super(); - id = JsonUtilities.str(fi, "id"); - resourceType = JsonUtilities.str(fi, "resourceType"); - url = JsonUtilities.str(fi, "url"); - version = JsonUtilities.str(fi, "version"); - filename = Utilities.path(root, JsonUtilities.str(fi, "filename")); - supplements = JsonUtilities.str(fi, "supplements"); - stype = JsonUtilities.str(fi, "type"); + id = fi.asString("id"); + resourceType = fi.asString("resourceType"); + url = fi.asString("url"); + version = fi.asString("version"); + filename = Utilities.path(root, fi.asString("filename")); + supplements = fi.asString("supplements"); + stype = fi.asString("type"); } public String getId() { return id; @@ -146,8 +144,8 @@ public class NpmPackage { @Override public int compare(JsonObject o0, JsonObject o1) { - String v0 = JsonUtilities.str(o0, "version"); - String v1 = JsonUtilities.str(o1, "version"); + String v0 = o0.asString("version"); + String v1 = o1.asString("version"); return v0.compareTo(v1); } } @@ -181,14 +179,13 @@ public class NpmPackage { } public boolean readIndex(JsonObject index) { - if (!index.has("index-version") || (index.get("index-version").getAsInt() != 1)) { + if (!index.has("index-version") || (index.asInteger("index-version") != 1)) { return false; } this.index = index; - for (JsonElement e : index.getAsJsonArray("files")) { - JsonObject file = (JsonObject) e; - String type = JsonUtilities.str(file, "resourceType"); - String name = JsonUtilities.str(file, "filename"); + for (JsonObject file : index.getJsonObjects("files")) { + String type = file.asString("resourceType"); + String name = file.asString("filename"); if (!types.containsKey(type)) types.put(type, new ArrayList<>()); types.get(type).add(name); @@ -301,7 +298,7 @@ public class NpmPackage { } public void loadFiles(String path, File source, String... exemptions) throws FileNotFoundException, IOException { - this.npm = (JsonObject) new com.google.gson.JsonParser().parse(TextFile.fileToString(Utilities.path(path, "package", "package.json"))); + this.npm = JsonParser.parseObject(TextFile.fileToString(Utilities.path(path, "package", "package.json"))); this.path = path; File dir = new File(path); @@ -318,7 +315,7 @@ public class NpmPackage { File ij = new File(Utilities.path(f.getAbsolutePath(), ".index.json")); if (ij.exists()) { try { - if (!folder.readIndex(JsonTrackingParser.parseJson(ij))) { + if (!folder.readIndex(JsonParser.parseObject(ij))) { indexFolder(folder.getName(), folder); } } catch (Exception e) { @@ -353,7 +350,7 @@ public class NpmPackage { File ij = new File(Utilities.path(f.getAbsolutePath(), ".index.json")); if (ij.exists()) { try { - if (!folder.readIndex(JsonTrackingParser.parseJson(ij))) { + if (!folder.readIndex(JsonParser.parseObject(ij))) { indexFolder(folder.getName(), folder); } } catch (Exception e) { @@ -374,7 +371,7 @@ public class NpmPackage { if (!res.folders.get("package").hasFile("package.json") && defType != null) { TextFile.stringToFile("{ \"type\" : \""+defType.getCode()+"\"}", Utilities.path(res.folders.get("package").folder.getAbsolutePath(), "package.json")); } - res.npm = (JsonObject) new com.google.gson.JsonParser().parse(new String(res.folders.get("package").fetchFile("package.json"))); + res.npm = JsonParser.parseObject(new String(res.folders.get("package").fetchFile("package.json"))); return res; } @@ -439,7 +436,7 @@ public class NpmPackage { } } try { - npm = JsonTrackingParser.parseJson(folders.get("package").fetchFile("package.json")); + npm = JsonParser.parseObject(folders.get("package").fetchFile("package.json")); } catch (Exception e) { throw new IOException("Error parsing "+(desc == null ? "" : desc+"#")+"package/package.json: "+e.getMessage(), e); } @@ -482,7 +479,7 @@ public class NpmPackage { } String json = indexer.build(); try { - folder.readIndex(JsonTrackingParser.parseJson(json)); + folder.readIndex(JsonParser.parseObject(json)); if (folder.folder != null) { TextFile.stringToFile(json, Utilities.path(folder.folder.getAbsolutePath(), ".index.json")); } @@ -520,7 +517,7 @@ public class NpmPackage { } zip.close(); try { - res.npm = JsonTrackingParser.parseJson(res.folders.get("package").fetchFile("package.json")); + res.npm = JsonParser.parseObject(res.folders.get("package").fetchFile("package.json")); } catch (Exception e) { throw new IOException("Error parsing "+(desc == null ? "" : desc+"#")+"package/package.json: "+e.getMessage(), e); } @@ -561,9 +558,8 @@ public class NpmPackage { List res = new ArrayList(); for (NpmPackageFolder folder : folders.values()) { if (folder.index != null) { - for (JsonElement e : folder.index.getAsJsonArray("files")) { - JsonObject fi = e.getAsJsonObject(); - if (Utilities.existsInList(JsonUtilities.str(fi, "resourceType"), types)) { + for (JsonObject fi : folder.index.getJsonObjects("files")) { + if (Utilities.existsInList(fi.asString("resourceType"), types)) { res.add(new PackageResourceInformation(folder.folder == null ? "@"+folder.getName() : folder.folder.getAbsolutePath(), fi)); } } @@ -634,21 +630,20 @@ public class NpmPackage { public InputStream loadByCanonicalVersion(String folder, String canonical, String version) throws IOException { NpmPackageFolder f = folders.get(folder); List matches = new ArrayList<>(); - for (JsonElement e : f.index.getAsJsonArray("files")) { - JsonObject file = (JsonObject) e; - if (canonical.equals(JsonUtilities.str(file, "url"))) { - if (version != null && version.equals(JsonUtilities.str(file, "version"))) { - return load("package", JsonUtilities.str(file, "filename")); + for (JsonObject file : f.index.getJsonObjects("files")) { + if (canonical.equals(file.asString("url"))) { + if (version != null && version.equals(file.asString("version"))) { + return load("package", file.asString("filename")); } else if (version == null) { matches.add(file); } } if (matches.size() > 0) { if (matches.size() == 1) { - return load("package", JsonUtilities.str(matches.get(0), "filename")); + return load("package", matches.get(0).asString("filename")); } else { Collections.sort(matches, new IndexVersionSorter()); - return load("package", JsonUtilities.str(matches.get(matches.size()-1), "filename")); + return load("package", matches.get(matches.size()-1).asString("filename")); } } } @@ -708,7 +703,7 @@ public class NpmPackage { * @return */ public String name() { - return JsonUtilities.str(npm, "name"); + return npm.asString("name"); } /** @@ -716,15 +711,15 @@ public class NpmPackage { * @return */ public String id() { - return JsonUtilities.str(npm, "name"); + return npm.asString("name"); } public String date() { - return JsonUtilities.str(npm, "date"); + return npm.asString("date"); } public String canonical() { - return JsonUtilities.str(npm, "canonical"); + return npm.asString("canonical"); } /** @@ -732,7 +727,7 @@ public class NpmPackage { * @return */ public String version() { - return JsonUtilities.str(npm, "version"); + return npm.asString("version"); } /** @@ -740,28 +735,28 @@ public class NpmPackage { * @return */ public String fhirVersion() { - if ("hl7.fhir.core".equals(JsonUtilities.str(npm, "name"))) - return JsonUtilities.str(npm, "version"); - else if (JsonUtilities.str(npm, "name").startsWith("hl7.fhir.r2.") || JsonUtilities.str(npm, "name").startsWith("hl7.fhir.r2b.") || JsonUtilities.str(npm, "name").startsWith("hl7.fhir.r3.") || - JsonUtilities.str(npm, "name").startsWith("hl7.fhir.r4.") || JsonUtilities.str(npm, "name").startsWith("hl7.fhir.r4b.") || JsonUtilities.str(npm, "name").startsWith("hl7.fhir.r5.")) - return JsonUtilities.str(npm, "version"); + if ("hl7.fhir.core".equals(npm.asString("name"))) + return npm.asString("version"); + else if (npm.asString("name").startsWith("hl7.fhir.r2.") || npm.asString("name").startsWith("hl7.fhir.r2b.") || npm.asString("name").startsWith("hl7.fhir.r3.") || + npm.asString("name").startsWith("hl7.fhir.r4.") || npm.asString("name").startsWith("hl7.fhir.r4b.") || npm.asString("name").startsWith("hl7.fhir.r5.")) + return npm.asString("version"); else { JsonObject dep = null; - if (npm.has("dependencies") && npm.get("dependencies").isJsonObject()) { - dep = npm.getAsJsonObject("dependencies"); + if (npm.hasObject("dependencies")) { + dep = npm.getJsonObject("dependencies"); if (dep != null) { - for (Entry e : dep.entrySet()) { - if (Utilities.existsInList(e.getKey(), "hl7.fhir.r2.core", "hl7.fhir.r2b.core", "hl7.fhir.r3.core", "hl7.fhir.r4.core")) - return e.getValue().getAsString(); - if (Utilities.existsInList(e.getKey(), "hl7.fhir.core")) // while all packages are updated - return e.getValue().getAsString(); + for (JsonProperty e : dep.getProperties()) { + if (Utilities.existsInList(e.getName(), "hl7.fhir.r2.core", "hl7.fhir.r2b.core", "hl7.fhir.r3.core", "hl7.fhir.r4.core")) + return e.getValue().asString(); + if (Utilities.existsInList(e.getName(), "hl7.fhir.core")) // while all packages are updated + return e.getValue().asString(); } } } - if (npm.has("fhirVersions")) { - JsonElement e = npm.get("fhirVersions"); - if (e.isJsonArray() && e.getAsJsonArray().size() > 0) { - return npm.getAsJsonArray("fhirVersions").get(0).getAsString(); + if (npm.hasArray("fhirVersions")) { + JsonArray e = npm.getJsonArray("fhirVersions"); + if (e.size() > 0) { + return e.getItems().get(0).asString(); } } if (dep != null) { @@ -789,11 +784,11 @@ public class NpmPackage { } public String type() { - return JsonUtilities.str(npm, "type"); + return npm.asString("type"); } public String description() { - return JsonUtilities.str(npm, "description"); + return npm.asString("description"); } public String getPath() { @@ -803,32 +798,32 @@ public class NpmPackage { public List dependencies() { List res = new ArrayList<>(); if (npm.has("dependencies")) { - for (Entry e : npm.getAsJsonObject("dependencies").entrySet()) { - res.add(e.getKey()+"#"+e.getValue().getAsString()); + for (JsonProperty e : npm.getJsonObject("dependencies").getProperties()) { + res.add(e.getName()+"#"+e.getValue().asString()); } } return res; } public String homepage() { - return JsonUtilities.str(npm, "homepage"); + return npm.asString("homepage"); } public String url() { - return JsonUtilities.str(npm, "url"); + return npm.asString("url"); } public String title() { - return JsonUtilities.str(npm, "title"); + return npm.asString("title"); } public String toolsVersion() { - return JsonUtilities.str(npm, "tools-version"); + return npm.asString("tools-version"); } public String license() { - return JsonUtilities.str(npm, "license"); + return npm.asString("license"); } // /** @@ -841,20 +836,20 @@ public class NpmPackage { // } public String getWebLocation() { - if (npm.has("url") && npm.get("url").isJsonPrimitive()) { - return PackageHacker.fixPackageUrl(npm.get("url").getAsString()); + if (npm.hasPrimitive("url")) { + return PackageHacker.fixPackageUrl(npm.asString("url")); } else { - return JsonUtilities.str(npm, "canonical"); + return npm.asString("canonical"); } } public InputStream loadResource(String type, String id) throws IOException { NpmPackageFolder f = folders.get("package"); - JsonArray files = f.index.getAsJsonArray("files"); - for (JsonElement e : files) { + JsonArray files = f.index.getJsonArray("files"); + for (JsonElement e : files.getItems()) { JsonObject i = (JsonObject) e; - if (type.equals(JsonUtilities.str(i, "resourceType")) && id.equals(JsonUtilities.str(i, "id"))) { - return load("package", JsonUtilities.str(i, "filename")); + if (type.equals(i.asString("resourceType")) && id.equals(i.asString("id"))) { + return load("package", i.asString("filename")); } } return null; @@ -866,11 +861,11 @@ public class NpmPackage { f = folders.get("package/example"); } if (f != null) { - JsonArray files = f.index.getAsJsonArray("files"); - for (JsonElement e : files) { + JsonArray files = f.index.getJsonArray("files"); + for (JsonElement e : files.getItems()) { JsonObject i = (JsonObject) e; - if (type.equals(JsonUtilities.str(i, "resourceType")) && id.equals(JsonUtilities.str(i, "id"))) { - return load("example", JsonUtilities.str(i, "filename")); + if (type.equals(i.asString("resourceType")) && id.equals(i.asString("id"))) { + return load("example", i.asString("filename")); } } } @@ -909,7 +904,7 @@ public class NpmPackage { byte[] cnt = indexer.build().getBytes(StandardCharsets.UTF_8); TextFile.bytesToFile(cnt, Utilities.path(dir.getAbsolutePath(), n, ".index.json")); } - byte[] cnt = TextFile.stringToBytes(new GsonBuilder().setPrettyPrinting().create().toJson(npm), false); + byte[] cnt = TextFile.stringToBytes(JsonParser.compose(npm, true), false); TextFile.bytesToFile(cnt, Utilities.path(dir.getAbsolutePath(), "package", "package.json")); } @@ -955,7 +950,7 @@ public class NpmPackage { tar.write(cnt); tar.closeArchiveEntry(); } - byte[] cnt = TextFile.stringToBytes(new GsonBuilder().setPrettyPrinting().create().toJson(npm), false); + byte[] cnt = TextFile.stringToBytes(JsonParser.compose(npm, true), false); TarArchiveEntry entry = new TarArchiveEntry("package/package.json"); entry.setSize(cnt.length); tar.putArchiveEntry(entry); @@ -981,13 +976,13 @@ public class NpmPackage { public String fhirVersionList() { if (npm.has("fhirVersions")) { CommaSeparatedStringBuilder b = new CommaSeparatedStringBuilder(); - if (npm.get("fhirVersions").isJsonArray()) { - for (JsonElement n : npm.getAsJsonArray("fhirVersions")) { - b.append(n.getAsString()); + if (npm.hasArray("fhirVersions")) { + for (String n : npm.getJsonArray("fhirVersions").asStrings()) { + b.append(n); } } - if (npm.get("fhirVersions").isJsonPrimitive()) { - b.append(npm.get("fhirVersions").getAsString()); + if (npm.hasPrimitive("fhirVersions")) { + b.append(npm.asString("fhirVersions")); } return b.toString(); } else @@ -997,8 +992,8 @@ public class NpmPackage { public String dependencySummary() { if (npm.has("dependencies")) { CommaSeparatedStringBuilder b = new CommaSeparatedStringBuilder(); - for (Entry e : npm.getAsJsonObject("dependencies").entrySet()) { - b.append(e.getKey()+"#"+e.getValue().getAsString()); + for (JsonProperty e : npm.getJsonObject("dependencies").getProperties()) { + b.append(e.getName()+"#"+e.getValue().asString()); } return b.toString(); } else @@ -1080,7 +1075,7 @@ public class NpmPackage { folder.types.get(type).add(name); if ("package".equals(folderName) && "package.json".equals(name)) { try { - npm = JsonTrackingParser.parseJson(cnt); + npm = JsonParser.parseObject(cnt); } catch (IOException e) { } } @@ -1118,11 +1113,11 @@ public class NpmPackage { } public boolean isCore() { - return Utilities.existsInList(JsonUtilities.str(npm, "type"), "fhir.core", "Core"); + return Utilities.existsInList(npm.asString("type"), "fhir.core", "Core"); } public boolean isTx() { - return JsonUtilities.str(npm, "name").startsWith("hl7.terminology"); + return npm.asString("name").startsWith("hl7.terminology"); } public boolean hasCanonical(String url) { @@ -1133,10 +1128,9 @@ public class NpmPackage { String v = url.contains("|") ? url.substring(url.indexOf("|")+1) : null; NpmPackageFolder folder = folders.get("package"); if (folder != null) { - for (JsonElement e : folder.index.getAsJsonArray("files")) { - JsonObject o = (JsonObject) e; - if (u.equals(JsonUtilities.str(o, "url"))) { - if (v == null || v.equals(JsonUtilities.str(o, "version"))) { + for (JsonObject o : folder.index.getJsonObjects("files")) { + if (u.equals(o.asString("url"))) { + if (v == null || v.equals(o.asString("version"))) { return true; } } @@ -1154,7 +1148,7 @@ public class NpmPackage { if (Utilities.existsInList(name(), "fhir.test.data.r2", "fhir.test.data.r3", "fhir.test.data.r4", "fhir.tx.support.r2", "fhir.tx.support.r3", "fhir.tx.support.r4", "us.nlm.vsac")) { return true; } - if (JsonUtilities.bool(npm, "lazy-load")) { + if (npm.asBoolean("lazy-load")) { return true; } if (!hasFile("other", "spec.internals")) { @@ -1164,7 +1158,7 @@ public class NpmPackage { } public boolean isNotForPublication() { - return JsonUtilities.bool(npm, "notForPublication"); + return npm.asBoolean("notForPublication"); } public InputStream load(PackageResourceInformation p) throws FileNotFoundException { diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/NpmPackageIndexBuilder.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/NpmPackageIndexBuilder.java index 15fffc704..57c735456 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/NpmPackageIndexBuilder.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/NpmPackageIndexBuilder.java @@ -6,11 +6,9 @@ import java.io.IOException; import org.hl7.fhir.exceptions.FHIRException; import org.hl7.fhir.utilities.TextFile; import org.hl7.fhir.utilities.Utilities; -import org.hl7.fhir.utilities.json.JsonTrackingParser; - -import com.google.gson.GsonBuilder; -import com.google.gson.JsonArray; -import com.google.gson.JsonObject; +import org.hl7.fhir.utilities.json.model.JsonArray; +import org.hl7.fhir.utilities.json.model.JsonObject; +import org.hl7.fhir.utilities.json.parser.JsonParser; /** * This class builds the .index.json for a package @@ -25,7 +23,7 @@ public class NpmPackageIndexBuilder { public void start() { index = new JsonObject(); - index.addProperty("index-version", 1); + index.add("index-version", 1); files = new JsonArray(); index.add("files", files); } @@ -33,30 +31,30 @@ public class NpmPackageIndexBuilder { public boolean seeFile(String name, byte[] content) { if (name.endsWith(".json")) { try { - JsonObject json = JsonTrackingParser.parseJson(content); + JsonObject json = JsonParser.parseObject(content); if (json.has("resourceType")) { // ok we treat it as a resource JsonObject fi = new JsonObject(); files.add(fi); - fi.addProperty("filename", name); - fi.addProperty("resourceType", json.get("resourceType").getAsString()); - if (json.has("id") && json.get("id").isJsonPrimitive()) { - fi.addProperty("id", json.get("id").getAsString()); + fi.add("filename", name); + fi.add("resourceType", json.asString("resourceType")); + if (json.hasPrimitive("id")) { + fi.add("id", json.asString("id")); } - if (json.has("url") && json.get("url").isJsonPrimitive()) { - fi.addProperty("url", json.get("url").getAsString()); + if (json.hasPrimitive("url")) { + fi.add("url", json.asString("url")); } - if (json.has("version") && json.get("version").isJsonPrimitive()) { - fi.addProperty("version", json.get("version").getAsString()); + if (json.hasPrimitive("version")) { + fi.add("version", json.asString("version")); } - if (json.has("kind") && json.get("kind").isJsonPrimitive()) { - fi.addProperty("kind", json.get("kind").getAsString()); + if (json.hasPrimitive("kind")) { + fi.add("kind", json.asString("kind")); } - if (json.has("type") && json.get("type").isJsonPrimitive()) { - fi.addProperty("type", json.get("type").getAsString()); + if (json.hasPrimitive("type")) { + fi.add("type", json.asString("type")); } - if (json.has("supplements") && json.get("supplements").isJsonPrimitive()) { - fi.addProperty("supplements", json.get("supplements").getAsString()); + if (json.hasPrimitive("supplements")) { + fi.add("supplements", json.asString("supplements")); } } } catch (Exception e) { @@ -70,7 +68,7 @@ public class NpmPackageIndexBuilder { } public String build() { - String res = new GsonBuilder().setPrettyPrinting().create().toJson(index); + String res = JsonParser.compose(index, true); index = null; files = null; return res; diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/PackageClient.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/PackageClient.java index 2445ac714..f1a854739 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/PackageClient.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/PackageClient.java @@ -17,12 +17,10 @@ import org.hl7.fhir.utilities.SimpleHTTPClient.HTTPResult; import org.hl7.fhir.utilities.TextFile; import org.hl7.fhir.utilities.Utilities; import org.hl7.fhir.utilities.VersionUtilities; -import org.hl7.fhir.utilities.json.JsonTrackingParser; -import org.hl7.fhir.utilities.json.JsonUtilities; - -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; +import org.hl7.fhir.utilities.json.model.JsonArray; +import org.hl7.fhir.utilities.json.model.JsonObject; +import org.hl7.fhir.utilities.json.model.JsonProperty; +import org.hl7.fhir.utilities.json.parser.JsonParser; public class PackageClient { @@ -81,21 +79,21 @@ public class PackageClient { JsonObject json; try { json = fetchJson(Utilities.pathURL(address, id)); - JsonObject versions = json.getAsJsonObject("versions"); + JsonObject versions = json.getJsonObject("versions"); boolean hasDates = true; if (versions != null) { - for (String v : versions.keySet()) { - JsonObject obj = versions.getAsJsonObject(v); - Instant d = obj.has("date") ? JsonUtilities.parseDate(obj, "date") : null; + for (JsonProperty v : versions.getProperties()) { + JsonObject obj = versions.getJsonObject(v.getName()); + Instant d = obj.hasString("date") ? obj.asDate("date") : null; if (d == null) { hasDates = false; } - res.add(new PackageInfo(JsonUtilities.str(obj, "Name", "name"), - JsonUtilities.str(obj, "Version", "version"), - JsonUtilities.str(obj, "FhirVersion", "fhirVersion"), - JsonUtilities.str(obj, "Description", "description"), - JsonUtilities.str(obj, "url"), - JsonUtilities.str(obj, "canonical"), + res.add(new PackageInfo(obj.asString("Name", "name"), + obj.asString("Version", "version"), + obj.asString("FhirVersion", "fhirVersion"), + obj.asString("Description", "description"), + obj.asString("url"), + obj.asString("canonical"), address, d)); } } @@ -127,18 +125,17 @@ public class PackageClient { try { JsonArray json = fetchJsonArray(Utilities.pathURL(address, "catalog?")+params.toString()); boolean hasDates = true; - for (JsonElement e : json) { - JsonObject obj = (JsonObject) e; - Instant d = obj.has("date") ? JsonUtilities.parseDate(obj, "date") : null; + for (JsonObject obj : json.asJsonObjects()) { + Instant d = obj.has("date") ? obj.asDate("date") : null; if (d == null) { hasDates = false; } - res.add(new PackageInfo(JsonUtilities.str(obj, "Name", "name"), - JsonUtilities.str(obj, "Version", "version"), - JsonUtilities.str(obj, "FhirVersion", "fhirVersion"), - JsonUtilities.str(obj, "Description", "description"), - JsonUtilities.str(obj, "url"), - JsonUtilities.str(obj, "canonical"), + res.add(new PackageInfo(obj.asString("Name", "name"), + obj.asString("Version", "version"), + obj.asString("FhirVersion", "fhirVersion"), + obj.asString("Description", "description"), + obj.asString("url"), + obj.asString("canonical"), address, d)); } if (hasDates) { @@ -165,13 +162,13 @@ public class PackageClient { private JsonObject fetchJson(String source) throws IOException { String src = TextFile.streamToString(fetchUrl(source, "application/json")); //System.out.println(src); - return (JsonObject) new com.google.gson.JsonParser().parse(src); + return JsonParser.parseObject(src); } private JsonArray fetchJsonArray(String source) throws IOException { String src = TextFile.streamToString(fetchUrl(source, "application/json")); //System.out.println(src); - return (JsonArray) new com.google.gson.JsonParser().parse(src); + return (JsonArray) JsonParser.parse(src); } public String url(String id, String v) { @@ -209,11 +206,11 @@ public class PackageClient { } protected PackageInfo getPackageInfoFromJSON(JsonObject o, String name, String canonical, String fhirVersion) { - String id = JsonUtilities.str(o, "npm-name"); - String pname = JsonUtilities.str(o, "name"); - String pcanonical = JsonUtilities.str(o, "canonical"); - String description = JsonUtilities.str(o, "description"); - Instant d = o.has("date") ? JsonUtilities.parseDate(o, "date") : null; + String id = o.asString("npm-name"); + String pname = o.asString("name"); + String pcanonical = o.asString("canonical"); + String description = o.asString("description"); + Instant d = o.has("date") ? o.asDate("date") : null; boolean ok = true; if (ok && !Utilities.noString(name)) { ok = (pname != null && pname.contains(name)) || (description != null && description.contains(name)) || (id != null && id.contains(name)); @@ -227,15 +224,15 @@ public class PackageClient { if (ok) { // if we can find something... - for (JsonObject e : JsonUtilities.objects(o, "editions")) { - if (fhirVersion == null || fhirVersion.equals(JsonUtilities.str(e, "fhir-version"))) { - String v = JsonUtilities.str(e, "ig-version"); + for (JsonObject e : o.getJsonObjects("editions")) { + if (fhirVersion == null || fhirVersion.equals(e.asString("fhir-version"))) { + String v = e.asString("ig-version"); if (version == null || VersionUtilities.isThisOrLater(version, v)) { version = v; - fVersion = e.getAsJsonArray("fhir-version").get(0).getAsString(); - url = JsonUtilities.str(e, "url"); + fVersion = e.getJsonArray("fhir-version").get(0).asString(); + url = e.asString("url"); - String npmPackage = JsonUtilities.str(e, "package"); + String npmPackage = e.asString("package"); if (npmPackage != null && id == null) { id = npmPackage.substring(0, npmPackage.indexOf("#")); } @@ -248,8 +245,8 @@ public class PackageClient { public List listFromRegistry(String name, String canonical, String fhirVersion) throws IOException { List result = new ArrayList<>(); - JsonObject packages = JsonTrackingParser.fetchJson("https://raw.githubusercontent.com/FHIR/ig-registry/master/fhir-ig-list.json?nocache=" + System.currentTimeMillis()); - for (JsonObject o : JsonUtilities.objects(packages, "guides")) { + JsonObject packages = JsonParser.parseObjectFromUrl("https://raw.githubusercontent.com/FHIR/ig-registry/master/fhir-ig-list.json?nocache=" + System.currentTimeMillis()); + for (JsonObject o : packages.getJsonObjects("guides")) { if (o.has("canonical")) { final PackageInfo packageInfo = getPackageInfoFromJSON(o, name, canonical, fhirVersion); if (packageInfo.getVersion() != null) { @@ -265,9 +262,8 @@ public class PackageClient { params.append("dependency="+id.replace("#", "|")); try { JsonArray json = fetchJsonArray(Utilities.pathURL(address, "catalog?")+params.toString()); - for (JsonElement e : json) { - JsonObject obj = (JsonObject) e; - list.add(JsonUtilities.str(obj, "Name", "name")); + for (JsonObject obj : json.asJsonObjects()) { + list.add(obj.asString("Name", "name")); } } catch (IOException e1) { } diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/PackageGenerator.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/PackageGenerator.java index a0d24ef4e..4afdb2a9a 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/PackageGenerator.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/PackageGenerator.java @@ -39,14 +39,9 @@ import java.util.List; import org.hl7.fhir.utilities.TextFile; import org.hl7.fhir.utilities.Utilities; - -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.google.gson.JsonArray; -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; -import com.google.gson.JsonPrimitive; -import com.google.gson.JsonSyntaxException; +import org.hl7.fhir.utilities.json.model.JsonArray; +import org.hl7.fhir.utilities.json.model.JsonObject; +import org.hl7.fhir.utilities.json.parser.JsonParser; public class PackageGenerator { @@ -94,11 +89,10 @@ public class PackageGenerator { object = new JsonObject(); } - public PackageGenerator(OutputStream stream, InputStream template) throws JsonSyntaxException, IOException { + public PackageGenerator(OutputStream stream, InputStream template) throws IOException { super(); this.stream = stream; - JsonParser parser = new com.google.gson.JsonParser(); - object = parser.parse(TextFile.streamToString(template)).getAsJsonObject(); + object = JsonParser.parseObject(TextFile.streamToString(template)); } @@ -107,8 +101,7 @@ public class PackageGenerator { } public void commit() throws IOException { - Gson gson = new GsonBuilder().setPrettyPrinting().create(); - String json = gson.toJson(object); + String json = JsonParser.compose(object, true); OutputStreamWriter sw = new OutputStreamWriter(stream, "UTF-8"); sw.write('\ufeff'); // Unicode BOM, translates to UTF-8 with the configured outputstreamwriter sw.write(json); @@ -118,17 +111,17 @@ public class PackageGenerator { public PackageGenerator name(String value) { // NOTE: I removed a prefix of "@fhir/" here. What was this for? -JA - object.addProperty("name", value); + object.add("name", value); return this; } public PackageGenerator version(String value) { - object.addProperty("version", value); + object.add("version", value); return this; } public PackageGenerator toolsVersion(int value) { - object.addProperty("tools-version", value); + object.add("tools-version", value); return this; } @@ -142,44 +135,44 @@ public class PackageGenerator { } public PackageGenerator description(String value) { - object.addProperty("description", value); + object.add("description", value); return this; } public PackageGenerator license(String value) { - object.addProperty("license", value); + object.add("license", value); return this; } public PackageGenerator homepage(String value) { - object.addProperty("homepage", value); + object.add("homepage", value); return this; } public PackageGenerator bugs(String value) { - object.addProperty("bugs", value); + object.add("bugs", value); return this; } public PackageGenerator author(String name, String email, String url) { JsonObject person = new JsonObject(); - person.addProperty("name", name); + person.add("name", name); if (!Utilities.noString(email)) - person.addProperty("email", email); + person.add("email", email); if (!Utilities.noString(url)) - person.addProperty("url", url); + person.add("url", url); object.add("author", person); return this; } public PackageGenerator contributor(String name, String email, String url) { JsonObject person = new JsonObject(); - person.addProperty("name", name); + person.add("name", name); if (!Utilities.noString(email)) - person.addProperty("email", email); + person.add("email", email); if (!Utilities.noString(url)) - person.addProperty("url", url); - JsonArray c = object.getAsJsonArray("contributors"); + person.add("url", url); + JsonArray c = object.getJsonArray("contributors"); if (c == null) { c = new JsonArray(); object.add("contributors", c); @@ -189,27 +182,23 @@ public class PackageGenerator { } public PackageGenerator dependency(String name, String version) { - JsonObject dep = object.getAsJsonObject("dependencies"); + JsonObject dep = object.getJsonObject("dependencies"); if (dep == null) { dep = new JsonObject(); object.add("dependencies", dep); } - dep.addProperty(name, version); + dep.add(name, version); return this; } public PackageGenerator file(String name) { - JsonArray files = object.getAsJsonArray("files"); - if (files == null) { - files = new JsonArray(); - object.add("files", files); - } - files.add(new JsonPrimitive(name)); + JsonArray files = object.forceArray("files"); + files.add(name); return this; } public PackageGenerator kind(PackageType kind) { - object.addProperty("type", kind.getCode()); + object.add("type", kind.getCode()); return this; } diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/PackageHacker.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/PackageHacker.java index c1e3c6cc4..764143e72 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/PackageHacker.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/PackageHacker.java @@ -12,10 +12,11 @@ import java.util.Map; import org.hl7.fhir.utilities.TextFile; import org.hl7.fhir.utilities.Utilities; +import org.hl7.fhir.utilities.json.model.JsonArray; +import org.hl7.fhir.utilities.json.model.JsonObject; +import org.hl7.fhir.utilities.json.parser.JsonParser; + -import com.google.gson.GsonBuilder; -import com.google.gson.JsonArray; -import com.google.gson.JsonObject; /** * intenral use only - set the file name to edit in main(), and fill out the edit routine @@ -74,14 +75,14 @@ public class PackageHacker { } private String nice(JsonObject json) { - return new GsonBuilder().setPrettyPrinting().create().toJson(json); + return JsonParser.compose(json, true); } private void change(JsonObject npm) throws FileNotFoundException, IOException { // fixVersions(npm); // npm.remove("notForPublication"); npm.remove("url"); - npm.addProperty("url", "https://hl7chile.cl/fhir/ig/CoreCL/1.7.0"); + npm.add("url", "https://hl7chile.cl/fhir/ig/CoreCL/1.7.0"); // npm.remove("name"); // npm.addProperty("name", "hl7.fhir.uv.smart-app-launch"); // npm.remove("canonical"); @@ -128,7 +129,7 @@ public class PackageHacker { private void setProperty(JsonObject npm, String name, String value) { npm.remove("homepage"); - npm.addProperty("homepage", "http://hl7.org/fhir"); + npm.add("homepage", "http://hl7.org/fhir"); } private void fixNames(Map content) { diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/PackageScanner.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/PackageScanner.java index 3c39eeb45..cb2b24391 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/PackageScanner.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/PackageScanner.java @@ -9,10 +9,9 @@ import java.util.Set; import org.hl7.fhir.utilities.TextFile; import org.hl7.fhir.utilities.Utilities; -import org.hl7.fhir.utilities.json.JsonTrackingParser; -import org.hl7.fhir.utilities.json.JsonUtilities; +import org.hl7.fhir.utilities.json.model.JsonObject; +import org.hl7.fhir.utilities.json.parser.JsonParser; -import com.google.gson.JsonObject; public class PackageScanner { @@ -63,7 +62,7 @@ public class PackageScanner { } catch (Exception e) { fv = "--"; } - output.add(pck.name()+"\t"+pck.version()+"\t"+pck.canonical()+"\t"+fv+'\t'+pck.fhirVersionList()+'\t'+JsonUtilities.str(json, "kind")+'\t'+JsonUtilities.str(json, "type")+'\t'+JsonTrackingParser.writeDense(json)); } catch (Exception e) { + output.add(pck.name()+"\t"+pck.version()+"\t"+pck.canonical()+"\t"+fv+'\t'+pck.fhirVersionList()+'\t'+json.asString("kind")+'\t'+json.asString("type")+'\t'+JsonParser.compose(json)); } catch (Exception e) { System.out.println("Error acessing "+pi.getId()+"#"+piv.getVersion()+": "+e.getMessage()); e.printStackTrace(); } diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/ResourceRenamer.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/ResourceRenamer.java index 272db7f38..dd9a451da 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/ResourceRenamer.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/ResourceRenamer.java @@ -1,13 +1,13 @@ package org.hl7.fhir.utilities.npm; import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; import org.hl7.fhir.utilities.Utilities; -import org.hl7.fhir.utilities.json.JsonTrackingParser; -import org.hl7.fhir.utilities.json.JsonUtilities; +import org.hl7.fhir.utilities.json.model.JsonObject; +import org.hl7.fhir.utilities.json.parser.JsonParser; -import com.google.gson.JsonObject; public class ResourceRenamer { @@ -24,13 +24,13 @@ public class ResourceRenamer { } private void unbundle(File f) throws IOException { - JsonObject j = JsonTrackingParser.parseJson(f); - for (JsonObject e : JsonUtilities.objects(j, "entry")) { - JsonObject r = e.getAsJsonObject("resource"); - String rt = r.get("resourceType").getAsString(); - String id = r.get("id").getAsString(); + JsonObject j = JsonParser.parseObject(f); + for (JsonObject e : j.getJsonObjects("entry")) { + JsonObject r = e.getJsonObject("resource"); + String rt = r.asString("resourceType"); + String id = r.asString("id"); String nn = Utilities.path(Utilities.getDirectoryForFile(f.getAbsolutePath()), rt+"-"+id+".json"); - JsonTrackingParser.write(r, new File(nn), true); + JsonParser.compose(r, new FileOutputStream(nn), true); } } @@ -39,9 +39,9 @@ public class ResourceRenamer { for (File f : dir.listFiles()) { if (f.getName().endsWith(".json")) { try { - JsonObject j = JsonTrackingParser.parseJson(f); - String rt = j.get("resourceType").getAsString(); - String id = j.get("id").getAsString(); + JsonObject j = JsonParser.parseObject(f); + String rt = j.asString("resourceType"); + String id = j.asString("id"); String nn = Utilities.path(Utilities.getDirectoryForFile(f.getAbsolutePath()), rt+"-"+id+".json"); File nf = new File(nn); if (!nn.equals(f.getAbsolutePath())) { diff --git a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/DateTimeUtilTests.java b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/DateTimeUtilTests.java index 0caceb764..2a398785c 100644 --- a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/DateTimeUtilTests.java +++ b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/DateTimeUtilTests.java @@ -1,6 +1,12 @@ package org.hl7.fhir.utilities; -import ca.uhn.fhir.model.api.TemporalPrecisionEnum; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.Date; +import java.util.Locale; +import java.util.TimeZone; +import java.util.stream.Stream; + import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Disabled; @@ -8,12 +14,7 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; -import java.util.Date; -import java.util.Locale; -import java.util.TimeZone; -import java.util.stream.Stream; - -import static org.junit.jupiter.api.Assertions.assertEquals; +import ca.uhn.fhir.model.api.TemporalPrecisionEnum; public class DateTimeUtilTests { diff --git a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/DurationUtilTest.java b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/DurationUtilTest.java index 838ae860f..e1705f14a 100644 --- a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/DurationUtilTest.java +++ b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/DurationUtilTest.java @@ -1,18 +1,19 @@ package org.hl7.fhir.utilities; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.Arguments; -import org.junit.jupiter.params.provider.MethodSource; -import org.xml.sax.SAXException; +import static org.junit.jupiter.api.Assertions.assertEquals; -import javax.xml.parsers.ParserConfigurationException; import java.io.IOException; import java.time.Duration; import java.util.ArrayList; import java.util.List; import java.util.stream.Stream; -import static org.junit.jupiter.api.Assertions.assertEquals; +import javax.xml.parsers.ParserConfigurationException; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; +import org.xml.sax.SAXException; public class DurationUtilTest { diff --git a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/FileFormatTest.java b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/FileFormatTest.java index c79625399..84b930020 100644 --- a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/FileFormatTest.java +++ b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/FileFormatTest.java @@ -1,6 +1,8 @@ package org.hl7.fhir.utilities; -import org.junit.jupiter.api.Test; +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertEquals; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -8,9 +10,7 @@ import java.io.PrintStream; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; -import static org.hamcrest.CoreMatchers.containsString; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.Assert.assertEquals; +import org.junit.jupiter.api.Test; public class FileFormatTest { @Test diff --git a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/SimpleHTTPClientTest.java b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/SimpleHTTPClientTest.java index bb7030d99..d2fe2e6c9 100644 --- a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/SimpleHTTPClientTest.java +++ b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/SimpleHTTPClientTest.java @@ -1,11 +1,10 @@ package org.hl7.fhir.utilities; -import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.io.IOException; -import java.nio.charset.StandardCharsets; -import static org.junit.jupiter.api.Assertions.assertTrue; +import org.junit.jupiter.api.Test; public class SimpleHTTPClientTest { @Test diff --git a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/UtilitiesTest.java b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/UtilitiesTest.java index 9f5f51877..98732e35c 100644 --- a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/UtilitiesTest.java +++ b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/UtilitiesTest.java @@ -1,9 +1,6 @@ package org.hl7.fhir.utilities; -import org.apache.commons.lang3.SystemUtils; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.io.File; import java.io.IOException; @@ -11,7 +8,10 @@ import java.nio.file.Paths; import java.text.MessageFormat; import java.util.Random; -import static org.junit.jupiter.api.Assertions.*; +import org.apache.commons.lang3.SystemUtils; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; class UtilitiesTest { diff --git a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/VersionUtilitiesTest.java b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/VersionUtilitiesTest.java index 2ed5cab5b..1acf3bacc 100644 --- a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/VersionUtilitiesTest.java +++ b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/VersionUtilitiesTest.java @@ -1,8 +1,9 @@ package org.hl7.fhir.utilities; -import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.Test; public class VersionUtilitiesTest { diff --git a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/i18n/I18nBaseTest.java b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/i18n/I18nBaseTest.java index 28c861a94..7961ddc40 100644 --- a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/i18n/I18nBaseTest.java +++ b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/i18n/I18nBaseTest.java @@ -1,10 +1,8 @@ package org.hl7.fhir.utilities.i18n; -import com.ibm.icu.text.PluralRules; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.assertFalse; import java.io.BufferedReader; import java.io.IOException; @@ -12,12 +10,13 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.nio.charset.StandardCharsets; import java.text.MessageFormat; -import java.util.*; +import java.util.Arrays; +import java.util.Locale; +import java.util.ResourceBundle; -import static org.hamcrest.CoreMatchers.containsString; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; class I18nBaseTest { diff --git a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/i18n/ICU4JTests.java b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/i18n/ICU4JTests.java index 86e63815f..21ea59998 100644 --- a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/i18n/ICU4JTests.java +++ b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/i18n/ICU4JTests.java @@ -1,14 +1,15 @@ package org.hl7.fhir.utilities.i18n; -import com.ibm.icu.text.PluralRules; -import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; import java.util.Arrays; import java.util.HashSet; import java.util.Locale; import java.util.Set; -import static org.junit.jupiter.api.Assertions.assertEquals; +import org.junit.jupiter.api.Test; + +import com.ibm.icu.text.PluralRules; public class ICU4JTests { diff --git a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/json/JsonParserTests.java b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/json/JsonParserTests.java index 3ce0422da..a96e98044 100644 --- a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/json/JsonParserTests.java +++ b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/json/JsonParserTests.java @@ -26,7 +26,7 @@ public class JsonParserTests { public void testComments2() throws IOException, JsonException { JsonObject obj = JsonParser.parseObject("{\n // some comment \n \"n1\" : \"v1\"\n}\n", true); Assertions.assertEquals(0, obj.getComments().size()); - JsonString c = obj.getString("n1"); + JsonString c = obj.getJsonString("n1"); Assertions.assertEquals(1, c.getComments().size()); Assertions.assertEquals("some comment", c.getComments().get(0)); Assertions.assertEquals("{\"n1\":\"v1\"}", JsonParser.compose(obj, false)); @@ -38,7 +38,7 @@ public class JsonParserTests { JsonObject obj = JsonParser.parseObject("// some comment\n{\n \"n1\" : \"v1\"\n}\n", true); Assertions.assertEquals(1, obj.getComments().size()); Assertions.assertEquals("some comment", obj.getComments().get(0)); - JsonString c = obj.getString("n1"); + JsonString c = obj.getJsonString("n1"); Assertions.assertEquals(0, c.getComments().size()); Assertions.assertEquals("{\"n1\":\"v1\"}", JsonParser.compose(obj, false)); Assertions.assertEquals("// some comment\n{\n \"n1\" : \"v1\"\n}\n", JsonParser.compose(obj, true)); diff --git a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/npm/PackageClientTest.java b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/npm/PackageClientTest.java index 5984fc8ed..3d91409c8 100644 --- a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/npm/PackageClientTest.java +++ b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/npm/PackageClientTest.java @@ -1,20 +1,16 @@ package org.hl7.fhir.utilities.npm; -import com.google.gson.Gson; -import com.google.gson.JsonObject; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.net.URISyntaxException; + +import org.hl7.fhir.utilities.json.model.JsonObject; +import org.hl7.fhir.utilities.json.parser.JsonParser; import org.hl7.fhir.utilities.tests.ResourceLoaderTests; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import java.io.BufferedReader; -import java.io.InputStreamReader; -import java.net.URISyntaxException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; - -import static org.junit.jupiter.api.Assertions.assertEquals; - public class PackageClientTest implements ResourceLoaderTests { PackageClient packageClient = new PackageClient(PackageClient.PRIMARY_SERVER); @@ -32,7 +28,7 @@ public class PackageClientTest implements ResourceLoaderTests { @Test @DisplayName("test getting package from JSON works") public void getPackageInfoFromJSONTest() throws java.io.IOException, URISyntaxException { - final JsonObject jsonObject = new Gson().fromJson(new InputStreamReader(getResourceAsInputStream("npm","PackageClient-baseTestCase.json")), JsonObject.class); + final JsonObject jsonObject = JsonParser.parseObject(getResourceAsInputStream("npm","PackageClient-baseTestCase.json")); final PackageInfo packageInfo = packageClient.getPackageInfoFromJSON(jsonObject, null, null, null); assertExpectedFields(packageInfo); @@ -41,7 +37,7 @@ public class PackageClientTest implements ResourceLoaderTests { @Test @DisplayName("test getting package from JSON works") public void getPackageInfoWithIdFromJSONTest() throws java.io.IOException { - final JsonObject jsonObject = new Gson().fromJson(new InputStreamReader(getResourceAsInputStream("npm", "PackageClient-testCaseWithId.json")), JsonObject.class); + final JsonObject jsonObject = JsonParser.parseObject(getResourceAsInputStream("npm", "PackageClient-testCaseWithId.json")); final PackageInfo packageInfo = packageClient.getPackageInfoFromJSON(jsonObject, null, null, null); assertExpectedFields(packageInfo); diff --git a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/tests/CachingPackageClientTests.java b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/tests/CachingPackageClientTests.java index ce4e63960..238bd3f06 100644 --- a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/tests/CachingPackageClientTests.java +++ b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/tests/CachingPackageClientTests.java @@ -1,14 +1,14 @@ package org.hl7.fhir.utilities.tests; +import java.io.IOException; +import java.util.List; + import org.hl7.fhir.utilities.npm.CachingPackageClient; import org.hl7.fhir.utilities.npm.CommonPackages; import org.hl7.fhir.utilities.npm.PackageInfo; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -import java.io.IOException; -import java.util.List; - public class CachingPackageClientTests { private static final String SERVER1 = "http://packages.fhir.org"; diff --git a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/tests/PackageCacheTests.java b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/tests/PackageCacheTests.java index db0ccc70f..541d536dd 100644 --- a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/tests/PackageCacheTests.java +++ b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/tests/PackageCacheTests.java @@ -1,5 +1,9 @@ package org.hl7.fhir.utilities.tests; +import java.io.File; +import java.io.IOException; +import java.util.List; + import org.hl7.fhir.utilities.Utilities; import org.hl7.fhir.utilities.npm.CommonPackages; import org.hl7.fhir.utilities.npm.FilesystemPackageCacheManager; @@ -8,10 +12,6 @@ import org.hl7.fhir.utilities.npm.ToolsVersion; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -import java.io.File; -import java.io.IOException; -import java.util.List; - public class PackageCacheTests { @Test diff --git a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/tests/XLSXmlNormaliserTests.java b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/tests/XLSXmlNormaliserTests.java index 8e430c957..4934d421e 100644 --- a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/tests/XLSXmlNormaliserTests.java +++ b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/tests/XLSXmlNormaliserTests.java @@ -1,17 +1,12 @@ package org.hl7.fhir.utilities.tests; -import java.io.FileOutputStream; import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; import java.nio.file.Path; import java.nio.file.Paths; -import java.nio.file.StandardCopyOption; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.TransformerException; -import org.apache.commons.io.IOUtils; import org.hl7.fhir.exceptions.FHIRException; import org.hl7.fhir.utilities.xls.XLSXmlNormaliser; import org.junit.jupiter.api.Assertions; diff --git a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/tests/XhtmlNodeTest.java b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/tests/XhtmlNodeTest.java index b7353153f..1eb8cfa3d 100644 --- a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/tests/XhtmlNodeTest.java +++ b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/tests/XhtmlNodeTest.java @@ -1,5 +1,9 @@ package org.hl7.fhir.utilities.tests; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.ObjectOutputStream; + import org.hl7.fhir.exceptions.FHIRException; import org.hl7.fhir.exceptions.FHIRFormatError; import org.hl7.fhir.utilities.xhtml.XhtmlNode; @@ -9,10 +13,6 @@ import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.ObjectOutputStream; - public class XhtmlNodeTest { private static final Logger ourLog = LoggerFactory.getLogger(XhtmlNodeTest.class); diff --git a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/tests/execution/ModuleTestExecutorTests.java b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/tests/execution/ModuleTestExecutorTests.java index 2d590d0be..5ded8e6df 100644 --- a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/tests/execution/ModuleTestExecutorTests.java +++ b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/tests/execution/ModuleTestExecutorTests.java @@ -1,13 +1,13 @@ package org.hl7.fhir.utilities.tests.execution; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.Arrays; + import org.hl7.fhir.utilities.tests.execution.junit4.JUnit4TestExecutor; import org.hl7.fhir.utilities.tests.execution.junit5.JUnit5ModuleTestExecutor; import org.junit.jupiter.api.Test; -import java.util.Arrays; - -import static org.junit.jupiter.api.Assertions.assertEquals; - public class ModuleTestExecutorTests { @Test