-
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
toJSON drops names of named vectors #76
Comments
I'm not so sure if this is a good idea. We could add it as some sort of legacy mode, but I really believe you are better off without this feature. Here my concern:
So all in all I feel it is more safe and simple to require users to |
Those are all fair points. However... there is already quite a bit of code out there for Shiny, and I know that some of it uses named vectors with the expectation that they will be turned into JSON objects. This code isn't common, but it is out there. How about having a backward-compatibility option for this, where it will output JSON objects and also print a message saying that a named list should be used instead? Then I think after some time, it could be changed to an error, and then later it could be removed. |
Ugh, first thing I try: x <- structure(as.complex(1:3), names=c("foo", "bar", "baz"))
RJSONIO::toJSON(x) |
Another reason that I'd like to move away from RJSONIO. :) |
This is also interesting: x <- matrix(1:4, 2)
names(x) <- letters[1:4]
cat(RJSONIO::toJSON(x)) It gets even better when you add: colnames(x) <- c("foo", "bar")
cat(RJSONIO::toJSON(x)) |
I'm not sure if there is a sensible way to add compatibility with an implementation that is broken for so many edge cases :/ |
Well, I was just planning on making it work for 1-d named vectors, and ignoring matrices and arrays with names. As far as I'm concerned, those can be treated as though they're unnamed, and I don't feel a need to keep backward compatibility with them. I think that in order to migrate shiny to jsonlite, we need at least some way of working with named 1-d vectors, otherwise people will start experience mysterious breakage (that's how I encountered this issue in the first place). My thought is that the compatibility option would just be there as a stopgap solution, until developers have enough time to switch their code to named lists. I've written a partial implementation for if (isTRUE(keep_vec_names) && !is.null(names(x))) {
message("Input to asJSON(keep_vec_names=TRUE) is a named vector. ",
"In a future version of jsonlite, this option will not be supported, ",
"and named vectors will be translated into arrays instead of objects. ",
"If you need JSON object output, please use a named list. See ?toJSON.")
return(asJSON(as.list(x), digits = digits, use_signif = use_signif, na = na,
auto_unbox = TRUE, collapse = collapse, ...))
} |
This is similar to the RJSONIO implementation. It will result in strange output if the vector at hand is part of a data frame or matrix. |
Oh, good point. I'll look into this some more later and hopefully I'll come up with something that's not too horrible. |
We could also not change the mapping, but still give a warning/error for named vectors. Then the shiny apps will still be broken, but it will be immediately obvious what the problem is and we can help people fix it. |
I'm really reluctant to just allow apps to break. It's really unpleasant for a user to upgrade their packages and have it break their code. It's even worse if a package has a Shiny app that breaks - then the package has to be updated, submitted, accepted, and the user has to install the updated package. I think that perfect compatibility with RJSONIO isn't necessary. For example, as you pointed out, RJSONIO's conversion of named matrices is almost nonsensical, and I doubt anyone would want that output -- but I believe that it's not that rare for users to expect named vectors to turn into JSON objects. |
So I am getting the warning
in a (rather large) shiny app. How do I find out which part of my app is causing this? |
If you use |
Thanks for the lightning fast reply! I am using the latest shiny 0.13.1. However, setting Anyway, the stack trace I see does not involve any of my app code but to me looks like shiny internals... Now, I've turned on |
Hi @mattflor , I am having this issue as well. Have you found a way to get a stack trace? |
Well, as described in the debugging article there's |
Thank you! |
For example:
This differs from the behavior of RJSONIO:
It would be great to have an option to control the behavior of this. (There are some features of Shiny don't work correctly because they send named vectors and expect them to be translated into objects instead of arrays).
The text was updated successfully, but these errors were encountered: