JsonExport Issue

LiveCode Builder is a language for extending LiveCode's capabilities, creating new object types as Widgets, and libraries that access lower-level APIs in OSes, applications, and DLLs.

Moderators: LCMark, LCfraser

Post Reply
pink
Posts: 272
Joined: Wed Mar 12, 2014 6:18 pm

JsonExport Issue

Post by pink » Wed May 04, 2016 8:59 pm

I tried submitting a bug at http://quality.livecode.com/ but I keep getting an error "A value must be set for the 'Desktop OS' field." but I see no such field on the page...

There are 2 problems I have observed with the JsonExport command from the JSON library included in LC8.

First, numbers are not always correctly handled.
Second, there is no way to generate a JSON array. Other JSON libraries generally assume that an array that has all numeric keys should be a JSON array.

Example 1 (Array into JSON)

Code: Select all

put 3.1415 into tData["somekey"]
put "12345abcde" into tData["otherkey"]
put "abc" into tData["array"][1]
put "def" into tData["array"][2]
put JsonExport(tData) into field "a3"
EXPECTED:

Code: Select all

{"somekey": 3.1415, "otherkey": "12345abcde","array": ["abc","def"]}
OBSERVED:

Code: Select all

{"somekey": "3.1415","otherkey": "12345abcde","array": {"1": "abc","2": "def"}}
--the numeric value of "somekey" is presented as a string instead of a number
--if I use this instead I get the correct result: add 3.1415 to tData["somekey"]
--the JSON array called "array" is presented as just another JSON

Example 2: JSON to Array and Back
in field "a4" is:

Code: Select all

{"somekey": 3.1415, "otherkey": "12345abcde","array": ["abc","def"]}

Code: Select all

put field "a4" into tJson
put JsonImport(tJson) into tArray
put JsonExport(tArray) into field "a5"
OBSERVED:

Code: Select all

{"somekey": 3.1415,"otherkey": "12345abcde","array": {"1": "abc","2": "def"}}
--in this example, the number shows up correctly
--tArray looks the same in the variable viewer as tData that is created in the first example
--the JSON array still does not get produced
Greg (pink) Miller

MadPink, LLC
I'm Mad, Pink and Dangerous to Know

peter-b
Posts: 182
Joined: Thu Nov 20, 2014 2:14 pm
Location: LiveCode Ltd.

Re: JsonExport Issue

Post by peter-b » Mon May 09, 2016 1:02 pm

I'm copying my reply from Greg's bug report:
These two issues are related to the typelessness of LiveCode Script, and are actually _not_ bugs in the JSON extension!

JSON has a distinction between an array (an ordered sequence of values) and an object (an unordered string-to-value map). So does LCB! However, LiveCode Script doesn't.

The JSON library always exports a LCB "List" as a JSON array, and an LCB "Array" as a JSON object. For example:

Code: Select all

JsonExport([1, 2, 3]) -> '[1,2,3]'
and

Code: Select all

JsonExport({"1": 1, "2": 2, "3": 3}) -> '{"1": 1, "2": 2, "3": 3}'
However, when values are passed from LiveCode Script (LCS) to LCB they get "bridged" to whatever their actual type is. So a number-indexed array in LCS is _correctly_ bridged to a number-indexed array in LCB.


Similarly, JSON and LCB make a very clear distinction between strings and numbers -- but LCS doesn't. When you write:

Code: Select all

local tVar
put 25 into tVar
the contents of tVar is actually a string, "25", and it will only get converted to a "real" number when you do a number-based operation on it:

Code: Select all

local tVar
put 25 + 0 into tVar
Now tVar is a number, 25. This is why in your report, you noted that you got a string in your JSON when you were expecting a number.


At the moment, we can't change the behaviour of the JSON library -- people will already have code that relies on the current behaviour, and we are going to be trying to keep stable ABIs for all LCB library extensions that we ship with the LiveCode IDE once they have gone into a stable release. In the future we are hoping to add library versioning, where multiple API/ABI versions can be shipped in the same extension (or multiple versions of the same extension can be installed simultaneously) and this will enable backwards-incompatible changes to be made without breaking existing code.

My recommendation would be to create your own LCB library extension that understands the format that you are trying to export, and which can correctly convert numerically-indexed arrays to Lists where necessary before calling JsonExport from LCB.
LiveCode Open Source Team — @PeterTBBrett — peter.brett@livecode.com

Post Reply

Return to “LiveCode Builder”