Merge pull request #1009 from hapifhir/gg-202211-json-fixes

Gg 202211 json fixes
This commit is contained in:
Grahame Grieve 2022-11-26 00:51:10 -03:00 committed by GitHub
commit cbb317e8c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 115 additions and 24 deletions

View File

@ -78,6 +78,9 @@ public class Identifier30_40 {
case TEMP:
tgt.setValue(org.hl7.fhir.dstu3.model.Identifier.IdentifierUse.TEMP);
break;
case OLD:
tgt.setValue(org.hl7.fhir.dstu3.model.Identifier.IdentifierUse.SECONDARY);
break;
case SECONDARY:
tgt.setValue(org.hl7.fhir.dstu3.model.Identifier.IdentifierUse.SECONDARY);
break;

View File

@ -78,6 +78,9 @@ public class Identifier30_50 {
case TEMP:
tgt.setValue(org.hl7.fhir.dstu3.model.Identifier.IdentifierUse.TEMP);
break;
case OLD:
tgt.setValue(org.hl7.fhir.dstu3.model.Identifier.IdentifierUse.TEMP);
break;
case SECONDARY:
tgt.setValue(org.hl7.fhir.dstu3.model.Identifier.IdentifierUse.SECONDARY);
break;

View File

@ -23795,7 +23795,9 @@ public class JsonParser extends JsonParserBase {
if (json.has("issue")) {
JsonArray array = getJArray(json, "issue");
for (int i = 0; i < array.size(); i++) {
res.getIssue().add(parseOperationOutcomeIssueComponent(array.get(i).getAsJsonObject()));
if (array.get(i).isJsonObject()) {
res.getIssue().add(parseOperationOutcomeIssueComponent(array.get(i).getAsJsonObject()));
}
}
};
}

View File

@ -218,8 +218,6 @@ public abstract class JsonParserBase extends ParserBase implements IParser {
json.finish();
osw.flush();
}
/* -- json routines --------------------------------------------------- */
@ -227,14 +225,14 @@ public abstract class JsonParserBase extends ParserBase implements IParser {
private boolean htmlPretty;
private JsonObject loadJson(InputStream input) throws JsonSyntaxException, IOException {
return JsonTrackingParser.parse(TextFile.streamToString(input), null, allowUnknownContent, allowComments);
// return parser.parse(TextFile.streamToString(input)).getAsJsonObject();
// the GSON parser is the fastest, but the least robust
if (allowComments || allowUnknownContent) {
return JsonTrackingParser.parse(TextFile.streamToString(input), null, allowUnknownContent, allowComments);
} else {
return (JsonObject) com.google.gson.JsonParser.parseString(TextFile.streamToString(input));
}
}
// private JsonObject loadJson(String input) {
// return parser.parse(input).getAsJsonObject();
// }
//
protected void parseElementProperties(JsonObject json, Element e) throws IOException, FHIRFormatError {
if (json != null && json.has("id"))
e.setId(json.get("id").getAsString());

View File

@ -0,0 +1,63 @@
package org.hl7.fhir.r5.test;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.hl7.fhir.r5.test.utils.TestingUtilities;
import org.hl7.fhir.utilities.json.JsonTrackingParser;
import org.hl7.fhir.utilities.json.JsonTrackingParser.LocationData;
import org.hl7.fhir.utilities.json.model.JsonObject;
import org.hl7.fhir.utilities.json.parser.JsonParser;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
public class JsonParserSpeedTests {
private static final int MAX = 20;
@Test
public void testSpeed() throws IOException {
String cnt = TestingUtilities.loadTestResource("r5", "structuredefinition-language.json");
com.google.gson.JsonObject g = JsonTrackingParser.parse(cnt, null);
JsonObject j = JsonParser.parseObject(cnt);
JsonElement ge = new com.google.gson.JsonParser().parse(cnt);
long t1 = 0;
long t2 = 0;
long t3 = 0;
long t4 = 0;
for (int k = 0; k < MAX; k++) {
long l = System.currentTimeMillis();
for (int i = 0; i < MAX; i++) {
new com.google.gson.JsonParser().parse(cnt);
}
t4 = t4 + System.currentTimeMillis() - l;
l = System.currentTimeMillis();
for (int i = 0; i < MAX; i++) {
JsonTrackingParser.parse(cnt, null);
}
t1 = t1 + System.currentTimeMillis() - l;
l = System.currentTimeMillis();
for (int i = 0; i < MAX; i++) {
Map<JsonElement, LocationData> map = new HashMap<>();
JsonTrackingParser.parse(cnt, map);
}
t2 = t2 + System.currentTimeMillis() - l;
l = System.currentTimeMillis();
for (int i = 0; i < MAX; i++) {
JsonParser.parseObject(cnt);
}
t3 = t3 + System.currentTimeMillis() - l;
}
System.out.println("jtp- = "+t1);
System.out.println("jtp+ = "+t2);
System.out.println("jp = "+t3);
System.out.println("gson = "+t4);
Assertions.assertTrue(t1 > 0);
}
}

View File

@ -5,7 +5,7 @@ import org.hl7.fhir.exceptions.FHIRException;
public class JsonException extends FHIRException {
public JsonException(String msg) {
// TODO Auto-generated constructor stub
super(msg);
}
}

View File

@ -79,7 +79,7 @@ public class JsonArray extends JsonElement implements Iterable<JsonElement> {
}
public JsonObject findByStringProp(JsonArray arr, String prop, String value) {
public JsonObject findByStringProp(String prop, String value) {
for (JsonObject obj : asJsonObjects()) {
if (obj.has(prop) && value.equals(obj.asString(prop)))
return obj;

View File

@ -24,7 +24,9 @@ public class JsonObject extends JsonElement {
public JsonObject add(String name, JsonElement value) throws JsonException {
check(name != null, "Name is null");
check(value != null, "Value is null");
check(get(name) == null, "Name '"+name+"' already exists");
if (get(name) != null) {
check(false, "Name '"+name+"' already exists (value = "+get(name).toString()+")");
}
JsonProperty p = new JsonProperty(name, value);
properties.add(p);
propMap.put(name, p);
@ -116,6 +118,15 @@ public class JsonObject extends JsonElement {
return propMap.containsKey(name);
}
public boolean has(String... names) {
for (String n : names) {
if (propMap.containsKey(n)) {
return true;
}
}
return false;
}
public void remove(String name) {
if (propMap.containsKey(name)) {
propMap.remove(name);
@ -127,6 +138,10 @@ public class JsonObject extends JsonElement {
return properties;
}
public List<String> getNames() {
return Utilities.sorted(propMap.keySet());
}
public String str(String name) {
if (hasPrimitive(name)) {
return get(name).asJsonPrimitive().getValue();
@ -298,6 +313,14 @@ public class JsonObject extends JsonElement {
return new JsonObject();
}
public JsonObject findByStringProp(String arrName, String prop, String value) {
for (JsonObject obj : getJsonObjects(arrName)) {
if (obj.has(prop) && value.equals(obj.asString(prop)))
return obj;
}
return null;
}
public void merge(JsonObject source) {
for (JsonProperty pp : source.getProperties()) {
if (has(pp.getName())) {

View File

@ -12,4 +12,6 @@ public class JsonTrackingParserTests {
JsonTrackingParser.parseJson("{\r\n \"index-version\": 1,\r\n \"files\": []\r\n}");
}
}

View File

@ -34,7 +34,6 @@ 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.npm.FilesystemPackageCacheManager;
import org.hl7.fhir.utilities.npm.NpmPackage;
import org.hl7.fhir.utilities.turtle.Turtle;
@ -718,7 +717,7 @@ public class IgLoader {
private Manager.FhirFormat checkFormat(byte[] cnt, String filename) {
System.out.println(" ..Detect format for " + filename);
try {
JsonTrackingParser.parseJson(cnt);
org.hl7.fhir.utilities.json.parser.JsonParser.parseObject(cnt);
return Manager.FhirFormat.JSON;
} catch (Exception e) {
log("Not JSON: " + e.getMessage());

View File

@ -11,9 +11,8 @@ import org.hl7.fhir.r5.elementmodel.SHCParser.JWT;
import org.hl7.fhir.r5.utils.structuremap.StructureMapUtilities;
import org.hl7.fhir.utilities.TextFile;
import org.hl7.fhir.utilities.Utilities;
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 ResourceChecker {
@ -56,7 +55,7 @@ public class ResourceChecker {
}
// no, we have to look inside, and decide.
try {
JsonObject json = JsonTrackingParser.parseJson(cnt);
JsonObject json = JsonParser.parseObject(cnt);
if (json.has("verifiableCredential")) {
return FhirFormat.SHC;
}

View File

@ -30,12 +30,11 @@ import org.hl7.fhir.r5.utils.validation.constants.ReferenceValidationPolicy;
import org.hl7.fhir.utilities.Utilities;
import org.hl7.fhir.utilities.VersionUtilities;
import org.hl7.fhir.utilities.VersionUtilities.VersionURLInfo;
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 org.hl7.fhir.utilities.npm.FilesystemPackageCacheManager;
import org.hl7.fhir.utilities.npm.NpmPackage;
import com.google.gson.JsonObject;
public class StandAloneValidatorFetcher implements IValidatorResourceFetcher, IValidationPolicyAdvisor, IWorkerContextManager.ICanonicalResourceLocator {
@ -166,9 +165,9 @@ public class StandAloneValidatorFetcher implements IValidatorResourceFetcher, IV
if (mappingsUris.isEmpty()) {
JsonObject json;
try {
json = JsonTrackingParser.fetchJson("http://hl7.org/fhir/mappingspaces.json");
for (JsonObject ms : JsonUtilities.objects(json, "spaces")) {
mappingsUris.add(JsonUtilities.str(ms, "url"));
json = JsonParser.parseObjectFromUrl("http://hl7.org/fhir/mappingspaces.json");
for (JsonObject ms : json.getJsonObjects("spaces")) {
mappingsUris.add(ms.asString("url"));
}
} catch (IOException e) {
// frozen R4 list