jsonExport converts number to scientific notation

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller

Post Reply
hopkins
Posts: 41
Joined: Wed Nov 28, 2018 11:09 am

jsonExport converts number to scientific notation

Post by hopkins » Tue Jan 15, 2019 5:40 pm

I am reading a JSON formatted string into an array with jsonImport, modifying it, and saving it back to a file in JSON format with jsonExport. I noticed that integers are converted to a scientific notation with jsonExport, so when I read back the file into an array again with jsonImport, I get a different result.

ex: "id": 7873356 -> exported as "id": 7.87336e+06 -> imported as 7873360

Any idea why ?

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3122
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

Re: jsonExport converts number to scientific notation

Post by mwieder » Tue Jan 15, 2019 6:15 pm

Well, neither JSONImport nor JSONexport are doing number conversion, so that's not where your problem lies. Here's a simple test that shows that the result of exporting and importing gives identical arrays.

Code: Select all

on mouseUp pMouseButton
   local tJSON
   local tJSONArray, tOutputArray
   
   put 7873356 into tJSONArray["id"]
   put JSONexport(tJSONArray) into tJSON
   put JSONimport(tJSON) into tOutputArray
   breakpoint
end mouseUp

hopkins
Posts: 41
Joined: Wed Nov 28, 2018 11:09 am

Re: jsonExport converts number to scientific notation

Post by hopkins » Tue Jan 15, 2019 6:48 pm

With your script, the jsonExport generates {"id": "7873356"}
In the json format that I get (from a website) it is {"id": 7873356}.

try this:

on mouseup
local tStr
put "{" & quote & "id" & quote & ": 7873356}" into tStr
put jsonImport(tStr) into tStr
put jsonExport(tStr)
end mouseup

Obviously a solution would be to convert every number in the original json format to put quotes around it.

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3122
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

Re: jsonExport converts number to scientific notation

Post by mwieder » Tue Jan 15, 2019 7:33 pm

Ah, I see - you're doing an import first, and then an export. Yes, LC is internally flagging the input value as numeric rather than performing a default string conversion. Interestingly, examining the resulting internal arrays shows no difference. It's JSONExport that's doing the internal conversion.

Code: Select all

on mouseUp pMouseButton
   local tJSON, tJSONout1
   local tJSONArray, tOutputArray1
   
   put 7873356 into tJSONArray["id"]
   put JSONexport(tJSONArray) into tJSON
   put JSONimport(tJSON) into tOutputArray1
   put jsonExport(tOutputArray1) into tJSONout1
   
   local tStr, tJSONout2
   local tOutputArray2
   put "{" & quote & "id" & quote & ": 7873356}" into tStr
   put jsonImport(tStr) into tOutputArray2
   put jsonExport(tOutputArray2) into tJSONout2
   breakpoint
   put tOutputArray2 is tOutputArray1 -- the arrays are internally identical!!!
end mouseUp

hopkins
Posts: 41
Joined: Wed Nov 28, 2018 11:09 am

Re: jsonExport converts number to scientific notation

Post by hopkins » Tue Jan 15, 2019 7:39 pm

Yes. Is this a bug or is there something I am missing?

hopkins
Posts: 41
Joined: Wed Nov 28, 2018 11:09 am

Re: jsonExport converts number to scientific notation

Post by hopkins » Tue Jan 15, 2019 9:04 pm

The solution I found is to loop through the array and do:

put format("%u",array_value) into array_value

It is unfortunate I have to do this, but it works.

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3122
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

Re: jsonExport converts number to scientific notation

Post by mwieder » Tue Jan 15, 2019 9:40 pm

I would consider this a bug in LCB JSON formatting. The code in the JSONExport handler in json.lcb is

Code: Select all

public handler JsonExport(in pValue as optional any) returns String
	if pValue is a string then
		return JsonExportString(pValue)
	else if pValue is a number then
		return JsonExportNumber(pValue)
	else if pValue is a list then
		return JsonExportList(pValue)
	else if pValue is an array then
		return JsonExportArray(pValue)
	else if pValue is true then
		return "true"
	else if pValue is false then
		return "false"
	else if pValue is nothing then
		return "null"
	else
		throw "Unsupported value type for JSON"
	end if
end handler

private handler JsonExportNumber(in pValue as Number) returns String
	return pValue formatted as string
end handler
So there's really nothing to fix there... looks like it's deep in the LCB formatting routines.

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”