Parsing FHIR Objects with Custom JSON Converters in C#

Published Mar 23 2022 02:12 PM 1,676 Views
Microsoft

pjirsa_0-1648068763242.png

Introduction

Working with complex JSON data can be quite cumbersome in a strongly typed language like C#. Especially if you want to deserialize that data into POCO model classes. Firely.NET offers a library of model classes for FHIR. However, when writing an ASP.NET WebAPI or Azure Function the default System.Text.Json serializer can get tripped up by the incoming and outgoing objects.

 

For example: Here is a sample FHIR Patient object being sent to an HttpTrigger Azure Function.

pjirsa_1-1648068763273.png

I want the function to parse this object directly to the Firely.NET Patient class. Some of the properties are successfully hydrated. However, most are not. Even primitive data types are not correctly parsed. No exception is thrown, and the properties are null.

 

So, why is this happening?

 

JSON Serializers and Parsers (Deserializers)

By default, the C# Azure Function is using System.Text.Json.JsonSerializer to parse the incoming request body when the ReadFromJsonAsync() method is used. An alternative, and popular, serializer is Newtonsoft.Json. However, the same behavior is observed.

 

The Firely.NET model classes are complex objects composed of many layers of other FHIR model classes. Therefore, most of them are not able to be properly parsed without customization. To solve this issue, the Firely SDK provides its own set of parsers and serializers to handle the job. So, one option is to just read the body of the incoming request as a string and pass it directly to the Firely parser. This would be a perfectly acceptable solution if my function was only expected to receive FHIR Bundle objects as the full body of the request. But what if my request includes other data as well? Now I would be forced to manually parse the string body or parse it to a generic JObject so I can navigate through the properties. Again, this would be an acceptable solution. But it creates a lot more code and maintenance overhead.

 

Custom Converter

A better solution is to use a custom converter to parse our object. Custom converters can be configured for the default System.Text.Json serializer or Newtonsoft.Json. It inherits from the JsonConverter class and specifies which model class it applies to.

 

Here is a simple example of a custom converter using System.Text.Json to parse a DateTimeOffset value:

 

using System.Globalization;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Models.Converters
{
    public class DateTimeOffsetJsonConverter : JsonConverter<DateTimeOffset>
    {
        public override DateTimeOffset Read(
            ref Utf8JsonReader reader,
            Type typeToConvert,
            JsonSerializerOptions options) =>
                DateTimeOffset.ParseExact(reader.GetString()!,
                    "MM/dd/yyyy", CultureInfo.InvariantCulture);

        public override void Write(
            Utf8JsonWriter writer,
            DateTimeOffset dateTimeValue,
            JsonSerializerOptions options) =>
                writer.WriteStringValue(dateTimeValue.ToString(
                    "MM/dd/yyyy", CultureInfo.InvariantCulture));
    }
}

 

 

The converter includes two methods that override from the base class:

  • Read() is used to parse the incoming JSON
  • Write() is used to serialize the POCO object back to JSON

 

Learn more about writing custom converters here - How to write custom converters for JSON serialization - .NET | Microsoft Docs

 

Now that we know how to write a converter, let’s create a generic converter, this time for Newtonsoft.JSON, that can handle any of our FHIR model classes that inherit from our FHIR Base class.

 

 

using Hl7.Fhir.ElementModel;
using Hl7.Fhir.Serialization;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Models.Converters
{
    public class FhirResourceConverter<T> : JsonConverter<T> where T : Hl7.Fhir.Model.Base
    {
        public override T ReadJson(JsonReader reader, Type objectType, T? existingValue, bool hasExistingValue, JsonSerializer serializer)
        {
            var valStr = JRaw.Create(reader).ToString();
            return FhirJsonNode.Parse(valStr).ToPoco<T>();
        }

        public override void WriteJson(JsonWriter writer, T? value, JsonSerializer serializer)
        {
            var fhirSerializer = new FhirJsonSerializer();
            writer.WriteRaw(fhirSerializer.SerializeToString(value));
        }
    }
}

 

 

We could have used System.Text.Json. But the Newtonsoft JsonReader makes it a little bit easier for us to work with the raw JSON. To solve our original issue of parsing a FHIR Patient, we simply override the ReadJson() method and use it to call the Firely FhirJsonNode parser.

 

