Unserialize selected elements of a tree-sitter tree
Source:R/ts-tree-unserialize.R
ts_tree_unserialize.RdUnserialize 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.
| Package | Version | Title | Method |
| tsjsonc | 0.0.0.9000 | Edit JSON Files. | ts_tree_unserialize(<ts_tree_tsjsonc>) |
| tstoml | 0.0.0.9000 | Edit TOML files. |
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] }")ts_tree_unserialize(tree)#> [[1]] #> [[1]]$a #> [1] TRUE #> #> [[1]]$b #> [[1]]$b[[1]] #> [1] 1 #> #> [[1]]$b[[2]] #> [1] 2 #> #> [[1]]$b[[3]] #> [1] 3 #> #> #>
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 #>
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
#>
#>