BeamMP / BeamMP-Server

Server for the multiplayer mod BeamMP for BeamNG.drive
https://beammp.com
GNU Affero General Public License v3.0
116 stars 49 forks source link

[Bug] Util.JsonEncode considers tables with index 0 as arrays #348

Open my-name-is-samael opened 4 days ago

my-name-is-samael commented 4 days ago

Have you read our FAQ/Wiki/#before-you-ask ? Yes

Describe the bug When I use Util.JsonEncode on a LUA Object with index 0 (followed by 1, 2, 3, etc), the returned JSON string represents an array. LUA "arrays" (as I understand with ipairs) have indices starting with 1, so my example should be considered as an object when it is converted to JSON.

To Reproduce Steps to reproduce the behavior:

  1. Execute following code:
    print(Util.JsonEncode({
    [0] = "a",
    [1] = true,
    [2] = 0.1
    }))
  2. The result string is ["a",true,0.1] but should be {0:"a",1:true,2:0.1}

Expected behavior When passing to Util.JsonEncode a table containing an index out of the range 1-N (or present in subobjects), it should return an object and not an array

Desktop (please complete the following information):