Skip to content

Writes the document of a ts ts_tree object to a file or connection.

Usage

ts_tree_write(tree, file = NULL)

Arguments

tree

A ts_tree object as returned by ts_tree_new().

file

Character string, connection, or NULL. The file or connection to write to. By default it writes to the same file that was used in ts_tree_new(), if tree was read from a file.

Value

Invisibly returns NULL.

Details

If tree was created from a file, then ts_tree_write() by default writes it back to the same file. Otherwise, the file argument must be specified.

Format a JSONC file:

 

tree <- tsjsonc::ts_read_jsonc("config.json")
tree |> ts_tree_format() |> ts_tree_write()
To write to a connection, pass a connection object to the file argument. If the connection is opened in binary mode, the raw bytes are written using base::writeBin(). Otherwise, the raw bytes are converted to characters using the system encoding before writing using base::rawToChar().

Use file = stdout() to write to the standard output, i.e. to the console in an interactive R session.

Examples

# Create a parse tree with tsjsonc -------------------------------------
tree <- tsjsonc::ts_parse_jsonc('{"foo": 42, "bar": [1, 2, 3]}')

# Format and write to file
tree |> ts_tree_format() |> ts_tree_write("example.json")