File: asJSON.numeric.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 (43 lines) | stat: -rw-r--r-- 1,159 bytes parent folder | download | duplicates (2)
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
40
41
42
43
asjson_numeric_fun <- function(x, digits = 5, use_signif = is(digits, "AsIs"),
  na = c("string", "null", "NA"), auto_unbox = FALSE, collapse = TRUE,
  keep_vec_names = FALSE, indent = NA_integer_, always_decimal = FALSE, ...) {

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

  na <- match.arg(na);
  na_as_string <- switch(na,
    "string" = TRUE,
    "null" = FALSE,
    "NA" = NA,
    stop("invalid na_as_string")
  )

  # old R implementation
  # tmp <- num_to_char_R(x, digits, na_as_string);

  # fast C implementation
  tmp <- if(is(x, "integer64")){
    integer64_to_char(x, na_as_string)
  } else {
    num_to_char(x, digits, na_as_string, use_signif, always_decimal);
  }

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

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

# This is for the bit64 package
setMethod("asJSON", "numeric", asjson_numeric_fun)
setMethod("asJSON", "integer64", asjson_numeric_fun)