Skip to content

Commit

Permalink
Merge pull request #562 from microsoft/personal/rogordon/fixDateDeser…
Browse files Browse the repository at this point in the history
…ializationOnJsonConversion

Treat datetime values as Strings during json deserialization in Json…
  • Loading branch information
rogordon01 authored Jun 3, 2024
2 parents e98864b + 9a4205e commit 253307f
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 11 deletions.
3 changes: 2 additions & 1 deletion data/SampleData/Json/ExamplePatient.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
"1234-5679"
],
"Gender": "M",
"DOB": "20010110"
"DOB": "20010110",
"TimeOfDeath": "2023-07-28T01:59:23.388-05:00"
}
3 changes: 2 additions & 1 deletion data/Templates/Json/ExamplePatient.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,6 @@
"managingOrganization": {
"reference": "Organization/2.16.840.1.113883.19.5",
"display": "Good Health Clinic"
}
},
"deceasedDateTime": "{{ msg.TimeOfDeath }}"
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@

using System;
using System.Collections.Generic;
using System.Data.Common;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading;
using DotLiquid;
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;

Expand Down Expand Up @@ -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<JObject>(_jsonTestData, new JsonSerializerSettings() { DateParseHandling = DateParseHandling.None });
var result = processor.Convert(testData, "ExamplePatient", templateProvider);
Assert.True(JToken.DeepEquals(JObject.Parse(_jsonExpectData), JToken.Parse(result)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -22,7 +41,7 @@ public object Parse(string json)

try
{
return JToken.Parse(json).ToObject();
return JsonConvert.DeserializeObject<JToken>(json, JsonSerializerSettings).ToObject();
}
catch (Exception ex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<JsonProcessor> logger)
: this(processorSettings, new JsonDataParser(), logger)
{
}

public JsonProcessor(ProcessorSettings processorSettings, IDataParser parser, ILogger<JsonProcessor> logger)
: base(processorSettings, logger)
{
_parser = EnsureArg.IsNotNull(parser, nameof(parser));
}

protected override DataType DataType { get; set; } = DataType.Json;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,20 +19,32 @@
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;

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<JsonToHl7v2Processor> logger)
: this(processorSettings, new JsonDataParser(), logger)
{
}

public JsonToHl7v2Processor(ProcessorSettings processorSettings, IDataParser parser, ILogger<JsonToHl7v2Processor> logger)
: base(processorSettings, logger)
{
_parser = EnsureArg.IsNotNull(parser, nameof(parser));
}

protected override string InternalConvert(string data, string rootTemplate, ITemplateProvider templateProvider, TraceInfo traceInfo = null)
Expand All @@ -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);

Expand All @@ -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;
Expand Down Expand Up @@ -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<JObject>(input, DefaultSerializerSettings);
}
}
}

0 comments on commit 253307f

Please sign in to comment.