();
+
+ public JacksonJsonArray(ArrayNode json) {
+ this.nativeArray = json;
+ }
+
+ @Override
+ public Object getValue() {
+ return null;
+ }
+
+ @Override
+ public int size() {
+ return nativeArray.size();
+ }
+
+ @Override
+ public JsonLikeValue get(int index) {
+ Integer key = index;
+ JsonLikeValue result = null;
+ if (jsonLikeMap.containsKey(key)) {
+ result = jsonLikeMap.get(key);
+ } else {
+ JsonNode child = nativeArray.get(index);
+ if (child != null) {
+ result = new JacksonJsonValue(child);
+ }
+ jsonLikeMap.put(key, result);
+ }
+ return result;
+ }
+ }
+
+ private static class JacksonJsonValue extends JsonLikeValue {
+ private final JsonNode nativeValue;
+ private JsonLikeObject jsonLikeObject = null;
+ private JsonLikeArray jsonLikeArray = null;
+
+ public JacksonJsonValue(JsonNode jsonNode) {
+ this.nativeValue = jsonNode;
+ }
+
+ @Override
+ public Object getValue() {
+ if (nativeValue != null && nativeValue.isValueNode()) {
+ if (nativeValue.isNumber()) {
+ return nativeValue.numberValue();
+ }
+
+ if (nativeValue.isBoolean()) {
+ return nativeValue.booleanValue();
+ }
+
+ return nativeValue.asText();
+ }
+ return null;
+ }
+
+ @Override
+ public ValueType getJsonType() {
+ if (null == nativeValue || nativeValue.isNull()) {
+ return ValueType.NULL;
+ }
+ if (nativeValue.isObject()) {
+ return ValueType.OBJECT;
+ }
+ if (nativeValue.isArray()) {
+ return ValueType.ARRAY;
+ }
+ if (nativeValue.isValueNode()) {
+ return ValueType.SCALAR;
+ }
+ return null;
+ }
+
+ @Override
+ public ScalarType getDataType() {
+ if (nativeValue != null && nativeValue.isValueNode()) {
+ if (nativeValue.isNumber()) {
+ return ScalarType.NUMBER;
+ }
+ if (nativeValue.isTextual()) {
+ return ScalarType.STRING;
+ }
+ if (nativeValue.isBoolean()) {
+ return ScalarType.BOOLEAN;
+ }
+ }
+ return null;
+ }
+
+ @Override
+ public JsonLikeArray getAsArray() {
+ if (nativeValue != null && nativeValue.isArray()) {
+ if (null == jsonLikeArray) {
+ jsonLikeArray = new JacksonJsonArray((ArrayNode) nativeValue);
+ }
+ }
+ return jsonLikeArray;
+ }
+
+ @Override
+ public JsonLikeObject getAsObject() {
+ if (nativeValue != null && nativeValue.isObject()) {
+ if (null == jsonLikeObject) {
+ jsonLikeObject = new JacksonJsonObject((ObjectNode) nativeValue);
+ }
+ }
+ return jsonLikeObject;
+ }
+
+ @Override
+ public Number getAsNumber() {
+ return nativeValue != null ? nativeValue.numberValue() : null;
+ }
+
+ @Override
+ public String getAsString() {
+ if (nativeValue != null) {
+ if (nativeValue instanceof DecimalNode) {
+ BigDecimal value = nativeValue.decimalValue();
+ return value.toPlainString();
+ }
+ return nativeValue.asText();
+ }
+ return null;
+ }
+
+ @Override
+ public boolean getAsBoolean() {
+ if (nativeValue != null && nativeValue.isValueNode() && nativeValue.isBoolean()) {
+ return nativeValue.asBoolean();
+ }
+ return super.getAsBoolean();
+ }
+ }
+
+ private static ObjectMapper createObjectMapper() {
+ ObjectMapper retVal = new ObjectMapper();
+ retVal = retVal.setNodeFactory(new JsonNodeFactory(true));
+ retVal = retVal.enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS);
+ retVal = retVal.enable(DeserializationFeature.FAIL_ON_TRAILING_TOKENS);
+ retVal = retVal.disable(JsonParser.Feature.INCLUDE_SOURCE_IN_LOCATION);
+ retVal = retVal.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
+ retVal = retVal.disable(JsonParser.Feature.AUTO_CLOSE_SOURCE);
+ retVal = retVal.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
+ return retVal;
+ }
+}
diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/json/jackson/JacksonWriter.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/json/jackson/JacksonWriter.java
new file mode 100644
index 00000000000..54c40e36e9c
--- /dev/null
+++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/json/jackson/JacksonWriter.java
@@ -0,0 +1,197 @@
+package ca.uhn.fhir.parser.json.jackson;
+
+import ca.uhn.fhir.parser.json.JsonLikeWriter;
+import com.fasterxml.jackson.core.JsonFactory;
+import com.fasterxml.jackson.core.JsonGenerator;
+import com.fasterxml.jackson.core.PrettyPrinter;
+import com.fasterxml.jackson.core.util.DefaultIndenter;
+import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
+import com.fasterxml.jackson.core.util.Separators;
+
+import java.io.IOException;
+import java.io.Writer;
+import java.math.BigDecimal;
+import java.math.BigInteger;
+
+public class JacksonWriter extends JsonLikeWriter {
+
+ private JsonGenerator myJsonGenerator;
+
+ public JacksonWriter(JsonFactory theJsonFactory, Writer theWriter) throws IOException {
+ myJsonGenerator = theJsonFactory.createGenerator(theWriter);
+ setWriter(theWriter);
+ }
+
+ public JacksonWriter() {
+ }
+
+ @Override
+ public JsonLikeWriter init() {
+ if (isPrettyPrint()) {
+ DefaultPrettyPrinter prettyPrinter = new DefaultPrettyPrinter() {
+
+ /**
+ * Objects should serialize as
+ *
+ * {
+ * "key": "value"
+ * }
+ *
+ * in order to be consistent with Gson behaviour, instead of the jackson default
+ *
+ * {
+ * "key" : "value"
+ * }
+ *
+ */
+ @Override
+ public DefaultPrettyPrinter withSeparators(Separators separators) {
+ _separators = separators;
+ _objectFieldValueSeparatorWithSpaces = separators.getObjectFieldValueSeparator() + " ";
+ return this;
+ }
+
+ };
+ prettyPrinter = prettyPrinter.withObjectIndenter(new DefaultIndenter(" ", "\n"));
+
+ myJsonGenerator.setPrettyPrinter(prettyPrinter);
+ }
+ return this;
+ }
+
+ @Override
+ public JsonLikeWriter flush() {
+ return this;
+ }
+
+ @Override
+ public void close() throws IOException {
+ myJsonGenerator.close();
+ }
+
+ @Override
+ public JsonLikeWriter beginObject() throws IOException {
+ myJsonGenerator.writeStartObject();
+ return this;
+ }
+
+ @Override
+ public JsonLikeWriter beginObject(String name) throws IOException {
+ myJsonGenerator.writeObjectFieldStart(name);
+ return this;
+ }
+
+ @Override
+ public JsonLikeWriter beginArray(String name) throws IOException {
+ myJsonGenerator.writeArrayFieldStart(name);
+ return this;
+ }
+
+ @Override
+ public JsonLikeWriter write(String value) throws IOException {
+ myJsonGenerator.writeObject(value);
+ return this;
+ }
+
+ @Override
+ public JsonLikeWriter write(BigInteger value) throws IOException {
+ myJsonGenerator.writeObject(value);
+ return this;
+ }
+
+ @Override
+ public JsonLikeWriter write(BigDecimal value) throws IOException {
+ myJsonGenerator.writeObject(value);
+ return this;
+ }
+
+ @Override
+ public JsonLikeWriter write(long value) throws IOException {
+ myJsonGenerator.writeObject(value);
+ return this;
+ }
+
+ @Override
+ public JsonLikeWriter write(double value) throws IOException {
+ myJsonGenerator.writeObject(value);
+ return this;
+ }
+
+ @Override
+ public JsonLikeWriter write(Boolean value) throws IOException {
+ myJsonGenerator.writeObject(value);
+ return this;
+ }
+
+ @Override
+ public JsonLikeWriter write(boolean value) throws IOException {
+ myJsonGenerator.writeObject(value);
+ return this;
+ }
+
+ @Override
+ public JsonLikeWriter writeNull() throws IOException {
+ myJsonGenerator.writeNull();
+ return this;
+ }
+
+ @Override
+ public JsonLikeWriter write(String name, String value) throws IOException {
+ myJsonGenerator.writeObjectField(name, value);
+ return this;
+ }
+
+ @Override
+ public JsonLikeWriter write(String name, BigInteger value) throws IOException {
+ myJsonGenerator.writeObjectField(name, value);
+ return this;
+ }
+
+ @Override
+ public JsonLikeWriter write(String name, BigDecimal value) throws IOException {
+ myJsonGenerator.writeObjectField(name, value);
+ return this;
+ }
+
+ @Override
+ public JsonLikeWriter write(String name, long value) throws IOException {
+ myJsonGenerator.writeObjectField(name, value);
+ return this;
+ }
+
+ @Override
+ public JsonLikeWriter write(String name, double value) throws IOException {
+ myJsonGenerator.writeObjectField(name, value);
+ return this;
+ }
+
+ @Override
+ public JsonLikeWriter write(String name, Boolean value) throws IOException {
+ myJsonGenerator.writeObjectField(name, value);
+ return this;
+ }
+
+ @Override
+ public JsonLikeWriter write(String name, boolean value) throws IOException {
+ myJsonGenerator.writeObjectField(name, value);
+ return this;
+ }
+
+ @Override
+ public JsonLikeWriter endObject() throws IOException {
+ myJsonGenerator.writeEndObject();
+ return this;
+ }
+
+ @Override
+ public JsonLikeWriter endArray() throws IOException {
+ myJsonGenerator.writeEndArray();
+ return this;
+ }
+
+ @Override
+ public JsonLikeWriter endBlock() throws IOException {
+ myJsonGenerator.writeEndObject();
+ return this;
+ }
+}
diff --git a/hapi-fhir-base/src/test/java/ca/uhn/fhir/parser/json/JsonLikeStructureTest.java b/hapi-fhir-base/src/test/java/ca/uhn/fhir/parser/json/JsonLikeStructureTest.java
index bc120a0e2c6..3b71f8a8840 100644
--- a/hapi-fhir-base/src/test/java/ca/uhn/fhir/parser/json/JsonLikeStructureTest.java
+++ b/hapi-fhir-base/src/test/java/ca/uhn/fhir/parser/json/JsonLikeStructureTest.java
@@ -5,6 +5,7 @@ import static org.junit.Assert.assertNotNull;
import java.io.StringReader;
+import ca.uhn.fhir.parser.json.jackson.JacksonStructure;
import org.junit.Test;
public class JsonLikeStructureTest {
@@ -39,7 +40,7 @@ public class JsonLikeStructureTest {
@Test
public void testStructureLoading() {
StringReader reader = new StringReader(TEST_STRUCTURELOADING_DATA);
- JsonLikeStructure jsonStructure = new GsonStructure();
+ JsonLikeStructure jsonStructure = new JacksonStructure();
jsonStructure.load(reader);
JsonLikeObject rootObject = jsonStructure.getRootObject();
@@ -70,7 +71,7 @@ public class JsonLikeStructureTest {
@Test
public void testJsonAndDataTypes() {
StringReader reader = new StringReader(TEST_JSONTYPES_DATA);
- JsonLikeStructure jsonStructure = new GsonStructure();
+ JsonLikeStructure jsonStructure = new JacksonStructure();
jsonStructure.load(reader);
JsonLikeObject rootObject = jsonStructure.getRootObject();
diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/r4/ResourceProviderR4Test.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/r4/ResourceProviderR4Test.java
index f110b447a96..56c98b94cfd 100644
--- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/r4/ResourceProviderR4Test.java
+++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/r4/ResourceProviderR4Test.java
@@ -5368,7 +5368,7 @@ public class ResourceProviderR4Test extends BaseResourceProviderR4Test {
@Test
public void testValidateJsonWithDuplicateKey() throws IOException {
- String inputStr = "{\"resourceType\":\"Patient\", \"name\":[{\"text\":foo\"}], name:[{\"text\":\"foo\"}] }";
+ String inputStr = "{\"resourceType\":\"Patient\", \"name\":[{\"text\":\"foo\"}], \"name\":[{\"text\":\"foo\"}] }";
HttpPost post = new HttpPost(ourServerBase + "/Patient/$validate");
post.setEntity(new StringEntity(inputStr, ContentType.create(Constants.CT_FHIR_JSON_NEW, "UTF-8")));
@@ -5378,7 +5378,7 @@ public class ResourceProviderR4Test extends BaseResourceProviderR4Test {
ourLog.info(resp);
assertEquals(412, response.getStatusLine().getStatusCode());
- assertThat(resp, stringContainsInOrder("Error parsing JSON source: Syntax error in json reading special word false at Line 1"));
+ assertThat(resp, stringContainsInOrder("Duplicated property name: name"));
} finally {
response.getEntity().getContent().close();
response.close();
diff --git a/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/parser/JsonParserDstu2_1Test.java b/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/parser/JsonParserDstu2_1Test.java
index b611feb0055..f8a232a60a4 100644
--- a/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/parser/JsonParserDstu2_1Test.java
+++ b/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/parser/JsonParserDstu2_1Test.java
@@ -1518,13 +1518,13 @@ public class JsonParserDstu2_1Test {
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());
+ assertEquals("Failed to parse JSON encoded FHIR content: 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("Failed to parse JSON encoded FHIR content: 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());
diff --git a/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/parser/XmlParserDstu2_1Test.java b/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/parser/XmlParserDstu2_1Test.java
index be084ff6cf4..2e810637a11 100644
--- a/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/parser/XmlParserDstu2_1Test.java
+++ b/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/parser/XmlParserDstu2_1Test.java
@@ -1996,41 +1996,27 @@ public class XmlParserDstu2_1Test {
assertThat(output, stringContainsInOrder(
"{",
- " \"resourceType\": \"Patient\",",
- " \"id\": \"someid\",",
- " \"_id\": {",
- " \"fhir_comments\": [",
- " \" comment 1 \"",
- " ]",
- " },",
- " \"extension\": [",
- " {",
- " \"fhir_comments\": [",
- " \" comment 2 \",",
- " \" comment 7 \"",
- " ],",
- " \"url\": \"urn:patientext:att\",",
- " \"valueAttachment\": {",
- " \"fhir_comments\": [",
- " \" comment 3 \",",
- " \" comment 6 \"",
- " ],",
- " \"contentType\": \"aaaa\",",
- " \"_contentType\": {",
- " \"fhir_comments\": [",
- " \" comment 4 \"",
- " ]",
- " },",
- " \"data\": \"AAAA\",",
- " \"_data\": {",
- " \"fhir_comments\": [",
- " \" comment 5 \"",
- " ]",
- " }",
- " }",
- " }",
- " ]",
- "}"
+ " \"resourceType\": \"Patient\",",
+ " \"id\": \"someid\",",
+ " \"_id\": {",
+ " \"fhir_comments\": [ \" comment 1 \" ]",
+ " },",
+ " \"extension\": [ {",
+ " \"fhir_comments\": [ \" comment 2 \", \" comment 7 \" ],",
+ " \"url\": \"urn:patientext:att\",",
+ " \"valueAttachment\": {",
+ " \"fhir_comments\": [ \" comment 3 \", \" comment 6 \" ],",
+ " \"contentType\": \"aaaa\",",
+ " \"_contentType\": {",
+ " \"fhir_comments\": [ \" comment 4 \" ]",
+ " },",
+ " \"data\": \"AAAA\",",
+ " \"_data\": {",
+ " \"fhir_comments\": [ \" comment 5 \" ]",
+ " }",
+ " }",
+ " } ]",
+ "}"
));
//@formatter:on
diff --git a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/JsonParserDstu2Test.java b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/JsonParserDstu2Test.java
index f57d0d49993..bef8cddd281 100644
--- a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/JsonParserDstu2Test.java
+++ b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/JsonParserDstu2Test.java
@@ -498,37 +498,28 @@ public class JsonParserDstu2Test {
String enc = ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p);
ourLog.info(enc);
- //@formatter:off
String actual = enc.trim();
ourLog.info("Actual:\n{}", actual);
- assertEquals("{\n" +
- " \"resourceType\": \"Patient\",\n" +
- " \"meta\": {\n" +
- " \"security\": [\n" +
- " {\n" +
- " \"system\": \"SYSTEM1\",\n" +
- " \"version\": \"VERSION1\",\n" +
- " \"code\": \"CODE1\",\n" +
- " \"display\": \"DISPLAY1\"\n" +
- " },\n" +
- " {\n" +
- " \"system\": \"SYSTEM2\",\n" +
- " \"version\": \"VERSION2\",\n" +
- " \"code\": \"CODE2\",\n" +
- " \"display\": \"DISPLAY2\"\n" +
- " }\n" +
- " ]\n" +
- " },\n" +
- " \"name\": [\n" +
- " {\n" +
- " \"family\": [\n" +
- " \"FAMILY\"\n" +
- " ]\n" +
- " }\n" +
- " ]\n" +
- "}", actual);
- //@formatter:on
+ assertThat(actual, stringContainsInOrder("{",
+ " \"resourceType\": \"Patient\",",
+ " \"meta\": {",
+ " \"security\": [ {",
+ " \"system\": \"SYSTEM1\",",
+ " \"version\": \"VERSION1\",",
+ " \"code\": \"CODE1\",",
+ " \"display\": \"DISPLAY1\"",
+ " }, {",
+ " \"system\": \"SYSTEM2\",",
+ " \"version\": \"VERSION2\",",
+ " \"code\": \"CODE2\",",
+ " \"display\": \"DISPLAY2\"",
+ " } ]",
+ " },",
+ " \"name\": [ {",
+ " \"family\": [ \"FAMILY\" ]",
+ " } ]",
+ "}"));
Patient parsed = ourCtx.newJsonParser().parseResource(Patient.class, enc);
List gotLabels = ResourceMetadataKeyEnum.SECURITY_LABELS.get(parsed);
diff --git a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/jsonlike/JsonLikeParserDstu2Test.java b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/jsonlike/JsonLikeParserDstu2Test.java
index 3f800d809ce..f577cc8502b 100644
--- a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/jsonlike/JsonLikeParserDstu2Test.java
+++ b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/jsonlike/JsonLikeParserDstu2Test.java
@@ -1,16 +1,15 @@
package ca.uhn.fhir.parser.jsonlike;
-import java.io.StringReader;
-
+import ca.uhn.fhir.context.FhirContext;
+import ca.uhn.fhir.parser.IJsonLikeParser;
+import ca.uhn.fhir.parser.json.JsonLikeStructure;
+import ca.uhn.fhir.parser.json.jackson.JacksonStructure;
+import ca.uhn.fhir.util.TestUtil;
import org.apache.commons.io.IOUtils;
import org.junit.AfterClass;
import org.junit.Test;
-import ca.uhn.fhir.context.FhirContext;
-import ca.uhn.fhir.parser.IJsonLikeParser;
-import ca.uhn.fhir.parser.json.GsonStructure;
-import ca.uhn.fhir.parser.json.JsonLikeStructure;
-import ca.uhn.fhir.util.TestUtil;
+import java.io.StringReader;
public class JsonLikeParserDstu2Test {
private static FhirContext ourCtx = FhirContext.forDstu2();
@@ -28,7 +27,7 @@ public class JsonLikeParserDstu2Test {
String encoded = ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(parsed);
ourLog.info(encoded);
- JsonLikeStructure jsonLikeStructure = new GsonStructure();
+ JsonLikeStructure jsonLikeStructure = new JacksonStructure();
jsonLikeStructure.load(new StringReader(encoded));
IJsonLikeParser jsonLikeparser = (IJsonLikeParser)ourCtx.newJsonParser();
diff --git a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/JsonParserDstu3Test.java b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/JsonParserDstu3Test.java
index 3b58f877a14..b87a3408961 100644
--- a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/JsonParserDstu3Test.java
+++ b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/JsonParserDstu3Test.java
@@ -72,7 +72,8 @@ public class JsonParserDstu3Test {
p.parseResource(input);
fail();
} catch (DataFormatException e) {
- assertEquals("Found incorrect type for element subject - Expected OBJECT and found SCALAR (STRING)", e.getMessage());
+ assertEquals("Failed to parse JSON encoded FHIR content: Unexpected character ('=' (code 61)): was expecting a colon to separate field name and value\n" +
+ " at [Source: UNKNOWN; line: 4, column: 18]", e.getMessage());
}
}
@@ -488,32 +489,25 @@ public class JsonParserDstu3Test {
String enc = ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p);
ourLog.info(enc);
- //@formatter:off
- assertEquals("{\n" +
- " \"resourceType\": \"Patient\",\n" +
- " \"meta\": {\n" +
- " \"security\": [\n" +
- " {\n" +
- " \"system\": \"SYSTEM1\",\n" +
- " \"version\": \"VERSION1\",\n" +
- " \"code\": \"CODE1\",\n" +
- " \"display\": \"DISPLAY1\"\n" +
- " },\n" +
- " {\n" +
- " \"system\": \"SYSTEM2\",\n" +
- " \"version\": \"VERSION2\",\n" +
- " \"code\": \"CODE2\",\n" +
- " \"display\": \"DISPLAY2\"\n" +
- " }\n" +
- " ]\n" +
- " },\n" +
- " \"name\": [\n" +
- " {\n" +
- " \"family\": \"FAMILY\"\n" +
- " }\n" +
- " ]\n" +
- "}", enc.trim());
- //@formatter:on
+ assertThat(enc.trim(), stringContainsInOrder("{",
+ " \"resourceType\": \"Patient\",",
+ " \"meta\": {",
+ " \"security\": [ {",
+ " \"system\": \"SYSTEM1\",",
+ " \"version\": \"VERSION1\",",
+ " \"code\": \"CODE1\",",
+ " \"display\": \"DISPLAY1\"",
+ " }, {",
+ " \"system\": \"SYSTEM2\",",
+ " \"version\": \"VERSION2\",",
+ " \"code\": \"CODE2\",",
+ " \"display\": \"DISPLAY2\"",
+ " } ]",
+ " },",
+ " \"name\": [ {",
+ " \"family\": \"FAMILY\"",
+ " } ]",
+ "}"));
Patient parsed = ourCtx.newJsonParser().parseResource(Patient.class, enc);
List gotLabels = parsed.getMeta().getSecurity();
@@ -1401,7 +1395,8 @@ public class JsonParserDstu3Test {
String input = "{\"resourceType\":\"Observation\",\"valueQuantity\":{\"value\":0.0000000000000001}}";
Observation obs = ourCtx.newJsonParser().parseResource(Observation.class, input);
- assertEquals("0.0000000000000001", ((Quantity) obs.getValue()).getValueElement().getValueAsString());
+ DecimalType valueElement = ((Quantity) obs.getValue()).getValueElement();
+ assertEquals("0.0000000000000001", valueElement.getValueAsString());
String str = ourCtx.newJsonParser().encodeResourceToString(obs);
ourLog.info(str);
@@ -1993,13 +1988,13 @@ public class JsonParserDstu3Test {
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());
+ assertEquals("Failed to parse JSON encoded FHIR content: 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("Failed to parse JSON encoded FHIR content: 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());
@@ -2221,25 +2216,47 @@ public class JsonParserDstu3Test {
@Test
public void testParseWithPrecision() {
- String input = "{\"resourceType\":\"Observation\",\"valueQuantity\":{\"value\":0.000000000000000100}}";
- Observation obs = ourCtx.newJsonParser().parseResource(Observation.class, input);
- DecimalType valueElement = ((Quantity) obs.getValue()).getValueElement();
- assertEquals("0.000000000000000100", valueElement.getValueAsString());
+// BigDecimal d0 = new BigDecimal("0.1");
+// BigDecimal d1 = new BigDecimal("0.1000");
+//
+// ourLog.info("Value: {}", d0);
+// ourLog.info("Value: {}", d1);
- String str = ourCtx.newJsonParser().encodeResourceToString(obs);
- ourLog.info(str);
- assertEquals("{\"resourceType\":\"Observation\",\"valueQuantity\":{\"value\":0.000000000000000100}}", str);
+ {
+ String input = "{\"resourceType\":\"Observation\",\"valueQuantity\":{\"value\":0.0100}}";
+ Observation obs = ourCtx.newJsonParser().parseResource(Observation.class, input);
+ DecimalType valueElement = ((Quantity) obs.getValue()).getValueElement();
+ assertEquals("0.0100", valueElement.getValueAsString());
+ String str = ourCtx.newJsonParser().encodeResourceToString(obs);
+ ourLog.info(str);
+ assertEquals("{\"resourceType\":\"Observation\",\"valueQuantity\":{\"value\":0.0100}}", str);
+ }
+ {
+ String input = "{\"resourceType\":\"Observation\",\"valueQuantity\":{\"value\":0.000000000000000100}}";
+ Observation obs = ourCtx.newJsonParser().parseResource(Observation.class, input);
+ DecimalType valueElement = ((Quantity) obs.getValue()).getValueElement();
+ assertEquals("0.000000000000000100", valueElement.getValueAsString());
+ String str = ourCtx.newJsonParser().encodeResourceToString(obs);
+ ourLog.info(str);
+ assertEquals("{\"resourceType\":\"Observation\",\"valueQuantity\":{\"value\":0.000000000000000100}}", str);
+ }
}
- @Test(expected = DataFormatException.class)
+ @Test
public void testParseWithTrailingContent() {
String bundle = "{\n" +
- " \"resourceType\" : \"Bundle\",\n" +
- " \"total\" : 1\n" +
+ " \"resourceType\": \"Bundle\",\n" +
+ " \"total\": 1\n" +
"}}";
- ourCtx.newJsonParser().parseResource(Bundle.class, bundle);
+ try {
+ ourCtx.newJsonParser().parseResource(Bundle.class, bundle);
+ fail();
+ } catch (DataFormatException e) {
+ assertEquals("Failed to parse JSON encoded FHIR content: Unexpected close marker '}': expected ']' (for root starting at [Source: UNKNOWN; line: 1, column: 0])\n" +
+ " at [Source: UNKNOWN; line: 4, column: 3]", e.getMessage());
+ }
}
@Test
diff --git a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/XmlParserDstu3Test.java b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/XmlParserDstu3Test.java
index 8023417d1de..98fdae848b5 100644
--- a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/XmlParserDstu3Test.java
+++ b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/XmlParserDstu3Test.java
@@ -2508,42 +2508,27 @@ public class XmlParserDstu3Test {
output = ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(pat);
ourLog.info(output);
- assertThat(output, stringContainsInOrder(
- "{",
+ assertThat(output, stringContainsInOrder("{",
" \"resourceType\": \"Patient\",",
" \"id\": \"someid\",",
" \"_id\": {",
- " \"fhir_comments\": [",
- " \" comment 1 \"",
- " ]",
+ " \"fhir_comments\": [ \" comment 1 \" ]",
" },",
- " \"extension\": [",
- " {",
- " \"fhir_comments\": [",
- " \" comment 2 \",",
- " \" comment 7 \"",
- " ],",
- " \"url\": \"urn:patientext:att\",",
- " \"valueAttachment\": {",
- " \"fhir_comments\": [",
- " \" comment 3 \",",
- " \" comment 6 \"",
- " ],",
- " \"contentType\": \"aaaa\",",
- " \"_contentType\": {",
- " \"fhir_comments\": [",
- " \" comment 4 \"",
- " ]",
- " },",
- " \"data\": \"AAAA\",",
- " \"_data\": {",
- " \"fhir_comments\": [",
- " \" comment 5 \"",
- " ]",
- " }",
+ " \"extension\": [ {",
+ " \"fhir_comments\": [ \" comment 2 \", \" comment 7 \" ],",
+ " \"url\": \"urn:patientext:att\",",
+ " \"valueAttachment\": {",
+ " \"fhir_comments\": [ \" comment 3 \", \" comment 6 \" ],",
+ " \"contentType\": \"aaaa\",",
+ " \"_contentType\": {",
+ " \"fhir_comments\": [ \" comment 4 \" ]",
+ " },",
+ " \"data\": \"AAAA\",",
+ " \"_data\": {",
+ " \"fhir_comments\": [ \" comment 5 \" ]",
" }",
" }",
- " ]",
+ " } ]",
"}"));
}
diff --git a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/jsonlike/JsonLikeParserDstu3Test.java b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/jsonlike/JsonLikeParserDstu3Test.java
index 082a737d845..d63d4dcb8d9 100644
--- a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/jsonlike/JsonLikeParserDstu3Test.java
+++ b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/jsonlike/JsonLikeParserDstu3Test.java
@@ -1,426 +1,404 @@
-package ca.uhn.fhir.parser.jsonlike;
-
-import java.io.IOException;
-import java.io.StringReader;
-import java.math.BigDecimal;
-import java.math.BigInteger;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Stack;
-
-import org.apache.commons.io.IOUtils;
-import org.hl7.fhir.dstu3.model.Bundle;
-import org.hl7.fhir.dstu3.model.Extension;
-import org.hl7.fhir.dstu3.model.Patient;
-import org.hl7.fhir.dstu3.model.Reference;
-import org.junit.AfterClass;
-import org.junit.Assert;
-import org.junit.Test;
-
-import ca.uhn.fhir.context.FhirContext;
-import ca.uhn.fhir.parser.IJsonLikeParser;
-import ca.uhn.fhir.parser.json.GsonStructure;
-import ca.uhn.fhir.parser.json.JsonLikeStructure;
-import ca.uhn.fhir.parser.json.JsonLikeWriter;
-import ca.uhn.fhir.util.TestUtil;
-
-public class JsonLikeParserDstu3Test {
- private static FhirContext ourCtx = FhirContext.forDstu3();
- private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(JsonLikeParserDstu3Test.class);
-
- /**
- * Test for JSON Parser with user-supplied JSON-like structure (use default GSON)
- */
- @Test
- public void testJsonLikeParseAndEncodeBundleFromXmlToJson() throws Exception {
- String content = IOUtils.toString(JsonLikeParserDstu3Test.class.getResourceAsStream("/bundle_with_woven_obs.xml"));
-
- Bundle parsed = ourCtx.newXmlParser().parseResource(Bundle.class, content);
-
- String encoded = ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(parsed);
- ourLog.info(encoded);
-
- JsonLikeStructure jsonLikeStructure = new GsonStructure();
- jsonLikeStructure.load(new StringReader(encoded));
-
- IJsonLikeParser jsonLikeparser = (IJsonLikeParser)ourCtx.newJsonParser();
-
- Bundle bundle = jsonLikeparser.parseResource(Bundle.class, jsonLikeStructure);
-
- }
-
- /**
- * Test JSON-Like writer using custom stream writer
- *
- */
- @Test
- public void testJsonLikeParseWithCustomJSONStreamWriter() throws Exception {
- String refVal = "http://my.org/FooBar";
-
- Patient fhirPat = new Patient();
- fhirPat.addExtension().setUrl("x1").setValue(new Reference(refVal));
-
- IJsonLikeParser jsonLikeParser = (IJsonLikeParser)ourCtx.newJsonParser();
- JsonLikeMapWriter jsonLikeWriter = new JsonLikeMapWriter();
-
- jsonLikeParser.encodeResourceToJsonLikeWriter(fhirPat, jsonLikeWriter);
- Map jsonLikeMap = jsonLikeWriter.getResultMap();
-
- System.out.println("encoded map: " + jsonLikeMap.toString());
-
- Assert.assertNotNull("Encoded resource missing 'resourceType' element", jsonLikeMap.get("resourceType"));
- Assert.assertEquals("Expecting 'resourceType'='Patient'; found '"+jsonLikeMap.get("resourceType")+"'", jsonLikeMap.get("resourceType"), "Patient");
-
- Assert.assertNotNull("Encoded resource missing 'extension' element", jsonLikeMap.get("extension"));
- Assert.assertTrue("'extension' element is not a List", (jsonLikeMap.get("extension") instanceof List));
-
- List