Difference between revisions of "Textutils.serializeJSON"
From ComputerCraft Wiki
MKlegoman357 (Talk | contribs) (Created page with "{{lowercase}} {{Function |name=textutils.serializeJSON |args={{type|table}}/{{type|string}}/{{type|number}}/{{type|boolean}} data |returns={{type|string}} serializedData |desc...") |
Bomb Bloke (Talk | contribs) (CC 1.76) |
||
Line 2: | Line 2: | ||
{{Function | {{Function | ||
|name=textutils.serializeJSON | |name=textutils.serializeJSON | ||
− | |args={{type|table}}/{{type|string}}/{{type|number}}/{{type|boolean}} data | + | |args={{type|table}}/{{type|string}}/{{type|number}}/{{type|boolean}} data [, {{type|boolean}} unquote keys] |
|returns={{type|string}} serializedData | |returns={{type|string}} serializedData | ||
− | |desc=Returns a JSON representation of the data <var>data</var> in a form of a {{type|string}}, mainly for [[Commands (API)|command usage]]. There is a special key in <var>textutils</var> table: <var>textutils.EMPTY_ARRAY</var> which represents an empty JSON array ( <var>[<nowiki/> ]</var> ). You can use it to serialize an empty JSON array inside, for example a table. | + | |desc=Returns a JSON representation of the data <var>data</var> in a form of a {{type|string}}, mainly for [[Commands (API)|command usage]]. There is a special key in <var>textutils</var> table: <var>textutils.EMPTY_ARRAY</var> which represents an empty JSON array ( <var>[<nowiki/> ]</var> ). 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). [[Commands_(API)#commands..3Ccommand.3E.28.29.2Fcommands.async..3Ccommand.3E.28.29|See here]] for details. | ||
+ | |||
Also exists as textutils.seriali'''s'''eJSON. Requires CC 1.7 or later. | Also exists as textutils.seriali'''s'''eJSON. Requires CC 1.7 or later. | ||
|api=textutils | |api=textutils |
Latest revision as of 02:13, 26 December 2015
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
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":[]} |