Skip to content

Add keep_vec_names option for converting named vectors to JSON objects #82

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

Closed
wants to merge 2 commits into from

Conversation

wch
Copy link
Contributor

@wch wch commented Mar 18, 2015

This PR fixes #76.

It adds the keep_vec_names option, which, when TRUE, tells asJSON to convert named vectors to JSON objects, instead of the default arrays.

It also prints out an annoying message, encouraging people to use named lists instead.

jsonlite::toJSON(c(a=1), keep_vec_names=TRUE)
# 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 want JSON object output, please use a named list instead. See ?toJSON.
# {"a":1} 

I needed to special cases factors so that they preserved names, to be passed on to asJSON.character. Other non-basic vector types (Dates, POSIXct, etc) didn't seem to need this.

@jeroen
Copy link
Owner

jeroen commented Mar 19, 2015

Thanks! I have merged your code manually because there were some issues, and I don't want to formally expose the keep_vec_names argument in toJSON. But all your unit tests pass.

@jeroen jeroen closed this Mar 19, 2015
@wch
Copy link
Contributor Author

wch commented Mar 19, 2015

Great, thanks!

@KrishnaPG
Copy link

KrishnaPG commented Aug 11, 2017

While searching for a solution, came across this - not sure where the problem is. Hoping some information.

In OpenCPU while returning results from a RCPP based function, returning a value of type below

// [[Rcpp::export]]
std::map<std::string, double> getMaterialHardnessList()
{
  return gMaterialHardness; // some global variable
}

For the C++ std::map<std::string, double>, on the javascript JSON side I would expect key-value pairs (name: value), where as I am just seeing the values array. The names (strings) are dropped and only the values (doubles) are being received by the browser.

How to tell the OpenCPU (or RCPP) that it should return the std::map values as key-value object pairs and not just vector array?

Even vector array without dropping the names would be correct (since the C++ map being a dictionary in reality).

@jeroen
Copy link
Owner

jeroen commented Aug 11, 2017

I don't fully understand the question, but perhaps you should return an Rcpp::List in your C++ function rather than a map.

In OpenCPU you can append any toJSON parameter to the GET url so you can always do ? keep_vec_names=true

@KrishnaPG
Copy link

Thanks. The ?keep_vec_names=true did the trick. The std::map is now correctly coming in the key-value format (without the need for List creation).

Thanks for the help.

@rdmtinez
Copy link

This little gem saved me a huge headache, thanks!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

toJSON drops names of named vectors
4 participants