json.encode(table [,compatible])
Convert a Lua table into a JSON encoded string. The special value
json.NULL can be used to store a null value in a Lua table since it is not possible
to store a Lua null value in a table.
JSON supports an array with integer indexes or an object that has string indexes whereas Lua
allows a table to use integer and string values for keys in the same table. To allow any Lua table
to be serialized to a string, nzLua by default uses an encoding format that may result in a
serialized table that is not compatible with standard JSON. For example, the table below cannot be
encoded in the standard JSON format since it has both integer indexes and a string
index.
t = {111,222,333,foo="abc"}
Using json.encode(t) on this table yields the string
'{1:111,2:222,3:333,"foo":"abc"}' which is not a legal encoding for JSON, whereas
json.encode(t,true) yields the string '{"1":111,"2":222,"3":333,"foo":"abc"}'. In
the compatible format, all of the integer keys of the table are converted to string keys.
The
json.encode function makes no attempt to detect recursive tables, therefore
the code shown below will result in a stack overflow
error.t = {1,2,3}
t[4] = t
str = json.encode(t)
Examplet = {a=123,b=987,c=333,d=json.NULL}
str = json.encode(t)