use of org.hl7.fhir.r4b.formats.JsonParser in project org.hl7.fhir.core by hapifhir.
the class VocabTests method test.
@ParameterizedTest(name = "{index}: file {0}")
@MethodSource("data")
public void test(String id, TestDetails test) throws Exception {
Resource source;
if (test.getSource().endsWith(".json")) {
source = (Resource) new JsonParser().parse(TestingUtilities.loadTestResourceStream("r5", "vocab", test.getSource()));
} else {
source = (Resource) new XmlParser().parse(TestingUtilities.loadTestResourceStream("r5", "vocab", test.getSource()));
}
Resource target;
if (test.getTarget().endsWith(".json")) {
target = (Resource) new JsonParser().parse(TestingUtilities.loadTestResourceStream("r5", "vocab", test.getTarget()));
} else {
target = (Resource) new XmlParser().parse(TestingUtilities.loadTestResourceStream("r5", "vocab", test.getTarget()));
}
if (test.getType() == TestType.Expansion) {
testExpansion(test, (ValueSet) source, (ValueSet) target);
} else {
Assertions.fail("not done yet");
}
}
use of org.hl7.fhir.r4b.formats.JsonParser in project org.hl7.fhir.core by hapifhir.
the class ResourceTest method test.
public Resource test() throws FHIRFormatError, FileNotFoundException, IOException {
IParser p;
if (isJson())
p = new JsonParser();
else
p = new XmlParser(false);
Resource rf = p.parse(new FileInputStream(source));
FileOutputStream out = new FileOutputStream(source.getAbsoluteFile() + ".out.json");
JsonParser json1 = new JsonParser();
json1.setOutputStyle(OutputStyle.PRETTY);
json1.compose(out, rf);
out.close();
JsonParser json = new JsonParser();
rf = json.parse(new FileInputStream(source.getAbsoluteFile() + ".out.json"));
out = new FileOutputStream(source.getAbsoluteFile() + ".out.xml");
XmlParser atom = new XmlParser();
atom.setOutputStyle(OutputStyle.PRETTY);
atom.compose(out, rf, true);
out.close();
return rf;
}
use of org.hl7.fhir.r4b.formats.JsonParser in project org.hl7.fhir.core by hapifhir.
the class JsonDirectTests method test.
@Test
// Hard coded path here
@Disabled
public void test() throws FHIRFormatError, FileNotFoundException, IOException {
File src = new File(Utilities.path("[tmp]", "obs.xml"));
File xml = new File(Utilities.path("[tmp]", "xml.xml"));
File json = new File(Utilities.path("[tmp]", "json.json"));
File json2 = new File(Utilities.path("[tmp]", "json2.json"));
FileUtils.copyFile(new File("C:\\work\\org.hl7.fhir\\build\\publish\\observation-decimal.xml"), src);
Observation obs = (Observation) new XmlParser().parse(new FileInputStream(src));
new JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(json), obs);
obs = (Observation) new JsonParser().parse(new FileInputStream(json));
new JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(json2), obs);
new XmlParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(xml), obs);
}
use of org.hl7.fhir.r4b.formats.JsonParser in project org.hl7.fhir.core by hapifhir.
the class NarrativeGenerationTests method test.
@ParameterizedTest(name = "{index}: file {0}")
@MethodSource("data")
public void test(String id, TestDetails test) throws Exception {
RenderingContext rc = new RenderingContext(context, null, null, "http://hl7.org/fhir", "", null, ResourceRendererMode.END_USER);
rc.setDestDir("");
rc.setHeader(test.isHeader());
rc.setDefinitionsTarget("test.html");
rc.setTerminologyServiceOptions(TerminologyServiceOptions.defaults());
rc.setParser(new TestTypeParser());
// getting timezones correct (well, at least consistent, so tests pass on any computer)
rc.setLocale(new java.util.Locale("en", "AU"));
rc.setTimeZoneId(ZoneId.of("Australia/Sydney"));
rc.setDateTimeFormatString("yyyy-MM-dd'T'HH:mm:ssZZZZZ");
rc.setDateFormatString("yyyy-MM-dd");
rc.setMode(test.technical ? ResourceRendererMode.TECHNICAL : ResourceRendererMode.END_USER);
Resource source;
if (TestingUtilities.findTestResource("r5", "narrative", test.getId() + ".json")) {
source = (Resource) new JsonParser().parse(TestingUtilities.loadTestResourceStream("r5", "narrative", test.getId() + ".json"));
} else {
source = (Resource) new XmlParser().parse(TestingUtilities.loadTestResourceStream("r5", "narrative", test.getId() + ".xml"));
}
XhtmlNode x = RendererFactory.factory(source, rc).build(source);
String expected = TextFile.streamToString(TestingUtilities.loadTestResourceStream("r5", "narrative", test.getId() + ".html"));
String actual = HEADER + new XhtmlComposer(true, true).compose(x) + FOOTER;
String expectedFileName = CompareUtilities.tempFile("narrative", test.getId() + ".expected.html");
String actualFileName = CompareUtilities.tempFile("narrative", test.getId() + ".actual.html");
TextFile.stringToFile(expected, expectedFileName);
TextFile.stringToFile(actual, actualFileName);
String msg = CompareUtilities.checkXMLIsSame(expectedFileName, actualFileName);
Assertions.assertTrue(msg == null, "Output does not match expected: " + msg);
if (test.isMeta()) {
org.hl7.fhir.r5.elementmodel.Element e = Manager.parseSingle(context, TestingUtilities.loadTestResourceStream("r5", "narrative", test.getId() + ".xml"), FhirFormat.XML);
x = RendererFactory.factory(source, rc).render(new ElementWrappers.ResourceWrapperMetaElement(rc, e));
expected = TextFile.streamToString(TestingUtilities.loadTestResourceStream("r5", "narrative", test.getId() + "-meta.html"));
actual = HEADER + new XhtmlComposer(true, true).compose(x) + FOOTER;
actualFileName = CompareUtilities.tempFile("narrative", test.getId() + "-meta.actual.html");
TextFile.stringToFile(actual, actualFileName);
msg = CompareUtilities.checkXMLIsSame(expectedFileName, actualFileName);
Assertions.assertTrue(msg == null, "Meta output does not match expected: " + msg);
}
}
use of org.hl7.fhir.r4b.formats.JsonParser in project org.hl7.fhir.core by hapifhir.
the class BaseValidator method loadFoundResource.
protected Resource loadFoundResource(List<ValidationMessage> errors, String path, Element resource, Class<? extends Resource> class1) throws FHIRException {
try {
FhirPublication v = FhirPublication.fromCode(context.getVersion());
ByteArrayOutputStream bs = new ByteArrayOutputStream();
new JsonParser(context).compose(resource, bs, OutputStyle.NORMAL, resource.getIdBase());
byte[] json = bs.toByteArray();
Resource r5 = null;
switch(v) {
case DSTU1:
rule(errors, IssueType.INVALID, resource.line(), resource.col(), path, false, I18nConstants.UNSUPPORTED_VERSION_R1, resource.getIdBase());
// this can't happen
return null;
case DSTU2:
org.hl7.fhir.dstu2.model.Resource r2 = new org.hl7.fhir.dstu2.formats.JsonParser().parse(json);
r5 = VersionConvertorFactory_10_50.convertResource(r2);
break;
case DSTU2016May:
org.hl7.fhir.dstu2016may.model.Resource r2a = new org.hl7.fhir.dstu2016may.formats.JsonParser().parse(json);
r5 = VersionConvertorFactory_14_50.convertResource(r2a);
break;
case STU3:
org.hl7.fhir.dstu3.model.Resource r3 = new org.hl7.fhir.dstu3.formats.JsonParser().parse(json);
r5 = VersionConvertorFactory_30_50.convertResource(r3);
break;
case R4:
org.hl7.fhir.r4.model.Resource r4 = new org.hl7.fhir.r4.formats.JsonParser().parse(json);
r5 = VersionConvertorFactory_40_50.convertResource(r4);
break;
case R5:
r5 = new org.hl7.fhir.r5.formats.JsonParser().parse(json);
break;
default:
// this can't happen
return null;
}
if (class1.isInstance(r5))
return (Resource) r5;
else {
rule(errors, IssueType.INVALID, resource.line(), resource.col(), path, false, I18nConstants.REFERENCE_REF_WRONGTARGET_LOAD, resource.getIdBase(), class1.toString(), r5.fhirType());
return null;
}
} catch (IOException e) {
throw new FHIRException(e);
}
}
Aggregations