Yes, I remembered that, but Kem’s tests are ancient and new benchmarks as of Xojo 2021 would be very helpful.
I’m mostly interested in the speed of JSON lookups through a child or two vs making a dictionary of the child once, then looking up in the child dictionary in my loops. The former makes code cleaner, but I suspect the latter is much faster.
That’s the main reason I haven’t bothered trying it.
Looks like I have in about a dozen projects, all originally dating back years, so at the time I would have used it because of the performance advantages. If I had to refactor to something else, it would not be a big deal to me.
New benchmarks comparing the various JSON and Dictionary alternatives since Xojo 2021 would be enlightening though…
So I am about to switch to using a new JSON library which can do more.
The new class should cover > 90% of both old JSONMBS and JSONItem to be a drop in for both.
And it gets a few new features like iterators.
The Convert function will stay of course for @David_Cox.
You may be thinking of The ZAZ: JSON Performance Tests Round 2 which is very out of date these days. I always intended to run another set of tests with Einhugur, but never got around to it. These days, you can’t really go wrong, but I suspect MBS is still the performance king if you need the absolute fastest parsing.
For JSONItem_MBS, you would need to remove this in the constructor since the parsing constructor now raises an exception:
If (mJSON.Handle = 0) or (mJSON.ParseError <> "") Then
Raise New JSONException("JSONMBS ParseError: " + mJSON.ParseError, mJSON.Handle)
End If
and for FromJSON, you need to handle the new types:
Case JSONMBS.kTypeFalse
Return False
Case JSONMBS.kTypeNumber
If pJSON.IsInt64 Then Return pJSON.ValueInteger
Return pJSON.ValueDouble
Case JSONMBS.kTypeString
Return pJSON.ValueString
Case JSONMBS.kTypeTrue
Return True
replaced with:
Case JSONMBS.kTypeDouble
Return pJSON.ValueDouble
Case JSONMBS.kTypeInt64
Return pJSON.ValueInt64
Case JSONMBS.kTypeUInt64
Return pJSON.ValueUInt64
Case JSONMBS.kTypeString
If pJSON.IsInt64 Then Return pJSON.ValueInt64
If pJSON.IsNumber Then Return pJSON.ValueDouble
Return pJSON.ValueString
Case JSONMBS.kTypeSingle
Return pJSON.ValueDouble
or just use JSONMBS as JSONItem replacement itself.
With JSON, something is missing : the currency type.
This is strange because using amounts is very common.
There are 3 ways to use currencies : convert them to double with the risk of having something such as : 125.589999999…
Convert them to string to get “125.59” (with quotes that is quite strange for an amount).
The 3rd way is to build the json as a string and do what we want.
I’ll like to have something like
myJson.value(“amount”) = myCurrencyValue and getting : {…“amount” : 125.59, …}
Or the fourth way, which I’ve seen most often, is to treat the currency as an integer. Multiply by 100 for storage, and divide by 100 for displaying to the user.
heu… if this json is in reply of an api, you never know who and what will treat it. (“hey guy, don’t forget to divide this amount by 100 !..”)
Gag : with the same json built with a postgreslql json function :
Xojo jsonitem.tostring gives : “theAmount”:1572078.99,…
Postman gives : “theAmount”:1572078.9899999999907,…
It is, and APIs are where I’ve seen this. All environments will have this problem. JSON is more-or-less typeless. So using an integer is a reliable way to avoid the problem entirely.
That said, it sure would be nice if Xojo were to apply some sort of formatting rules so you would get something like what you see in Postman. But Xojo is aiming for accuracy, and doubles aren’t very accurate. Which… again… is why integers are a common solution.
Yes, the Set-ters for .Value are not (yet) available.
use JSONMBS as JSONItem replacement itself
So one can’t use it as a replacement (by renaming JSONItem → JSONMBS).
Additionally: MBS.VersionNumber returns 23.4 (expected: 23.5).
So one can’t use conditional compilation for testing the changes while still using 23.4 for productive usage:
#if MBS.VersionNumber < 23.5 then
'existing productive (shared) code
#else
'modified to work with MBS 23.5
#endif