textutils.serializeJSON

From ComputerCraft Wiki
Revision as of 02:13, 26 December 2015 by Bomb Bloke (Talk | contribs) (CC 1.76)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search


Grid Redstone.png  Function textutils.serializeJSON
Returns a JSON representation of the data data in a form of a string, mainly for command usage. There is a special key in textutils table: textutils.EMPTY_ARRAY which represents an empty JSON array ( [ ] ). You can use it to serialize an empty JSON array inside, for example a table.

As of ComputerCraft 1.76, setting the second parameter to true strips the quotes that would otherwise be placed around the key-names in the output. Although outside of the JSON standard, this is required by most (but not all!) Minecraft commands.

Commands in the commands API which accept multiple parameters will automatically make use of this function if a table is included among them (and should also pick up on the correct key format, based on the command name). See here for details.

Also exists as textutils.serialiseJSON. Requires CC 1.7 or later.
Syntax textutils.serializeJSON(table/string/number/boolean data [, boolean unquote keys])
Returns string serializedData
Part of ComputerCraft
API textutils

Examples

Grid paper.png  Example
Serializes a table into JSON and prints it.
Code
local myTable = {
  key = "value",
  anotherKey = true,
  number = 5,
  array = {1, 2, 3},
  emptyArray = textutils.EMPTY_ARRAY
}

print( textutils.serializeJSON( myTable ) )
Output {"key":"value","anotherKey":true,"number":5,"array":[1,2,3],"emptyArray":[]}