Skip to content

Commit

Permalink
Add support for specifying indendation
Browse files Browse the repository at this point in the history
  • Loading branch information
jeroen committed Oct 31, 2014
1 parent 216725f commit c80478d
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 8 deletions.
1 change: 1 addition & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion R/fromJSON.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions R/prettify.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions R/toJSON.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion man/fromJSON.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}
Expand Down
4 changes: 3 additions & 1 deletion man/prettify.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion src/prettify.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 */
Expand Down

0 comments on commit c80478d

Please sign in to comment.