FirelyTeam / firely-net-sdk

The official Firely .NET SDK for HL7 FHIR
Other
830 stars 345 forks source link

Downloaded JSON loses precision from uploaded XML #1697

Closed wardweistra closed 7 months ago

wardweistra commented 3 years ago

Describe the bug

Re: SIM-1609, uploading an XML file with <value value="10.10" /> to Simplifier will lead to JSON representation (in both JSON tab and downloaded JSON file) of "value": 10.1,

Which is a loss in precision which, as I understand from the dragon in the spec, shouldn't happen. See for example https://simplifier.net/snippet/wardweistra/27, which was created from XML input.

To Reproduce Steps to reproduce the behavior:

  1. Upload the XML from https://simplifier.net/snippet/wardweistra/27 to Simplifier.net (snippet or project).
  2. Download the JSON
  3. View the precision of the 'value'

Expected behavior Precision is maintained as original 10.10

Screenshots See https://simplifier.net/snippet/wardweistra/27

Version used:

wardweistra commented 3 years ago

Discussed in #firely-net-sdk from here: https://firelyteam.slack.com/archives/C011XD1TKL0/p1618911374003800

Same occurs when uploading JSON file with 10.10 -> Downloaded file will have 10.1

@ewoutkramer's first comments:

my first suspicion is the json serializer then - I am betting that internally we have it correctly

mmsmits commented 7 months ago

Refinement: We should create unit tests for both serializers to check whether this occurs, and why.

Kasdejong commented 7 months ago

Hi @wardweistra, we tried to reproduce the issue in the SDK but failed to do so. This is our test:

[TestMethod]
      public void RetainsPrecision()
      {
          var xml = """
                    <Questionnaire xmlns="http://hl7.org/fhir">
                      <id value="3141"/>   
                      <url value="http://hl7.org/fhir/Questionnaire/3141"/>   
                      <name value="CancerQualityForumQuestionnaire2012"/>     
                      <status value="draft"/>
                      <item> 
                        <linkId value="1.1.1"/> 
                        <code> 
                          <system value="http://example.org/system/code/sections"/> 
                          <code value="CARDIAL"/> 
                        </code> 
                        <type value="group"/> 
                        <enableWhen> 
                          <question value="foo?"/>
                          <answerQuantity> 
                            <value value="1.10"/>
                            <system value="http://unitsofmeasure.org"/>
                            <code value="mm[Hg]"/>
                          </answerQuantity> 
                        </enableWhen> 
                      </item>
                    </Questionnaire> 
                    """;

          var node = FhirXmlNode.Parse(xml).ToTypedElement(ModelInspector.ForAssembly(typeof(TestQuestionnaire).Assembly));

          var json = node.ToJson(); // has correct precision

          var engine = FhirSerializationEngineFactory.Strict(ModelInspector.ForAssembly(typeof(TestQuestionnaire).Assembly));

          var poco = engine.DeserializeFromXml(xml);

          var json2 = engine.SerializeToJson(poco!); // has correct precision
      }

We need a few more details on how exactly the simplifier implementation works before we can more accurately test and fix this issue

wardweistra commented 7 months ago

Thanks, will investigate our code and see if we can tell more about our implementation or if the error is in Simplifier code.

PaulDeac commented 7 months ago

In Simplifier we are still using Newtonsoft.Json. We fixed it through a configuration setting:

JsonConvert.DefaultSettings = () => new JsonSerializerSettings
{
    FloatParseHandling = FloatParseHandling.Decimal
};

We are planning to switch to System.Text.Json which is what I think the SDK is using, and that would also fix it. I think you can close this issue, the problem was in Simplifier.