diff --git a/data/SampleData/Json/ExamplePatient.json b/data/SampleData/Json/ExamplePatient.json index e8870e2f8..6883781e5 100644 --- a/data/SampleData/Json/ExamplePatient.json +++ b/data/SampleData/Json/ExamplePatient.json @@ -8,5 +8,6 @@ "1234-5679" ], "Gender": "M", - "DOB": "20010110" + "DOB": "20010110", + "TimeOfDeath": "2023-07-28T01:59:23.388-05:00" } \ No newline at end of file diff --git a/data/Templates/Json/ExamplePatient.liquid b/data/Templates/Json/ExamplePatient.liquid index 660d75fa5..2bc697c4e 100644 --- a/data/Templates/Json/ExamplePatient.liquid +++ b/data/Templates/Json/ExamplePatient.liquid @@ -50,5 +50,6 @@ "managingOrganization": { "reference": "Organization/2.16.840.1.113883.19.5", "display": "Good Health Clinic" - } + }, + "deceasedDateTime": "{{ msg.TimeOfDeath }}" } \ No newline at end of file diff --git a/src/Microsoft.Health.Fhir.Liquid.Converter.FunctionalTests/TestData/Expected/Json/ExamplePatient/ExamplePatient-expected.json b/src/Microsoft.Health.Fhir.Liquid.Converter.FunctionalTests/TestData/Expected/Json/ExamplePatient/ExamplePatient-expected.json index 53addab2f..28d940a49 100644 --- a/src/Microsoft.Health.Fhir.Liquid.Converter.FunctionalTests/TestData/Expected/Json/ExamplePatient/ExamplePatient-expected.json +++ b/src/Microsoft.Health.Fhir.Liquid.Converter.FunctionalTests/TestData/Expected/Json/ExamplePatient/ExamplePatient-expected.json @@ -40,5 +40,6 @@ "managingOrganization": { "reference": "Organization/2.16.840.1.113883.19.5", "display": "Good Health Clinic" - } + }, + "deceasedDateTime": "2023-07-28T01:59:23.388-05:00" } \ No newline at end of file diff --git a/src/Microsoft.Health.Fhir.Liquid.Converter.UnitTests/Processors/ProcessorTests.cs b/src/Microsoft.Health.Fhir.Liquid.Converter.UnitTests/Processors/ProcessorTests.cs index 3678c1549..4fa9faeba 100644 --- a/src/Microsoft.Health.Fhir.Liquid.Converter.UnitTests/Processors/ProcessorTests.cs +++ b/src/Microsoft.Health.Fhir.Liquid.Converter.UnitTests/Processors/ProcessorTests.cs @@ -5,7 +5,6 @@ using System; using System.Collections.Generic; -using System.Data.Common; using System.IO; using System.Text.RegularExpressions; using System.Threading; @@ -13,6 +12,7 @@ using Microsoft.Health.Fhir.Liquid.Converter.Exceptions; using Microsoft.Health.Fhir.Liquid.Converter.Models; using Microsoft.Health.Fhir.Liquid.Converter.Processors; +using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Xunit; @@ -362,7 +362,7 @@ public void GivenJObjectInput_WhenConvertWithJsonProcessor_CorrectResultShouldBe { var processor = _jsonProcessor; var templateProvider = new TemplateProvider(TestConstants.JsonTemplateDirectory, DataType.Json); - var testData = JObject.Parse(_jsonTestData); + var testData = JsonConvert.DeserializeObject(_jsonTestData, new JsonSerializerSettings() { DateParseHandling = DateParseHandling.None }); var result = processor.Convert(testData, "ExamplePatient", templateProvider); Assert.True(JToken.DeepEquals(JObject.Parse(_jsonExpectData), JToken.Parse(result))); } diff --git a/src/Microsoft.Health.Fhir.Liquid.Converter.UnitTests/TestData/Expected/ExamplePatient.json b/src/Microsoft.Health.Fhir.Liquid.Converter.UnitTests/TestData/Expected/ExamplePatient.json index 1e65aca0c..bb93fa811 100644 --- a/src/Microsoft.Health.Fhir.Liquid.Converter.UnitTests/TestData/Expected/ExamplePatient.json +++ b/src/Microsoft.Health.Fhir.Liquid.Converter.UnitTests/TestData/Expected/ExamplePatient.json @@ -40,5 +40,6 @@ "managingOrganization": { "reference": "Organization/2.16.840.1.113883.19.5", "display": "Good Health Clinic" - } + }, + "deceasedDateTime": "2023-07-28T01:59:23.388-05:00" } \ No newline at end of file diff --git a/src/Microsoft.Health.Fhir.Liquid.Converter/Parsers/JsonDataParser.cs b/src/Microsoft.Health.Fhir.Liquid.Converter/Parsers/JsonDataParser.cs index c1be2cf78..dea353920 100644 --- a/src/Microsoft.Health.Fhir.Liquid.Converter/Parsers/JsonDataParser.cs +++ b/src/Microsoft.Health.Fhir.Liquid.Converter/Parsers/JsonDataParser.cs @@ -4,15 +4,34 @@ // ------------------------------------------------------------------------------------------------- using System; +using EnsureThat; using Microsoft.Health.Fhir.Liquid.Converter.Exceptions; using Microsoft.Health.Fhir.Liquid.Converter.Extensions; using Microsoft.Health.Fhir.Liquid.Converter.Models; +using Newtonsoft.Json; using Newtonsoft.Json.Linq; namespace Microsoft.Health.Fhir.Liquid.Converter.Parsers { public class JsonDataParser : IDataParser { + private static readonly JsonSerializerSettings DefaultSerializerSettings = new JsonSerializerSettings() + { + DateParseHandling = DateParseHandling.None, + }; + + public JsonDataParser() + : this(DefaultSerializerSettings) + { + } + + public JsonDataParser(JsonSerializerSettings jsonSerializerSettings) + { + JsonSerializerSettings = EnsureArg.IsNotNull(jsonSerializerSettings, nameof(jsonSerializerSettings)); + } + + protected JsonSerializerSettings JsonSerializerSettings { get; private set; } + public object Parse(string json) { if (string.IsNullOrWhiteSpace(json)) @@ -22,7 +41,7 @@ public object Parse(string json) try { - return JToken.Parse(json).ToObject(); + return JsonConvert.DeserializeObject(json, JsonSerializerSettings).ToObject(); } catch (Exception ex) { diff --git a/src/Microsoft.Health.Fhir.Liquid.Converter/Processors/JsonProcessor.cs b/src/Microsoft.Health.Fhir.Liquid.Converter/Processors/JsonProcessor.cs index 18c56ee32..933e6eaef 100644 --- a/src/Microsoft.Health.Fhir.Liquid.Converter/Processors/JsonProcessor.cs +++ b/src/Microsoft.Health.Fhir.Liquid.Converter/Processors/JsonProcessor.cs @@ -7,6 +7,7 @@ using System.Globalization; using System.Threading; using DotLiquid; +using EnsureThat; using Microsoft.Extensions.Logging; using Microsoft.Health.Fhir.Liquid.Converter.Extensions; using Microsoft.Health.Fhir.Liquid.Converter.Models; @@ -20,11 +21,17 @@ namespace Microsoft.Health.Fhir.Liquid.Converter.Processors { public class JsonProcessor : BaseProcessor { - private readonly IDataParser _parser = new JsonDataParser(); + private readonly IDataParser _parser; public JsonProcessor(ProcessorSettings processorSettings, ILogger logger) + : this(processorSettings, new JsonDataParser(), logger) + { + } + + public JsonProcessor(ProcessorSettings processorSettings, IDataParser parser, ILogger logger) : base(processorSettings, logger) { + _parser = EnsureArg.IsNotNull(parser, nameof(parser)); } protected override DataType DataType { get; set; } = DataType.Json; diff --git a/src/Microsoft.Health.Fhir.Liquid.Converter/Processors/JsonToHl7v2Processor.cs b/src/Microsoft.Health.Fhir.Liquid.Converter/Processors/JsonToHl7v2Processor.cs index 8dd82e90d..14e8afacc 100644 --- a/src/Microsoft.Health.Fhir.Liquid.Converter/Processors/JsonToHl7v2Processor.cs +++ b/src/Microsoft.Health.Fhir.Liquid.Converter/Processors/JsonToHl7v2Processor.cs @@ -10,6 +10,7 @@ using System.Text; using System.Threading; using DotLiquid; +using EnsureThat; using Microsoft.Extensions.Logging; using Microsoft.Health.Fhir.Liquid.Converter.Exceptions; using Microsoft.Health.Fhir.Liquid.Converter.Extensions; @@ -18,6 +19,7 @@ using Microsoft.Health.Fhir.Liquid.Converter.Models.Json; using Microsoft.Health.Fhir.Liquid.Converter.Parsers; using Microsoft.Health.MeasurementUtility; +using Newtonsoft.Json; using Newtonsoft.Json.Linq; using NJsonSchema; @@ -25,13 +27,24 @@ namespace Microsoft.Health.Fhir.Liquid.Converter.Processors { public class JsonToHl7v2Processor : BaseProcessor { - private readonly IDataParser _parser = new JsonDataParser(); + private static readonly JsonSerializerSettings DefaultSerializerSettings = new JsonSerializerSettings() + { + DateParseHandling = DateParseHandling.None, + }; + + private readonly IDataParser _parser; private string[] _segmentsWithFieldSeparator = new string[] { "MSH", "BHS", "FHS" }; public JsonToHl7v2Processor(ProcessorSettings processorSettings, ILogger logger) + : this(processorSettings, new JsonDataParser(), logger) + { + } + + public JsonToHl7v2Processor(ProcessorSettings processorSettings, IDataParser parser, ILogger logger) : base(processorSettings, logger) { + _parser = EnsureArg.IsNotNull(parser, nameof(parser)); } protected override string InternalConvert(string data, string rootTemplate, ITemplateProvider templateProvider, TraceInfo traceInfo = null) @@ -45,7 +58,7 @@ protected override string InternalConvert(string data, string rootTemplate, ITem var result = InternalConvertFromObject(jsonData, rootTemplate, templateProvider, traceInfo); - var hl7Message = GenerateHL7Message(JObject.Parse(result)); + var hl7Message = GenerateHL7Message(ConvertToJObject(result)); var hl7String = ConvertHl7MessageToString(hl7Message); @@ -56,7 +69,7 @@ public string Convert(JObject data, string rootTemplate, ITemplateProvider templ { var jsonData = data.ToObject(); var result = InternalConvertFromObject(jsonData, rootTemplate, templateProvider, traceInfo); - var hl7Message = GenerateHL7Message(JObject.Parse(result)); + var hl7Message = GenerateHL7Message(ConvertToJObject(result)); var hl7String = ConvertHl7MessageToString(hl7Message); return hl7String; @@ -239,5 +252,10 @@ protected override void CreateTraceInfo(object data, Context context, TraceInfo jsonTraceInfo.ValidateSchemas = jsonContext.ValidateSchemas; } } + + private JObject ConvertToJObject(string input) + { + return JsonConvert.DeserializeObject(input, DefaultSerializerSettings); + } } }