Skip to content

Conversation

@Kipstz
Copy link
Contributor

@Kipstz Kipstz commented Dec 24, 2025

Summary

  • Fix Util.JsonEncode incorrectly encoding Lua tables with index 0 as JSON arrays
  • Added IsLuaArray() helper function that validates all keys are integers >= 1
  • Tables with non-standard indices (like 0) are now correctly encoded as JSON objects

Closes #348

Before

Util.JsonEncode({[0]="a", [1]=true, [2]=0.1})
-- Output: ["a",true,0.1]  (incorrect - data loss)

After

Util.JsonEncode({[0]="a", [1]=true, [2]=0.1})
-- Output: {"0":"a","1":true,"2":0.1}  (correct)

Tables with index 0 should be encoded as JSON objects, not arrays.
Lua arrays start at index 1, so any table with keys outside 1-N range
should be treated as an object.

Added IsLuaArray() helper that checks all keys are integers >= 1.

Closes BeamMP#348
@WiserTixx
Copy link
Collaborator

The issue also mentions this related behaviour:
#348 (comment)

I'd like to add another weird behavior with tables to the ticket, with Util.JsonDecode this time:

When I run Util.JsonDecode('[1,null,3,4]') I expect to get this object : { [1] = 1, [3] = 3, [4] = 4 }

But instead I get : { [1] = 1, [2] = 3, [3] = 4 } or simplified { 1, 3, 4 }

The null 2nd index was completely omitted by the JSON parser

Would you mind attempting to fix this as well in this pr?

@Kipstz
Copy link
Contributor Author

Kipstz commented Dec 29, 2025

The issue also mentions this related behaviour: #348 (comment)

I'd like to add another weird behavior with tables to the ticket, with Util.JsonDecode this time:
When I run Util.JsonDecode('[1,null,3,4]') I expect to get this object : { [1] = 1, [3] = 3, [4] = 4 }
But instead I get : { [1] = 1, [2] = 3, [3] = 4 } or simplified { 1, 3, 4 }
The null 2nd index was completely omitted by the JSON parser

Would you mind attempting to fix this as well in this pr?

Yes, I can. I'll look at this tomorrow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants