I deal with a lot of numerical (real) data who’s values can have very different magnitudes and numbers of significant figures.
I wanted to see how the Xojo JSON options would handled them.
Bottom line: Given that JSON String size often matters, and Xojo is a strongly typed language it should be better.
First lets take a look at how Xojo.Core.GenerateJSON does
For for the first test I used:
D as Double = 1.0/3.0 And got this:
{“Xojo.JSON Double”:0.33333333333333331483}
Next I tried:
D as Double = 1e100*(1.0/3.0) And got:
{“Xojo.JSON Double”:3.3333333333333332245e+99}
A few extra insignificant digits (should only be 16 or 17) but not too bad, and it did switch to scientific notation when it needed to
But then I went to singles:
S as Single = 1.0/3.0 And that gave:
{“Xojo.JSON Single”:0.3333333432674407959}
S as Single = 1e35*(1.0/3.0) Gave:
{“Xojo.JSON Single”:3.3333333046695906274e+34}
For singles there are WAY too many digits so potentially a lot of wasted space!!!
A single only has about 8 digit meaningful significance!!! As i said Xojo is strongly typed to it knows what type of real umber it is and how many significant digits may sense!!!
And unlike JSONItem as far as I can see you cannot specify numeric format… which can be important in situations where you don’t need all this significant figures!
In any case intelligent defaults are important to able to handle the general case efficiently!!!
Speaking of JSONItem it is a much worst offender (though you can define the numeric format to use - but as I said smart defaults are important)
D as Double = 1.0/3.0 Gave:
{“JI_DefaultDouble”:0.333333333333333}
Not too bad but, it is 1 or 2 significant figures TOO SHORT. That can matter in some things!
D as Double = 1e100*(1.0/3.0) gave:
{“JI_DefaultDouble”:3333333333333333224453896013722304246165110619355184909726539264904319486405759542029132894851563520.0}
Now we are into the ridiculous in terms of digits…
It should have switched to scientific notation and used the correct number of sig figs for a double!
Same thing for singles:
S as Single = 1e35*(1.0/3.0) Gave:
{“JI_DefaultSingle”:33333333046695906273818073259573248.0}
One heck of a lot more than 8 sig figs!!!
Yes we can set our own format but when one writing what is supposed to general use code most won’t even thing about that!
JSONitem_MTC behaves the same way but can be modified as we have the source code
- Karen