AL icon indicating copy to clipboard operation
AL copied to clipboard

Serializing and Deserializing JsonObject rounds internal decimal values

Open KoFrank opened this issue 4 years ago • 2 comments

1. Describe the bug When working with decimal values with a high number of decimal places we noticed that writing a JsonObject with those values to a Text and then reading it from that text has rounded the decimal value internally. When we put the Formatted decimal as text into the JsonObject and then evaluate that text later we were able to get a higher precision.

2. To Reproduce Steps to reproduce the behavior: (We boiled it down to 1 Test function)

                trigger OnAction()
                var
                    decimal1: Decimal;
                    decimal2: Decimal;
                    text1: Text;
                    text2: Text;
                    obj1: JsonObject;
                    obj2: JsonObject;
                    obj3: JsonObject;
                    obj4: JsonObject;
                    value: JsonValue;
                    token: JsonToken;
                begin
                    decimal1 := 0.123456789011111111111111;
                   //Test with Format and Evaluate
                    text1 := Format(decimal1);
                    Evaluate(decimal2, text1);

                    if decimal1 = decimal2 then begin

                        if Confirm('Equal: Format & Evaluate') then begin

                            decimal1 := decimal1;
                        end;
                    end else begin
                        if Confirm('Fail1') then begin

                            decimal1 := decimal1;
                        end;
                    end;

                   //Testing jsonObject WriteTo and ReadFrom with a directly set decimal value
                    obj1.Add('Test', decimal1);
                    obj1.WriteTo(text2);

                    obj2.ReadFrom(text2);
                    obj2.Get('Test', token);
                    value := token.AsValue();

                    decimal2 := value.AsDecimal();

                    if decimal1 = decimal2 then begin
                        if Confirm('Equal: WriteTo and ReadFrom JSON') then begin
                            decimal1 := decimal1;
                        end
                    end else begin
                        if Confirm('Fail2') then begin
                            decimal1 := decimal1;
                        end;
                    end;

                   //Testing jsonObject WriteTo and ReadFrom with a text formatted decimal value
                    text1 := Format(decimal1);

                    obj3.Add('Test', text1);
                    obj3.WriteTo(text2);

                    obj4.ReadFrom(text2);
                    obj4.Get('Test', token);
                    value := token.AsValue();

                    text1 := value.AsText();
                    Evaluate(decimal2, text1);


                    if decimal1 = decimal2 then begin
                        if Confirm('Equal: WriteTo and ReadFrom JSON with text decimals') then begin
                            decimal1 := decimal1;
                        end
                    end else begin
                        if Confirm('Fail3') then begin
                            decimal1 := decimal1;
                        end;
                    end;
                end;

When you run the code the first comparison and last comparison will show a success, but the middle one will fail, as decimal1 and decimal2 (read from the deserialized Json) will differ.

3. Expected behavior The excpectation is that using JsonObject WriteTo and ReadFrom with a decimal value inside the object keeps the same level of precision as the original value. We are aware that decimal variables cannot store infinite precision, however at least the result should match the initial value.

4. Actual behavior The decimal value read from a deserialized jsonObject does not equal the same value used to add it to the jsonObject.

5. Versions:

  • AL Language: v10.0.687650
  • Visual Studio Code: 1.73
  • Business Central: 21.0.46256.46853-w1
  • List of Visual Studio Code extensions that you have installed:

Final Checklist

Please remember to do the following:

  • [ ] Search the issue repository to ensure you are reporting a new issue

  • [ ] Reproduce the issue after disabling all extensions except the AL Language extension

  • [ ] Simplify your code around the issue to better isolate the problem

KoFrank avatar Nov 10 '22 10:11 KoFrank

The fix for this issue has been checked in to the master branch. It will be available in the bcinsider.azurecr.io/bcsandbox-master Docker image starting from platform build number 22.0.50135.0 and VS Code Extension Version .

If you don’t have access to these images you need to become part of the Ready2Go program: aka.ms/readytogo

For more details on code branches and docker images please read: https://blogs.msdn.microsoft.com/nav/2018/05/03/al-developer-previews-multiple-releases-and-github/ https://freddysblog.com/2020/06/25/working-with-artifacts/

JesperSchulz avatar Dec 07 '22 07:12 JesperSchulz

Perhaps related or a counter-example: https://www.yammer.com/dynamicsnavdev/#/Threads/show?threadId=2166408112504832

dzzzb avatar Mar 06 '23 16:03 dzzzb