Skip to content

Instantly share code, notes, and snippets.

@jackliusr
Created July 23, 2024 14:21
Show Gist options
  • Save jackliusr/ae8aa63d678dc764ea701dc2a8a1c3bb to your computer and use it in GitHub Desktop.
Save jackliusr/ae8aa63d678dc764ea701dc2a8a1c3bb to your computer and use it in GitHub Desktop.
Test how firely sdk (Poco and ISourceNode) to handle extra fields in json data
// See https://aka.ms/new-console-template for more information
using Hl7.Fhir.ElementModel;
using Hl7.Fhir.Model;
using Hl7.Fhir.Serialization;
using System.IO;
using System.Text.Json;
Console.WriteLine("Hello, World!");
// example Patient setup, fictional data only
{
var pat = new Patient();
var id = new Identifier();
id.System = "http://hl7.org/fhir/sid/us-ssn";
id.Value = "000-12-3456";
pat.Identifier.Add(id);
var name = new HumanName().WithGiven("Christopher").WithGiven("C.H.").AndFamily("Parks");
name.Prefix = new string[] { "Mr." };
name.Use = HumanName.NameUse.Official;
var nickname = new HumanName();
nickname.Use = HumanName.NameUse.Nickname;
nickname.GivenElement.Add(new FhirString("Chris"));
pat.Name.Add(name);
pat.Name.Add(nickname);
pat.Gender = AdministrativeGender.Male;
pat.BirthDate = "1983-04-23";
var birthplace = new Extension();
birthplace.Url = "http://hl7.org/fhir/StructureDefinition/birthPlace";
birthplace.Value = new Address() { City = "Seattle" };
pat.Extension.Add(birthplace);
var birthtime = new Extension("http://hl7.org/fhir/StructureDefinition/patient-birthTime",
value: new FhirDateTime(1983, 4, 23));
pat.BirthDateElement.Extension.Add(birthtime);
var address = new Address()
{
Line = new string[] { "3300 Washtenaw Avenue, Suite 227" },
City = "Ann Arbor",
State = "MI",
PostalCode = "48104",
Country = "USA"
};
pat.Address.Add(address);
var contact = new Patient.ContactComponent();
contact.Name = new HumanName();
contact.Name.Given = new string[] { "Susan" };
contact.Name.Family = "Parks";
contact.Gender = AdministrativeGender.Female;
contact.Relationship.Add(new CodeableConcept("http://hl7.org/fhir/v2/0131", "N"));
contact.Telecom.Add(new ContactPoint(ContactPoint.ContactPointSystem.Phone, null, ""));
pat.Contact.Add(contact);
pat.Deceased = new FhirBoolean(false);
var options = new JsonSerializerOptions().ForFhir(ModelInfo.ModelInspector).Pretty();
string patientJson = JsonSerializer.Serialize(pat, options);
Console.WriteLine(patientJson);
}
{
//string to json
var patTest = $@"{{
""resourceType"": ""Patient"",
""extension"": [
{{
""url"": ""http://hl7.org/fhir/StructureDefinition/birthPlace"",
""valueAddress"": {{
""city"": ""Seattle""
}}
}}
],
""identifier"": [
{{
""system"": ""http://hl7.org/fhir/sid/us-ssn"",
""value"": ""000-12-3456""
}}
],
""name"": [
{{
""use"": ""official"",
""family"": ""Parks"",
""given"": [
""Christopher"",
""C.H.""
],
""prefix"": [
""Mr.""
]
}},
{{
""use"": ""nickname"",
""given"": [
""Chris""
]
}}
],
""gender"": ""male"",
""birthDate"": ""1983-04-23"",
""_birthDate"": {{
""extension"": [
{{
""url"": ""http://hl7.org/fhir/StructureDefinition/patient-birthTime"",
""valueDateTime"": ""1983-04-23""
}}
]
}},
""deceasedBoolean"": false,
""address"": [
{{
""line"": [
""3300 Washtenaw Avenue, Suite 227""
],
""city"": ""Ann Arbor"",
""state"": ""MI"",
""postalCode"": ""48104"",
""country"": ""USA""
}}
],
""contact"": [
{{
""relationship"": [
{{
""coding"": [
{{
""system"": ""http://hl7.org/fhir/v2/0131"",
""code"": ""N""
}}
]
}}
],
""name"": {{
""family"": ""Parks"",
""given"": [
""Susan""
]
}},
""telecom"": [
{{
""system"": ""phone"",
""value"": """"
}}
],
""gender"": ""female""
}}
],
""test"": ""testvalue""
}}";
var patNode = FhirJsonNode.Parse(patTest);
var test = patNode.Children("test").FirstOrDefault().Text;
Console.WriteLine(test);
var options = new JsonSerializerOptions().ForFhir(ModelInfo.ModelInspector);
try
{
Patient p = JsonSerializer.Deserialize<Patient>(patTest, options);
}
catch (DeserializationFailedException e)
{
Console.WriteLine(e.Message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment