fix json issue, and add tests for it

This commit is contained in:
Grahame Grieve 2021-10-13 08:10:27 +11:00
parent 46b500d1a9
commit f120a617c2
3 changed files with 56 additions and 11 deletions

View File

@ -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);
}
}
}

View File

@ -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<Boolean> valued = new ArrayList<Boolean>();
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

View File

@ -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);
}
}