use of org.hl7.fhir.r5.elementmodel.JsonParser in project org.hl7.fhir.core by hapifhir.
the class ByteUtils method resourceToByteArray.
public static <T extends Resource> byte[] resourceToByteArray(T resource, boolean pretty, boolean isJson) {
ByteArrayOutputStream baos = null;
byte[] byteArray = null;
try {
baos = new ByteArrayOutputStream();
IParser parser = null;
if (isJson) {
parser = new JsonParser();
} else {
parser = new XmlParser();
}
parser.setOutputStyle(pretty ? IParser.OutputStyle.PRETTY : IParser.OutputStyle.NORMAL);
parser.compose(baos, resource);
baos.close();
byteArray = baos.toByteArray();
baos.close();
} catch (Exception e) {
try {
baos.close();
} catch (Exception ex) {
throw new EFhirClientException("Error closing output stream", ex);
}
throw new EFhirClientException("Error converting output stream to byte array", e);
}
return byteArray;
}
use of org.hl7.fhir.r5.elementmodel.JsonParser in project org.hl7.fhir.core by hapifhir.
the class IntegrityChecker method dumpSD.
private void dumpSD(FileWriter w) throws FHIRFormatError, IOException {
Map<String, StructureDefinition> map = new HashMap<>();
for (String sdn : npm.listResources("StructureDefinition")) {
InputStream s = npm.load(sdn);
StructureDefinition sd = (StructureDefinition) new JsonParser().parse(s);
map.put(sd.getUrl(), sd);
}
msg("Loaded " + map.size() + " Structures");
List<String> structures = new ArrayList<>();
for (StructureDefinition sd : map.values()) {
structures.add(sd.getUrl());
}
Collections.sort(structures);
for (String sdn : structures) {
dumpSD(map.get(sdn), map, w);
}
}
use of org.hl7.fhir.r5.elementmodel.JsonParser in project org.hl7.fhir.core by hapifhir.
the class IntegrityChecker method checkExamplesJson.
private void checkExamplesJson(String dst) throws FileNotFoundException, IOException {
Map<String, byte[]> files = loadZip(new FileInputStream(Utilities.path(dst, "examples-json.zip")));
for (Entry<String, byte[]> t : files.entrySet()) {
try {
new JsonParser().parse(t.getValue());
System.out.print(".");
} catch (Exception e) {
System.out.println("");
System.out.println("Error parsing " + t.getKey() + ": " + e.getMessage());
}
}
}
use of org.hl7.fhir.r5.elementmodel.JsonParser in project org.hl7.fhir.core by hapifhir.
the class IntegrityChecker method checkSP.
private void checkSP() throws IOException {
List<SearchParameter> list = new ArrayList<>();
for (String sdn : npm.listResources("SearchParameter")) {
InputStream s = npm.load(sdn);
SearchParameter sp = (SearchParameter) new JsonParser().parse(s);
list.add(sp);
}
msg("Loaded " + list.size() + " resources");
Map<String, SearchParameterNode> map = new HashMap<>();
for (SearchParameter sp : list) {
for (CodeType c : sp.getBase()) {
String s = c.primitiveValue();
if (!map.containsKey(s)) {
map.put(s, new SearchParameterNode(s));
}
addNode(sp, sp.getBase().size() == 1, map.get(s));
}
}
for (SearchParameterNode node : sort(map.values())) {
dump(node);
}
}
use of org.hl7.fhir.r5.elementmodel.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);
}
}
}
Aggregations