textutils.serialise

From ComputerCraft
(Redirected from Serialize)
Jump to: navigation, search

textutils.serialise
Function
Syntax
textutils.serialise()
Returns string
API textutils
Source CC:Tweaked (source)

Converts value to a string representing it, which may be converted back to a value of the original type using textutils.unserialise.

ExampleExampleSerialising a table
Prints a string representation of the given table.
Code
print(textutils.serialise({ 1, 2, foo = "bar", baz = { 1, 2, 3 } })
Output
{
  1,
  2,
  baz = {
    1,
    2,
    3,
  },
  foo = "bar"
}
ExampleExampleSaving a table to a file
Converts the given table to a string representing it, stores it in a file, then reads it back.
Code
local t = { 1, 2, 3 }
print(t[1])
local f = fs.open("foo", "w")
f.write(textutils.serialise(t))
f.close()

local f = fs.open("foo", "r")
local t2 = textutils.unserialise(f.readAll())
f.close()
print(t2[1])
Output 1 1