Search in sources :

Example 71 with JsonParser

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;
}
Also used : XmlParser(org.hl7.fhir.r4b.formats.XmlParser) ByteArrayOutputStream(java.io.ByteArrayOutputStream) EFhirClientException(org.hl7.fhir.r4b.utils.client.EFhirClientException) IOException(java.io.IOException) EFhirClientException(org.hl7.fhir.r4b.utils.client.EFhirClientException) IParser(org.hl7.fhir.r4b.formats.IParser) JsonParser(org.hl7.fhir.r4b.formats.JsonParser)

Example 72 with JsonParser

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);
    }
}
Also used : StructureDefinition(org.hl7.fhir.r4b.model.StructureDefinition) HashMap(java.util.HashMap) ZipInputStream(java.util.zip.ZipInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) JsonParser(org.hl7.fhir.r4b.formats.JsonParser)

Example 73 with JsonParser

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());
        }
    }
}
Also used : FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) JsonParser(org.hl7.fhir.r4b.formats.JsonParser)

Example 74 with JsonParser

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);
    }
}
Also used : SearchParameterNode(org.hl7.fhir.r4b.utils.IntegrityChecker.SearchParameterNode) HashMap(java.util.HashMap) ZipInputStream(java.util.zip.ZipInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) CodeType(org.hl7.fhir.r4b.model.CodeType) SearchParameter(org.hl7.fhir.r4b.model.SearchParameter) JsonParser(org.hl7.fhir.r4b.formats.JsonParser)

Example 75 with JsonParser

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);
        }
    }
}
Also used : BundleEntryComponent(org.hl7.fhir.r4b.model.Bundle.BundleEntryComponent) Bundle(org.hl7.fhir.r4b.model.Bundle) FileOutputStream(java.io.FileOutputStream) Resource(org.hl7.fhir.r4b.model.Resource) FileInputStream(java.io.FileInputStream) JsonParser(org.hl7.fhir.r4b.formats.JsonParser)

Aggregations

FileOutputStream (java.io.FileOutputStream)82 JsonParser (org.hl7.fhir.r5.formats.JsonParser)66 FHIRException (org.hl7.fhir.exceptions.FHIRException)58 FileInputStream (java.io.FileInputStream)53 IOException (java.io.IOException)49 XmlParser (org.hl7.fhir.r5.formats.XmlParser)44 ByteArrayOutputStream (java.io.ByteArrayOutputStream)41 File (java.io.File)36 JsonParser (org.hl7.fhir.r4.formats.JsonParser)35 CSFileInputStream (org.hl7.fhir.utilities.CSFileInputStream)29 JsonParser (org.hl7.fhir.dstu3.formats.JsonParser)26 JsonParser (org.hl7.fhir.r4b.formats.JsonParser)26 CSFile (org.hl7.fhir.utilities.CSFile)26 ArrayList (java.util.ArrayList)23 TextFile (org.hl7.fhir.utilities.TextFile)20 InputStream (java.io.InputStream)18 IParser (org.hl7.fhir.r5.formats.IParser)18 Resource (org.hl7.fhir.dstu3.model.Resource)17 FileNotFoundException (java.io.FileNotFoundException)16 Resource (org.hl7.fhir.r4.model.Resource)16