fix json issue, and add tests for it
This commit is contained in:
parent
46b500d1a9
commit
f120a617c2
|
@ -354,6 +354,7 @@ public class TerminologyCache {
|
||||||
private void load() throws FHIRException {
|
private void load() throws FHIRException {
|
||||||
for (String fn : new File(folder).list()) {
|
for (String fn : new File(folder).list()) {
|
||||||
if (fn.endsWith(".cache") && !fn.equals("validation.cache")) {
|
if (fn.endsWith(".cache") && !fn.equals("validation.cache")) {
|
||||||
|
int c = 0;
|
||||||
try {
|
try {
|
||||||
String title = fn.substring(0, fn.lastIndexOf("."));
|
String title = fn.substring(0, fn.lastIndexOf("."));
|
||||||
NamedCache nc = new NamedCache();
|
NamedCache nc = new NamedCache();
|
||||||
|
@ -364,6 +365,7 @@ public class TerminologyCache {
|
||||||
src = src.substring(1);
|
src = src.substring(1);
|
||||||
int i = src.indexOf(ENTRY_MARKER);
|
int i = src.indexOf(ENTRY_MARKER);
|
||||||
while (i > -1) {
|
while (i > -1) {
|
||||||
|
c++;
|
||||||
String s = src.substring(0, i);
|
String s = src.substring(0, i);
|
||||||
src = src.substring(i+ENTRY_MARKER.length()+1);
|
src = src.substring(i+ENTRY_MARKER.length()+1);
|
||||||
i = src.indexOf(ENTRY_MARKER);
|
i = src.indexOf(ENTRY_MARKER);
|
||||||
|
@ -395,7 +397,7 @@ public class TerminologyCache {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new FHIRException("Error loading "+fn+": "+e.getMessage(), e);
|
throw new FHIRException("Error loading "+fn+": "+e.getMessage()+" entry "+c, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,6 +34,8 @@ package org.hl7.fhir.r5.formats;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import org.hl7.fhir.utilities.Utilities;
|
import org.hl7.fhir.utilities.Utilities;
|
||||||
|
|
||||||
|
@ -48,7 +50,7 @@ public class JsonCreatorDirect implements JsonCreator {
|
||||||
private Writer writer;
|
private Writer writer;
|
||||||
private boolean pretty;
|
private boolean pretty;
|
||||||
private boolean named;
|
private boolean named;
|
||||||
private boolean valued;
|
private List<Boolean> valued = new ArrayList<Boolean>();
|
||||||
private int indent;
|
private int indent;
|
||||||
|
|
||||||
public JsonCreatorDirect(Writer writer) {
|
public JsonCreatorDirect(Writer writer) {
|
||||||
|
@ -66,6 +68,10 @@ public class JsonCreatorDirect implements JsonCreator {
|
||||||
checkState();
|
checkState();
|
||||||
writer.write("{");
|
writer.write("{");
|
||||||
stepIn();
|
stepIn();
|
||||||
|
if (!valued.isEmpty()) {
|
||||||
|
valued.set(0, true);
|
||||||
|
}
|
||||||
|
valued.add(0, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void stepIn() throws IOException {
|
public void stepIn() throws IOException {
|
||||||
|
@ -96,7 +102,7 @@ public class JsonCreatorDirect implements JsonCreator {
|
||||||
writer.write(":");
|
writer.write(":");
|
||||||
named = false;
|
named = false;
|
||||||
}
|
}
|
||||||
if (valued) {
|
if (!valued.isEmpty() && valued.get(0)) {
|
||||||
writer.write(",");
|
writer.write(",");
|
||||||
if (pretty) {
|
if (pretty) {
|
||||||
writer.write("\r\n");
|
writer.write("\r\n");
|
||||||
|
@ -104,7 +110,7 @@ public class JsonCreatorDirect implements JsonCreator {
|
||||||
writer.write(" ");
|
writer.write(" ");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
valued = false;
|
valued.set(0, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -112,13 +118,14 @@ public class JsonCreatorDirect implements JsonCreator {
|
||||||
public void endObject() throws IOException {
|
public void endObject() throws IOException {
|
||||||
stepOut();
|
stepOut();
|
||||||
writer.write("}");
|
writer.write("}");
|
||||||
|
valued.remove(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void nullValue() throws IOException {
|
public void nullValue() throws IOException {
|
||||||
checkState();
|
checkState();
|
||||||
writer.write("null");
|
writer.write("null");
|
||||||
valued = true;
|
valued.set(0, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -132,7 +139,7 @@ public class JsonCreatorDirect implements JsonCreator {
|
||||||
public void value(String value) throws IOException {
|
public void value(String value) throws IOException {
|
||||||
checkState();
|
checkState();
|
||||||
writer.write("\""+Utilities.escapeJson(value)+"\"");
|
writer.write("\""+Utilities.escapeJson(value)+"\"");
|
||||||
valued = true;
|
valued.set(0, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -144,7 +151,7 @@ public class JsonCreatorDirect implements JsonCreator {
|
||||||
writer.write("true");
|
writer.write("true");
|
||||||
else
|
else
|
||||||
writer.write("false");
|
writer.write("false");
|
||||||
valued = true;
|
valued.set(0, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -154,7 +161,7 @@ public class JsonCreatorDirect implements JsonCreator {
|
||||||
writer.write("null");
|
writer.write("null");
|
||||||
else
|
else
|
||||||
writer.write(value.toString());
|
writer.write(value.toString());
|
||||||
valued = true;
|
valued.set(0, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -164,7 +171,7 @@ public class JsonCreatorDirect implements JsonCreator {
|
||||||
writer.write("null");
|
writer.write("null");
|
||||||
else
|
else
|
||||||
writer.write(value);
|
writer.write(value);
|
||||||
valued = true;
|
valued.set(0, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -174,23 +181,28 @@ public class JsonCreatorDirect implements JsonCreator {
|
||||||
writer.write("null");
|
writer.write("null");
|
||||||
else
|
else
|
||||||
writer.write(value.toString());
|
writer.write(value.toString());
|
||||||
valued = true;
|
valued.set(0, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void beginArray() throws IOException {
|
public void beginArray() throws IOException {
|
||||||
checkState();
|
checkState();
|
||||||
writer.write("[");
|
writer.write("[");
|
||||||
|
if (!valued.isEmpty()) {
|
||||||
|
valued.set(0, true);
|
||||||
|
}
|
||||||
|
valued.add(0, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void endArray() throws IOException {
|
public void endArray() throws IOException {
|
||||||
writer.write("]");
|
writer.write("]");
|
||||||
|
valued.remove(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void finish() throws IOException {
|
public void finish() throws IOException {
|
||||||
// nothing
|
writer.flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -1,14 +1,17 @@
|
||||||
package org.hl7.fhir.r5.test;
|
package org.hl7.fhir.r5.test;
|
||||||
|
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.OutputStreamWriter;
|
||||||
|
|
||||||
import org.apache.commons.io.FileUtils;
|
import org.apache.commons.io.FileUtils;
|
||||||
import org.hl7.fhir.exceptions.FHIRFormatError;
|
import org.hl7.fhir.exceptions.FHIRFormatError;
|
||||||
import org.hl7.fhir.r5.formats.IParser.OutputStyle;
|
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.JsonParser;
|
||||||
import org.hl7.fhir.r5.formats.XmlParser;
|
import org.hl7.fhir.r5.formats.XmlParser;
|
||||||
import org.hl7.fhir.r5.model.Observation;
|
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.Disabled;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import junit.framework.Assert;
|
||||||
|
|
||||||
public class JsonDirectTests {
|
public class JsonDirectTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -33,4 +38,30 @@ public class JsonDirectTests {
|
||||||
new XmlParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(xml), obs);
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue