Search in sources :

Example 36 with JsonParser

use of org.hl7.fhir.dstu2.formats.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);
        }
    }
}
Also used : XmlParser(org.hl7.fhir.dstu2.formats.XmlParser) 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) FileInputStream(java.io.FileInputStream) JsonParser(org.hl7.fhir.dstu2.formats.JsonParser)

Example 37 with JsonParser

use of org.hl7.fhir.dstu2.formats.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;
}
Also used : MetadataResource(org.hl7.fhir.dstu3.model.MetadataResource) StructureDefinitionKind(org.hl7.fhir.dstu3.model.StructureDefinition.StructureDefinitionKind) JsonParser(org.hl7.fhir.dstu2.formats.JsonParser) Bundle(org.hl7.fhir.dstu3.model.Bundle) VersionConvertorFactory_10_30(org.hl7.fhir.convertors.factory.VersionConvertorFactory_10_30) BundleEntryComponent(org.hl7.fhir.dstu3.model.Bundle.BundleEntryComponent) UriType(org.hl7.fhir.dstu3.model.UriType) BundleType(org.hl7.fhir.dstu3.model.Bundle.BundleType) BaseAdvisor_10_30(org.hl7.fhir.convertors.advisors.impl.BaseAdvisor_10_30) IOException(java.io.IOException) UUID(java.util.UUID) XmlParser(org.hl7.fhir.dstu2.formats.XmlParser) MetadataResource(org.hl7.fhir.dstu3.model.MetadataResource) ArrayList(java.util.ArrayList) List(java.util.List) Resource(org.hl7.fhir.dstu2.model.Resource) StructureDefinition(org.hl7.fhir.dstu3.model.StructureDefinition) FHIRException(org.hl7.fhir.exceptions.FHIRException) InputStream(java.io.InputStream) XmlParser(org.hl7.fhir.dstu2.formats.XmlParser) Bundle(org.hl7.fhir.dstu3.model.Bundle) MetadataResource(org.hl7.fhir.dstu3.model.MetadataResource) Resource(org.hl7.fhir.dstu2.model.Resource) ArrayList(java.util.ArrayList) UriType(org.hl7.fhir.dstu3.model.UriType) StructureDefinition(org.hl7.fhir.dstu3.model.StructureDefinition) BundleEntryComponent(org.hl7.fhir.dstu3.model.Bundle.BundleEntryComponent) JsonParser(org.hl7.fhir.dstu2.formats.JsonParser)

Example 38 with JsonParser

use of org.hl7.fhir.dstu2.formats.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");
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Bundle(org.hl7.fhir.dstu2.model.Bundle) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Resource(org.hl7.fhir.dstu2.model.Resource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FHIRException(org.hl7.fhir.exceptions.FHIRException) IParser(org.hl7.fhir.dstu2.formats.IParser) JsonParser(org.hl7.fhir.dstu2.formats.JsonParser) Test(org.junit.jupiter.api.Test)

Example 39 with JsonParser

use of org.hl7.fhir.dstu2.formats.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();
}
Also used : XmlParser(org.hl7.fhir.dstu2.formats.XmlParser) FileOutputStream(java.io.FileOutputStream) Resource(org.hl7.fhir.dstu2.model.Resource) FileInputStream(java.io.FileInputStream) IParser(org.hl7.fhir.dstu2.formats.IParser) JsonParser(org.hl7.fhir.dstu2.formats.JsonParser)

Example 40 with JsonParser

use of org.hl7.fhir.dstu2.formats.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;
    }
}
Also used : XmlParser(org.hl7.fhir.dstu2.formats.XmlParser) FileOutputStream(java.io.FileOutputStream) Resource(org.hl7.fhir.dstu2.model.Resource) FileInputStream(java.io.FileInputStream) CSFileInputStream(org.hl7.fhir.utilities.CSFileInputStream) JsonParser(org.hl7.fhir.dstu2.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