Configuration with Dependency Injection

To make our converter globally available, we must configure the serializer settings in our program with dependency injection. In our program file, we use an extension method to add the configuration to the HostBuilder.

 

 

var host = new HostBuilder()
    .ConfigureFunctionsWorkerDefaults(workerApp =>
    {
        workerApp.UseNewtonsoftJson();
    })
    .Build();

host.Run();

 

 

The extension method is defined using the following static class:

 

 

internal static class WorkerConfigurationExtensions
{
    public static IFunctionsWorkerApplicationBuilder UseNewtonsoftJson(this IFunctionsWorkerApplicationBuilder builder)
    {
        builder.Services.Configure<WorkerOptions>(workerOptions =>
        {
            var settings = NewtonsoftJsonObjectSerializer.CreateJsonSerializerSettings();
            settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
            settings.NullValueHandling = NullValueHandling.Ignore;
            settings.Converters.Add(new FhirResourceConverter<Patient>());

            workerOptions.Serializer = new NewtonsoftJsonObjectSerializer(settings);
        });

        return builder;
    }
}

 

 

In this extension, we add our custom converter for the type of Patient to the collection of converters available to the JsonSerializer. Anytime our code needs to deserialize JSON to a Patient object, our application will know to use our custom converter. You can also provide this same level of dependency injection for custom converters in a standard dotnet API as demonstrated in this blog article.

 

Summary

Problem statement: Sending a FHIR JSON payload to a WebAPI or Azure Function HttpTrigger results in incorrectly parsed POCO models.

Good solution: Read the request payload as a string and manually parse to desired models.

Better solution: Create a custom JsonConverter to handle the parsing logic and configure globally with dependency injection.

 

Resources

