Replies: 1 comment 1 reply
-
About the problem with the Newtonsoft implementation, I figured out that I can provide an private void TestJson1SerializerWith(Workspace workspace)
{
var modelInspector = new ModelInspector(Hl7.Fhir.Specification.FhirRelease.R4);
modelInspector.ImportType(typeof(FhirWorkspace));
var serializer = new CommonFhirJsonSerializer(modelInspector);
var jsonContent = serializer.SerializeToString(new FhirWorkspace(workspace));
var parser = new BaseFhirParser(modelInspector);
string rootName = modelInspector.GetFhirTypeNameForType(typeof(FhirWorkspace));
ISourceNode node = FhirJsonNode.Parse(jsonContent, rootName);
var fhirWorkspace = parser.Parse<FhirWorkspace>(node);
Assert.IsNotNull(fhirWorkspace);
var workspace2 = fhirWorkspace.Original;
Assert.AreEqual(workspace.Name, workspace2.Name);
Assert.AreEqual(workspace.Active, workspace2.Active);
} About my problems with System.Text.Json serializer, I also debugged deeper into it. And found this issue #2235, which means I need to validate my resource first myself. Never thought of that ... for performance reasons, hm ok ... but separating the code does not make it faster, only when you look at it in a specific angle. And what about bundles, I mean when the data structure resembles a hierarchy. Here a validation which only works for the tests, more worse than anything else. Honestly I do not like this solution and will only just workaround these problems for me now. // Base.Validate() will not work, because it is mostly not implemented e.g. Bundle
// this means I need do something like this?
private void Validate(Resource resource)
{
Stack<Base> stack = new Stack<Base>(new[] { resource });
while (stack.Count > 0)
{
var current = stack.Pop();
bool hasValue = false;
foreach (var child in current.NamedChildren)
{
if (child.Value == null)
{
throw new InvalidOperationException("No nulls");
}
if (child.Value is FhirString value && string.IsNullOrEmpty(value.Value))
{
throw new InvalidOperationException("No null or empty strings");
}
if (child.Value is BackboneElement res)
{
stack.Push(res);
}
if (child.Value is Resource res2)
{
stack.Push(res2);
}
hasValue = true;
}
if (!hasValue)
{
throw new InvalidOperationException("No empty objects");
}
}
} |
Beta Was this translation helpful? Give feedback.
-
Help! Been stuck a while now. Tried different approaches to make it work, but so far nothing worked.
Basically I am trying to serialize and deserialize an custom resource. Tried the System.Text.Json and Newtonsoft approach.
Here is my code, if you like to try it out: Create a new MSTest .NET6 project, add the Hl7.Fhir.R4 (v5.3.0) library and paste the following code. I added the results over the tests, where 5 of 8 are failing.
So my problems are:
Beta Was this translation helpful? Give feedback.
All reactions