Search in sources :

Example 21 with Property

use of org.hl7.fhir.r4.elementmodel.Property in project org.hl7.fhir.core by hapifhir.

the class JsonParser method parse.

public Element parse(JsonObject object) throws FHIRFormatError, DefinitionException {
    JsonElement rt = object.get("resourceType");
    if (rt == null) {
        logError(line(object), col(object), "$", IssueType.INVALID, "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));
        checkObject(object, path);
        result.markLocation(line(object), col(object));
        result.setType(name);
        parseChildren(path, object, result, true);
        result.numberChildren();
        return result;
    }
}
Also used : StructureDefinition(org.hl7.fhir.dstu3.model.StructureDefinition) JsonElement(com.google.gson.JsonElement) JsonElement(com.google.gson.JsonElement) SpecialElement(org.hl7.fhir.dstu3.elementmodel.Element.SpecialElement)

Example 22 with Property

use of org.hl7.fhir.r4.elementmodel.Property in project org.hl7.fhir.core by hapifhir.

the class JsonParser method parseChildren.

private void parseChildren(String path, JsonObject object, Element context, boolean hasResourceType) throws DefinitionException, FHIRFormatError {
    reapComments(object, context);
    List<Property> properties = context.getProperty().getChildProperties(context.getName(), null);
    Set<String> processed = new HashSet<String>();
    if (hasResourceType)
        processed.add("resourceType");
    processed.add("fhir_comments");
    // first pass: process the properties
    for (Property property : properties) {
        if (property.isChoice()) {
            for (TypeRefComponent type : property.getDefinition().getType()) {
                String eName = property.getName().substring(0, property.getName().length() - 3) + Utilities.capitalize(type.getCode());
                if (!isPrimitive(type.getCode()) && object.has(eName)) {
                    parseChildComplex(path, object, context, processed, property, eName);
                    break;
                } else if (isPrimitive(type.getCode()) && (object.has(eName) || object.has("_" + eName))) {
                    parseChildPrimitive(object, context, processed, property, path, eName);
                    break;
                }
            }
        } else if (property.isPrimitive(property.getType(null))) {
            parseChildPrimitive(object, context, processed, property, path, property.getName());
        } else if (object.has(property.getName())) {
            parseChildComplex(path, object, context, processed, property, property.getName());
        }
    }
    // second pass: check for things not processed
    if (policy != ValidationPolicy.NONE) {
        for (Entry<String, JsonElement> e : object.entrySet()) {
            if (!processed.contains(e.getKey())) {
                logError(line(e.getValue()), col(e.getValue()), path, IssueType.STRUCTURE, "Unrecognised property '@" + e.getKey() + "'", IssueSeverity.ERROR);
            }
        }
    }
}
Also used : TypeRefComponent(org.hl7.fhir.dstu3.model.ElementDefinition.TypeRefComponent) JsonElement(com.google.gson.JsonElement) HashSet(java.util.HashSet)

Example 23 with Property

use of org.hl7.fhir.r4.elementmodel.Property in project org.hl7.fhir.core by hapifhir.

the class JsonParser method parseChildComplexInstance.

private void parseChildComplexInstance(String npath, JsonObject object, Element context, Property property, String name, JsonElement e) throws FHIRFormatError, DefinitionException {
    if (e instanceof JsonObject) {
        JsonObject child = (JsonObject) e;
        Element n = new Element(name, property).markLocation(line(child), col(child));
        checkObject(child, npath);
        context.getChildren().add(n);
        if (property.isResource())
            parseResource(npath, child, n, property);
        else
            parseChildren(npath, child, n, false);
    } else
        logError(line(e), col(e), npath, IssueType.INVALID, "This property must be " + (property.isList() ? "an Array" : "an Object") + ", not a " + e.getClass().getName(), IssueSeverity.ERROR);
}
Also used : JsonElement(com.google.gson.JsonElement) SpecialElement(org.hl7.fhir.dstu3.elementmodel.Element.SpecialElement) JsonObject(com.google.gson.JsonObject)

Example 24 with Property

use of org.hl7.fhir.r4.elementmodel.Property in project org.hl7.fhir.core by hapifhir.

the class JsonParser method parse.

public Element parse(String source, String type) throws Exception {
    JsonObject obj = (JsonObject) new com.google.gson.JsonParser().parse(source);
    String path = "/" + type;
    StructureDefinition sd = getDefinition(-1, -1, type);
    if (sd == null)
        return null;
    Element result = new Element(type, new Property(context, sd.getSnapshot().getElement().get(0), sd));
    checkObject(obj, path);
    result.setType(type);
    parseChildren(path, obj, result, true);
    result.numberChildren();
    return result;
}
Also used : StructureDefinition(org.hl7.fhir.dstu3.model.StructureDefinition) JsonElement(com.google.gson.JsonElement) SpecialElement(org.hl7.fhir.dstu3.elementmodel.Element.SpecialElement) JsonObject(com.google.gson.JsonObject)

Example 25 with Property

use of org.hl7.fhir.r4.elementmodel.Property in project org.hl7.fhir.core by hapifhir.

the class JsonParser method parseResource.

private void parseResource(String npath, JsonObject res, Element parent, Property elementProperty) throws DefinitionException, FHIRFormatError {
    JsonElement rt = res.get("resourceType");
    if (rt == null) {
        logError(line(res), col(res), npath, IssueType.INVALID, "Unable to find resourceType property", IssueSeverity.FATAL);
    } else {
        String name = rt.getAsString();
        StructureDefinition sd = context.fetchTypeDefinition(name);
        if (sd == null)
            throw new FHIRFormatError("Contained resource does not appear to be a FHIR resource (unknown name '" + name + "')");
        parent.updateProperty(new Property(context, sd.getSnapshot().getElement().get(0), sd), SpecialElement.fromProperty(parent.getProperty()), elementProperty);
        parent.setType(name);
        parseChildren(npath, res, parent, true);
    }
}
Also used : StructureDefinition(org.hl7.fhir.dstu3.model.StructureDefinition) JsonElement(com.google.gson.JsonElement) FHIRFormatError(org.hl7.fhir.exceptions.FHIRFormatError)

Aggregations

ArrayList (java.util.ArrayList)35 FHIRException (org.hl7.fhir.exceptions.FHIRException)35 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)30 JsonElement (com.google.gson.JsonElement)23 FHIRFormatError (org.hl7.fhir.exceptions.FHIRFormatError)21 StructureDefinition (org.hl7.fhir.dstu3.model.StructureDefinition)15 JsonObject (com.google.gson.JsonObject)14 List (java.util.List)14 StructureDefinition (org.hl7.fhir.r4.model.StructureDefinition)14 Complex (org.hl7.fhir.r4.utils.formats.Turtle.Complex)13 StructureDefinition (org.hl7.fhir.r4b.model.StructureDefinition)13 StructureDefinition (org.hl7.fhir.r5.model.StructureDefinition)13 ElementDefinition (org.hl7.fhir.dstu3.model.ElementDefinition)11 HashMap (java.util.HashMap)8 HashSet (java.util.HashSet)8 Collectors (java.util.stream.Collectors)8 ElementDefinition (org.hl7.fhir.r4.model.ElementDefinition)8 SpecialElement (org.hl7.fhir.r4b.elementmodel.Element.SpecialElement)8 NamedElement (org.hl7.fhir.r4b.elementmodel.ParserBase.NamedElement)8 SpecialElement (org.hl7.fhir.r5.elementmodel.Element.SpecialElement)8