Delete selected elements from a ts_tree_jsonc object
Source:R/ts-tree-delete.R
ts_tree_delete.ts_tree_jsonc.RdDelete selected elements from a ts_tree_jsonc object
Usage
# S3 method for class 'ts_tree_jsonc'
ts_tree_delete(tree, ...)Details
The formatting of the rest of the document is left as is.
jsonc <- tsjsonc::ts_parse_jsonc(
"{ \"a\": true, \"b\": [1, 2, 3] }"
) |>
ts::ts_tree_format()
jsonc#> # jsonc (8 lines) #> 1 | { #> 2 | "a": true, #> 3 | "b": [ #> 4 | 1, #> 5 | 2, #> 6 | 3 #> 7 | ] #> 8 | }
jsonc |> ts_tree_select("a") |> ts_tree_delete()#> # jsonc (7 lines) #> 1 | { #> 2 | "b": [ #> 3 | 1, #> 4 | 2, #> 5 | 3 #> 6 | ] #> 7 | }
jsonc <- tsjsonc::ts_parse_jsonc("{ \"a\": true, \"b\": [1, 2, 3] }")
jsonc |> ts_tree_delete()
#> # jsonc (0 lines)
jsonc <- tsjsonc::ts_parse_jsonc("{ \"a\": true, \"b\": [1, 2, 3] }")
jsonc |> ts_tree_select("c") |> ts_tree_delete()#> # jsonc (1 line) #> 1 | { "a": true, "b": [1, 2, 3] }
jsonc <- tsjsonc::ts_parse_jsonc(
"// top comment\n{ \"a\": // comment\n true,\n \"b\": [1, 2, 3] }"
) |> ts::ts_tree_format()
jsonc#> # jsonc (11 lines) #> 1 | // top comment #> 2 | { #> 3 | "a": #> 4 | // comment #> 5 | true, #> 6 | "b": [ #> 7 | 1, #> 8 | 2, #> 9 | 3 #> 10 | ] #> ℹ 1 more line #> ℹ Use `print(n = ...)` to see more lines
jsonc |> ts_tree_select("a") |> ts_tree_delete()#> # jsonc (8 lines) #> 1 | // top comment #> 2 | { #> 3 | "b": [ #> 4 | 1, #> 5 | 2, #> 6 | 3 #> 7 | ] #> 8 | }
Comments appearing inside the deleted elements are also deleted. Other comments are left as is.
Examples
library(ts)
tree <- ts_parse_jsonc("{ \"a\": //comment\ntrue, \"b\": [1, 2, 3] }")
tree
#> # jsonc (2 lines)
#> 1 | { "a": //comment
#> 2 | true, "b": [1, 2, 3] }
tree |> ts_tree_select("a")
#> # jsonc (2 lines, 1 selected element)
#> > 1 | { "a": //comment
#> 2 | true, "b": [1, 2, 3] }
tree |> ts_tree_select("a") |> ts_tree_delete()
#> # jsonc (1 line)
#> 1 | { "b": [1, 2, 3] }