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?
CodePudding user response:
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,
};
CodePudding user response:
Answered my own question.
Once you've set your variable from the choice property, you can just type check it against the expected values.
For example:
var deceasedType = patient.Deceased?.GetType();
if (deceasedType != null && deceasedType == typeof(FhirDateTime))
{
// foo
}
else if (deceasedType != null && deceasedType == typeof(FhirBoolean))
{
// bar
}