HttpService::JSONEncode produces invalid JSON

In some cases, calling HttpService’s JSONEncode method can produce invalid JSON when you supply it with incorrect values.

> local a = {math.huge, math.huge * 0, 1,2,3,{4,5,6}"1\r\nhi\tuau84d0\/uwotm8"}; print(game:service("HttpService"):JSONEncode(a))
[1.#INF,-1.#IND,1,2,3,[4,5,6],"1\r\nhi\tuau84d0/uwotm8"]

As you can see, it produces the raw Lua output for NaN and Inf - the correct way to handle this would be (in my opinion) to encode them into null.

And another thing; when you encode tables which contain themselves (for example; a = {b=“hi”, c=“bye”} a.a = a) causes a crash. According to the lua-users wiki, the correct way to handle this is to throw an error.

Sometimes I get a full JSON “string” of null values, even though I’m only saving strings/numbers (Yes they are nested tables).

This doesn’t happen all the time, however.