Search code examples
c#hl7-fhir

How to deserialise JSON to FHIR valueset resource in .net


I'm having trouble casting a a response from a valueset expansion to the valueset resource object in c#.

I'm currently using RestSharp, the REST call is successful, and the following outputs the expected JSON.

IRestResponse response = client.Execute(request);
var result = JsonConvert.DeserializeObject(response.Content);
Console.WriteLine(result);

I've tried

var result = JsonConvert.DeserializeObject<ValueSet>(response.Content);

But it produces a null object. I'm sure I'm making some rookie error, and perhaps should look into using the Hl7.Fhir.Rest instead of RestSharp?


Solution

  • So I did end up being able to deserialise the RestSharp JSON response, by creating custom ValueSet class (I just used http://json2csharp.com/ for my experiment).

    However I took @Mirjam's advice, and used Hl7.Fhir.Rest instead (And ValueSet class from HL7.Fhir.Model - which contains so much more goodness than could be achieved using a custom class.

    // using using Hl7.Fhir.Model;
    // using Hl7.Fhir.Rest;
    
    const string Endpoint = "https://ontoserver.csiro.au/stu3-latest";
    var client = new FhirClient(Endpoint);
    
    //uri for the value set to be searched, and text filter         
    var filter = new FhirString("inr");
    var vs_uri = new FhirUri("http://snomed.info/sct?fhir_vs=refset/1072351000168102");
    
    ValueSet result = client.ExpandValueSet(vs_uri, filter);
     
    //Write out the display term of the first result.
    Console.WriteLine(result.Expansion.Contains.FirstOrDefault().Display);
    

    There's a couple of other approaches, that support additional parameters...

    Code available - https://gist.github.com/MattCordell/32f3c62b4e66bd1ecb17b65f2f498acb