Skip to content

Commit

Permalink
Added unbox generic function to handle jsonstat objects
Browse files Browse the repository at this point in the history
  • Loading branch information
MansMeg committed Jun 12, 2016
1 parent ba1abef commit 98d9350
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
3 changes: 3 additions & 0 deletions NAMESPACE
Expand Up @@ -13,6 +13,8 @@ S3method(dim,jsonstat_dataset)
S3method(dimnames,jsonstat_dataset)
S3method(print,jsonstat)
S3method(print,jsonstat_dataset)
S3method(unbox,default)
S3method(unbox,jsonstat)
export("id<-")
export("status<-")
export(as.json)
Expand All @@ -25,5 +27,6 @@ export(is.jsonstat_dataset)
export(is.jsonstat_dimension)
export(status)
export(toJSONstat)
export(unbox)
import(checkmate)
import(jsonlite)
26 changes: 24 additions & 2 deletions R/jsonstat_methods.R
Expand Up @@ -135,7 +135,7 @@ as.json <- function(x, ...){

#' @export
as.json.jsonstat <- function(x, ...){
jsonlite::toJSON(unbox_jsonstat(x), na = "null", pretty = TRUE, digits = parse_digits(x$value), ...)
jsonlite::toJSON(unbox(x), na = "null", pretty = TRUE, digits = parse_digits(x$value), ...)
}

parse_digits <-function(x){
Expand All @@ -148,7 +148,29 @@ parse_digits <-function(x){
}
}

unbox_jsonstat <- function(x){
#' Unbox generic function
#'
#' @details
#' This is a generic function to unbox more general objects,
#' such as \code{jsonstat} objects.
#' See \code{\link[jsonlite]{unbox}} for mor general information.
#'
#' @param x an object to unbox
#'
#' @seealso \code{\link[jsonlite]{unbox}}
#'
#' @export
unbox <- function(x){
UseMethod("unbox")
}

#' @export
unbox.default <- function(x){
jsonlite::unbox(x)
}

#' @export
unbox.jsonstat <- function(x){
x$version <- unbox(x$version)
x$class <- unbox(x$class)
if(!is.null(x$label)) x$label <- unbox(x$label)
Expand Down
23 changes: 23 additions & 0 deletions man/unbox.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 98d9350

Please sign in to comment.