Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions src/Hl7.Fhir.Base/Serialization/BaseFhirJsonPocoSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Hl7.Fhir.Specification;
using Hl7.Fhir.Utility;
using System;
using System.Buffers;
using System.Collections;
using System.Collections.Generic;
using System.IO;
Expand Down Expand Up @@ -271,12 +272,35 @@ private void serializeFhirPrimitive(string elementName, PrimitiveType value, Utf

if (value.HasElements)
{
// Write a property with '_elementName'
writer.WritePropertyName("_" + elementName);
serializeInternal(value, writer, skipValue: true);
deferSerializeForFilter(elementName, value, writer);
}
}

private void deferSerializeForFilter(string elementName, PrimitiveType value, Utf8JsonWriter writer)
{
#if NETSTANDARD2_0
var data = new MemoryStream();
#else
var data = new ArrayBufferWriter<byte>();
#endif
using (var deferredWriter = new Utf8JsonWriter(data, writer.Options))
{
serializeInternal(value, deferredWriter, skipValue: true);
}

#if NETSTANDARD2_0
var bytes = data.ToArray();
#else
var bytes = data.WrittenSpan;
#endif
if(bytes.Length < 3) return;

// Write a property with '_elementName'
writer.WritePropertyName("_" + elementName);
// write the deferred value
writer.WriteRawValue(bytes, skipInputValidation: true);
}

/// <summary>
/// Serialize a primitive .NET value that may occur in the POCOs into Json.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Hl7.Fhir.Model;
using Hl7.Fhir.Serialization;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
Expand All @@ -15,6 +16,20 @@ namespace Hl7.Fhir.Support.Poco.Tests
[TestClass]
public class SummaryFilterIntegrationTests
{
[TestMethod]
public void SummaryHasNoEmptyObject()
{
var patient = new Patient
{
BirthDateElement = new Date("1990")
{
Extension = [new Extension("birthTime", new Instant(DateTimeOffset.Now))]
}
};
var (original, summarized) = runSummarize(patient, SerializationFilter.ForSummary());
summarized.Extension.Should().BeEmpty();
}

[TestMethod]
public void Basics()
{
Expand Down Expand Up @@ -152,14 +167,12 @@ public void SummaryCount()
// check if result contains the link
traverse(summarized).Should().ContainKey("link");
}

private (T full, T summarized) runSummarize<T>(string filename, SerializationFilter filter) where T : Resource


private (T full, T summarized) runSummarize<T>(T full, SerializationFilter filter) where T : Resource
{
var fullXml = File.ReadAllText(Path.Combine("TestData", filename));
var full = FhirXmlNode.Parse(fullXml).ToPoco<T>(ModelInspector.ForType<T>());

var options = new JsonSerializerOptions()
.ForFhir(typeof(Patient).Assembly, new FhirJsonPocoSerializerSettings { SummaryFilter = filter })
.ForFhir(typeof(Patient).Assembly, new FhirJsonPocoSerializerSettings() { SummaryFilter = filter })
.Pretty();
string summarizedJson = JsonSerializer.Serialize(full, options);

Expand All @@ -168,6 +181,14 @@ public void SummaryCount()
return (full, summarized);
}

private (T full, T summarized) runSummarize<T>(string filename, SerializationFilter filter) where T : Resource
{
var fullXml = File.ReadAllText(Path.Combine("TestData", filename));
var full = FhirXmlNode.Parse(fullXml).ToPoco<T>();

return runSummarize(full, filter);
}

private static IEnumerable<KeyValuePair<string, object>> traverse(Base x)
{
return childrenAndMe(KeyValuePair.Create("(root)", (object)x));
Expand Down