File: asJSON.logical.R

package info (click to toggle)
r-cran-jsonlite 1.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,340 kB
  • sloc: ansic: 3,792; sh: 9; makefile: 6
file content (39 lines) | stat: -rw-r--r-- 1,017 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
setMethod("asJSON", "logical", function(x, collapse = TRUE, na = c("null", "string", "NA"),
  auto_unbox = FALSE, keep_vec_names = FALSE, indent = NA_integer_, ...) {

  # shiny legacy exception
  if(isTRUE(keep_vec_names) && length(names(x))){
    warn_keep_vec_names()
    return(asJSON(as.list(x), collapse = collapse, na = na, auto_unbox = TRUE, ...))
  }

  # validate arg
  na <- match.arg(na)

  # json true/false
  tmp <- ifelse(x, "true", "false")

  # replace missing values, unless na="NA"
  if(!identical(na, "NA")){
    # logical values can have NA (but not Inf/NaN). Default is to encode as null.
    if (any(missings <- which(is.na(x)))) {
      tmp[missings] <- ifelse(identical(na, "string"), "\"NA\"", "null")
    }
  }

  #this is needed when !length(tmp) or all(is.na(tmp))
  if(!is.character(tmp)){
    tmp <- as.character(tmp);
  }

  if(isTRUE(auto_unbox) && length(tmp) == 1){
    return(tmp);
  }

  # collapse it
  if(collapse) {
    collapse(tmp, indent = indent)
  } else {
    tmp
  }
})