| 1 |
|
| 2 |
# @param box A vector of names, whose values should be boxed. By default, |
| 3 |
# scalar values are unboxed. |
| 4 |
renv_json_config <- function(box = character()) { |
| 5 |
list(box = box) |
| 6 |
} |
| 7 |
|
| 8 |
renv_json_write <- function(object, |
| 9 |
config = NULL, |
| 10 |
file = stdout()) |
| 11 |
{ |
| 12 |
config <- config %||% renv_json_config() |
| 13 |
json <- renv_json_convert_impl(NULL, object, config, 0L) |
| 14 |
if (is.null(file)) |
| 15 |
return(json) |
| 16 |
|
| 17 |
writeLines(json, con = file) |
| 18 |
|
| 19 |
} |
| 20 |
|
| 21 |
renv_json_convert <- function(object, config = renv_json_config()) { |
| 22 |
renv_json_convert_impl(NULL, object, config, 0L) |
| 23 |
} |
| 24 |
|
| 25 |
renv_json_convert_impl <- function(key, value, config, depth) { |
| 26 |
|
| 27 |
# coerce numeric_version to character, since these are list objects |
| 28 |
# with a custom `[[` method that can cause infinite recursion |
| 29 |
if (inherits(value, "numeric_version")) |
| 30 |
value <- format(value) |
| 31 |
|
| 32 |
if (is.list(value) || !is.null(names(value))) |
| 33 |
return(renv_json_convert_list(key, value, config, depth)) |
| 34 |
|
| 35 |
json <- renv_json_convert_atom(key, value, config, depth) |
| 36 |
indent <- renv_json_convert_indent(depth) |
| 37 |
paste0(indent, json) |
| 38 |
|
| 39 |
} |
| 40 |
|
| 41 |
renv_json_convert_list <- function(key, value, config, depth) { |
| 42 |
indent <- renv_json_convert_indent(depth) |
| 43 |
if (empty(value)) { |
| 44 |
json <- if (is.null(names(value))) "[]" else "{}" |
| 45 |
paste0(indent, json) |
| 46 |
} else if (is.null(names(value))) { |
| 47 |
json <- enum_chr(value, renv_json_convert_impl, config = config, depth = depth + 1L) |
| 48 |
paste0(indent, "[", "\n", paste(json, collapse = ",\n"), "\n", indent, "]") |
| 49 |
} else { |
| 50 |
keys <- renv_json_quote(names(value)) |
| 51 |
vals <- enum_chr(value, renv_json_convert_impl, config = config, depth = depth + 1L) |
| 52 |
idx <- regexpr("[^[:space:]]", vals) |
| 53 |
json <- paste0(substring(vals, 1L, idx - 1L), keys, ": ", substring(vals, idx)) |
| 54 |
paste0(indent, "{", "\n", paste(json, collapse = ",\n"), "\n", indent, "}") |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
renv_json_convert_atom <- function(key, value, config, depth) { |
| 59 |
|
| 60 |
unbox <- is.null(key) || !key %in% config$box || inherits(value, "AsIs") |
| 61 |
if (is.null(value)) |
| 62 |
return(if (unbox) "null" else "[]") |
| 63 |
|
| 64 |
n <- length(value) |
| 65 |
if (n == 0L) |
| 66 |
return("[]") |
| 67 |
|
| 68 |
if (is.character(value)) { |
| 69 |
value <- renv_json_quote(value) |
| 70 |
value[value %in% c("NA")] <- "null" |
| 71 |
} |
| 72 |
|
| 73 |
if (is.logical(value)) { |
| 74 |
value <- ifelse(value, "true", "false") |
| 75 |
value[is.na(value)] <- "null" |
| 76 |
} |
| 77 |
|
| 78 |
if (unbox && n == 1L) |
| 79 |
return(if (is.na(value)) "null" else paste0(value)) |
| 80 |
|
| 81 |
indent <- renv_json_convert_indent(depth) |
| 82 |
json <- paste0(renv_json_convert_indent(depth + 1L), value) |
| 83 |
paste0("[", "\n", paste(json, collapse = ",\n"), "\n", indent, "]") |
| 84 |
|
| 85 |
} |
| 86 |
|
| 87 |
renv_json_convert_indent <- function(level) { |
| 88 |
paste(rep(" ", level), collapse = "") |
| 89 |
} |