From f120a617c2ffeffabe1487868f8a56f574ee87d5 Mon Sep 17 00:00:00 2001 From: Grahame Grieve Date: Wed, 13 Oct 2021 08:10:27 +1100 Subject: [PATCH] fix json issue, and add tests for it --- .../hl7/fhir/r5/context/TerminologyCache.java | 4 ++- .../fhir/r5/formats/JsonCreatorDirect.java | 32 +++++++++++++------ .../org/hl7/fhir/r5/test/JsonDirectTests.java | 31 ++++++++++++++++++ 3 files changed, 56 insertions(+), 11 deletions(-) diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/TerminologyCache.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/TerminologyCache.java index 529204720..1f0184dd1 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/TerminologyCache.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/TerminologyCache.java @@ -354,6 +354,7 @@ public class TerminologyCache { private void load() throws FHIRException { for (String fn : new File(folder).list()) { if (fn.endsWith(".cache") && !fn.equals("validation.cache")) { + int c = 0; try { String title = fn.substring(0, fn.lastIndexOf(".")); NamedCache nc = new NamedCache(); @@ -364,6 +365,7 @@ public class TerminologyCache { src = src.substring(1); int i = src.indexOf(ENTRY_MARKER); while (i > -1) { + c++; String s = src.substring(0, i); src = src.substring(i+ENTRY_MARKER.length()+1); i = src.indexOf(ENTRY_MARKER); @@ -395,7 +397,7 @@ public class TerminologyCache { } } } catch (Exception e) { - throw new FHIRException("Error loading "+fn+": "+e.getMessage(), e); + throw new FHIRException("Error loading "+fn+": "+e.getMessage()+" entry "+c, e); } } } diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/formats/JsonCreatorDirect.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/formats/JsonCreatorDirect.java index 26c35b3c1..a7a62e890 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/formats/JsonCreatorDirect.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/formats/JsonCreatorDirect.java @@ -34,6 +34,8 @@ package org.hl7.fhir.r5.formats; import java.io.IOException; import java.io.Writer; import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.List; import org.hl7.fhir.utilities.Utilities; @@ -48,7 +50,7 @@ public class JsonCreatorDirect implements JsonCreator { private Writer writer; private boolean pretty; private boolean named; - private boolean valued; + private List valued = new ArrayList(); private int indent; public JsonCreatorDirect(Writer writer) { @@ -66,6 +68,10 @@ public class JsonCreatorDirect implements JsonCreator { checkState(); writer.write("{"); stepIn(); + if (!valued.isEmpty()) { + valued.set(0, true); + } + valued.add(0, false); } public void stepIn() throws IOException { @@ -96,7 +102,7 @@ public class JsonCreatorDirect implements JsonCreator { writer.write(":"); named = false; } - if (valued) { + if (!valued.isEmpty() && valued.get(0)) { writer.write(","); if (pretty) { writer.write("\r\n"); @@ -104,7 +110,7 @@ public class JsonCreatorDirect implements JsonCreator { writer.write(" "); } } - valued = false; + valued.set(0, false); } } @@ -112,13 +118,14 @@ public class JsonCreatorDirect implements JsonCreator { public void endObject() throws IOException { stepOut(); writer.write("}"); + valued.remove(0); } @Override public void nullValue() throws IOException { checkState(); writer.write("null"); - valued = true; + valued.set(0, true); } @Override @@ -132,7 +139,7 @@ public class JsonCreatorDirect implements JsonCreator { public void value(String value) throws IOException { checkState(); writer.write("\""+Utilities.escapeJson(value)+"\""); - valued = true; + valued.set(0, true); } @Override @@ -144,7 +151,7 @@ public class JsonCreatorDirect implements JsonCreator { writer.write("true"); else writer.write("false"); - valued = true; + valued.set(0, true); } @Override @@ -154,7 +161,7 @@ public class JsonCreatorDirect implements JsonCreator { writer.write("null"); else writer.write(value.toString()); - valued = true; + valued.set(0, true); } @Override @@ -164,7 +171,7 @@ public class JsonCreatorDirect implements JsonCreator { writer.write("null"); else writer.write(value); - valued = true; + valued.set(0, true); } @Override @@ -174,23 +181,28 @@ public class JsonCreatorDirect implements JsonCreator { writer.write("null"); else writer.write(value.toString()); - valued = true; + valued.set(0, true); } @Override public void beginArray() throws IOException { checkState(); writer.write("["); + if (!valued.isEmpty()) { + valued.set(0, true); + } + valued.add(0, false); } @Override public void endArray() throws IOException { writer.write("]"); + valued.remove(0); } @Override public void finish() throws IOException { - // nothing + writer.flush(); } @Override diff --git a/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/test/JsonDirectTests.java b/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/test/JsonDirectTests.java index 773c107c0..03ac51d8b 100644 --- a/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/test/JsonDirectTests.java +++ b/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/test/JsonDirectTests.java @@ -1,14 +1,17 @@ package org.hl7.fhir.r5.test; +import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; +import java.io.OutputStreamWriter; import org.apache.commons.io.FileUtils; import org.hl7.fhir.exceptions.FHIRFormatError; import org.hl7.fhir.r5.formats.IParser.OutputStyle; +import org.hl7.fhir.r5.formats.JsonCreatorDirect; import org.hl7.fhir.r5.formats.JsonParser; import org.hl7.fhir.r5.formats.XmlParser; import org.hl7.fhir.r5.model.Observation; @@ -16,6 +19,8 @@ import org.hl7.fhir.utilities.Utilities; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; +import junit.framework.Assert; + public class JsonDirectTests { @Test @@ -33,4 +38,30 @@ public class JsonDirectTests { new XmlParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(xml), obs); } + @Test + public void testEmptyObject() throws FHIRFormatError, FileNotFoundException, IOException { + ByteArrayOutputStream bs = new ByteArrayOutputStream(); + JsonCreatorDirect json = new JsonCreatorDirect(new OutputStreamWriter(bs, "UTF-8")); + json.beginObject(); + json.name("a"); + json.beginObject(); + json.endObject(); + json.name("b"); + json.beginArray(); + json.beginObject(); + json.endObject(); + json.beginObject(); + json.endObject(); + json.endArray(); + json.name("c"); + json.beginArray(); + json.endArray(); + json.name("test"); + json.value("test"); + json.endObject(); + json.finish(); + String s = new String(bs.toByteArray()); + Assert.assertEquals("{\"a\":{},\"b\":[{},{}],\"c\":[],\"test\":\"test\"}", s); + } + } \ No newline at end of file