diff --git a/R/fromJSON.R b/R/fromJSON.R index 60ab812d..d396cb1b 100644 --- a/R/fromJSON.R +++ b/R/fromJSON.R @@ -23,6 +23,7 @@ #' @param simplifyVector coerce JSON arrays containing only primitives into an atomic vector #' @param simplifyDataFrame coerce JSON arrays containing only records (JSON objects) into a data frame #' @param simplifyMatrix coerce JSON arrays containing vectors of equal mode and dimension into matrix or array +#' @param nesting allow data frames to contain other data frame as columns #' @param flatten automatically \code{\link{flatten}} nested data frames into a single non-nested data frame #' @param x the object to be encoded #' @param dataframe how to encode data.frame objects: must be one of 'rows', 'columns' or 'values' @@ -75,7 +76,7 @@ #' identical(data3, flatten(data2)) #' } fromJSON <- function(txt, simplifyVector = TRUE, simplifyDataFrame = simplifyVector, - simplifyMatrix = simplifyVector, flatten = FALSE, ...) { + simplifyMatrix = simplifyVector, flatten = FALSE, nesting = TRUE, ...) { # check type if (!is.character(txt) && !inherits(txt, "connection")) { @@ -98,7 +99,7 @@ fromJSON <- function(txt, simplifyVector = TRUE, simplifyDataFrame = simplifyVec # call the actual function (with deprecated arguments) parse_and_simplify(txt = txt, simplifyVector = simplifyVector, simplifyDataFrame = simplifyDataFrame, - simplifyMatrix = simplifyMatrix, flatten = flatten, ...) + simplifyMatrix = simplifyMatrix, nesting = nesting, flatten = flatten, ...) } parse_and_simplify <- function(txt, simplifyVector = TRUE, simplifyDataFrame = simplifyVector, diff --git a/R/simplify.R b/R/simplify.R index e59206eb..18a8a4cb 100644 --- a/R/simplify.R +++ b/R/simplify.R @@ -1,6 +1,6 @@ simplify <- function(x, simplifyVector = TRUE, simplifyDataFrame = TRUE, simplifyMatrix = TRUE, simplifyDate = simplifyVector, homoList = TRUE, flatten = FALSE, columnmajor = FALSE, - simplifySubMatrix = simplifyMatrix) { + simplifySubMatrix = simplifyMatrix, nesting = TRUE, allowNestHere = TRUE) { #This includes '[]' and '{}') if (!is.list(x) || !length(x)) { @@ -8,8 +8,8 @@ simplify <- function(x, simplifyVector = TRUE, simplifyDataFrame = TRUE, simplif } # list can be a dataframe recordlist - if (isTRUE(simplifyDataFrame) && is.recordlist(x)) { - mydf <- simplifyDataFrame(x, flatten = flatten, simplifyMatrix = simplifySubMatrix) + if (isTRUE(allowNestHere) && isTRUE(simplifyDataFrame) && is.recordlist(x)) { + mydf <- simplifyDataFrame(x, flatten = flatten, simplifyMatrix = simplifySubMatrix, nesting = nesting) if(isTRUE(simplifyDate) && is.data.frame(mydf) && is.datelist(mydf)){ return(parse_date(mydf[["$date"]])) } @@ -23,7 +23,7 @@ simplify <- function(x, simplifyVector = TRUE, simplifyDataFrame = TRUE, simplif # apply recursively out <- lapply(x, simplify, simplifyVector = simplifyVector, simplifyDataFrame = simplifyDataFrame, - simplifyMatrix = simplifySubMatrix, columnmajor = columnmajor, flatten = flatten) + simplifyMatrix = simplifySubMatrix, columnmajor = columnmajor, flatten = flatten, nesting = nesting) # fix for mongo style dates turning into scalars *after* simplifying # only happens when simplifyDataframe=FALSE diff --git a/R/simplifyDataFrame.R b/R/simplifyDataFrame.R index bd653cf5..63e2ac30 100644 --- a/R/simplifyDataFrame.R +++ b/R/simplifyDataFrame.R @@ -1,4 +1,4 @@ -simplifyDataFrame <- function(recordlist, columns, flatten, simplifyMatrix) { +simplifyDataFrame <- function(recordlist, columns, flatten, simplifyMatrix, nesting) { # no records at all if (!length(recordlist)) { @@ -26,8 +26,8 @@ simplifyDataFrame <- function(recordlist, columns, flatten, simplifyMatrix) { columnlist <- transpose_list(recordlist, columns) # simplify vectors and nested data frames - columnlist <- lapply(columnlist, simplify, simplifyVector = TRUE, simplifyDataFrame = TRUE, - simplifyMatrix = FALSE, simplifySubMatrix = simplifyMatrix, flatten = flatten) + columnlist <- lapply(columnlist, simplify, simplifyMatrix = FALSE, simplifySubMatrix = simplifyMatrix, + flatten = flatten, nesting = nesting, allowNestHere = nesting) # check that all elements have equal length columnlengths <- unlist(vapply(columnlist, function(z) { diff --git a/man/fromJSON.Rd b/man/fromJSON.Rd index d26c7010..d488e06f 100644 --- a/man/fromJSON.Rd +++ b/man/fromJSON.Rd @@ -13,6 +13,7 @@ fromJSON( simplifyDataFrame = simplifyVector, simplifyMatrix = simplifyVector, flatten = FALSE, + nesting = TRUE, ... ) @@ -45,6 +46,8 @@ toJSON( \item{flatten}{automatically \code{\link{flatten}} nested data frames into a single non-nested data frame} +\item{nesting}{allow data frames to contain other data frame as columns} + \item{...}{arguments passed on to class specific \code{print} methods} \item{x}{the object to be encoded}