You are not logged in.
Pages: 1
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
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
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
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
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
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
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
learning
Offline
I have introduced a new TDocVariantData.InitJsonFromFile method.
Very good, thanks.
Offline
Pages: 1