Skip to content

Commit

Permalink
Add validation argument to fromJSON
Browse files Browse the repository at this point in the history
  • Loading branch information
jeroen committed Jul 28, 2014
1 parent 6f1dd73 commit 678135f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
12 changes: 9 additions & 3 deletions R/fromJSON.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#' @param simplifyMatrix coerse \code{JSON} arrays containing vectors of equal mode and dimension into matrix or array
#' @param flatten flatten nested data frames into a single non-nested data frame (see example)
#' @param unicode parse escaped (hexadecimal) unicode characters \code{\\uXXXX}. See details.
#' @param validate automatically \code{\link{validate}} \code{JSON} before parsing it.
#' @param x the object to be encoded
#' @param dataframe how to encode data.frame objects: must be one of 'rows' or 'columns'
#' @param matrix how to encode matrices and higher dimensional arrays: must be one of 'rowmajor' or 'columnmajor'.
Expand Down Expand Up @@ -75,7 +76,7 @@
#' options(scipen=3)
#' toJSON(10 ^ (0:10))
fromJSON <- function(txt, simplifyVector = TRUE, simplifyDataFrame = simplifyVector,
simplifyMatrix = simplifyVector, flatten = FALSE, unicode = FALSE, ...) {
simplifyMatrix = simplifyVector, flatten = FALSE, unicode = FALSE, validate = FALSE, ...) {

# check type
if (!is.character(txt)) {
Expand All @@ -100,8 +101,13 @@ fromJSON <- function(txt, simplifyVector = TRUE, simplifyDataFrame = simplifyVec
txt <- paste(txt, collapse = "\n")
}

# simple check
if (!grepl("^[ \t\r\n]*(\\{|\\[)", txt)) {
# Validate JSON
if (isTRUE(validate)) {
if(!validate(txt)) {
stop("Validation failed! String contains invalid JSON.")
}
} else if (!grepl("^[ \t\r\n]*(\\{|\\[)", txt)) {
#Always do basic validation
stop("String does not contain valid JSON: \"", gsub("\\s+", " ", substring(txt, 0, 25)), "...\"")
}

Expand Down
5 changes: 4 additions & 1 deletion man/fromJSON.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
\title{Convert \R{} objects to/from \code{JSON}}
\usage{
fromJSON(txt, simplifyVector = TRUE, simplifyDataFrame = simplifyVector,
simplifyMatrix = simplifyVector, flatten = FALSE, unicode = FALSE, ...)
simplifyMatrix = simplifyVector, flatten = FALSE, unicode = FALSE,
validate = FALSE, ...)

toJSON(x, dataframe = c("rows", "columns"), matrix = c("rowmajor",
"columnmajor"), Date = c("ISO8601", "epoch"), POSIXt = c("string",
Expand All @@ -28,6 +29,8 @@ toJSON(x, dataframe = c("rows", "columns"), matrix = c("rowmajor",

\item{unicode}{parse escaped (hexadecimal) unicode characters \code{\\uXXXX}. See details.}

\item{validate}{automatically \code{\link{validate}} \code{JSON} before parsing it.}

\item{x}{the object to be encoded}

\item{dataframe}{how to encode data.frame objects: must be one of 'rows' or 'columns'}
Expand Down

0 comments on commit 678135f

Please sign in to comment.