#1 2021-10-03 01:25:02

mvg7
Member
Registered: 2021-10-03
Posts: 2

Wrong floating point json parsing?

Hi!
this code

var
  obj:    Variant;
  z:      double;
begin
  obj := _JsonFast('{"x": 8.1E-2}');
  z1 := obj.x;
  Memo1.Lines.Add(Format('x: %f', [z]));
end.

produces output "x: 8.10" instead of 0.081, why?

Last edited by mvg7 (2021-10-03 01:35:47)

Offline

#2 2021-10-03 06:46:14

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,182
Website

Re: Wrong floating point json parsing?

You need to enable float process.
Floats may not be consistent when converted back and forth.

Use _JsonFastFloat() as documented.

Offline

#3 2021-10-03 09:58:42

mvg7
Member
Registered: 2021-10-03
Posts: 2

Re: Wrong floating point json parsing?

ab wrote:

You need to enable float process.
Floats may not be consistent when converted back and forth.

Use _JsonFastFloat() as documented.

Thanks, I dont see a _JsonFastFloat anywhere even if searching the mORMot-master.zip sources

Offline

#4 2021-10-04 08:10:23

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,182
Website

Re: Wrong floating point json parsing?

You should have read the documentation/comments to the end:

...
// - will handle only currency for floating point values to avoid precision
// loss: use _JsonFastFloat() instead if you want to support double values
function _JsonFast(const Json: RawUtf8): variant;

wink

Offline

#5 2021-10-04 11:07:48

mpv
Member
From: Ukraine
Registered: 2012-03-24
Posts: 1,534
Website

Re: Wrong floating point json parsing?

It's here (in mORMot2) - https://github.com/synopse/mORMot2/blob … .pas#L2197
There is no analog in mORMot1

Last edited by mpv (2021-10-04 11:09:27)

Offline

#6 2021-10-05 10:05:53

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,182
Website

Re: Wrong floating point json parsing?

You are right about the source code.
But this is clearly stated in the documentation:
https://synopse.info/files/html/Synopse … l#TITL_194

I have made explicit that dvoAllowDoubleValue option is not included by default in mORMot 1 comments too. It was confusing.
- also introducing _JsonFastFloat() wrapper.
https://synopse.info/fossil/info/7de179b819

Offline

Board footer

Powered by FluxBB