From b44da5c5dc3526a1b276de5e906baa7f6da2e094 Mon Sep 17 00:00:00 2001 From: Eric Coissac Date: Wed, 22 May 2024 20:34:20 +0200 Subject: [PATCH 1/3] Patch the infinite recursion of asJSON on subclasses of vctrs_vctr instances. --- DESCRIPTION | 6 ++++-- NAMESPACE | 2 ++ R/asJSON.vctrs.R | 22 +++++++++++++++++++--- jsonlite.Rproj | 1 + 4 files changed, 26 insertions(+), 5 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index a512444a..5b9957ee 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -21,6 +21,7 @@ Description: A reasonably fast JSON parser and generator, optimized for statisti functions to stream, validate, and prettify JSON data. The unit tests included with the package verify that all edge cases are encoded and decoded consistently for use with dynamic data in systems and applications. +Imports: Suggests: httr, vctrs, @@ -28,7 +29,8 @@ Suggests: knitr, rmarkdown, R.rsp, - sf -RoxygenNote: 7.2.3 + sf, + vctrs +RoxygenNote: 7.3.1 Encoding: UTF-8 Roxygen: list(markdown = TRUE) diff --git a/NAMESPACE b/NAMESPACE index 68762152..dd53214c 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -3,6 +3,7 @@ S3method("[",json) S3method(print,json) S3method(print,scalar) +S3method(vec_proxy_json,vctrs_vctr) export(as_gzjson_b64) export(as_gzjson_raw) export(base64_dec) @@ -25,6 +26,7 @@ export(toJSON) export(unbox) export(unserializeJSON) export(validate) +export(vec_proxy_json) export(write_json) import(methods) useDynLib(jsonlite,C_collapse_array) diff --git a/R/asJSON.vctrs.R b/R/asJSON.vctrs.R index 353434b4..c5dd310c 100644 --- a/R/asJSON.vctrs.R +++ b/R/asJSON.vctrs.R @@ -1,5 +1,21 @@ setMethod("asJSON", "vctrs_vctr", function(x, ...) { - # dispatch based on the underlying type - class(x) <- setdiff(class(x), 'vctrs_vctr') - asJSON(x, ...) + tryCatch( + { + asJSON(vec_proxy_json(x), ...) + }, + error = function(cond) { + selectMethod(asJSON, "ANY")(x) + } + ) }) + +#' @export +vec_proxy_json <- function(x) { + UseMethod("vec_proxy_json", x) +} + +#' @export +vec_proxy_json.vctrs_vctr <- function(x) { + class(x) <- setdiff(class(x), "vctrs_vctr") + x +} diff --git a/jsonlite.Rproj b/jsonlite.Rproj index 6a04a73f..8d5f5fc9 100644 --- a/jsonlite.Rproj +++ b/jsonlite.Rproj @@ -17,3 +17,4 @@ StripTrailingWhitespace: Yes BuildType: Package PackageInstallArgs: --no-multiarch --with-keep.source --install-tests +PackageRoxygenize: rd,collate,namespace From 54f5a5a125805c223c8d9a0fa66c3960ecc9824d Mon Sep 17 00:00:00 2001 From: Eric Coissac Date: Wed, 22 May 2024 21:10:55 +0200 Subject: [PATCH 2/3] Remove a dupplicated declaration of vctrs in Suggests --- DESCRIPTION | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 5b9957ee..eff1187f 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -29,8 +29,7 @@ Suggests: knitr, rmarkdown, R.rsp, - sf, - vctrs + sf RoxygenNote: 7.3.1 Encoding: UTF-8 Roxygen: list(markdown = TRUE) From 022e01fb22d65bbc4e08bcea58aa608d6dd84590 Mon Sep 17 00:00:00 2001 From: Eric Coissac Date: Wed, 22 May 2024 21:21:30 +0200 Subject: [PATCH 3/3] Add documentation for the generic S3 method vec_proxy_json --- R/asJSON.vctrs.R | 10 ++++++++++ man/vec_proxy_json.Rd | 20 ++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 man/vec_proxy_json.Rd diff --git a/R/asJSON.vctrs.R b/R/asJSON.vctrs.R index c5dd310c..858d7346 100644 --- a/R/asJSON.vctrs.R +++ b/R/asJSON.vctrs.R @@ -9,6 +9,16 @@ setMethod("asJSON", "vctrs_vctr", function(x, ...) { ) }) +#' JSON conversion proxy +#' +#' vec_proxy_json method returns proxy objects, i.e. an atomic vector or a list of atomic vectors, +#' able to be converted to JSON using the jsonlite package. This is a generic S3 method, that has +#' to be implemented for subclasses requesting particualar JSON conversion. A default implementation +#' is provided for `vctrs_vctr` class. +#' +#' @param x object instance of class `vctrs_vctr` to be converted to JSON +#' +#' @return an atomic vector or a list of atomic vectors. #' @export vec_proxy_json <- function(x) { UseMethod("vec_proxy_json", x) diff --git a/man/vec_proxy_json.Rd b/man/vec_proxy_json.Rd new file mode 100644 index 00000000..4b60c381 --- /dev/null +++ b/man/vec_proxy_json.Rd @@ -0,0 +1,20 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/asJSON.vctrs.R +\name{vec_proxy_json} +\alias{vec_proxy_json} +\title{JSON conversion proxy} +\usage{ +vec_proxy_json(x) +} +\arguments{ +\item{x}{object instance of class \code{vctrs_vctr} to be converted to JSON} +} +\value{ +an atomic vector or a list of atomic vectors. +} +\description{ +vec_proxy_json method returns proxy objects, i.e. an atomic vector or a list of atomic vectors, +able to be converted to JSON using the jsonlite package. This is a generic S3 method, that has +to be implemented for subclasses requesting particualar JSON conversion. A default implementation +is provided for \code{vctrs_vctr} class. +}