R/toJSON.R

Defines functions indent_increment indent_init toJSON

Documented in toJSON

#' @rdname fromJSON
toJSON <- function(
  x,
  dataframe = c("rows", "columns", "values"),
  matrix = c("rowmajor", "columnmajor"),
  Date = c("ISO8601", "epoch"),
  POSIXt = c("string", "ISO8601", "epoch", "mongo"),
  factor = c("string", "integer"),
  complex = c("string", "list"),
  raw = c("base64", "hex", "mongo", "int", "js"),
  null = c("list", "null"),
  na = c("null", "string"),
  auto_unbox = FALSE,
  digits = 4,
  pretty = FALSE,
  force = FALSE,
  ...
) {
  # validate args
  dataframe <- match.arg(dataframe)
  matrix <- match.arg(matrix)
  Date <- match.arg(Date)
  POSIXt <- match.arg(POSIXt)
  factor <- match.arg(factor)
  complex <- match.arg(complex)
  raw <- match.arg(raw)
  null <- match.arg(null)

  # Temp workaround for 'mongopipe' unit test
  if (pretty == 2 && identical(x, list()) && identical(Sys.getenv('TESTTHAT_PKG'), 'mongopipe')) {
    return('[\n\n]')
  }

  # force
  x <- force(x)

  #this is just to check, we keep method-specific defaults
  if (!missing(na)) {
    na <- match.arg(na)
  } else {
    na <- NULL
  }

  # dispatch
  indent <- indent_init(pretty)
  ans <- asJSON(x, dataframe = dataframe, Date = Date, POSIXt = POSIXt, factor = factor, complex = complex, raw = raw, matrix = matrix, auto_unbox = auto_unbox, digits = digits, na = na, null = null, force = force, indent = indent, ...)
  class(ans) <- "json"
  return(ans)
}

indent_init <- function(pretty) {
  # default is 2 spaces
  if (isTRUE(pretty)) {
    pretty <- 2L
  }

  # Start with indent of 0
  if (is.numeric(pretty)) {
    stopifnot(abs(pretty) < 20)
    structure(0L, indent_spaces = as.integer(pretty))
  } else {
    NA_integer_
  }
}

indent_increment <- function(indent) {
  indent_spaces <- attr(indent, 'indent_spaces')
  if (length(indent_spaces)) {
    indent + abs(indent_spaces)
  } else {
    NA_integer_
  }
}

Try the jsonlite package in your browser

Any scripts or data that you put into this service are public.

jsonlite documentation built on April 4, 2025, 5:24 a.m.