Search in sources :

Example 51 with JsonParser

use of org.hl7.fhir.r5.elementmodel.JsonParser in project org.hl7.fhir.core by hapifhir.

the class ValueSet10_40Test method testValueSetConversion.

@Test
@DisplayName("Test 10_40 ValueSet conversion")
public void testValueSetConversion() throws IOException {
    InputStream dstu2_input = this.getClass().getResourceAsStream("/0_valueset_vision_base_codes_10.json");
    InputStream r4_exepected_input = this.getClass().getResourceAsStream("/0_valueset_vision_base_codes_40.json");
    org.hl7.fhir.dstu2.model.ValueSet dstu2 = (org.hl7.fhir.dstu2.model.ValueSet) new org.hl7.fhir.dstu2.formats.JsonParser().parse(dstu2_input);
    BaseAdvisor_10_40 advisor = new IGR2ConvertorAdvisor();
    org.hl7.fhir.r4.model.Resource r4_actual = VersionConvertorFactory_10_40.convertResource(dstu2, advisor);
    org.hl7.fhir.r4.formats.JsonParser r4_parser = new org.hl7.fhir.r4.formats.JsonParser();
    JsonParser parser = new JsonParser();
    String composeString = parser.composeString(r4_actual);
    org.hl7.fhir.r4.model.Resource r4_expected = r4_parser.parse(r4_exepected_input);
    Assertions.assertTrue(r4_expected.equalsDeep(r4_actual), "Failed comparing\n" + r4_parser.composeString(r4_actual) + "\nand\n" + r4_parser.composeString(r4_expected));
}
Also used : InputStream(java.io.InputStream) IGR2ConvertorAdvisor(org.hl7.fhir.convertors.misc.IGR2ConvertorAdvisor) BaseAdvisor_10_40(org.hl7.fhir.convertors.advisors.impl.BaseAdvisor_10_40) JsonParser(org.hl7.fhir.r4.formats.JsonParser) JsonParser(org.hl7.fhir.r4.formats.JsonParser) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 52 with JsonParser

use of org.hl7.fhir.r5.elementmodel.JsonParser in project org.hl7.fhir.core by hapifhir.

the class ClientUtils method encodeFormSubmission.

private byte[] encodeFormSubmission(Map<String, String> parameters, String resourceName, Resource resource, String boundary) throws IOException {
    ByteArrayOutputStream b = new ByteArrayOutputStream();
    OutputStreamWriter w = new OutputStreamWriter(b, "UTF-8");
    for (String name : parameters.keySet()) {
        w.write("--");
        w.write(boundary);
        w.write("\r\nContent-Disposition: form-data; name=\"" + name + "\"\r\n\r\n");
        w.write(parameters.get(name) + "\r\n");
    }
    w.write("--");
    w.write(boundary);
    w.write("\r\nContent-Disposition: form-data; name=\"" + resourceName + "\"\r\n\r\n");
    w.close();
    JsonParser json = new JsonParser();
    json.setOutputStyle(OutputStyle.NORMAL);
    json.compose(b, resource);
    b.close();
    w = new OutputStreamWriter(b, "UTF-8");
    w.write("\r\n--");
    w.write(boundary);
    w.write("--");
    w.close();
    return b.toByteArray();
}
Also used : OutputStreamWriter(java.io.OutputStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JsonParser(org.hl7.fhir.dstu2.formats.JsonParser)

Example 53 with JsonParser

use of org.hl7.fhir.r5.elementmodel.JsonParser in project org.hl7.fhir.core by hapifhir.

the class ClientUtils method getFeedAsByteArray.

public byte[] getFeedAsByteArray(Bundle feed, 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 ? OutputStyle.PRETTY : OutputStyle.NORMAL);
        parser.compose(baos, feed);
        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.dstu2.formats.XmlParser) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ParseException(java.text.ParseException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) IParser(org.hl7.fhir.dstu2.formats.IParser) JsonParser(org.hl7.fhir.dstu2.formats.JsonParser)

Example 54 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) {
            if (StringUtils.isBlank(r.getId())) {
                if (r instanceof ValueSet)
                    r.setId(tail((ValueSet) r));
            }
            if (!StringUtils.isBlank(r.getId())) {
                String tgt = Utilities.path(folder, r.fhirType() + "-" + r.getId() + ".json");
                new JsonParser().compose(new FileOutputStream(tgt), r);
            }
        }
    }
}
Also used : BundleEntryComponent(org.hl7.fhir.dstu2.model.Bundle.BundleEntryComponent) Bundle(org.hl7.fhir.dstu2.model.Bundle) FileOutputStream(java.io.FileOutputStream) Resource(org.hl7.fhir.dstu2.model.Resource) ValueSet(org.hl7.fhir.dstu2.model.ValueSet) FileInputStream(java.io.FileInputStream) JsonParser(org.hl7.fhir.dstu2.formats.JsonParser)

Example 55 with JsonParser

use of org.hl7.fhir.r5.elementmodel.JsonParser in project org.hl7.fhir.core by hapifhir.

the class ClientUtils method getFeedAsByteArray.

public byte[] getFeedAsByteArray(Bundle feed, 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 ? OutputStyle.PRETTY : OutputStyle.NORMAL);
        parser.compose(baos, feed);
        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.dstu2016may.formats.XmlParser) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ParseException(java.text.ParseException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) IParser(org.hl7.fhir.dstu2016may.formats.IParser) JsonParser(org.hl7.fhir.dstu2016may.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