R/json.R

Defines functions checkFromJSON fromJSON toJSON

toJSON <- function(x, force = TRUE, ...) {
  jsonlite::toJSON(
    list(
      class = class(x),
      object = unclass(x)
    ),
    force = force,
    ...
  )
}

fromJSON <- function(txt, ...) {
  obj <- jsonlite::fromJSON(txt, ...)
  if (is.list(obj) && identical(names(obj), c("class", "object"))) {
    structure(obj$object, class = obj$class)
  } else {
    obj
  }
}

checkFromJSON <- function(txt, ...) {
  if (identical(txt, NA_character_)) {
    return()
  }

  check <- fromJSON(txt, ...)
  check$errors   <- as.character(check$errors)
  check$warnings <- as.character(check$warnings)
  check$notes    <- as.character(check$notes)
  check
}
r-lib/revdepcheck documentation built on Oct. 22, 2024, 2:13 a.m.