Patch the infinite recursion of asJSON on subclasses of vctrs_vctr instances… #438
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Goals of the PR
To answer what I expect to be a better way to #424, I propose the following patch, which changes the
asJSON
method for thevcrts::vctrs_vctr
class.Principle of the patch
The idea of this patch is to allow people who develop classes derived from the
vcrts::vctrs_vctr
class to add an S3 method calledvec_proxy_json
to their class. This follows the same principle implemented in thevctrs
package for the comparison operators and ordering operations involving subclasses ofvctrs_vctr
(i.e., thevec_proxy_compare
andvec_proxy_order
methods). The method must change the original vector into a new one that can be easily converted into JSON (e.g. a simplecharacter
,integer
,numeric
, orlogical
vector), or into something more sophisticated if needed, such as alist
.A version of the
vec_proxy_json
method is implemented for the originalvctrs_vctr
class. It mimics the originalasJSON
code for thevcrts::vctrs_vctr
class.Effect of the patch
According to the exchange in issue #424, the bug was introduced as a workaround for issue #408. I have reproduced the example with the patch applied to test if it works as expected.
If you create two simple instances of
vctrs_vctr
, one wrapping an integer vector, the second wrapping a character vector, both will produce the correct json.Hopping that it can help.
All the best
Eric