jsonlite icon indicating copy to clipboard operation
jsonlite copied to clipboard

Allow auto_unbox=TRUE when dataframe="column"

Open heavywatal opened this issue 7 years ago • 0 comments

auto_unbox=TRUE in toJSON() has been ignored when dataframe="column" since 65680f68; therefore it was not possible to unbox single-row data.frames automatically:

iris1 = head(iris, 1L)
cat(toJSON(iris1, dataframe = "column", auto_unbox = TRUE, pretty = TRUE))
#> {
#>   "Sepal.Length": [5.1],
#>   "Sepal.Width": [3.5],
#>   "Petal.Length": [1.4],
#>   "Petal.Width": [0.2],
#>   "Species": ["setosa"]
#> }

This PR enables it:

iris1 = head(iris, 1L)
cat(toJSON(iris1, dataframe = "column", auto_unbox = TRUE, pretty = TRUE))
#> {
#>   "Sepal.Length": 5.1,
#>   "Sepal.Width": 3.5,
#>   "Petal.Length": 1.4,
#>   "Petal.Width": 0.2,
#>   "Species": "setosa"
#> }

Note that it is slightly different from what we get from dataframe="row":

iris1 = head(iris, 1L)
cat(toJSON(iris1, dataframe = "row", pretty = TRUE))
#> [
#>   {
#>     "Sepal.Length": 5.1,
#>     "Sepal.Width": 3.5,
#>     "Petal.Length": 1.4,
#>     "Petal.Width": 0.2,
#>     "Species": "setosa"
#>   }
#> ]

heavywatal avatar Sep 20 '18 07:09 heavywatal