JSONItem.ToString(); performance;

Hello!

As this is a general question about JSONItem.ToString()-performance, I have no source code as an example.

I have a database request which results in an about 848KB size for a string after JSONItem.ToString().
The table was 10 columns and 4200 rows; the JSONItem object is already built up.
I am only talking about performance of the ToString()-method.
I already measured encodeBase64 and database request itself and converting to JSONItem -> they are all very fast!

The call to ToString() needed about 4 seconds.

First, is this speed to be assumed for the given result bytes amount?

My guess is the following: the performance is not so good because no string builder is used internally but string is repeatedly extended on and on.
Hopefully I am wrong here.

So, last question, is there an add-on/class from Monkeybread Software for faster JSONItem-to-string handling?
Unfortunately, did not find it.

Any idea really apreciated!

Regards

Did you try “ParseJSON” and “GenerateJSON” ? Those may be faster

I have a JSONItem and need a string.

Just “found” Monkeybread Software’s JSONMBS and will switch to this; should work correct.

That’s funny.

ObjJSONMBS.toString() is very fast.

But this

J.AddItemToArray(JSONMBS.NewStringNode(Field.asStringValue))

seems to be far slower as JSONMBS.NewStringNode(Field.asStringValue) seems to be slow…

Any ideas how to solve this?

[quote=462365:@Hans-Norbert Gratzal]I have a JSONItem and need a string.

Just “found” Monkeybread Software’s JSONMBS and will switch to this; should work correct.[/quote]

[quote=462365:@Hans-Norbert Gratzal]I have a JSONItem and need a string.

Just “found” Monkeybread Software’s JSONMBS and will switch to this; should work correct.[/quote]

The parseJSON without Xojo.Core is string based

You can contact @Christian Schmitz for details about his classes

Hello Derk!

What have ParseJSON and GenerateJSON to do with JSONItem toString()???

As, I wrote ToString() for JSONItem is slow.
And AddItemToArray() or JSONMBS.newStringNode("") seems to be slow.

The benchmark example which is given for MonkeyBread Software’s JSONMBS also shows that JSON creation is slower for JSONMBS compared to Xojo’s JSONItem; which was horrible for me to see:-(

As I serialize the data catched from the database via web socket, I could build up the JSON string as what it is a string.
But as the string gets big (848KB), I would bet to have the same problem.

So, next task for me is to see if StringBuilder is avaiable for Xojo.

Still ned help with this;-)

Thanks!

Finally, this method
J = JSONMBS.NewStringArray(D)
where D is a string array is even slower then toString from JSONItem…

Damn… I forgot ChilkatSoft… That JSON will be definitely-hopefully fast;-)

Ok, I will try some tests with it and close this conversation hopefully soon;-)

Another option for you is my drop in replacement, JSONItem_MTC. I think you’ll find it’s quite a bit faster.

https://github.com/ktekinay/JSONItem_MTC

[quote=462394:@Hans-Norbert Gratzal]Hello Derk!

What have ParseJSON and GenerateJSON to do with JSONItem toString()???

As, I wrote ToString() for JSONItem is slow.
And AddItemToArray() or JSONMBS.newStringNode("") seems to be slow.

The benchmark example which is given for MonkeyBread Software’s JSONMBS also shows that JSON creation is slower for JSONMBS compared to Xojo’s JSONItem; which was horrible for me to see:-(

As I serialize the data catched from the database via web socket, I could build up the JSON string as what it is a string.
But as the string gets big (848KB), I would bet to have the same problem.

So, next task for me is to see if StringBuilder is avaiable for Xojo.

Still ned help with this;-)

Thanks![/quote]

You can use those functions to parseJSON or GenerateJSON from or to a dictionary instead of using JSONItem.
JSONItem is known for being “slow” @Kem Tekinay has some classes Einhugur has json plugins and MBS has JSON plugins
and ChilkatSoft all are Alternatives to JSONItem usage.
If .ToString is too slow, don’t expect it to get faster by “saying JSONItem.ToString is slow”. So you might wanna use one of the alternatives instead to workaround your issue.

Hello Derk,
hello Kem,

thanks a lot for Your help!!!

I will try these advices and see if I can speed everything up a little.

I did some performance testing with all the available JSON options in Xojo, with an entry for Xojo 2019r2’s ParseJSON still coming.

https://thezaz.com/blog/json_performance_tests_round_2

But the TLDR is: if you need pure performance, use Xojo.GenerateJSON or JSONMBS.Convert.

did you happen to evaluate “correctness” ?
they should all be able to encode using one and decode using another no ?

So, finally I switched to ChilkatSoft.JsonObject with

System.DebugLog("sendClientWebSocketResponseC;before J.Emit(): " + cstr(DT.getTimestamp())) Res = J.Emit() System.DebugLog("sendClientWebSocketResponseC; after J.Emit(): " + cstr(DT.getTimestamp()))

Result is:

sendClientWebSocketResponseC;before J.Emit(): 20191110143141227
sendClientWebSocketResponseC; after  J.Emit(): 20191110143141232

ChilkatSoft also has a Stringbuilder object…

Then there is only one question left: where does the name Chilkat come from???
(No, I did not google it… :wink:

Thanks a lot for all Your help and tips!