#1 2021-10-26 10:02:43

sakura
Member
From: Germany
Registered: 2018-02-21
Posts: 239
Website

Json Float handling not easy to understand

Paste: https://pastebin.com/SfP4GSJQ

I have a simple object with two extended values.

One, I fill with "Pi", the other with "3.1415".
Next, I convert the object to json, using "ObjectToJson"
The resulting Json, is, as expected:

{"FloatLong":3.14159265358979,"FloatShort":3.1415}

Adding a value to that DocVaraint, named "Pi" with Delphis Pi as content and calling ToJson I get:

{"FloatLong":"3.14159265358979","FloatShort":3.1415,"Pi":3.14159265358979}

I am wondering, why if FloatLong now quoted as string?
FloatShort is output as expected.
Pi is output as expected.

Regards,
Daniel

P.S.: mORMot1 and mORMot2 behave the same.

Offline

#2 2021-10-26 10:44:04

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

Re: Json Float handling not easy to understand

Did you search the forum and the documentation?
See e.g. https://synopse.info/forum/viewtopic.php?id=4083

This is the expected and documented behavior: by default floats are converted into strings to preserve the value.
Please read https://synopse.info/files/html/Synopse … l#TITL_194

Offline

#3 2021-10-26 10:50:57

macfly
Member
From: Brasil
Registered: 2016-08-20
Posts: 374

Re: Json Float handling not easy to understand

From 4 decimal places double is quoted as a string.

What I don't understand in your example is why "pi" wasn't quoted as a string.

Expected would be:

{"FloatLong":"3.14159265358979","FloatShort":3.1415,"Pi":"3.14159265358979"}

Offline

#4 2021-10-26 10:59:41

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

Re: Json Float handling not easy to understand

Because ObjectToJson() is not TDocVariantData, e.g. in the way it holds its data.

ObjectToJson() holds its value as a float, so there is no conversion issue, and we can assume that it is OK to loose some digits.

TDocVariantData is a way to store and handle JSON, so by default we don't allow storing some JSON values which may be truncated.
For instance, we defined a dedicated 128-bit float type to properly work with BSON / MongoDB - which handles it properly. If we have used floats here, we would only use 64-bit of precision, not 128-bit as stored in MongoDB.

I know this is confusing, sorry for it.
But we are a bit paranoid about loosing data precision.

Offline

#5 2021-10-26 11:06:20

sakura
Member
From: Germany
Registered: 2018-02-21
Posts: 239
Website

Re: Json Float handling not easy to understand

ab wrote:

Did you search the forum and the documentation?

Both, but did not find it.

The forum search options are sometimes too limited, to find what you are looking for, without having the right keywords. And after five minutes I gave up - mostly due to the 60 second wait time between the searches. That is too long for logged in users ;-)

And the doc is so darn expansive, thanks for that, that sometimes it's just hard to find it.

But yes, I do always try both places BEFORE posting.

Regards,
Daniel

Offline

#6 2021-10-27 10:49:40

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

Re: Json Float handling not easy to understand

It was in the doc FAQ by the way. wink

Offline

#7 2021-10-27 16:46:05

macfly
Member
From: Brasil
Registered: 2016-08-20
Posts: 374

Re: Json Float handling not easy to understand

This is something to pay attention.

  doc.InitObject(['value', 1.012345678]);
  FileFromString(doc.ToJson(), 'test.json');

  doc.InitJson(StringFromFile('test.json'));
  writeln(FloatToStr(doc.D['value'])); // print 0 because 'value' are a string now      

After having problems with this conversion, when saving and loading a json, I'm just using string for the doubles.

Last edited by macfly (2021-10-27 20:29:09)

Offline

#8 2021-10-27 18:50:34

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

Re: Json Float handling not easy to understand

The easiest is to write:

doc.InitObject(['value',1.012345678], JSON_FAST_FLOAT);
...
doc.InitJson(StringFromFile('test.json'), JSON_FAST_FLOAT);
or
doc.InitJson(StringFromFile('test.json'), mFastFloat);

I have introduced a new TDocVariantData.InitJsonFromFile method.

Offline

#9 2021-10-28 13:44:21

China Lee
Member
Registered: 2021-10-28
Posts: 4

Re: Json Float handling not easy to understand

learning

Offline

#10 2021-10-28 19:36:02

macfly
Member
From: Brasil
Registered: 2016-08-20
Posts: 374

Re: Json Float handling not easy to understand

ab wrote:

I have introduced a new TDocVariantData.InitJsonFromFile method.

Very good, thanks.

Offline

Board footer

Powered by FluxBB