Recent

Author Topic: Writing Float values to JSON  (Read 442 times)

mirce.vladimirov

  • Sr. Member
  • ****
  • Posts: 269
Writing Float values to JSON
« on: April 08, 2026, 02:28:08 pm »
I am doind something wrong but i cant find out what it is.

This code :
Code: Pascal  [Select][+][-]
  1.     while not qry1.EOF do begin
  2.       jsonItem := TJSONObject.Create;
  3.       jsonItem.Add('LineNo', qry1.FieldByName('line').AsInteger);
  4.       jsonItem.Add('Qty',  qry1.FieldByName('qty').AsFloat);
  5.       jsonItem.Add('Price', qry1.FieldByName('price').AsFloat);
  6.       jsonItems.Add(jsonItem);
  7.       qry1.Next;
  8.     end;
  9.     jsonDoc.Add('Items', jsonItems);

   
Gives me scientific notations, which i do not want, my data are float numbers with max 4 decimal places and i want them to be exported to json as such.

And when i call it as a string it encloses it in double quotes, as it should because i called it as a string :
Code: Pascal  [Select][+][-]
  1. jsonItem.Add('Price', qry1.FieldByName('price').asstring);

i just want it exported as
Code: Pascal  [Select][+][-]
  1. {"Price": 123.01}
and not as
Code: Pascal  [Select][+][-]
  1. {"Price":"123.01"}

Khrys

  • Sr. Member
  • ****
  • Posts: 411
Re: Writing Float values to JSON
« Reply #1 on: April 08, 2026, 02:48:22 pm »
That's just the default stringification behavior of  TJSONFloatNumber.
You can customize it by creating a subclass of  TJSONData  with the desired behavior and then registering it using  SetJSONInstanceType.

I've been using a variation of the following for a while now:

Code: Pascal  [Select][+][-]
  1. const
  2.   JSON_NUMBER_FORMAT: TFormatSettings = ({via initialization});
  3.  
  4. type
  5.   TJSONDecimalNumber = class(TJSONFloatNumber)
  6.   protected
  7.     function GetAsString(): TJSONStringType; override;
  8.   end;
  9.  
  10. function TJSONDecimalNumber.GetAsString(): TJSONStringType;
  11. begin
  12.   Result := FormatFloat('0.0###', GetAsFloat(), JSON_NUMBER_FORMAT);
  13. end;
  14.  
  15. initialization
  16. begin
  17.   JSON_NUMBER_FORMAT := DefaultFormatSettings;
  18.   JSON_NUMBER_FORMAT.DecimalSeparator := '.';
  19.   SetJSONInstanceType(jitNumberFloat, TJSONDecimalNumber);
  20. end;

Thaddy

  • Hero Member
  • *****
  • Posts: 18918
  • Glad to be alive.
Re: Writing Float values to JSON
« Reply #2 on: April 08, 2026, 03:22:03 pm »
In trunk, you could also use a custom attribute for that, where the attribute code handles how to format the float values.
If you want I can write an example for that (if you use trunk!).
I have recently added a code example to the custom attributes wiki entry that manipulates json:
https://wiki.freepascal.org/Custom_Attributes#Complete_example_2

In this case, you want to divert from the standard json handling of floats and that is a particularly good case for using a - maybe even parameterized - custom attribute.
You can integrate Khrys's code for that almost ad verbatum.
[edit]
I will do that anyway, but given some time.
« Last Edit: April 08, 2026, 03:47:17 pm by Thaddy »
Recovered from removal of tumor in tongue following tongue reconstruction with a part from my leg.

 

TinyPortal © 2005-2018