How to: what is the last update on Json/array conversion?

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
trevix
Posts: 970
Joined: Sat Feb 24, 2007 11:25 pm
Location: Italy
Contact:

How to: what is the last update on Json/array conversion?

Post by trevix » Mon Mar 06, 2023 12:34 pm

Hello.
As usual, when some problem emerges, I realise that I may have not chosen the best solution, or that OS/LC version updates have surpassed me, leaving me with wrong solutions.

In this case, my problem is how to do "json to array" and "array to json" conversion between my standalone and the data sent and returned by my FileMaker Server and by Microsoft Azure.
The data exchanged are multidimensional array, that may contain numbers, text, image data and so on.

As of now, in the inclusion of the standalone, I have the "JSON library". But I also use a substack as library with the " FASTJSON v1.1.1" by Bob Hall.
I have to use the JSON library (jsonImport) for converting Json to array

Code: Select all

put textDecode(pJSON,"UTF8") into tJSON
put JsonImport(tJSON) into tArray --JSON library in inclusion
while using the FASTJSON lib for converting Array to json.

Code: Select all

 put ArrayToJSON(pArray) into tArrayJson --convert to json in fastjson library
put textEncode(tArrayJson, "utf-8") into tArrayJson
It wasn't easy but, with some fix here and there, it worked, until I started dealing with image content.
"jsonImport" got VERY slow dealing with a 150k image data in the content of the array, and sometime things go wrong.

And then there is mergJSON, with I never dealt with, but may be I should?

So, the point is: is there a unique solution for dealing with JSON in LC, fast and reliable for any situation, or this is a wishful thinking and I need to always patch things here and there?

Sorry if my post demonstrates naivety.
Thanks
Trevix
OSX 14.3.1 xCode 15 LC 10 DP7 iOS 15> Android 7>

prometheus
Posts: 40
Joined: Sun Dec 13, 2015 6:05 pm

Re: How to: what is the last update on Json/array conversion?

Post by prometheus » Mon Mar 06, 2023 3:30 pm

Hi Trevix

Have you tried this one?
https://github.com/Ferruslogic/PhotonJSON

I've got success with this one when others has failed

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7258
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: How to: what is the last update on Json/array conversion?

Post by jacque » Mon Mar 06, 2023 6:32 pm

I think I know what the problem is but I'm not sure how to solve it. Images are binary so using textDecode on them will mangle them. You'd need a way to identify images and leave their binary content alone while still decoding the text portions.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

trevix
Posts: 970
Joined: Sat Feb 24, 2007 11:25 pm
Location: Italy
Contact:

Re: How to: what is the last update on Json/array conversion?

Post by trevix » Mon Mar 06, 2023 6:49 pm

mmmmh...
I could compress and decompress the binary data?
Trevix
OSX 14.3.1 xCode 15 LC 10 DP7 iOS 15> Android 7>

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9859
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: How to: what is the last update on Json/array conversion?

Post by FourthWorld » Mon Mar 06, 2023 7:08 pm

Is the data in the JSON truly binary, or Base64?

If compressing the JSON is an option it'll help for transport, but not decoding.

If you have control over how the FMP server packs the data, could externalizing the images be a solution? Then the JSON would include only a URL, which could then be used to obtain the image in truly binary form. Image-format-specific compression is hard to beat, and it keeps the JSON light and nimble.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7258
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: How to: what is the last update on Json/array conversion?

Post by jacque » Mon Mar 06, 2023 8:27 pm

I have no idea if this would work, but maybe encoding the binary to UTF8 would decode it correctly later? Otherwise I think I'd do what Richard suggests and use base64, which would require decoding the base64 before display but wouldn't interfere with UTF8...I think.

Using URLs instead of the actual binary is probably the best way.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9859
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: How to: what is the last update on Json/array conversion?

Post by FourthWorld » Tue Mar 07, 2023 4:45 pm

To clarify, I'm not suggesting Base64, just asking if that's what's being used in FMP's JSON. JSON is a plain text format, so the images are unlikely represented in binary within it.

My suggestion, more a general preference without knowing the specifics of this DB, is to consider not putting binary data like images in the DB at all, instead leaving them in a separate HTTP-accesible folder and putting only the file path in the DB.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

trevix
Posts: 970
Joined: Sat Feb 24, 2007 11:25 pm
Location: Italy
Contact:

Re: How to: what is the last update on Json/array conversion?

Post by trevix » Tue Mar 07, 2023 5:47 pm

Saving the binary data in a folder is something that eventually will have to do, but now it would be too complex to change.
As of now, I came up with the below mess, that works on oSX, iOS and (with some trouble) Android.
This is what I do:

Code: Select all

export snapshot from rect (the rect of graphic "PhotoCircle") of this card to tFileVar as PNG
the snapshot tFileVar is the user logo or photo in the standalone.
I then encode the tFileVar and put it in an multidimensional array, together with other prefs info.

Code: Select all

put "Bill" into tPref["UserName"]
put base64Encode(textEncode(tFileVar,"UTF-8")) into tPref["UserPhoto"]
The all package is sent to FMS DB using its API:

Code: Select all

put ArrayToJSON(tPref) into tPrefJson --convert to json in fastjson library
put textEncode(tPrefJson, "utf-8") into tPrefJson
In the DB, the UserPhoto data is saved in a text column (doesn't need to be seen here).
The reverse is done when the user needs to login in a different device, with the same account.

The scary part is always as to have not intercepted some strange char that will disrupt the all thing, linke in the password or other prefs data.
Did I made some error?
Trevix
OSX 14.3.1 xCode 15 LC 10 DP7 iOS 15> Android 7>

trevix
Posts: 970
Joined: Sat Feb 24, 2007 11:25 pm
Location: Italy
Contact:

Re: How to: what is the last update on Json/array conversion?

Post by trevix » Tue Mar 07, 2023 7:21 pm

I specified "...with some trouble in Android" because an image that is good in iOS/OSX devices, does not show on Android. And viceversa.
I guess something is wrong in the way I encode things.
May be the texEncode should use utf-16? Or else?
The fact that the FMS is running on a OSX may have something to do with it?
Trevix
OSX 14.3.1 xCode 15 LC 10 DP7 iOS 15> Android 7>

Post Reply

Return to “Talking LiveCode”