Skip to content

Commit

Permalink
Patch the infinite recursion of asJSON on subclasses of vctrs_vctr in…
Browse files Browse the repository at this point in the history
…stances.
  • Loading branch information
coissac committed May 22, 2024
1 parent 98c6d3b commit b44da5c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
6 changes: 4 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@ 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,
testthat,
knitr,
rmarkdown,
R.rsp,
sf
RoxygenNote: 7.2.3
sf,
vctrs
RoxygenNote: 7.3.1
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
22 changes: 19 additions & 3 deletions R/asJSON.vctrs.R
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions jsonlite.Rproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ StripTrailingWhitespace: Yes

BuildType: Package
PackageInstallArgs: --no-multiarch --with-keep.source --install-tests
PackageRoxygenize: rd,collate,namespace

0 comments on commit b44da5c

Please sign in to comment.