use of org.hl7.fhir.r5.elementmodel.JsonParser in project org.hl7.fhir.core by hapifhir.
the class GenValueSetExpansionConvertor method main.
public static void main(String[] args) throws FHIRFormatError, IOException {
String src = args[0];
String tgt = args[1];
Bundle bundle = (Bundle) new XmlParser().parse(new FileInputStream(src));
for (BundleEntryComponent be : bundle.getEntry()) {
Resource res = be.getResource();
if (res != null) {
String id = res.getId();
if (Utilities.noString(id))
id = tail(((ValueSet) res).getUrl());
String dst = Utilities.path(tgt, res.fhirType() + "-" + id + ".json");
System.out.println(dst);
new JsonParser().setOutputStyle(OutputStyle.NORMAL).compose(new FileOutputStream(dst), res);
}
}
}
use of org.hl7.fhir.r5.elementmodel.JsonParser in project org.hl7.fhir.core by hapifhir.
the class R2ToR3Loader method loadBundle.
@Override
public Bundle loadBundle(InputStream stream, boolean isJson) throws FHIRException, IOException {
Resource r2;
if (isJson)
r2 = new JsonParser().parse(stream);
else
r2 = new XmlParser().parse(stream);
org.hl7.fhir.dstu3.model.Resource r3 = VersionConvertorFactory_10_30.convertResource(r2, advisor_10_30);
Bundle b;
if (r3 instanceof Bundle) {
b = (Bundle) r3;
} else {
b = new Bundle();
b.setId(UUID.randomUUID().toString().toLowerCase());
b.setType(BundleType.COLLECTION);
b.addEntry().setResource(r3).setFullUrl(r3 instanceof MetadataResource ? ((MetadataResource) r3).getUrl() : null);
}
advisor_10_30.getCslist().forEach(cs -> {
BundleEntryComponent be = b.addEntry();
be.setFullUrl(cs.getUrl());
be.setResource(cs);
});
advisor_10_30.getCslist().clear();
if (killPrimitives) {
List<BundleEntryComponent> remove = new ArrayList<BundleEntryComponent>();
for (BundleEntryComponent be : b.getEntry()) {
if (be.hasResource() && be.getResource() instanceof StructureDefinition) {
StructureDefinition sd = (StructureDefinition) be.getResource();
if (sd.getKind() == StructureDefinitionKind.PRIMITIVETYPE)
remove.add(be);
}
}
b.getEntry().removeAll(remove);
}
if (patchUrls) {
b.getEntry().stream().filter(be -> be.hasResource() && be.getResource() instanceof StructureDefinition).map(be -> (StructureDefinition) be.getResource()).forEach(sd -> {
sd.setUrl(sd.getUrl().replace(URL_BASE, URL_DSTU2));
sd.addExtension().setUrl(URL_ELEMENT_DEF_NAMESPACE).setValue(new UriType(URL_BASE));
});
}
return b;
}
use of org.hl7.fhir.r5.elementmodel.JsonParser in project org.hl7.fhir.core by hapifhir.
the class MessageTest method test.
@Test
public void test() throws FHIRException, IOException {
// Create new Atom Feed
Bundle feed = new Bundle();
// Serialize Atom Feed
IParser comp = new JsonParser();
ByteArrayOutputStream os = new ByteArrayOutputStream();
comp.compose(os, feed);
os.close();
String json = os.toString();
// Deserialize Atom Feed
JsonParser parser = new JsonParser();
InputStream is = new ByteArrayInputStream(json.getBytes("UTF-8"));
Resource result = parser.parse(is);
if (result == null)
throw new FHIRException("Bundle was null");
}
use of org.hl7.fhir.r5.elementmodel.JsonParser in project org.hl7.fhir.core by hapifhir.
the class ResourceTest method test.
public void 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();
}
use of org.hl7.fhir.r5.elementmodel.JsonParser in project org.hl7.fhir.core by hapifhir.
the class ToolsHelper method testRoundTrip.
public void testRoundTrip(String rootDir, String tmpDir, Collection<String> names) throws Throwable {
try {
System.err.println("Round trip from " + rootDir + " to " + tmpDir + ":" + Integer.toString(names.size()) + " files");
for (String n : names) {
System.err.print(" " + n);
String source = rootDir + n + ".xml";
// String tmpJson = tmpDir + n + ".json";
String tmp = tmpDir + n.replace(File.separator, "-") + ".tmp";
String dest = tmpDir + n.replace(File.separator, "-") + ".java.xml";
FileInputStream in = new FileInputStream(source);
XmlParser xp = new XmlParser();
Resource r = xp.parse(in);
System.err.print(".");
JsonParser jp = new JsonParser();
FileOutputStream out = new FileOutputStream(tmp);
jp.setOutputStyle(OutputStyle.PRETTY);
jp.compose(out, r);
out.close();
r = null;
System.err.print(".");
in = new FileInputStream(tmp);
System.err.print(",");
r = jp.parse(in);
System.err.print(".");
out = new FileOutputStream(dest);
new XmlParser().compose(out, r, true);
System.err.println("!");
out.close();
r = null;
System.gc();
}
} catch (Throwable e) {
System.err.println("Error: " + e.getMessage());
throw e;
}
}
Aggregations