Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Json encode/decode problem? — Gideros Forum

Json encode/decode problem?

rrraptorrrraptor Member
edited December 2020 in General questions
I have a problem with saving and loading data. The problem is when I print my table using inspect lib Im getting this:
{ { {
      result = 1
    }, {
      result = 1
    } }, { { 3.0525409300258, -7.1188186867317,
      bias = -8.3549980727365e-005,
      delta = -0.00027849993575788,
      result = 0.016855024522915
    }, { 2.4140298299185, -0.60132939530735,
      bias = 0.00033360795785989,
      delta = 0.0011120265261996,
      result = 0.85955375972833
    }, { -5.7339817836074, -5.5835565614512,
      bias = 1.2580778860342e-007,
      delta = 4.1935929534474e-007,
      result = 1.2157334382094e-005
    }, { -7.021765927752, 2.6025379863218,
      bias = -4.3028971089474e-005,
      delta = -0.00014342990363158,
      result = 0.011903162333153
    } }, { { 6.5062302538448, -3.5666890973304, -13.35381471873, 4.7209472334399,
      bias = -0.00077494182403473,
      delta = -0.0025831394134491,
      result = 0.052205570616817
} } }
It looks absolutely OK. Then I use json to encode this table to a string like that:
local str = json.encode(data)
And Im getting this:
[[{"result":1},{"result":1}],[{"1":3.0525409300258,"2":-7.1188186867317,"bias":-8.3549980727365e-005,"result":0.016855024522915,"delta":-0.00027849993575788},{"1":2.4140298299185,"2":-0.60132939530735,"bias":0.00033360795785989,"result":0.85955375972833,"delta":0.0011120265261996},{"1":-5.7339817836074,"2":-5.5835565614512,"bias":1.2580778860342e-007,"result":1.2157334382094e-005,"delta":4.1935929534474e-007},{"1":-7.021765927752,"2":2.6025379863218,"bias":-4.3028971089474e-005,"result":0.011903162333153,"delta":-0.00014342990363158}],[{"1":6.5062302538448,"2":-3.5666890973304,"3":-13.35381471873,"4":4.7209472334399,"bias":-0.00077494182403473,"result":0.052205570616817,"delta":-0.0025831394134491}]]
Looks OK again.

The fun part is when I load data from string using
local tmp = json.decode(myStrData)
and print tmp table with inspect, Im getting this:
{ { {
      result = 1
    }, {
      result = 1
    } }, { {
      ["1"] = -6.7174306784856,
      ["2"] = 3.2358287613015,
      bias = -7.191155644351e-005,
      delta = -0.00023970518814503,
      result = 0.02984773214605
    }, {
      ["1"] = -1.1534704652545,
      ["2"] = 2.8737936954513,
      bias = 0.00018268117948989,
      delta = 0.00060893726496631,
      result = 0.84811175708117
    }, {
      ["1"] = 2.6744957060553,
      ["2"] = -5.9305356187969,
      bias = -5.5072801877998e-005,
      delta = -0.00018357600625999,
      result = 0.037114576551705
    }, {
      ["1"] = -5.4239175469685,
      ["2"] = -5.5179995861126,
      bias = 8.1621380532062e-008,
      delta = 2.7207126844021e-007,
      result = 1.7699783810485e-005
    } }, { {
      ["1"] = 7.8235855177385,
      ["2"] = -4.4678815169207,
      ["3"] = 4.8548510429201,
      ["4"] = -14.527891065454,
      bias = -0.00031742496963396,
      delta = -0.0010580832321132,
      result = 0.033079933620093
} } }
As you can see number keys became string keys. What can I do about it?
Tagged:

Comments

  • yes, i think i had the same issue, those strings need to be converted manually to numbers.
  • from the github:
    Gotchas / Warnings

    This method is not appropriate for saving/restoring tables. It is meant to be used by the programmer mainly while debugging a program.

    :/
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • Well, it kinda works, but only if table containts only number keys.
    So this works OK:
    local inspect = require "Libs/inspect"
    require "json"
     
    local t = {2, 4, 6}
    print("inspected:", inspect(t))
    local str = json.encode(t)
    print("encoded", str)
    print("decoding...")
    local tmp = json.decode(str)
    print("inspected:", inspect(tmp))
    --[[ 
    output:
     
    inspected:	{ 2, 4, 6 }
    encoded	[2,4,6]
    decoding...
    inspected:	{ 2, 4, 6 }
    --]]
    And with this:
    local t = {2, 4, 6, key = "value"}
    --[[
    inspected:	{ 2, 4, 6,
      key = "value"
    }
    encoded	{"1":2,"2":4,"3":6,"key":"value"}
    decoding...
    inspected:	{
      ["1"] = 2,
      ["2"] = 4,
      ["3"] = 6,
      key = "value"
    }
    ]]
  • hgy29hgy29 Maintainer
    Accepted Answer
    JSON tables have strings only as keys, while JSON arrays have implicit numerical keys. Encoder does its best to map lua tables to json, but it can’t handle all kind of lua tables

    Likes: MoKaLux

    +1 -1 (+1 / -0 )Share on Facebook
  • rrraptorrrraptor Member
    edited December 2020
    hgy29 said:

    JSON tables have strings only as keys, while JSON arrays have implicit numerical keys. Encoder does its best to map lua tables to json, but it can’t handle all kind of lua tables

    Well, its ok, I can change structure a bit :)
    -- works perfectly
    {
          weights = {6.5062302538448, -3.5666890973304, -13.35381471873, 4.7209472334399},
          bias = -0.00077494182403473,
          delta = -0.0025831394134491,
          result = 0.052205570616817
    }
    Its even more readable now :blush:
Sign In or Register to comment.