hubverse-org / hubAdmin

Utilities for administering hubverse Infectious Disease Modeling Hubs
https://hubverse-org.github.io/hubAdmin/
Other
1 stars 2 forks source link

Force single element vectors to arrays in write_json #44

Open annakrystalli opened 2 hours ago

annakrystalli commented 2 hours ago

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:

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!

annakrystalli commented 2 hours ago

Useful purrr functionality: