2

I'm using the Fhir-net-api with DSTU2 to parse JSON objects to C# models. Everything works well, except that I can't access the Reason element of the resource type Procedure. As an example, I parse following JSON object to the Procedure model using the FhirParser:

{
"resourceType": "Procedure",
"identifier": [
    {
        "system": "https://mrd2.melanoma.org.au/fhir",
        "value": "100200199664802"
    }
],
"subject": { "reference": "Patient/10101000001733" },
"status": "completed",
"category": {
    "coding": [
        {
            "system": "https://mrd2.melanoma.org.au/fhir/RootType",
            "code": "3004"
        }
    ],
    "text": "Primary Surgery"
},
"bodySite": [
    {
        "coding": [
            {
                "system": "http://snomed.info/sct",
                "code": "7771000"
            }
        ],
        "text": "Left Forearm, Anterior"
    }
],
"reasonReference": { "reference": "/Condition/10106000001807" },
"performedDateTime": "1968-03-11",
"report": [ { "reference": "/DiagnosticReport/100200199664828" } ]
}

and the generated object has following entries (excerpt): Procedure

I can access Report[0].Reference just fine but it won't work with Reason.Reference. Is the data in my JSON object wrong? I've seen that Reason is of type Hl7.Fhir.Model.Element and Report of type Hl7.Fhir.Model.ResourceReference. Is there a way to change Reason to Hl7.Fhir.Model.ResourceReference and then access the Reference element?

Would be grateful for any hints. Thanks.

Regards,

Trammy

ftramnitzke
  • 151
  • 9

1 Answers1

2

As you noticed, the type of reasonReference is Model.Element, while the type of report is ResourceReference. This difference has its origin in the definition of these elements in the FHIR specification for Procedure, where report is fixed to the type Reference but reason (or rather reason[x]) can either be a CodeableConcept or a Reference.

When elements can be of multiple types (we call this a "choice element", and you can recognize them because their name ends with [x] in the specification), we have created a C# member that is of type Model.Element (the base class for both Reference and CodeableConcept).

Now, depending on the instance you have just parsed or received, the contents of the reason member can be one of both types. So, you'll have to check in your code:

if(Reports[0].reason is ResourceReference)
{
    var reference = (ResourceReference)Reports[0].reason;
    //handle the case where this is a reference
    //doing reference.Reference will now work as expected
}
else if(Reports[0].reason is CodeableConcept)
{
    var concept = (CodeableConcept)Reports[0].reason;
    //handle the case where this is a codeable concept
}
else
{
    // Oops! Should not happen unless the standard has changed
}

Of course, if you are sure you can only receive instances where reason is a ResourceReference, you can do a cast directly:

var myReference = (ResourceReference)Reports[0].Reference;
// myReference.Reference and myReference.Display will now work
Ewout Kramer
  • 984
  • 6
  • 9
  • Great. That should be feasible enough on my end. I haven't thought of simply casting the object to the desired type. Thanks – ftramnitzke Dec 16 '15 at 12:08