1 Comment
%3CLINGO-SUB%20id%3D%22lingo-sub-3265234%22%20slang%3D%22en-US%22%3EParsing%20FHIR%20Objects%20with%20Custom%20JSON%20Converters%20in%20C%23%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3265234%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22pjirsa_0-1648068763242.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F358089i1A2376E1074BE52A%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22pjirsa_0-1648068763242.png%22%20alt%3D%22pjirsa_0-1648068763242.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CH1%20id%3D%22toc-hId--2105153708%22%20id%3D%22toc-hId--2080098484%22%3EIntroduction%3C%2FH1%3E%0A%3CP%3EWorking%20with%20complex%20JSON%20data%20can%20be%20quite%20cumbersome%20in%20a%20strongly%20typed%20language%20like%20C%23.%20Especially%20if%20you%20want%20to%20deserialize%20that%20data%20into%20%3CA%20href%3D%22https%3A%2F%2Fdocs.fire.ly%2Fprojects%2FFirely-NET-SDK%2Fparsing%2Fpoco-parsing.html%23pocos-and-parsing-incorrect-data%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3EPOCO%20model%20classes%3C%2FA%3E.%20%3CA%20href%3D%22https%3A%2F%2Ffire.ly%2Fproducts%2Ffirely-net-sdk%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3EFirely.NET%3C%2FA%3E%20offers%20a%20library%20of%20model%20classes%20for%20%3CA%20href%3D%22https%3A%2F%2Fwww.fhir.org%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3EFHIR%3C%2FA%3E.%20However%2C%20when%20writing%20an%20ASP.NET%20WebAPI%20or%20Azure%20Function%20the%20default%20System.Text.Json%20serializer%20can%20get%20tripped%20up%20by%20the%20incoming%20and%20outgoing%20objects.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFor%20example%3A%20Here%20is%20a%20sample%20FHIR%20Patient%20object%20being%20sent%20to%20an%20HttpTrigger%20Azure%20Function.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22pjirsa_1-1648068763273.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F358090i6D405266EB3BED2A%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22pjirsa_1-1648068763273.png%22%20alt%3D%22pjirsa_1-1648068763273.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EI%20want%20the%20function%20to%20parse%20this%20object%20directly%20to%20the%20Firely.NET%20Patient%20class.%20Some%20of%20the%20properties%20are%20successfully%20hydrated.%20However%2C%20most%20are%20not.%20Even%20primitive%20data%20types%20are%20not%20correctly%20parsed.%20No%20exception%20is%20thrown%2C%20and%20the%20properties%20are%20null.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESo%2C%20why%20is%20this%20happening%3F%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH1%20id%3D%22toc-hId-382359125%22%20id%3D%22toc-hId-407414349%22%3EJSON%20Serializers%20and%20Parsers%20(Deserializers)%3C%2FH1%3E%0A%3CP%3EBy%20default%2C%20the%20C%23%20Azure%20Function%20is%20using%20System.Text.Json.JsonSerializer%20to%20parse%20the%20incoming%20request%20body%20when%20the%20ReadFromJsonAsync()%20method%20is%20used.%20An%20alternative%2C%20and%20popular%2C%20serializer%20is%20%3CA%20href%3D%22https%3A%2F%2Fwww.newtonsoft.com%2Fjson%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3ENewtonsoft.Json%3C%2FA%3E.%20However%2C%20the%20same%20behavior%20is%20observed.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20Firely.NET%20model%20classes%20are%20complex%20objects%20composed%20of%20many%20layers%20of%20other%20FHIR%20model%20classes.%20Therefore%2C%20most%20of%20them%20are%20not%20able%20to%20be%20properly%20parsed%20without%20customization.%20To%20solve%20this%20issue%2C%20the%20Firely%20SDK%20provides%20its%20own%20set%20of%20parsers%20and%20serializers%20to%20handle%20the%20job.%20So%2C%20one%20option%20is%20to%20just%20read%20the%20body%20of%20the%20incoming%20request%20as%20a%20string%20and%20pass%20it%20directly%20to%20the%20Firely%20parser.%20This%20would%20be%20a%20perfectly%20acceptable%20solution%20if%20my%20function%20was%20only%20expected%20to%20receive%20FHIR%20Bundle%20objects%20as%20the%20full%20body%20of%20the%20request.%20But%20what%20if%20my%20request%20includes%20other%20data%20as%20well%3F%20Now%20I%20would%20be%20forced%20to%20manually%20parse%20the%20string%20body%20or%20parse%20it%20to%20a%20generic%20JObject%20so%20I%20can%20navigate%20through%20the%20properties.%20Again%2C%20this%20would%20be%20an%20acceptable%20solution.%20But%20it%20creates%20a%20lot%20more%20code%20and%20maintenance%20overhead.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH1%20id%3D%22toc-hId--1425095338%22%20id%3D%22toc-hId--1400040114%22%3ECustom%20Converter%3C%2FH1%3E%0A%3CP%3EA%20better%20solution%20is%20to%20use%20a%20custom%20converter%20to%20parse%20our%20object.%20Custom%20converters%20can%20be%20configured%20for%20the%20default%20System.Text.Json%20serializer%20or%20Newtonsoft.Json.%20It%20inherits%20from%20the%20JsonConverter%20class%20and%20specifies%20which%20model%20class%20it%20applies%20to.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHere%20is%20a%20simple%20example%20of%20a%20custom%20converter%20using%20System.Text.Json%20to%20parse%20a%20DateTimeOffset%20value%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Eusing%20System.Globalization%3B%0Ausing%20System.Text.Json%3B%0Ausing%20System.Text.Json.Serialization%3B%0A%0Anamespace%20Models.Converters%0A%7B%0A%20%20%20%20public%20class%20DateTimeOffsetJsonConverter%20%3A%20JsonConverter%3CDATETIMEOFFSET%3E%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20public%20override%20DateTimeOffset%20Read(%0A%20%20%20%20%20%20%20%20%20%20%20%20ref%20Utf8JsonReader%20reader%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20Type%20typeToConvert%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20JsonSerializerOptions%20options)%20%3D%26gt%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20DateTimeOffset.ParseExact(reader.GetString()!%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22MM%2Fdd%2Fyyyy%22%2C%20CultureInfo.InvariantCulture)%3B%0A%0A%20%20%20%20%20%20%20%20public%20override%20void%20Write(%0A%20%20%20%20%20%20%20%20%20%20%20%20Utf8JsonWriter%20writer%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20DateTimeOffset%20dateTimeValue%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20JsonSerializerOptions%20options)%20%3D%26gt%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20writer.WriteStringValue(dateTimeValue.ToString(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22MM%2Fdd%2Fyyyy%22%2C%20CultureInfo.InvariantCulture))%3B%0A%20%20%20%20%7D%0A%7D%3C%2FDATETIMEOFFSET%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20converter%20includes%20two%20methods%20that%20override%20from%20the%20base%20class%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3ERead()%20is%20used%20to%20parse%20the%20incoming%20JSON%3C%2FLI%3E%0A%3CLI%3EWrite()%20is%20used%20to%20serialize%20the%20POCO%20object%20back%20to%20JSON%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ELearn%20more%20about%20writing%20custom%20converters%20here%20-%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fdotnet%2Fstandard%2Fserialization%2Fsystem-text-json-converters-how-to%3Fpivots%3Ddotnet-6-0%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EHow%20to%20write%20custom%20converters%20for%20JSON%20serialization%20-%20.NET%20%7C%20Microsoft%20Docs%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENow%20that%20we%20know%20how%20to%20write%20a%20converter%2C%20let%E2%80%99s%20create%20a%20generic%20converter%2C%20this%20time%20for%20Newtonsoft.JSON%2C%20that%20can%20handle%20any%20of%20our%20FHIR%20model%20classes%20that%20inherit%20from%20our%20FHIR%20Base%20class.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Eusing%20Hl7.Fhir.ElementModel%3B%0Ausing%20Hl7.Fhir.Serialization%3B%0Ausing%20Newtonsoft.Json%3B%0Ausing%20Newtonsoft.Json.Linq%3B%0A%0Anamespace%20Models.Converters%0A%7B%0A%20%20%20%20public%20class%20FhirResourceConverter%3CT%3E%20%3A%20JsonConverter%3CT%3E%20where%20T%20%3A%20Hl7.Fhir.Model.Base%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20public%20override%20T%20ReadJson(JsonReader%20reader%2C%20Type%20objectType%2C%20T%3F%20existingValue%2C%20bool%20hasExistingValue%2C%20JsonSerializer%20serializer)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20var%20valStr%20%3D%20JRaw.Create(reader).ToString()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20FhirJsonNode.Parse(valStr).ToPoco%3CT%3E()%3B%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20public%20override%20void%20WriteJson(JsonWriter%20writer%2C%20T%3F%20value%2C%20JsonSerializer%20serializer)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20var%20fhirSerializer%20%3D%20new%20FhirJsonSerializer()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20writer.WriteRaw(fhirSerializer.SerializeToString(value))%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%7D%3C%2FT%3E%3C%2FT%3E%3C%2FT%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWe%20could%20have%20used%20System.Text.Json.%20But%20the%20Newtonsoft%20JsonReader%20makes%20it%20a%20little%20bit%20easier%20for%20us%20to%20work%20with%20the%20raw%20JSON.%20To%20solve%20our%20original%20issue%20of%20parsing%20a%20FHIR%20Patient%2C%20we%20simply%20override%20the%20ReadJson()%20method%20and%20use%20it%20to%20call%20the%20Firely%20FhirJsonNode%20parser.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH1%20id%3D%22toc-hId-1062417495%22%20id%3D%22toc-hId-1087472719%22%3EConfiguration%20with%20Dependency%20Injection%3C%2FH1%3E%0A%3CP%3ETo%20make%20our%20converter%20globally%20available%2C%20we%20must%20configure%20the%20serializer%20settings%20in%20our%20program%20with%20dependency%20injection.%20In%20our%20program%20file%2C%20we%20use%20an%20extension%20method%20to%20add%20the%20configuration%20to%20the%20HostBuilder.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Evar%20host%20%3D%20new%20HostBuilder()%0A%20%20%20%20.ConfigureFunctionsWorkerDefaults(workerApp%20%3D%26gt%3B%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20workerApp.UseNewtonsoftJson()%3B%0A%20%20%20%20%7D)%0A%20%20%20%20.Build()%3B%0A%0Ahost.Run()%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20extension%20method%20is%20defined%20using%20the%20following%20static%20class%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Einternal%20static%20class%20WorkerConfigurationExtensions%0A%7B%0A%20%20%20%20public%20static%20IFunctionsWorkerApplicationBuilder%20UseNewtonsoftJson(this%20IFunctionsWorkerApplicationBuilder%20builder)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20builder.Services.Configure%3CWORKEROPTIONS%3E(workerOptions%20%3D%26gt%3B%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20var%20settings%20%3D%20NewtonsoftJsonObjectSerializer.CreateJsonSerializerSettings()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20settings.ContractResolver%20%3D%20new%20CamelCasePropertyNamesContractResolver()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20settings.NullValueHandling%20%3D%20NullValueHandling.Ignore%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20settings.Converters.Add(new%20FhirResourceConverter%3CPATIENT%3E())%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20workerOptions.Serializer%20%3D%20new%20NewtonsoftJsonObjectSerializer(settings)%3B%0A%20%20%20%20%20%20%20%20%7D)%3B%0A%0A%20%20%20%20%20%20%20%20return%20builder%3B%0A%20%20%20%20%7D%0A%7D%3C%2FPATIENT%3E%3C%2FWORKEROPTIONS%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20this%20extension%2C%20we%20add%20our%20custom%20converter%20for%20the%20type%20of%20Patient%20to%20the%20collection%20of%20converters%20available%20to%20the%20JsonSerializer.%20Anytime%20our%20code%20needs%20to%20deserialize%20JSON%20to%20a%20Patient%20object%2C%20our%20application%20will%20know%20to%20use%20our%20custom%20converter.%20You%20can%20also%20provide%20this%20same%20level%20of%20dependency%20injection%20for%20custom%20converters%20in%20a%20standard%20dotnet%20API%20as%20demonstrated%20in%20%3CA%20href%3D%22https%3A%2F%2Fmakolyte.com%2Faspdotnet-how-to-make-the-controllers-use-newtonsoft%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3Ethis%20blog%20article%3C%2FA%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH1%20id%3D%22toc-hId--745036968%22%20id%3D%22toc-hId--719981744%22%3ESummary%3C%2FH1%3E%0A%3CP%3E%3CSTRONG%3EProblem%20statement%3C%2FSTRONG%3E%3A%20Sending%20a%20FHIR%20JSON%20payload%20to%20a%20WebAPI%20or%20Azure%20Function%20HttpTrigger%20results%20in%20incorrectly%20parsed%20POCO%20models.%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EGood%20solution%3C%2FSTRONG%3E%3A%20Read%20the%20request%20payload%20as%20a%20string%20and%20manually%20parse%20to%20desired%20models.%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EBetter%20solution%3A%20%3C%2FSTRONG%3ECreate%20a%20custom%20JsonConverter%20to%20handle%20the%20parsing%20logic%20and%20configure%20globally%20with%20dependency%20injection.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--54475494%22%20id%3D%22toc-hId--29420270%22%3EResources%3C%2FH2%3E%0A%3CUL%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Ffire.ly%2Fproducts%2Ffirely-net-sdk%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3EFirely.NET%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fwww.fhir.org%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3EFHIR.org%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fwww.newtonsoft.com%2Fjson%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3ENewtonsoft.Json%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fdotnet%2Fstandard%2Fserialization%2Fsystem-text-json-converters-how-to%3Fpivots%3Ddotnet-6-0%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EHow%20to%20write%20custom%20converters%20for%20JSON%20serialization%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fmakolyte.com%2Faspdotnet-how-to-make-the-controllers-use-newtonsoft%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3EASP.NET%20Core%20%E2%80%93%20How%20to%20make%20the%20controllers%20use%20Newtonsoft%3C%2FA%3E%3C%2FLI%3E%0A%3C%2FUL%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-3265234%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22jsonconvert-fhir.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F358088i535A783AD9CEF548%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22jsonconvert-fhir.png%22%20alt%3D%22jsonconvert-fhir.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EUse%20a%20custom%20Json%20converter%20to%20handle%20parsing%20and%20serializing%20complex%20FHIR%20model%20objects.%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3265234%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EHLS_Hack%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3268495%22%20slang%3D%22en-US%22%3ERe%3A%20Parsing%20FHIR%20Objects%20with%20Custom%20JSON%20Converters%20in%20C%23%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3268495%22%20slang%3D%22en-US%22%3E%3CP%3EWe%20good%20article.%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Co-Authors
Version history
Last update:
‎Mar 23 2022 02:10 PM
Updated by: