From 3f7b57e57c36abb07f5540bee9cd3ced5b7178c0 Mon Sep 17 00:00:00 2001 From: Thomas Lin Pedersen Date: Mon, 8 Dec 2025 22:29:09 +0100 Subject: [PATCH 1/2] hardcode choices in match.arg --- R/asJSON.Date.R | 2 +- R/asJSON.POSIXt.R | 2 +- R/asJSON.array.R | 2 +- R/asJSON.character.R | 2 +- R/asJSON.complex.R | 4 ++-- R/asJSON.data.frame.R | 4 ++-- R/asJSON.difftime.R | 2 +- R/asJSON.factor.R | 2 +- R/asJSON.function.R | 2 +- R/asJSON.logical.R | 2 +- R/asJSON.numeric.R | 2 +- R/asJSON.raw.R | 2 +- R/asJSON.sf.R | 2 +- R/toJSON.R | 18 +++++++++--------- 14 files changed, 24 insertions(+), 24 deletions(-) diff --git a/R/asJSON.Date.R b/R/asJSON.Date.R index a68280b..abac977 100644 --- a/R/asJSON.Date.R +++ b/R/asJSON.Date.R @@ -1,6 +1,6 @@ setMethod("asJSON", "Date", function(x, Date = c("ISO8601", "epoch"), always_decimal = FALSE, ...) { # Validate argument - Date <- match.arg(Date) + Date <- match.arg(Date, c("ISO8601", "epoch")) # select a schema output <- switch( diff --git a/R/asJSON.POSIXt.R b/R/asJSON.POSIXt.R index 7f447a1..251b236 100644 --- a/R/asJSON.POSIXt.R +++ b/R/asJSON.POSIXt.R @@ -3,7 +3,7 @@ setMethod("asJSON", "POSIXt", function(x, POSIXt = c("string", "ISO8601", "epoch # instead of ?as.character # Validate - POSIXt <- match.arg(POSIXt) + POSIXt <- match.arg(POSIXt, c("string", "ISO8601", "epoch", "mongo")) # Encode based on a schema if (POSIXt == "mongo") { diff --git a/R/asJSON.array.R b/R/asJSON.array.R index 6c583cd..f8ef1a5 100644 --- a/R/asJSON.array.R +++ b/R/asJSON.array.R @@ -1,5 +1,5 @@ asjson_array_fun <- function(x, collapse = TRUE, na = NULL, oldna = NULL, matrix = c("rowmajor", "columnmajor"), auto_unbox = FALSE, keep_vec_names = FALSE, indent = NA_integer_, ...) { - matrix <- match.arg(matrix) + matrix <- match.arg(matrix, c("rowmajor", "columnmajor")) # reset na arg when called from data frame if (identical(na, "NA")) { diff --git a/R/asJSON.character.R b/R/asJSON.character.R index 9ce45c2..5b30e15 100644 --- a/R/asJSON.character.R +++ b/R/asJSON.character.R @@ -20,7 +20,7 @@ setMethod("asJSON", "character", function(x, collapse = TRUE, na = c("null", "st # validate NA if (any(missings <- which(is.na(x)))) { - na <- match.arg(na) + na <- match.arg(na, c("null", "string", "NA")) if (na %in% c("null")) { tmp[missings] <- "null" } else if (na %in% "string") { diff --git a/R/asJSON.complex.R b/R/asJSON.complex.R index a4aa909..c498ab5 100644 --- a/R/asJSON.complex.R +++ b/R/asJSON.complex.R @@ -1,7 +1,7 @@ setMethod("asJSON", "complex", function(x, digits = 5, collapse = TRUE, complex = c("string", "list"), na = c("string", "null", "NA"), oldna = NULL, ...) { # validate - na <- match.arg(na) - complex <- match.arg(complex) + na <- match.arg(na, c("null", "string", "NA")) + complex <- match.arg(complex, c("string", "list")) #turn into strings if (complex == "string") { diff --git a/R/asJSON.data.frame.R b/R/asJSON.data.frame.R index 4091c5d..d3e1bae 100644 --- a/R/asJSON.data.frame.R +++ b/R/asJSON.data.frame.R @@ -5,7 +5,7 @@ setMethod("asJSON", "data.frame", function(x, na = c("NA", "null", "string"), co } # Validate some args - dataframe <- match.arg(dataframe) + dataframe <- match.arg(dataframe, c("rows", "columns", "values")) has_names <- identical(length(names(x)), ncol(x)) # Default to adding row names only if they are strings and not just stringified numbers @@ -43,7 +43,7 @@ setMethod("asJSON", "data.frame", function(x, na = c("NA", "null", "string"), co # Set default for row based, don't do it earlier because it will affect 'oldna' or dataframe="columns" if (dataframe == "rows" && has_names) { - na <- match.arg(na) + na <- match.arg(na, c("NA", "null", "string")) } # no records diff --git a/R/asJSON.difftime.R b/R/asJSON.difftime.R index d5c41d8..1b4b768 100644 --- a/R/asJSON.difftime.R +++ b/R/asJSON.difftime.R @@ -3,7 +3,7 @@ setMethod("asJSON", "ts", function(x, ...) { }) setMethod("asJSON", "hms", function(x, hms = c("string", "secs"), ...) { - hms <- match.arg(hms) + hms <- match.arg(hms, c("string", "secs")) output <- switch(hms, string = as.character(x), secs = as.numeric(x, units = "secs")) output[is.na(x)] <- NA asJSON(output, ...) diff --git a/R/asJSON.factor.R b/R/asJSON.factor.R index aa59003..485b257 100644 --- a/R/asJSON.factor.R +++ b/R/asJSON.factor.R @@ -1,6 +1,6 @@ setMethod("asJSON", "factor", function(x, factor = c("string", "integer"), keep_vec_names = FALSE, ...) { # validate - factor <- match.arg(factor) + factor <- match.arg(factor, c("string", "integer")) # dispatch if (factor == "integer") { diff --git a/R/asJSON.function.R b/R/asJSON.function.R index 041d1ab..d2505b6 100644 --- a/R/asJSON.function.R +++ b/R/asJSON.function.R @@ -1,6 +1,6 @@ setMethod("asJSON", "function", function(x, collapse = TRUE, fun = c("source", "list"), ...) { # validate - fun <- match.arg(fun) + fun <- match.arg(fun, c("source", "list")) if (fun == "source") { return(asJSON(deparse(x), ...)) diff --git a/R/asJSON.logical.R b/R/asJSON.logical.R index f738c44..2004771 100644 --- a/R/asJSON.logical.R +++ b/R/asJSON.logical.R @@ -5,7 +5,7 @@ setMethod("asJSON", "logical", function(x, collapse = TRUE, na = c("null", "stri } # validate arg - na <- match.arg(na) + na <- match.arg(na, c("null", "string", "NA")) # json true/false tmp <- ifelse(x, "true", "false") diff --git a/R/asJSON.numeric.R b/R/asJSON.numeric.R index 4b59213..4a9d01a 100644 --- a/R/asJSON.numeric.R +++ b/R/asJSON.numeric.R @@ -4,7 +4,7 @@ asjson_numeric_fun <- function(x, digits = 5, use_signif = is(digits, "AsIs"), n return(asJSON(as.list(x), digits = digits, use_signif = use_signif, na = na, auto_unbox = TRUE, collapse = collapse, ...)) } - na <- match.arg(na) + na <- match.arg(na, c("string", "null", "NA")) na_as_string <- switch(na, "string" = TRUE, "null" = FALSE, "NA" = NA, stop("invalid na_as_string")) # old R implementation diff --git a/R/asJSON.raw.R b/R/asJSON.raw.R index 42d1f44..0b28dbb 100644 --- a/R/asJSON.raw.R +++ b/R/asJSON.raw.R @@ -8,7 +8,7 @@ setMethod("asJSON", "blob", function(x, raw = "base64", ...) { setMethod("asJSON", "raw", function(x, raw = c("base64", "hex", "mongo", "int", "js"), ...) { # validate - raw <- match.arg(raw) + raw <- match.arg(raw, c("base64", "hex", "mongo", "int", "js")) # encode based on schema if (raw == "mongo") { diff --git a/R/asJSON.sf.R b/R/asJSON.sf.R index c85e2c5..ad43570 100644 --- a/R/asJSON.sf.R +++ b/R/asJSON.sf.R @@ -1,6 +1,6 @@ # For 'sf' geometry columns; use same structure as GeoJSON setMethod("asJSON", "sf", function(x, sf = c("dataframe", "features", "geojson"), ...) { - sf <- match.arg(sf) + sf <- match.arg(sf, c("dataframe", "features", "geojson")) if (sf == 'dataframe') { callNextMethod() } else { diff --git a/R/toJSON.R b/R/toJSON.R index 731bd77..2a1534c 100644 --- a/R/toJSON.R +++ b/R/toJSON.R @@ -17,14 +17,14 @@ toJSON <- function( ... ) { # validate args - dataframe <- match.arg(dataframe) - matrix <- match.arg(matrix) - Date <- match.arg(Date) - POSIXt <- match.arg(POSIXt) - factor <- match.arg(factor) - complex <- match.arg(complex) - raw <- match.arg(raw) - null <- match.arg(null) + dataframe <- match.arg(dataframe, c("rows", "columns", "values")) + matrix <- match.arg(matrix, c("rowmajor", "columnmajor")) + Date <- match.arg(Date, c("ISO8601", "epoch")) + POSIXt <- match.arg(POSIXt, c("string", "ISO8601", "epoch", "mongo")) + factor <- match.arg(factor, c("string", "integer")) + complex <- match.arg(complex, c("string", "list")) + raw <- match.arg(raw, c("base64", "hex", "mongo", "int", "js")) + null <- match.arg(null, c("list", "null")) # Temp workaround for 'mongopipe' unit test # if (pretty == 2 && identical(x, list()) && identical(Sys.getenv('TESTTHAT_PKG'), 'mongopipe')) { @@ -36,7 +36,7 @@ toJSON <- function( #this is just to check, we keep method-specific defaults if (!missing(na)) { - na <- match.arg(na) + na <- match.arg(na, c("null", "string")) } else { na <- NULL } From fdc9b81ba37c94c712bef89dacbe48ad5b04eb97 Mon Sep 17 00:00:00 2001 From: Thomas Lin Pedersen Date: Mon, 8 Dec 2025 23:06:59 +0100 Subject: [PATCH 2/2] fix order of na args --- R/asJSON.complex.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/asJSON.complex.R b/R/asJSON.complex.R index c498ab5..0a68f1c 100644 --- a/R/asJSON.complex.R +++ b/R/asJSON.complex.R @@ -1,6 +1,6 @@ setMethod("asJSON", "complex", function(x, digits = 5, collapse = TRUE, complex = c("string", "list"), na = c("string", "null", "NA"), oldna = NULL, ...) { # validate - na <- match.arg(na, c("null", "string", "NA")) + na <- match.arg(na, c("string", "null", "NA")) complex <- match.arg(complex, c("string", "list")) #turn into strings