Difference between revisions of "Textutils.serialize"

From ComputerCraft Wiki
Jump to: navigation, search
m (Fix bug in code example)
 
(7 intermediate revisions by 3 users not shown)
Line 2: Line 2:
 
{{Function
 
{{Function
 
|name=textutils.serialize
 
|name=textutils.serialize
|args= [[table (type)|anything]]
+
|args={{type|table}}/{{type|string}}/{{type|number}}/{{type|boolean}}/{{type|nil}} data
|returns=a string representing the object
+
|returns={{type|string}} serializedData
 
|api=textutils
 
|api=textutils
 
|addon=ComputerCraft
 
|addon=ComputerCraft
|desc=Converts the object to a string that can be saved or sent and then converted back into a copy of the object later, using [[textutils.unserialize()|unserialize]].   Nested structures are supported, but tables with circular graphs will be detected and raise an error.
+
|desc=Converts the object to a string that can be [[fs_(API)|saved in a file]] or sent over [[rednet_(API)|rednet]] and then converted back into a copy of the object later, using [[textutils.unserialize]](). Nested structures are supported, but tables with circular graphs will be detected and raise an error.<br/>
 +
<br/>
 +
In ComputerCraft 1.6 and up tables are serialized in a readable format: new lines and indention is applied.
 
|examples=
 
|examples=
 
{{Example
 
{{Example
|desc=Sends a table over rednet, demonstrates object recovery
+
|desc=Serializes a table and prints it
|code=myThing={name="Test", n=2}
+
|code=local myTable = {
sThing=textutils.serialize(myThing)
+
  key = "value",
[[rednet.send]](receiverID, sThing)
+
  [5] = "numbers",
myCopy=[[textutils.unserialize]](sThing)
+
  array = {1, 2, 3}
print(myCopy.name)
+
}
|output=Test
+
 +
[[print]]( '''textutils.serialize( myTable )''' )
 +
|output= &nbsp;
 +
{
 +
  key = "value",
 +
  [5] = "numbers",
 +
  array = {
 +
    1,
 +
    2,
 +
    3
 +
  }
 +
}
 +
&nbsp;
 
}}
 
}}
 
}}
 
}}
 +
 +
[[Category:API_Functions]]

Latest revision as of 14:57, 28 July 2016


Grid Redstone.png  Function textutils.serialize
Converts the object to a string that can be saved in a file or sent over rednet and then converted back into a copy of the object later, using textutils.unserialize(). Nested structures are supported, but tables with circular graphs will be detected and raise an error.


In ComputerCraft 1.6 and up tables are serialized in a readable format: new lines and indention is applied.
Syntax textutils.serialize(table/string/number/boolean/nil data)
Returns string serializedData
Part of ComputerCraft
API textutils

Examples

Grid paper.png  Example
Serializes a table and prints it
Code
local myTable = {
  key = "value",
  [5] = "numbers",
  array = {1, 2, 3}
}

print( textutils.serialize( myTable ) )
Output  
{
  key = "value",
  [5] = "numbers",
  array = {
    1,
    2,
    3
  }
}