Skip to content

Commit

Permalink
Fixup FhirResourceConverter to pretty print on multiple platforms.
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeShook committed Oct 13, 2024
1 parent 8c0d1a0 commit 1becca1
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
7 changes: 5 additions & 2 deletions Udap.CdsHooks.Model/FhirResourceConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using Hl7.Fhir.Model;
using Hl7.Fhir.Serialization;

Expand Down Expand Up @@ -65,6 +66,8 @@ public override void Write(Utf8JsonWriter writer, Dictionary<string, Resource> v
{
writer.WritePropertyName(kvp.Key);
var resourceJson = new FhirJsonSerializer(serializerSettings).SerializeToString(kvp.Value);

// Console.WriteLine(resourceJson);
var indentedResourceJson = IndentJson(resourceJson, 2);
writer.WriteRawValue(indentedResourceJson);
}
Expand All @@ -76,12 +79,12 @@ public override void Write(Utf8JsonWriter writer, Dictionary<string, Resource> v
private string IndentJson(string json, int additionalIndentationLevels)
{
var indentedJson = new StringBuilder();
var lines = json.Split('\n');
var lines = Regex.Split(json, @"\r\n|\r|\n");
var additionalIndentation = new string(' ', additionalIndentationLevels * 2); // Assuming 2 spaces per level

foreach (var line in lines)
{
indentedJson.Append(additionalIndentation + line);
indentedJson.AppendLine(additionalIndentation + line);
}

return indentedJson.ToString();
Expand Down
3 changes: 1 addition & 2 deletions _tests/Udap.Common.Tests/CdsHooks/Experimental.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
// */
#endregion

using Hl7.Fhir.Model;
using Hl7.Fhir.Serialization;
using System.Text.Json;
using System.Text.Json.Serialization;
using Udap.CdsHooks.Model;
Expand Down Expand Up @@ -41,5 +39,6 @@ public void TestCdsRequestDeserialize()
var serializedCdsRequest = JsonSerializer.Serialize(cdsRequest, options);

_testOutputHelper.WriteLine(serializedCdsRequest);

}
}

0 comments on commit 1becca1

Please sign in to comment.