Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch the infinite recursion of asJSON on subclasses of vctrs_vctr instances… #438

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -29,6 +30,6 @@ Suggests:
rmarkdown,
R.rsp,
sf
RoxygenNote: 7.2.3
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
32 changes: 29 additions & 3 deletions R/asJSON.vctrs.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
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)
}
)
})

#' 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)
}

#' @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
20 changes: 20 additions & 0 deletions man/vec_proxy_json.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading