FHIR's General-Purpose Data Type Object Parsing From JSON in .NET Core

1.8k Views Asked by At

I am using Hl7.Fhir.R4 library in my project which is an API Wrapper (in ASP.NET Core 2.2) around Azure API for FHIR. In a View Model, I declared a FHIR Element (FHIR's General-Purpose Data Type) as a property. Example:

public class MyPatient
{
    public string Name { get; set;}
    public Hl7.Fhir.Model.CodeableConcept MaritalStatus { get; set;} //This is 
                                                          //defined in the library
} 

Now, the problem is: The 'MaritalStatus' is not being parsed from json to the c# object (defined in the library), properly (i.e it is just 'not null'). Neither is there any exception being thrown.

Explained With Details: This is the JSON I am receiving from the front-end:

 { 
   "name": "TheName", 
   "maritalStatus": {
      "coding": [ 
         { 
           "system": "terminology.hl7.org/CodeSystem/v3-MaritalStatus", 
           "code": "U", 
           "display": "unmarried"
         } 
       ], 
      "text": "Unmarried" 
    } 
}

This is my model (C# class) against the JSON I am receiving from the front-end:

public class MyPatient
{
    public string Name { get; set;}
    public Hl7.Fhir.Model.CodeableConcept MaritalStatus { get; set;}
} 

This the controller's action (in the wrapper layer - my project) which is entertaining the request:

        [HttpPut("{id}")]
        [Consumes("application/json")]
        [Produces("application/json")]
        public async Task<ActionResult> Update([Required][FromRoute] string id,[Required][FromBody] MyPatient myPatient)
        {
            if (ModelState.IsValid)
            {

               Hl7.Fhir.Model.Patient patient = await _fhirClient.ReadAsync<Patient>(location: "Patient/" + id);

               patient.MaritalStatus = myPatient.MaritalStatus;

               patient.Name[0].Text = myPatient.Name;

               patient = await _fhirClient.UpdateAsync<Patient>(patient);

               return Ok();

            }//ends If ModelState.IsValid

            return BadRequest();
        }//ends Update
2

There are 2 best solutions below

3
Lloyd McKenzie On

First, your instance isn't valid FHIR. You need to adhere to the syntax defined in the FHIR spec - and 'name' on Patient is not a simple string. As well, you need to declare the resourceType. That's not optional. Second, if you want to use the official library, you can't go off and invent your own classes and expect them to magically work - use the built-in parser. It'll be perfectly fine even if you've only got two elements present (plus the resourceType).

0
Mirjam Baltus On

You mention using the default JSON Converter of .Net Core, so I'm assuming that is the one in System.Text.Json.

The problem is not with the FHIR library, since that is perfectly capable of parsing FHIR json correctly into FHIR objects. So you'll have to look at the JSON converter you use for your custom class, and get it to parse the json correctly into your class object. Looking at that, I think the problem is a case mismatch. This documentation indicates that property name matching is case-sensitive. FHIR properties use camel case, but the properties of the FHIR model start with uppercase. To get around this, set your serialization properties, and use them for the deserialization:

        var serializerOptions = new JsonSerializerOptions
        {
            PropertyNameCaseInsensitive = true
        };

        var myPatient = JsonSerializer.Deserialize<MyPatient>(json, serializerOptions);