JSON encode bug

I’m using the Corona json library.

Here’s an array I have in memory. Output is via “inspect.lua”:

{

  [36] = {

    inventory = { “garage-gas”, “rocketlauncher”, “newtires”, “flamerthrower”, “steeltires”, “westerntires”, “steelshield”,

    },

    name = “Fresno”,

    shelves = { { 2, 5,

      }, { 7,

      }, { 4, 6,

      }, { 1, 3,

      },

    },

    truck_on_display = “truck-family”,

    }

  },

}

This is what the JSON encode function spits out for me to save to a file:

{

  “36”:{

    “inventory”:[“garage-gas”,“rocketlauncher”,“newtires”,“flamerthrower”,“steeltires”,“westerntires”,“steelshield”],

    “truck_on_display”:“truck-family”,

    “name”:“Fresno”,

    “shelves”:[[2,5],[7],[4,6],[1,3]]

  },

}

Then, I read it in from that file and in memory it becomes:

{

  [“36”] = {

    inventory = { “garage-gas”, “rocketlauncher”, “newtires”, “flamerthrower”, “steeltires”, “westerntires”, “steelshield”,

    },

    name = “Fresno”,

    shelves = { { 2, 5,

      }, { 7,

      }, { 4, 6,

      }, { 1, 3,

      },

    },

    truck_on_display = “truck-family”,

    }

  },

}

The index changed from 36 to “36” which means I have to write a workaround to use a string as the key instead of a number.

This is a bug in the library unless there’s something I don’t understand.

That is not a bug. You can’t use integers as keys in JSON, only strings. The library is correctly converting the integer to a string in order to create valid JSON.

Thanks.

The above answer is correct, but not the actual reason for the issue you are seeing.

Corona Uses the dkjson library do to json encoding/decoding.

The actual cause of your issue is because of an optimization the library author has decided to do. If you look at the ‘isarray’ function in the above linked file you’ll see that he checks several things about the table you’re passing in to determine how sparse it is, or if it’s a dictionary.  The reasoning for this is that you can’t have sparse arrays in JSON like you can in a lua table. In your example above the json would need to have 35 null entries in front of it.

[lua]local json = require ‘json’

local foo = {}
local bar = {}

foo[10] = ‘test’
print(json.encode(foo,{indent=true}))

print("\n")

bar[11] = ‘test’
print(json.encode(bar,{indent=true}))[/lua]

[lua][null, null, null, null, null, null, null, null, null, “test”]
 

{
“11”: “test”
}[/lua]

That is not a bug. You can’t use integers as keys in JSON, only strings. The library is correctly converting the integer to a string in order to create valid JSON.

Thanks.

The above answer is correct, but not the actual reason for the issue you are seeing.

Corona Uses the dkjson library do to json encoding/decoding.

The actual cause of your issue is because of an optimization the library author has decided to do. If you look at the ‘isarray’ function in the above linked file you’ll see that he checks several things about the table you’re passing in to determine how sparse it is, or if it’s a dictionary.  The reasoning for this is that you can’t have sparse arrays in JSON like you can in a lua table. In your example above the json would need to have 35 null entries in front of it.

[lua]local json = require ‘json’

local foo = {}
local bar = {}

foo[10] = ‘test’
print(json.encode(foo,{indent=true}))

print("\n")

bar[11] = ‘test’
print(json.encode(bar,{indent=true}))[/lua]

[lua][null, null, null, null, null, null, null, null, null, “test”]
 

{
“11”: “test”
}[/lua]