diff --git a/DESCRIPTION b/DESCRIPTION index a512444..eff1187 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, @@ -29,6 +30,6 @@ Suggests: rmarkdown, R.rsp, sf -RoxygenNote: 7.2.3 +RoxygenNote: 7.3.1 Encoding: UTF-8 Roxygen: list(markdown = TRUE) diff --git a/NAMESPACE b/NAMESPACE index 6876215..dd53214 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 353434b..858d734 100644 --- a/R/asJSON.vctrs.R +++ b/R/asJSON.vctrs.R @@ -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 +} diff --git a/jsonlite.Rproj b/jsonlite.Rproj index 6a04a73..8d5f5fc 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 diff --git a/man/vec_proxy_json.Rd b/man/vec_proxy_json.Rd new file mode 100644 index 0000000..4b60c38 --- /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. +}