From c80478da44390ab7c83a9e003756e3b85573cdb9 Mon Sep 17 00:00:00 2001 From: Jeroen Ooms Date: Thu, 30 Oct 2014 20:30:14 -0700 Subject: [PATCH] Add support for specifying indendation --- NEWS | 1 + R/fromJSON.R | 2 +- R/prettify.R | 9 +++++---- R/toJSON.R | 2 ++ man/fromJSON.Rd | 2 +- man/prettify.Rd | 4 +++- src/prettify.c | 3 ++- 7 files changed, 15 insertions(+), 8 deletions(-) diff --git a/NEWS b/NEWS index ca895ac..4ac86a8 100644 --- a/NEWS +++ b/NEWS @@ -1,5 +1,6 @@ 0.9.14 - Add support for digits = I(n) to use significant precision. + - When 'pretty' in toJSON is numeric, it specifies the number of spaces to indent. 0.9.13 - Ported some number formatting to C diff --git a/R/fromJSON.R b/R/fromJSON.R index 3de597f..7ee2903 100644 --- a/R/fromJSON.R +++ b/R/fromJSON.R @@ -37,7 +37,7 @@ #' @param auto_unbox automatically \code{\link{unbox}} all atomic vectors of length 1. It is usually safer to avoid this and instead use the \code{\link{unbox}} function to unbox individual elements. #' @param digits max number of digits (after the dot) to print for numeric values. See: \code{\link{round}} #' @param force unclass/skip objects of classes with no defined JSON mapping -#' @param pretty adds indentation whitespace to JSON output. See \code{\link{prettify}} +#' @param pretty adds indentation whitespace to JSON output. Can be TRUE/FALSE or a number specifying the number of spaces to indent. See \code{\link{prettify}} #' @param ... arguments passed on to class specific \code{print} methods #' @references Jeroen Ooms (2014). The \code{jsonlite} Package: A Practical and Consistent Mapping Between JSON Data and \R{} Objects. \emph{arXiv:1403.2805}. \url{http://arxiv.org/abs/1403.2805} #' @examples #stringify some data diff --git a/R/prettify.R b/R/prettify.R index 2d6cebd..ceaa20c 100644 --- a/R/prettify.R +++ b/R/prettify.R @@ -6,14 +6,15 @@ #' @aliases minify prettify #' @export prettify minify #' @param txt JSON string +#' @param indent number of spaces to indent #' @useDynLib jsonlite R_reformat #' @examples myjson <- toJSON(cars) #' cat(myjson) #' prettify(myjson) #' minify(myjson) -prettify <- function(txt) { +prettify <- function(txt, indent = 4) { txt <- paste(txt, collapse = "\n") - reformat(txt, TRUE) + reformat(txt, TRUE, indent_string = paste(rep(" ", as.integer(indent)), collapse="")) } #' @rdname prettify @@ -22,8 +23,8 @@ minify <- function(txt) { reformat(txt, FALSE) } -reformat <- function(x, pretty){ - out <- .Call(R_reformat, x, pretty); +reformat <- function(x, pretty, indent_string = ""){ + out <- .Call(R_reformat, x, pretty, indent_string = indent_string); if(out[[1]] == 0) { return(out[[2]]) } else { diff --git a/R/toJSON.R b/R/toJSON.R index 77ca89d..44db8d3 100644 --- a/R/toJSON.R +++ b/R/toJSON.R @@ -38,6 +38,8 @@ toJSON <- function(x, dataframe = c("rows", "columns"), matrix = c("rowmajor", " #prettify if (isTRUE(pretty)) { prettify(ans) + } else if(is.numeric(pretty)) { + prettify(ans, pretty) } else { class(ans) <- "json" return(ans) diff --git a/man/fromJSON.Rd b/man/fromJSON.Rd index 9f12a38..c4f85fa 100644 --- a/man/fromJSON.Rd +++ b/man/fromJSON.Rd @@ -53,7 +53,7 @@ toJSON(x, dataframe = c("rows", "columns"), matrix = c("rowmajor", \item{digits}{max number of digits (after the dot) to print for numeric values. See: \code{\link{round}}} -\item{pretty}{adds indentation whitespace to JSON output. See \code{\link{prettify}}} +\item{pretty}{adds indentation whitespace to JSON output. Can be TRUE/FALSE or a number specifying the number of spaces to indent. See \code{\link{prettify}}} \item{force}{unclass/skip objects of classes with no defined JSON mapping} } diff --git a/man/prettify.Rd b/man/prettify.Rd index 62b1afb..b29f02f 100644 --- a/man/prettify.Rd +++ b/man/prettify.Rd @@ -5,12 +5,14 @@ \alias{prettify, minify} \title{Prettify or minify a JSON string} \usage{ -prettify(txt) +prettify(txt, indent = 4) minify(txt) } \arguments{ \item{txt}{JSON string} + +\item{indent}{number of spaces to indent} } \description{ Prettify adds indentation to a JSON string; minify removes all indentation/whitespace. diff --git a/src/prettify.c b/src/prettify.c index 4439332..6306e36 100644 --- a/src/prettify.c +++ b/src/prettify.c @@ -84,7 +84,7 @@ static yajl_callbacks callbacks = { reformat_end_array }; -SEXP R_reformat(SEXP x, SEXP pretty) { +SEXP R_reformat(SEXP x, SEXP pretty, SEXP indent_string) { yajl_status stat; yajl_handle hand; yajl_gen g; @@ -93,6 +93,7 @@ SEXP R_reformat(SEXP x, SEXP pretty) { /* init generator */ g = yajl_gen_alloc(NULL); yajl_gen_config(g, yajl_gen_beautify, asInteger(pretty)); + yajl_gen_config(g, yajl_gen_indent_string, translateCharUTF8(asChar(indent_string))); yajl_gen_config(g, yajl_gen_validate_utf8, 0); /* init parser */