Hello, i noticed that sys.locale doesn't actually change the locale, and because json must be tied to locale, when printing decimal number if the locale use a different decimal separator than "." the results are obviously wrong and different from the real value. Using os.setlocale actually change the locale and print the decimal numbers correctly.
I noticed that if i use another json library that must be independent from the locale and i don't change the locale to en_US but leave it in italian the value decoded are still correct, and the decimal is separated by "," when printing the value, while the included json library doesn't work unless the locale is changed to en-US ( or i suppose any locale that have "." as decimal separator.
local net = require "net"
local json= require "json"
--os.setlocale("it-IT")
print(sys.locale)
print(0.12+0.12)
sys.locale="en-US"
print(sys.locale)
print(0.12+0.12)
os.setlocale("en-US")
print(sys.locale)
print(0.12+0.12)
function readValueFromTable(t)
for k,v in pairs(t) do
if type(v)~="table" then print(k.."----"..tostring(v)) else
print("--------------------\n"..k.." is a table within a table\n--------------------")
readValueFromTable(v)
end
end
end
--get request of json from free open-meteo api, also create a local txt file containing the json
local client = net.Http("https://marine-api.open-meteo.com")
local task=client:get("/v1/marine?latitude=42.80775&longitude=10.73688&hourly=wave_height,wave_direction,wave_period&wind_speed_unit=kn&forecast_days=1")
local json_decoded=nil
task.after=function(self, response)
local jsonObj= response.content
local txtJson=sys.File(sys.currentdir.."\\test.txt")
txtJson:open("write")
txtJson:write(jsonObj)
txtJson:close()
json_decoded=json.decode(jsonObj)
end
waitall()
--print all the contet of the received json
for k,v in pairs(json_decoded) do
if type(v)~="table" then print(k.."----"..tostring(v))
else
print("--------------------\n"..k.." is a table within a jsonObject\n--------------------")
readValueFromTable(v)
end
end
io.read()