Skip to content

Unserialize the selected elements of a ts_tree object, i.e. convert them to R objects.

Available tree-sitter parsers

This is the manual page of the ts_tree_unserialize() S3 generic function. Methods in parser packages may override this generic. For the ones that do see the links to their manual pages in the table.

PackageVersionTitleMethod
tsjsonc0.0.0.9000Edit JSON Files.ts_tree_unserialize(<ts_tree_tsjsonc>)
tstoml0.0.0.9000Edit TOML files.

Usage

ts_tree_unserialize(tree)

Arguments

tree

A ts_tree object.

Value

List of R objects, with one entry for each selected element.

Details

If no elements are selected in the tree, then the whole document is unserialized.

 

tree <- tsjsonc::ts_parse_jsonc("{ \"a\": true, \"b\": [1, 2, 3] }")
#> [[1]]
#> [[1]]$a
#> [1] TRUE
#>
#> [[1]]$b
#> [[1]]$b[[1]]
#> [1] 1
#>
#> [[1]]$b[[2]]
#> [1] 2
#>
#> [[1]]$b[[3]]
#> [1] 3
#>
#>
#>
If the tree has an empty selection, then an empty list is returned.

 

tree <- tsjsonc::ts_parse_jsonc("{ \"a\": true, \"b\": [1, 2, 3] }")

 

tree |> ts_tree_select("nope") |> ts_tree_unserialize()
#> list()

The [[ operator

The [[ operator works similarly to the combination of ts_tree_select() and ts_tree_unserialize(), but it might be more readable.

 

json <- tsjsonc::ts_parse_jsonc(
  '{ "a": 1, "b": [10, 20, 30], "c": { "c1": true, "c2": null } }'
)
json |> ts_tree_select("b", 1)

#> # jsonc (1 line, 1 selected element)
#> > 1 | { "a": 1, "b": [10, 20, 30], "c": { "c1": true, "c2": null } }

 

json[[list("b", 1)]]

#> [[1]]
#> [1] 10
#>

For the details on how the selected elements are mapped to R objects, see the documentation of the methods in the parser packages. The methods in the installed parser packages are linked below.

See also

Method in installed package: ts_tree_unserialize(<ts_tree_tsjsonc>).

Other ts_tree generics: [[.ts_tree(), [[<-.ts_tree(), format.ts_tree(), print.ts_tree(), select-set, ts_tree_ast(), ts_tree_delete(), ts_tree_dom(), ts_tree_format(), ts_tree_insert(), ts_tree_new(), ts_tree_query(), ts_tree_select(), ts_tree_sexpr(), ts_tree_update(), ts_tree_write()

Other serialization functions: [[.ts_tree()

Examples

# Create a parse tree with tsjsonc -------------------------------------
tree <- tsjsonc::ts_parse_jsonc('{"a": 13, "b": [1, 2, 3], "c": "x"}')

tree
#> # jsonc (1 line)
#> 1 | {"a": 13, "b": [1, 2, 3], "c": "x"}

tree |> ts_tree_select(c("b", "c")) |> ts_tree_unserialize()
#> [[1]]
#> [[1]][[1]]
#> [1] 1
#> 
#> [[1]][[2]]
#> [1] 2
#> 
#> [[1]][[3]]
#> [1] 3
#> 
#> 
#> [[2]]
#> [1] "x"
#> 

tree |> ts_tree_select("b") |> ts_tree_unserialize()
#> [[1]]
#> [[1]][[1]]
#> [1] 1
#> 
#> [[1]][[2]]
#> [1] 2
#> 
#> [[1]][[3]]
#> [1] 3
#> 
#>