HttpService::JSONDecode Parsing Error Empty Arrays

There are parsing errors in the JSONDecode function when the function is given empty arrays. The function gives unexpected behavior when parsing these otherwise-valid JSON strings:

JSON Strings which assert the error: attempt to index a string value when sent to HttpService::JSONDecode

{"foo":[]}
{"foo":[],"bar":1}
{"foo":{"bar":[]}}

JSON Strings which will crash ROBLOX altogether when sent to HttpService::JSONDecode

{"foo":[[]]}

JSON Strings which do NOT assert an error, and successfully return the expected value

{"foo":[null]}
{"foo":[[null]]}
{"bar":1,"foo":[]}
{"foo":["bar"]}
{"foo":[1]}

Note that this is confirmed to be an internal ROBLOX bug and not an “it’s invalid JSON” bug. This is not the “can’t Parse JSON” error, but rather a different internal bug. According to json.org it is perfectly legal to have empty arrays, yet the function generates an error and even has the crash bug.

There are definitely workarounds in existing code, like tossing in a “null” to your array or adding a dummy key before the array. However, you cannot be assured that the dummy key will be before or after the empty array, so the best workaround I would recommend is adding null as dummy value as the first value.

Please fix these bugs. I had to find them The Hard WayΓäó, and the aftermath wasn’t fun.

Obligatory mention of RFC 4627.

Edit:
As it turns out, it can be rather difficult to turn a Lua table into JSON format. For example, an empty Lua table can be either an empty JSON object or array. As indicated on lua-users’ entry on JSON modules, there is no standard way to handle the conversion. The devs will have to decide how they will handle each conflict, and what they will and will not support.
Right now, they should work on ensuring that any Lua table thrown at the encoder will be converted without problems, and any valid JSON thrown at the decoder will be converted in a way that represents the original data accurately enough. Basically, it should handle “most cases”.
If a user needs more customization, they’re free to use one of the many pure-Lua implementations available (listed on the page).

Of course, the difficulty of handling an empty table when encoding doesn’t excuse errors when decoding.