Search in sources :

Example 31 with ResourceType

use of org.hl7.fhir.r4.model.ResourceType in project org.hl7.fhir.core by hapifhir.

the class JsonParser method compose.

@Override
public void compose(Element e, OutputStream stream, OutputStyle style, String identity) throws FHIRException, IOException {
    OutputStreamWriter osw = new OutputStreamWriter(stream, "UTF-8");
    if (style == OutputStyle.CANONICAL)
        json = new JsonCreatorCanonical(osw);
    else
        json = new JsonCreatorGson(osw);
    json.setIndent(style == OutputStyle.PRETTY ? "  " : "");
    json.beginObject();
    prop("resourceType", e.getType(), null);
    Set<String> done = new HashSet<String>();
    for (Element child : e.getChildren()) {
        compose(e.getName(), e, done, child);
    }
    json.endObject();
    json.finish();
    osw.flush();
}
Also used : JsonCreatorGson(org.hl7.fhir.r5.formats.JsonCreatorGson) JsonCreatorCanonical(org.hl7.fhir.r5.formats.JsonCreatorCanonical) JsonElement(com.google.gson.JsonElement) SpecialElement(org.hl7.fhir.r5.elementmodel.Element.SpecialElement) OutputStreamWriter(java.io.OutputStreamWriter) HashSet(java.util.HashSet)

Example 32 with ResourceType

use of org.hl7.fhir.r4.model.ResourceType in project org.hl7.fhir.core by hapifhir.

the class JsonParser method parse.

public Element parse(JsonObject object) throws FHIRException {
    JsonElement rt = object.get("resourceType");
    if (rt == null) {
        logError(line(object), col(object), "$", IssueType.INVALID, context.formatMessage(I18nConstants.UNABLE_TO_FIND_RESOURCETYPE_PROPERTY), IssueSeverity.FATAL);
        return null;
    } else {
        String name = rt.getAsString();
        String path = name;
        StructureDefinition sd = getDefinition(line(object), col(object), name);
        if (sd == null)
            return null;
        Element result = new Element(name, new Property(context, sd.getSnapshot().getElement().get(0), sd, this.profileUtilities));
        checkObject(object, path);
        result.markLocation(line(object), col(object));
        result.setType(name);
        result.setPath(result.fhirType());
        parseChildren(path, object, result, true);
        result.numberChildren();
        return result;
    }
}
Also used : StructureDefinition(org.hl7.fhir.r5.model.StructureDefinition) JsonElement(com.google.gson.JsonElement) JsonElement(com.google.gson.JsonElement) SpecialElement(org.hl7.fhir.r5.elementmodel.Element.SpecialElement)

Example 33 with ResourceType

use of org.hl7.fhir.r4.model.ResourceType in project org.hl7.fhir.core by hapifhir.

the class NpmPackageIndexBuilder method seeFile.

public boolean seeFile(String name, byte[] content) {
    if (name.endsWith(".json")) {
        try {
            JsonObject json = JsonTrackingParser.parseJson(content);
            if (json.has("resourceType")) {
                // ok we treat it as a resource
                JsonObject fi = new JsonObject();
                files.add(fi);
                fi.addProperty("filename", name);
                fi.addProperty("resourceType", json.get("resourceType").getAsString());
                if (json.has("id") && json.get("id").isJsonPrimitive()) {
                    fi.addProperty("id", json.get("id").getAsString());
                }
                if (json.has("url") && json.get("url").isJsonPrimitive()) {
                    fi.addProperty("url", json.get("url").getAsString());
                }
                if (json.has("version") && json.get("version").isJsonPrimitive()) {
                    fi.addProperty("version", json.get("version").getAsString());
                }
                if (json.has("kind") && json.get("kind").isJsonPrimitive()) {
                    fi.addProperty("kind", json.get("kind").getAsString());
                }
                if (json.has("type") && json.get("type").isJsonPrimitive()) {
                    fi.addProperty("type", json.get("type").getAsString());
                }
                if (json.has("supplements") && json.get("supplements").isJsonPrimitive()) {
                    fi.addProperty("supplements", json.get("supplements").getAsString());
                }
            }
        } catch (Exception e) {
            System.out.println("Error parsing " + name + ": " + e.getMessage());
            if (name.contains("openapi")) {
                return false;
            }
        }
    }
    return true;
}
Also used : JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 34 with ResourceType

