use of org.hl7.fhir.r4b.formats.JsonParser in project org.hl7.fhir.core by hapifhir.
the class Unbundler method unbundle.
private static void unbundle(String src) throws FHIRFormatError, FileNotFoundException, IOException {
String folder = Utilities.getDirectoryForFile(src);
Bundle bnd = (Bundle) new JsonParser().parse(new FileInputStream(src));
for (BundleEntryComponent be : bnd.getEntry()) {
Resource r = be.getResource();
if (r != null) {
String tgt = Utilities.path(folder, r.fhirType() + "-" + r.getId() + ".json");
new JsonParser().compose(new FileOutputStream(tgt), r);
}
}
}
use of org.hl7.fhir.r4b.formats.JsonParser in project org.hl7.fhir.core by hapifhir.
the class R3TEchnicalCorrectionProcessor method extractToPackageMaster.
private void extractToPackageMaster(List<Resource> list, String root) throws IOException, FHIRFormatError {
System.out.println("Updating Packages Master");
String corePath = Utilities.path(root, "hl7.fhir.r3.core", "package");
String examplesPath = Utilities.path(root, "hl7.fhir.r3.examples", "package");
String elementsPath = Utilities.path(root, "hl7.fhir.r3.elements", "package");
int coreTotal = new File(corePath).list().length - 1;
int examplesTotal = new File(examplesPath).list().length - 1;
int elementsTotal = new File(elementsPath).list().length - 1;
int coreCount = 0;
int examplesCount = 0;
int elementsCount = 0;
for (Resource r : list) {
String n = r.fhirType() + "-" + r.getId() + ".json";
FileOutputStream dst = null;
if (n.startsWith("DataElement-")) {
elementsCount++;
dst = new FileOutputStream(Utilities.path(elementsPath, n));
new JsonParser().setOutputStyle(OutputStyle.NORMAL).compose(dst, r);
} else {
dst = new FileOutputStream(Utilities.path(examplesPath, n));
new JsonParser().setOutputStyle(OutputStyle.NORMAL).compose(dst, r);
examplesCount++;
if (isCoreResource(r.fhirType())) {
coreCount++;
DomainResource dr = (DomainResource) r;
dr.setText(null);
new JsonParser().setOutputStyle(OutputStyle.NORMAL).compose(new FileOutputStream(Utilities.path(corePath, n)), r);
}
}
}
System.out.println(" Core @ " + corePath + ": Replaced " + coreCount + " of " + coreTotal);
System.out.println(" Examples @ " + examplesPath + ": Replaced " + examplesCount + " of " + examplesTotal);
System.out.println(" Elements @ " + elementsPath + ": Replaced " + elementsCount + " of " + elementsTotal);
}
use of org.hl7.fhir.r4b.formats.JsonParser in project org.hl7.fhir.core by hapifhir.
the class R3TEchnicalCorrectionProcessor method produceDefinitionsJson.
private void produceDefinitionsJson(Map<String, Resource> definitions, String dest) throws IOException {
for (String n : definitions.keySet()) {
File f = new File(Utilities.path(dest, "definitions.json", Utilities.changeFileExt(n, ".json")));
new JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(f), definitions.get(n));
}
}
use of org.hl7.fhir.r4b.formats.JsonParser in project org.hl7.fhir.core by hapifhir.
the class R3TEchnicalCorrectionProcessor method execute.
private void execute(String src, String packageRoot) throws FileNotFoundException, IOException {
System.out.println("Loading resources from " + src);
List<Resource> resources = new ArrayList<>();
Map<String, Resource> definitions = new HashMap<>();
for (File f : new File(src).listFiles()) {
if (f.getName().endsWith(".xml") && !(f.getName().endsWith("warnings.xml") || f.getName().endsWith(".diff.xml"))) {
try {
Resource r = new XmlParser().parse(new FileInputStream(f));
if (f.getName().contains("canonical")) {
resources.add(r);
}
if (Utilities.existsInList(f.getName(), "conceptmaps.xml", "dataelements.xml", "extension-definitions.xml", "profiles-others.xml", "profiles-resources.xml", "profiles-types.xml", "search-parameters.xml", "v2-tables.xml", "v3-codesystems.xml", "valuesets.xml")) {
definitions.put(f.getName(), r);
}
r.setUserData("path", f.getName().substring(0, f.getName().indexOf(".")));
// FileUtils.copyFile(f, new File(f.getAbsolutePath()+"1"));
// FileUtils.copyFile(f, new File(f.getAbsolutePath()+"2"));
} catch (Exception e) {
System.out.println("Unable to load " + f.getName() + ": " + e.getMessage());
}
}
if (f.getName().endsWith(".json") && !(f.getName().endsWith("schema.json") || f.getName().endsWith(".diff.json"))) {
try {
// new JsonParser().parse(new FileInputStream(f));
// FileUtils.copyFile(f, new File(f.getAbsolutePath()+"1"));
// FileUtils.copyFile(f, new File(f.getAbsolutePath()+"2"));
} catch (Exception e) {
System.out.println("Unable to load " + f.getName() + ": " + e.getMessage());
}
}
}
System.out.println(Integer.toString(resources.size()) + " resources");
System.out.println(Integer.toString(definitions.size()) + " resources");
produceExamplesXml(resources, src);
produceDefinitionsXml(definitions, src);
produceExamplesJson(resources, src);
produceDefinitionsJson(definitions, src);
for (Resource r : definitions.values()) {
if (r instanceof Bundle) {
Bundle bnd = (Bundle) r;
for (BundleEntryComponent be : bnd.getEntry()) {
resources.add(be.getResource());
}
}
}
extractToPackageMaster(resources, packageRoot);
System.out.println("Done");
}
use of org.hl7.fhir.r4b.formats.JsonParser in project org.hl7.fhir.core by hapifhir.
the class JsonDirectTests method test.
@Test
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);
}
Aggregations