Search code examples
c#.net.net-corehl7-fhir

How to infer the correct data type for a FHIR 'choice' property


I'm using the Fire.ly .NET SDK for parsing FHIR JSON into POCOs, to then send specific information to downstream processes.

As part of this, I need to read a FHIR choice property to a variable, which can basically be many different types. The Fire.ly documentation has information on how to set one of these properties, but not read them.

I've done something like this, but I'm not sure if this is going to work as the data I'm parsing doesn't have these fields set:

var deceasedDateTime = patient.Deceased as FhirDateTime;
var deceasedBool = patient.Deceased as FhirBoolean;

Does anyone know what the right way to parse these properties is?


Solution

  • It looks even better if you do pattern matching:

    
         if(patient.Deceased is FhirDateTime fdt)
         {
          // do things with fdt
         } 
         else if(patient.Deceased is FhirBoolean fb)
         {
           // etc.
         |
    
    

    or

    
        var x = patient.Deceased switch
        {
            FhirDateTime dft =>  something,
            FhirBoolean => something else,
        
        };