use of org.hl7.fhir.r4.model.ResourceType in project org.hl7.fhir.core by hapifhir.

the class JsonParser method compose.

public void compose(Element e, JsonCreator json) throws Exception {
    this.json = json;
    json.beginObject();
    prop("resourceType", e.getType(), linkResolver == null ? null : linkResolver.resolveProperty(e.getProperty()));
    Set<String> done = new HashSet<String>();
    for (Element child : e.getChildren()) {
        compose(e.getName(), e, done, child);
    }
    json.endObject();
    json.finish();
}
Also used : SpecialElement(org.hl7.fhir.r4.elementmodel.Element.SpecialElement) JsonElement(com.google.gson.JsonElement) HashSet(java.util.HashSet)

Example 35 with ResourceType

use of org.hl7.fhir.r4.model.ResourceType in project org.hl7.fhir.core by hapifhir.

the class JsonParser method composeList.

private void composeList(String path, List<Element> list) throws IOException {
    // there will be at least one element
    String name = list.get(0).getName();
    boolean complex = true;
    if (list.get(0).isPrimitive()) {
        boolean prim = false;
        complex = false;
        for (Element item : list) {
            if (item.hasValue())
                prim = true;
            if (item.hasChildren())
                complex = true;
        }
        if (prim) {
            openArray(name, linkResolver == null ? null : linkResolver.resolveProperty(list.get(0).getProperty()));
            for (Element item : list) {
                if (item.hasValue())
                    primitiveValue(null, item);
                else
                    json.nullValue();
            }
            closeArray();
        }
        name = "_" + name;
    }
    if (complex) {
        openArray(name, linkResolver == null ? null : linkResolver.resolveProperty(list.get(0).getProperty()));
        for (Element item : list) {
            if (item.hasChildren()) {
                open(null, null);
                if (item.getProperty().isResource()) {
                    prop("resourceType", item.getType(), linkResolver == null ? null : linkResolver.resolveType(item.getType()));
                }
                Set<String> done = new HashSet<String>();
                for (Element child : item.getChildren()) {
                    compose(path + "." + name + "[]", item, done, child);
                }
                close();
            } else
                json.nullValue();
        }
        closeArray();
    }
}
Also used : SpecialElement(org.hl7.fhir.r4.elementmodel.Element.SpecialElement) JsonElement(com.google.gson.JsonElement) HashSet(java.util.HashSet)

Aggregations

JsonElement (com.google.gson.JsonElement)33 HashSet (java.util.HashSet)26 Bundle (org.hl7.fhir.r4.model.Bundle)24 ResourceType (org.hl7.fhir.r4.model.Enumerations.ResourceType)21 Test (org.junit.Test)20 Nonnull (javax.annotation.Nonnull)18 ArrayList (java.util.ArrayList)15 IBaseResource (org.hl7.fhir.instance.model.api.IBaseResource)10 FhirContext (ca.uhn.fhir.context.FhirContext)9 File (java.io.File)9 Row (org.apache.spark.sql.Row)9 IdType (org.hl7.fhir.dstu3.model.IdType)9 BundleEntryComponent (org.hl7.fhir.r4.model.Bundle.BundleEntryComponent)9 JsonObject (com.google.gson.JsonObject)8 Test (org.junit.jupiter.api.Test)8 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)8 IOException (java.io.IOException)7 List (java.util.List)7 Bundle (org.hl7.fhir.dstu3.model.Bundle)7 Resource (org.hl7.fhir.r4.model.Resource)7