rstudio/shiny — pull request #606 by wch (2014-10-03 19:46:44+00:00)
Title: Switch from RJSONIO to jsonlite

This fixes #572. I think we'd want jsonlite to support different behavior for digits before merging this.

I'm a bit less certain of the `fromJSON()` compatibility than I am of `toJSON`.

Some comparisons:

```R
compare <- function(x, ...) {
resr <- jsonlite::minify(RJSONIO::toJSON(x, ...))
resj <- jsonlite::minify(jsonlite::toJSON(x, dataframe="columns",
null="null", na="null", auto_unbox=TRUE, ...))

if (!identical(resr, resj)) {
cat(paste0("Results differ.",
"\nRJSONIO: ", resr,
"\njsonlite: ", resj
))
} else {
cat(resj)
}

invisible()
}

compare(list(2))
# [2]

compare(data.frame(x=1:2, y=c("a","b")))
# {"x":[1,2],"y":["a","b"]}

# Differences in displaying digits
compare(
data.frame(x=c(1.2323123231, 5.12345678901234e19), y=c("a","b"))
)
# Results differ.
# RJSONIO: {"x":[1.2323,5.1235e+19],"y":["a","b"]}
# jsonlite: {"x":[1.2323,51234567890123399168],"y":["a","b"]}

# In RJSONIO, digits controls precision; in jsonlite, it controls digits after
# the decimal.
compare(
data.frame(x=c(1.2323123231, 5.12345678901234e19), y=c("a","b")),
digits = 2
)
# Results differ.
# RJSONIO: {"x":[1.2,5.1e+19],"y":["a","b"]}
# jsonlite: {"x":[1.23,51234567890123399168],"y":["a","b"]}

# This is a RJSONIO bug
compare(list(x = matrix(nrow = 0, ncol = 1)))
# Error: parse error: unallowed token at this point in JSON text
# { "x": }
# (right here) ------^

compare(list(matrix(1:2, nrow=1)))
# [[[1,2]]]

compare(list(matrix(1:2, ncol=1)))
# [[[1],[2]]]

compare(list(x = matrix(1:4, nrow=2)))
# {"x":[[1,3],[2,4]]}

# Difference in 1x1 matrix - I think jsonlite is correct
compare(list(x = matrix(1)))
# Results differ.
# RJSONIO: {"x":[1]}
# jsonlite: {"x":[[1]]}

compare(list(x=NULL))
# {"x":null}

compare(list(as.data.frame(t(matrix(1:2, ncol=1)))))
# [{"V1":[1],"V2":[2]}]

compare(list(x=NULL))
# {"x":null}

compare(list(x=list()))
# {"x":[]}

compare(list(x=list(a=1)[0])) # Empty named list
# {"x":{}}
```