Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON Serializing floating point properties. #704

Closed
LairdTurner opened this issue Oct 15, 2023 · 8 comments
Closed

JSON Serializing floating point properties. #704

LairdTurner opened this issue Oct 15, 2023 · 8 comments
Assignees
Labels
cannot-reproduce The issue is not reproducible question

Comments

@LairdTurner
Copy link

Hi!

I have vom JSon Objects with floating point number values in it.
For example:
{
"name": "Mr. Smith",
"value": 1.23
}

But evertime I want to parse the object via TJsonDataObjects I get the integer part backe, so 1
and not 1.23. This happens everytime I convert a json string to a jsonobject with a floating point number in it.

Any idea why?

Thanks!

@danieleteti
Copy link
Owner

Hi, can you show the code please?

@LairdTurner
Copy link
Author

Hi!
Sure...
s := RESTResponse1.Content;
In the string 's' the floating point number is still in there, but if I parse it afterwards with
Obj := TJsonBaseObject.ParseUtf8(s) AS TJsonObject;
and ask for the floating point of the appropriate property
workingHours := Obj['data'].Items[I].F['workingHours'];
I only get for example 2 instead of 2,25?

@LairdTurner
Copy link
Author

Any idea!?

@LairdTurner
Copy link
Author

Very strange... I ran the compilation on a normal Windows 11. There it works, but on my Mac M2 with Parallels it does not work. It must have something to do with the operating system or Parallels. Any idea?

@danieleteti
Copy link
Owner

Can you post the JSON contained in s?

@LairdTurner
Copy link
Author

LairdTurner commented Oct 16, 2023

'{"error":false,"message":"","data":[{"id":101,"personId":4,"date":"2023-10-15T00:00:00Z","allowanceCatalogId":77,"allowanceSurchargeNo":"63","heavy":5,"workingHours":2.5,"createdAt":"2023-10-16T11:30:11Z","updatedAt":"2023-10-16T11:30:11Z"},{"id":102,"personId":4,"date":"2023-10-15T00:00:00Z","allowanceCatalogId":47,"allowanceSurchargeNo":"39","heavy":6,"workingHours":2.5,"createdAt":"2023-10-16T11:32:12Z","updatedAt":"2023-10-16T11:32:12Z"},{"id":105,"personId":4,"date":"2023-10-15T00:00:00Z","allowanceCatalogId":10,"allowanceSurchargeNo":"10","dirt":9,"workingHours":2.25,"createdAt":"2023-10-16T15:00:55Z","updatedAt":"2023-10-16T15:00:55Z"}]}'

@LairdTurner
Copy link
Author

It really is a phenomenon. I have compiled the application for OSX ARM 64 and it works without problems. It must have something to do with Windows 11 ARM?

@danieleteti
Copy link
Owner

I don't have a Windows 11 ARM machine, however the following code works as expected in Windows 11 with Win32 and with Win64 target.

uses
  JsonDataObjects;

{$R *.dfm}

procedure TForm13.Button1Click(Sender: TObject);
begin
  var s :=  '{' +
            '    "error": false,' +
            '    "message": "",' +
            '    "data": [{' +
            '            "id": 101,' +
            '            "personId": 4,' +
            '            "date": "2023-10-15T00:00:00Z",' +
            '            "allowanceCatalogId": 77,' +
            '            "allowanceSurchargeNo": "63",' +
            '            "heavy": 5,' +
            '            "workingHours": 2.5,' +
            '            "createdAt": "2023-10-16T11:30:11Z",' +
            '            "updatedAt": "2023-10-16T11:30:11Z"' +
            '        }, {' +
            '            "id": 102,' +
            '            "personId": 4,' +
            '            "date": "2023-10-15T00:00:00Z",' +
            '            "allowanceCatalogId": 47,' +
            '            "allowanceSurchargeNo": "39",' +
            '            "heavy": 6,' +
            '            "workingHours": 2.5,' +
            '            "createdAt": "2023-10-16T11:32:12Z",' +
            '            "updatedAt": "2023-10-16T11:32:12Z"' +
            '        }, {' +
            '            "id": 105,' +
            '            "personId": 4,' +
            '            "date": "2023-10-15T00:00:00Z",' +
            '            "allowanceCatalogId": 10,' +
            '            "allowanceSurchargeNo": "10",' +
            '            "dirt": 9,' +
            '            "workingHours": 2.25,' +
            '            "createdAt": "2023-10-16T15:00:55Z",' +
            '            "updatedAt": "2023-10-16T15:00:55Z"' +
            '        }' +
            '    ]' +
            '}';

  var lObj := TJsonBaseObject.Parse(s) AS TJsonObject;
  try
    var workingHours := lObj['data'].Items[0].F['workingHours'];
    ShowMessage(FloatToStr(workingHours)); // shows 2.5
  finally
    lObj.Free;
  end;
end;

Do you have a different result?

@danieleteti danieleteti self-assigned this Nov 5, 2023
@danieleteti danieleteti added the cannot-reproduce The issue is not reproducible label Nov 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cannot-reproduce The issue is not reproducible question
Projects
None yet
Development

No branches or pull requests

2 participants