jsonExport converts number to scientific notation
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
jsonExport converts number to scientific notation
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 ?
ex: "id": 7873356 -> exported as "id": 7.87336e+06 -> imported as 7873360
Any idea why ?
-
- VIP Livecode Opensource Backer
- Posts: 3581
- Joined: Mon Jan 22, 2007 7:36 am
- Location: Berkeley, CA, US
- Contact:
Re: jsonExport converts number to scientific notation
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
PowerDebug http://powerdebug.ahsoftware.net
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
Re: jsonExport converts number to scientific notation
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.
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.
-
- VIP Livecode Opensource Backer
- Posts: 3581
- Joined: Mon Jan 22, 2007 7:36 am
- Location: Berkeley, CA, US
- Contact:
Re: jsonExport converts number to scientific notation
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
PowerDebug http://powerdebug.ahsoftware.net
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
Re: jsonExport converts number to scientific notation
Yes. Is this a bug or is there something I am missing?
Re: jsonExport converts number to scientific notation
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.
put format("%u",array_value) into array_value
It is unfortunate I have to do this, but it works.
-
- VIP Livecode Opensource Backer
- Posts: 3581
- Joined: Mon Jan 22, 2007 7:36 am
- Location: Berkeley, CA, US
- Contact:
Re: jsonExport converts number to scientific notation
I would consider this a bug in LCB JSON formatting. The code in the JSONExport handler in json.lcb is
So there's really nothing to fix there... looks like it's deep in the LCB formatting routines.
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
PowerDebug http://powerdebug.ahsoftware.net
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev