Open annakrystalli opened 2 hours ago
While working on #42 using example provided by @bsweger here I noticed the use of list() to wrap single element vectors.
list()
On further experimentation, I can confirm that this indeed works to force single element vectors to arrays in when writing to JSON!!! :tada:
x <- list(test = c("scalar")) jsonlite::toJSON( x = x, path = "attic/list_with_vector.json", auto_unbox = TRUE, na = "string", null = "null", pretty = TRUE ) #> { #> "test": "scalar" #> } x <- list(test = list("scalar")) jsonlite::toJSON( x = x, path = "attic/list_with_list.json", auto_unbox = TRUE, na = "string", null = "null", pretty = TRUE ) #> { #> "test": [ #> "scalar" #> ] #> }
Created on 2024-09-20 with reprex v2.1.0
Although when working with config objects we don't want this additional list structure, it's definitely sth we can utilise in write_json by ensuring all properties that should be arrays (if not NULL) as converted to lists prior to writing!
write_json
Useful purrr functionality:
purrr
modify_tree
While working on #42 using example provided by @bsweger here I noticed the use of
list()
to wrap single element vectors.On further experimentation, I can confirm that this indeed works to force single element vectors to arrays in when writing to JSON!!! :tada:
Created on 2024-09-20 with reprex v2.1.0
Although when working with config objects we don't want this additional list structure, it's definitely sth we can utilise in
write_json
by ensuring all properties that should be arrays (if not NULL) as converted to lists prior to writing!