Update to use GSON

This commit is contained in:
jamesagnew 2016-07-12 21:44:40 -04:00
parent 040c098742
commit ed941a116b
24 changed files with 738 additions and 654 deletions

View File

@ -26,6 +26,13 @@
<version>1.6.0</version>
</dependency>
<!-- Use an old version of Gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.3.1</version>
</dependency>
<!--
Woodstox note: The hapi-fhir-base-testmindeps-client project includes no Woodstox at all, so that we use the
StAX version that's bundled with the JDK. The hapi-fhir-base-testmindeps-server

View File

@ -38,6 +38,13 @@
<version>3.2</version><!--$NO-MVN-MAN-VER$-->
</dependency>
<!-- Use an old version of Gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.3.1</version>
</dependency>
<!--
Woodstox note: The hapi-fhir-base-testmindeps-client project includes no Woodstox at all, so that we use the
StAX version that's bundled with the JDK. The hapi-fhir-base-testmindeps-server

View File

@ -20,12 +20,9 @@
<!-- JSON -->
<dependency>
<groupId>javax.json</groupId>
<artifactId>javax.json-api</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.7</version>
</dependency>
<!-- XML -->

View File

@ -1333,7 +1333,7 @@ public class GenericJaxRsClientDstu2Test {
@Test
public void testReadWithElementsParam() throws Exception {
String msg = "{\"resourceType\":\"Patient\",\"id\":\"1\",\"meta\":{\"versionId\":\"1\",\"lastUpdated\":\"2014-12-20T18:41:29.706-05:00\"},\"identifier\":[{\"system\":\"urn:MultiFhirVersionTest\",\"value\":\"testSubmitPatient01\"}]}}";
String msg = "{\"resourceType\":\"Patient\",\"id\":\"1\",\"meta\":{\"versionId\":\"1\",\"lastUpdated\":\"2014-12-20T18:41:29.706-05:00\"},\"identifier\":[{\"system\":\"urn:MultiFhirVersionTest\",\"value\":\"testSubmitPatient01\"}]}";

View File

@ -1335,7 +1335,7 @@ public class GenericJaxRsClientDstu3Test {
@Test
public void testReadWithElementsParam() throws Exception {
String msg = "{\"resourceType\":\"Patient\",\"id\":\"1\",\"meta\":{\"versionId\":\"1\",\"lastUpdated\":\"2014-12-20T18:41:29.706-05:00\"},\"identifier\":[{\"system\":\"urn:MultiFhirVersionTest\",\"value\":\"testSubmitPatient01\"}]}}";
String msg = "{\"resourceType\":\"Patient\",\"id\":\"1\",\"meta\":{\"versionId\":\"1\",\"lastUpdated\":\"2014-12-20T18:41:29.706-05:00\"},\"identifier\":[{\"system\":\"urn:MultiFhirVersionTest\",\"value\":\"testSubmitPatient01\"}]}";

View File

@ -1635,7 +1635,9 @@ public class FhirResourceDaoDstu2Test extends BaseJpaDstu2Test {
public void testOrganizationName() {
//@formatter:off
String inputStr = "{\"resourceType\":\"Organization\",\n" +
String inputStr =
"{" +
" \"resourceType\":\"Organization\",\n" +
" \"extension\":[\n" +
" {\n" +
" \"url\":\"http://fhir.connectinggta.ca/Profile/organization#providerIdPool\",\n" +
@ -1655,8 +1657,7 @@ public class FhirResourceDaoDstu2Test extends BaseJpaDstu2Test {
" }\n" +
" ],\n" +
" \"name\":\"Peterborough Regional Health Centre\"\n" +
" }\n" +
" }";
"}\n";
//@formatter:on
Set<Long> val = myOrganizationDao.searchForIds("name", new StringParam("P"));

View File

@ -2044,7 +2044,9 @@ public class FhirResourceDaoDstu3Test extends BaseJpaDstu3Test {
public void testOrganizationName() {
//@formatter:off
String inputStr = "{\"resourceType\":\"Organization\",\n" +
String inputStr =
"{" +
" \"resourceType\":\"Organization\",\n" +
" \"extension\":[\n" +
" {\n" +
" \"url\":\"http://fhir.connectinggta.ca/Profile/organization#providerIdPool\",\n" +
@ -2064,8 +2066,7 @@ public class FhirResourceDaoDstu3Test extends BaseJpaDstu3Test {
" }\n" +
" ],\n" +
" \"name\":\"Peterborough Regional Health Centre\"\n" +
" }\n" +
" }";
"}\n";
//@formatter:on
Set<Long> val = myOrganizationDao.searchForIds("name", new StringParam("P"));

View File

@ -1232,13 +1232,7 @@ public class JsonParserTest {
@Test
public void testParseSingleQuotes() {
try {
ourCtx.newJsonParser().parseBundle("{ 'resourceType': 'Bundle' }");
fail();
} catch (DataFormatException e) {
// Should be an error message about how single quotes aren't valid JSON
assertThat(e.getMessage(), containsString("double quote"));
}
assertEquals("1", ourCtx.newJsonParser().parseBundle("{ 'resourceType': 'Bundle', 'id':'1' }").getBundleId().getValue());
}
@Test

View File

@ -222,7 +222,7 @@ public class CreateTest {
ourLog.info("Response was:\n{}", responseContent);
assertEquals(400, status.getStatusLine().getStatusCode());
assertThat(responseContent, containsString("Unexpected char"));
assertThat(responseContent, containsString("Content does not appear to be FHIR JSON, first non-whitespace character was: '&lt;' (must be '{')"));
}
@Test

View File

@ -775,7 +775,4 @@
"title" : "HTML Report"
}
]
},
"summary" : "\r\n<div xmlns=\"http://www.w3.org/1999/xhtml\"> &#x0A; &#x0A; \r\n <h3>CBC Report for Wile. E. COYOTE (MRN: 23453) issued 3-Mar 2011 11:45</h3> &#x0A;&#x0A; \r\n <!-- you could use ab html table here, but laboratories are still \n using fixed text tables, and this will take decades to change... -->&#x0A; &#x0A; \r\n <pre>&#x0A;Test Units Value Reference Range&#x0A;Haemoglobin g/L 176 135 - 180&#x0A;Red Cell Count x10*12/L 5.9 4.2 - 6.0&#x0A;Haematocrit 0.55+ 0.38 - 0.52&#x0A;Mean Cell Volume fL 99+ 80 - 98&#x0A;Mean Cell Haemoglobin pg 36+ 27 - 35&#x0A;Platelet Count x10*9/L 444 150 - 450&#x0A;White Cell Count x10*9/L 4.6 4.0 - 11.0&#x0A;Neutrophils % 20 &#x0A;Neutrophils x10*9/L 0.9--- 2.0 - 7.5&#x0A;Lymphocytes % 20 &#x0A;Lymphocytes x10*9/L 0.9- 1.1 - 4.0&#x0A;Monocytes % 20 &#x0A;Monocytes x10*9/L 0.9 0.2 - 1.0&#x0A;Eosinophils % 20 &#x0A;Eosinophils x10*9/L 0.92++ 0.04 - 0.40&#x0A;Basophils % 20 &#x0A;Basophils x10*9/L 0.92+++ &lt;0.21&#x0A; </pre>&#x0A; &#x0A; \r\n <p>Acme Laboratory, Inc signed: Dr Pete Pathologist</p>&#x0A; </div>"
}

View File

@ -377,6 +377,9 @@ public class JsonParserDstu2Test {
ourLog.info(enc);
//@formatter:off
String actual = enc.trim();
ourLog.info("Actual:\n{}", actual);
assertEquals("{\n" +
" \"resourceType\": \"Patient\",\n" +
" \"meta\": {\n" +
@ -402,7 +405,7 @@ public class JsonParserDstu2Test {
" ]\n" +
" }\n" +
" ]\n" +
"}", enc.trim());
"}", actual);
//@formatter:on
Patient parsed = ourCtx.newJsonParser().parseResource(Patient.class, enc);
@ -1547,7 +1550,16 @@ public class JsonParserDstu2Test {
*/
@Test
public void testParseResourceWithInvalidType() {
String input = "{" + "\"resourceType\":\"Patient\"," + "\"contained\":[" + " {" + " \"rezType\":\"Organization\"" + " }" + " ]" + "}";
//@formatter:off
String input = "{" +
"\"resourceType\":\"Patient\"," +
"\"contained\":[" +
" {" +
" \"rezType\":\"Organization\"" +
" }" +
" ]" +
"}";
//@formatter:on
IParser jsonParser = ourCtx.newJsonParser().setPrettyPrint(true);
try {
@ -1589,7 +1601,7 @@ public class JsonParserDstu2Test {
ourCtx.newJsonParser().parseResource(Conformance.class, input);
fail();
} catch (DataFormatException e) {
assertEquals("Syntax error parsing JSON FHIR structure: Expected ARRAY at element 'modifierExtension', found 'OBJECT'", e.getMessage());
assertEquals("Syntax error parsing JSON FHIR structure: Expected ARRAY at element 'modifierExtension', found 'JsonObject'", e.getMessage());
}
}

View File

@ -1724,7 +1724,7 @@ public class GenericClientDstu2Test {
@Test
public void testReadWithElementsParam() throws Exception {
String msg = "{\"resourceType\":\"Patient\",\"id\":\"1\",\"meta\":{\"versionId\":\"1\",\"lastUpdated\":\"2014-12-20T18:41:29.706-05:00\"},\"identifier\":[{\"system\":\"urn:MultiFhirVersionTest\",\"value\":\"testSubmitPatient01\"}]}}";
String msg = "{\"resourceType\":\"Patient\",\"id\":\"1\",\"meta\":{\"versionId\":\"1\",\"lastUpdated\":\"2014-12-20T18:41:29.706-05:00\"},\"identifier\":[{\"system\":\"urn:MultiFhirVersionTest\",\"value\":\"testSubmitPatient01\"}]}";
ArgumentCaptor<HttpUriRequest> capt = ArgumentCaptor.forClass(HttpUriRequest.class);
when(myHttpClient.execute(capt.capture())).thenReturn(myHttpResponse);

View File

@ -53,12 +53,6 @@
<version>9.6.0-4</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.3.1</version>
</dependency>
<!-- Testing -->
<dependency>
<groupId>xmlunit</groupId>

View File

@ -924,8 +924,21 @@ public class JsonParserDstu3Test {
*/
@Test
public void testExplanationOfBenefit() {
String input = "{" + " \"resourceType\":\"ExplanationOfBenefit\"," + " \"coverage\": {\n" + " \"coverageReference\": {\n" + " \"reference\": \"Coverage/123\"\n" + " }\n" + " },\n"
+ " \"relationship\": {\n" + " \"system\": \"http://hl7.org/fhir/relationship\",\n" + " \"code\": \"1\",\n" + " \"display\": \"self\"\n" + " }\n" + "}";
//@formatter:off
String input = "{" +
" \"resourceType\": \"ExplanationOfBenefit\"," +
" \"coverage\": {\n" +
" \"coverageReference\": {\n" +
" \"reference\": \"Coverage/123\"\n" +
" }\n" +
" },\n" +
" \"relationship\": {\n" +
" \"system\": \"http://hl7.org/fhir/relationship\",\n" +
" \"code\": \"1\",\n" +
" \"display\": \"self\"\n" +
" }\n" +
"}";
//@formatter:on
ExplanationOfBenefit eob = ourCtx.newJsonParser().parseResource(ExplanationOfBenefit.class, input);
assertEquals(Reference.class, eob.getCoverage().getCoverage().getClass());
@ -1372,9 +1385,24 @@ public class JsonParserDstu3Test {
/**
* See #342
*/
@Test(expected = DataFormatException.class)
@Test()
public void testParseInvalid() {
try {
ourCtx.newJsonParser().parseResource("FOO");
fail();
} catch (DataFormatException e) {
assertEquals("Failed to parse JSON content, error was: Content does not appear to be FHIR JSON, first non-whitespace character was: 'F' (must be '{')", e.getMessage());
}
try {
ourCtx.newJsonParser().parseResource("[\"aaa\"]");
fail();
} catch (DataFormatException e) {
assertEquals("Failed to parse JSON content, error was: Content does not appear to be FHIR JSON, first non-whitespace character was: '[' (must be '{')", e.getMessage());
}
assertEquals(Bundle.class, ourCtx.newJsonParser().parseResource(" {\"resourceType\" : \"Bundle\"}").getClass());
}
@Test
@ -1440,6 +1468,18 @@ public class JsonParserDstu3Test {
}
@Test
public void testParseWithTrailingContent() throws Exception {
//@formatter:off
String bundle = "{\n" +
" \"resourceType\" : \"Bundle\",\n" +
" \"total\" : 1\n" +
"}}";
//@formatter:on
Bundle b = ourCtx.newJsonParser().parseResource(Bundle.class, bundle);
}
/**
* See #163
*/

View File

@ -52,12 +52,6 @@
<version>9.6.0-4</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.3.1</version>
</dependency>
<!-- Testing -->
<dependency>
<groupId>xmlunit</groupId>

View File

@ -1104,13 +1104,7 @@ public class JsonParserHl7OrgDstu2Test {
@Test
public void testParseSingleQuotes() {
ourCtx.newJsonParser().parseResource(Bundle.class, "{ \"resourceType\": \"Bundle\" }");
try {
ourCtx.newJsonParser().parseResource(Bundle.class, "{ 'resourceType': 'Bundle' }");
fail();
} catch (DataFormatException e) {
// good
}
}
/**

View File

@ -5,14 +5,13 @@ import static org.apache.commons.lang3.StringUtils.defaultString;
import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.apache.commons.lang3.StringUtils.isNotBlank;
import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.TreeSet;
import javax.json.Json;
import javax.json.stream.JsonGenerator;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
@ -29,6 +28,8 @@ import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import com.google.gson.stream.JsonWriter;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.context.FhirVersionEnum;
import ca.uhn.fhir.context.RuntimeResourceDefinition;
@ -375,14 +376,14 @@ public class Controller extends BaseController {
@SuppressWarnings("unchecked")
@RequestMapping(value = { "/search" })
public String actionSearch(HttpServletRequest theReq, HomeRequest theRequest, BindingResult theBindingResult, ModelMap theModel) {
public String actionSearch(HttpServletRequest theReq, HomeRequest theRequest, BindingResult theBindingResult, ModelMap theModel) throws IOException {
addCommonParams(theReq, theRequest, theModel);
StringWriter clientCodeJsonStringWriter = new StringWriter();
JsonGenerator clientCodeJsonWriter = Json.createGenerator(clientCodeJsonStringWriter);
clientCodeJsonWriter.writeStartObject();
clientCodeJsonWriter.write("action", "search");
clientCodeJsonWriter.write("base", (String) theModel.get("base"));
JsonWriter clientCodeJsonWriter = new JsonWriter(clientCodeJsonStringWriter);
clientCodeJsonWriter.beginObject();
clientCodeJsonWriter.name("action");clientCodeJsonWriter.value("search");
clientCodeJsonWriter.name("base");clientCodeJsonWriter.value((String) theModel.get("base"));
CaptureInterceptor interceptor = new CaptureInterceptor();
GenericClient client = theRequest.newClient(theReq, getContext(theRequest), myConfig, interceptor);
@ -396,27 +397,28 @@ public class Controller extends BaseController {
theModel.put("errorMsg", e.toString());
return "resource";
}
clientCodeJsonWriter.write("resource", theReq.getParameter("resource"));
clientCodeJsonWriter.name("resource");clientCodeJsonWriter.value(theReq.getParameter("resource"));
} else {
query = search.forAllResources();
clientCodeJsonWriter.writeNull("resource");
clientCodeJsonWriter.name("resource");clientCodeJsonWriter.nullValue();
}
if (client.getPrettyPrint() != null) {
clientCodeJsonWriter.write("pretty", client.getPrettyPrint().toString());
clientCodeJsonWriter.name("pretty");clientCodeJsonWriter.value( client.getPrettyPrint().toString());
} else {
clientCodeJsonWriter.writeNull("pretty");
clientCodeJsonWriter.name("pretty");clientCodeJsonWriter.nullValue();
}
if (client.getEncoding() != null) {
clientCodeJsonWriter.write("format", client.getEncoding().getRequestContentType());
clientCodeJsonWriter.name("format");clientCodeJsonWriter.value(client.getEncoding().getRequestContentType());
} else {
clientCodeJsonWriter.writeNull("format");
clientCodeJsonWriter.name("format");clientCodeJsonWriter.nullValue();
}
String outcomeDescription = "Search for Resources";
clientCodeJsonWriter.writeStartArray("params");
clientCodeJsonWriter.name("params");
clientCodeJsonWriter.beginArray();
int paramIdx = -1;
while (true) {
paramIdx++;
@ -427,31 +429,33 @@ public class Controller extends BaseController {
break;
}
}
clientCodeJsonWriter.writeEnd();
clientCodeJsonWriter.endArray();
clientCodeJsonWriter.writeStartArray("includes");
clientCodeJsonWriter.name("includes");
clientCodeJsonWriter.beginArray();
String[] incValues = theReq.getParameterValues(Constants.PARAM_INCLUDE);
if (incValues != null) {
for (String next : incValues) {
if (isNotBlank(next)) {
query.include(new Include(next));
clientCodeJsonWriter.write(next);
clientCodeJsonWriter.value(next);
}
}
}
clientCodeJsonWriter.writeEnd();
clientCodeJsonWriter.endArray();
clientCodeJsonWriter.writeStartArray("revincludes");
clientCodeJsonWriter.name("revincludes");
clientCodeJsonWriter.beginArray();
String[] revIncValues = theReq.getParameterValues(Constants.PARAM_REVINCLUDE);
if (revIncValues != null) {
for (String next : revIncValues) {
if (isNotBlank(next)) {
query.revInclude(new Include(next));
clientCodeJsonWriter.write(next);
clientCodeJsonWriter.value(next);
}
}
}
clientCodeJsonWriter.writeEnd();
clientCodeJsonWriter.endArray();
String limit = theReq.getParameter("resource-search-limit");
if (isNotBlank(limit)) {
@ -461,9 +465,9 @@ public class Controller extends BaseController {
}
int limitInt = Integer.parseInt(limit);
query.limitTo(limitInt);
clientCodeJsonWriter.write("limit", limit);
clientCodeJsonWriter.name("limit");clientCodeJsonWriter.value(limit);
} else {
clientCodeJsonWriter.writeNull("limit");
clientCodeJsonWriter.name("limit");clientCodeJsonWriter.nullValue();
}
String[] sort = theReq.getParameterValues("sort_by");
@ -501,7 +505,7 @@ public class Controller extends BaseController {
processAndAddLastClientInvocation(client, returnsResource, theModel, delay, outcomeDescription, interceptor, theRequest);
clientCodeJsonWriter.writeEnd();
clientCodeJsonWriter.endObject();
clientCodeJsonWriter.close();
String clientCodeJson = clientCodeJsonStringWriter.toString();
theModel.put("clientCodeJson", clientCodeJson);
@ -837,7 +841,7 @@ public class Controller extends BaseController {
return haveSearchParams;
}
private boolean handleSearchParam(String paramIdxString, HttpServletRequest theReq, IQuery<?> theQuery, JsonGenerator theClientCodeJsonWriter) {
private boolean handleSearchParam(String paramIdxString, HttpServletRequest theReq, IQuery<?> theQuery, JsonWriter theClientCodeJsonWriter) throws IOException {
String nextName = theReq.getParameter("param." + paramIdxString + ".name");
if (isBlank(nextName)) {
return false;
@ -921,12 +925,12 @@ public class Controller extends BaseController {
for (String nextValue : values) {
theClientCodeJsonWriter.writeStartObject();
theClientCodeJsonWriter.write("type", nextType);
theClientCodeJsonWriter.write("name", nextName);
theClientCodeJsonWriter.write("qualifier", nextQualifier);
theClientCodeJsonWriter.write("value", nextValue);
theClientCodeJsonWriter.writeEnd();
theClientCodeJsonWriter.beginObject();
theClientCodeJsonWriter.name("type");theClientCodeJsonWriter.value(nextType);
theClientCodeJsonWriter.name("name");theClientCodeJsonWriter.value(nextName);
theClientCodeJsonWriter.name("qualifier");theClientCodeJsonWriter.value(nextQualifier);
theClientCodeJsonWriter.name("value");theClientCodeJsonWriter.value(nextValue);
theClientCodeJsonWriter.endObject();
if (addToWhere) {
theQuery.where(new StringClientParam(nextName + nextQualifier).matches().value(nextValue));
}