-
Notifications
You must be signed in to change notification settings - Fork 40
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
adding a toJSON method for custom class #62
Comments
Hmm. The Can you explain how you are trying to use this feature? Note that you can always define a new method which wraps around myJSON <- function(x, ...){
UseMethod("myJSON")
}
myJSON.default <- jsonlite::toJSON;
myJSON.foo <- function(x, ...){
return('["this is the foo"]')
}
test <- 1:3
class(test) <- "foo"
myJSON(test)
myJSON(1:3) |
as a hack, i’m doing this: asJSON <- jsonlite:::asJSON
setMethod('asJSON', 'repr', function(x, ...) jsonlite:::asJSON(unclass(x), ...)) because it’s enough for my use case. it would be nice to have some hook for own classes though. |
For the record, #90 was supposed to solve this kind of problems. The approach @jeroenooms mentioned here has a problem, that is it does not work on recursive data structures such as lists, e.g. I understand the point of consistency, but there is no absolute consistency in the universe. This is a general issue regarding reproducibility: I will not be surprised that toJSON() produces different results if users call it differently. What software authors should guarantee is that the software should give identical results when two environments are identical. I don't think you would think the difference between |
of course it shouldn’t. after all, they define those methods exactly for the purpose of making |
@yihui the problem with custom For example if a package Also now that |
To sum up, I think there are two choices in front of you when users ask for extensibility: 1) No, you are out of luck when your class is unknown to me, or you have to do hard and boring work to reinvent the S4 mechanism. 2) Yes, here I grant you the power, but remember your responsibility at the same time, since you will no longer be blessed by the Godfather. You seem to vote for 1, and I will vote for 2. BTW, I do not really desire this feature at the moment. I have got all I wanted from you (many thanks), but I'm just writing my thoughts here since I guess there will certainly be more users asking this question. |
i observed that when i load a am i correct that this means that for S4 classes, packages are automatically loaded? if so, |
I'm curious if over the years there's been a coalescence towards a 'standard' (or 'recommended') way to handle this type of scenario with {jsonlite}. I love working with {jsonlite}, except for the frequent want for both:
It seems (just from reading the various GitHub Issues threads) that writing one's own I routinely do resort to registering custom setMethod(getGeneric("asJSON", package = "jsonlite"), "mycls", function(x, ...) to_json.mycls(x, ...)) ... where This approach works particularly well when setMethod(getGeneric("asJSON", package = "jsonlite"), "mycls", function(x, ...) x$asJSON(...)) (Note that this can cause some headaches when R6 inheritance is used (and we'd like to inherit the I realize that having methods available (or not) via namespace-loading can then become a source of inconsistency, but I think I agree with @yihui that the authors and users of packages need to agree to good writing and reading of documentation, else all bets are off. I also see the point that {jsonlite} was originally intended for 1:1 serialization:deserialization of data within the R ecosystem, but I imagine a lot of usage nowadays is for two-way communication with external APIs, where the JSON requirements are outside of the R developer's control. (Nested lists of specially-encoded objects are the most common case of this problem for me -- I want to use {jsonlite}'s default list recursion but need to create special structures that may themselves have lists of other special structures.) In any case, I do think {jsonlite}'s been a huge help to the R community, so this isn't a complaint -- just musings and curiousness about others' patterns. I'd be more than happy to collect such patterns, discuss the pros & cons, and write up a vignette on this issue, too, if that'd be helpful. |
Hello, I would like to implement a toJSON method for a custom class.
In "The jsonlite Package: A Practical and Consistent Mapping Between JSON Data and R Objects", I see the following:
"For all of the common classes in R, the jsonlite package implements toJSON methods as described in this document. Users in R can extend this system by implementing additional methods for other classes. This also means that classes that do not have the toJSON method defined are not supported."
However, inspecting the implementation of toJSON, it seems that the above description might actually apply to the "asJSON" function. Is this accurate?
Based on my reasoning above, I then tried the following for the class 'foo', and a trivial asJSON function:
I'm guessing this is because asJSON is not exported from the jsonlite package. The above command does work when jsonlite is loaded into R via the load_all function in devtools (asJSON is then visible on the search path.)
Thanks!
The text was updated successfully, but these errors were encountered: