Name
WriteTable -- write table to file (V4.0)
Synopsis
WriteTable(id, table[, t])
Deprecated syntax
WriteTable(id, table[, txtmode, nobrk])
Function
This function writes the Hollywood table specified by table to the file specified by id. The table will be serialized using the serializer that can be specified in the optional arguments. It will be written to the file at the current cursor position which you can modify by using the Seek() command. Tables written to files can later be loaded back into Hollywood tables by using the ReadTable() command.

This function is fully recursive. Your table can contain as many subtables as you need. Additionally, the table can even contain Hollywood functions. See below for an example.

WriteTable() supports several optional arguments. Before Hollywood 9.0, those had to be passed as optional parameters (see above). Since Hollywood 9.0, however, it is recommended to use the new syntax, which has a single optional table argument that can be used to pass one or more optional arguments to WriteTable().

The following table fields are recognized by this function:

Adapter:
This table tag can be used to specify the serializer that should be used to export the Hollywood table. This can be the name of an external serializer plugin (e.g. xml) or it can be one of the following inbuilt serializers:

Default:
Use Hollywood's default serializer. This will serialize the table data to the JSON format. Note that even though the name of this serializer claims to be the default one, it is actually not. For compatibility reasons, WriteTable() will use the Inbuilt serializer by default (see below). If you want WriteTable() to use the JSON serializer, you explicitly have to request it by setting Adapter to Default.

Inbuilt:
Use Hollywood's legacy serializer. This will serialize the table into a custom, proprietary format. This is the format WriteTable() has used since Hollywood 4.0 and for compatibility reasons, it is still the default serializer. However, it is not recommended any longer as this serializer will output data that is not in a human-readable format. Using the JSON serializer is a much better choice.

If Adapter isn't specified, it defaults to the default set using SetDefaultAdapter(). Note that for compatibility reasons, this default isn't Default but Inbuilt. See above for an explanation.

TextMode:
When using Hollywood's legacy serializer, which is still the default, this argument can be set to True to tell WriteTable() to export binary data as text. Note that even if you set this tag to True, the text won't be in a human-readable format. If you want to serialize the table into human-readable text, use the JSON serializer (see above). Defaults to False.

NoLineBreak:
If the TextMode tag has been set to True, WriteTable() will automatically insert line breaks after every 72 characters for better readability. If you don't want that, set NoLineBreak to True. In that case, no line breaks will be inserted. Note that this tag only affects Hollywood's legacy serializer. It doesn't have any effect on other serializers. Defaults to False. (V6.1)

UserTags:
This tag can be used to specify additional data that should be passed to serializer plugins. If you use this tag, you must set it to a table of key-value pairs that contain the additional data that should be passed to plugins. See User tags for details. (V10.0)

Mode:
This tag can be used to set the serialization mode to use for the operation. It defaults to the serialization mode set using SetSerializeMode(). See SetSerializeMode for details. (V10.0)

Options:
This tag can be used to set the serialization options to use for the operation. It defaults to the serialization options set using SetSerializeOptions(). See SetSerializeOptions for details. (V10.0)

SrcEncoding:
This tag can be used to specify the source character encoding. This defaults to the string library's default character encoding as set by SetDefaultEncoding(). See SetDefaultEncoding for details. (V10.0)

DstEncoding:
This tag can be used to specify the destination character encoding. This defaults to the string library's default character encoding as set by SetDefaultEncoding(). See SetDefaultEncoding for details. (V10.0)

Inputs
id
file to write to
table
table to write to the file
t
optional: table containing further arguments (see above) (V9.0)
Example
mytable = {1, 2, 3, 4, 5,
  "Hello World",
  x = 100, y = 150,
  subtable = {10, 9, 8, 7},
  mulfunc = Function(a, b) Return(a*b) EndFunction
}

OpenFile(1, "table.json", #MODE_WRITE)
WriteTable(1, mytable, {Adapter = "default"})
CloseFile(1)

OpenFile(1, "table.json", #MODE_READ)
newtable = ReadTable(1, {Adapter = "default"})
CloseFile(1)

Print(newtable[0], newtable[5], newtable.x, newtable.y,
      newtable.subtable[0], newtable.mulfunc(9, 9))
The code above writes the table mytable to file "table.json". After that, it opens file "table.json" again and reads the table back into Hollywood. The imported table will be stored in the variable newtable. Finally, we will access the newly imported table and print some of its data to the screen. The output of the code above will be "1 Hello World 100 150 10 81".

Show TOC